Good Code
The good version checks that the config is a table and that required fields have the expected type. Runtime code receives a normalized shape.
Lesson 09
Validate Lua configuration tables before using them to build routes, plugins, or runtime behavior.
local function read_config(config)
assert(type(config) == "table", "config must be a table")
assert(type(config.review_path) == "string", "review_path must be a string")
-- Validated config returns a predictable shape to runtime code.
return {
review_path = config.review_path,
max_reviews = tonumber(config.max_reviews) or 100,
}
endlocal function read_config(config)
-- Runtime code receives whatever shape the config file happened to define.
return config
endThe good version checks that the config is a table and that required fields have the expected type. Runtime code receives a normalized shape.
The bad version passes the raw config table through. Typos and wrong types fail later, often far away from the file that introduced them.