diff --git a/config.lua b/config.lua index cc148bd..58b1750 100644 --- a/config.lua +++ b/config.lua @@ -69,7 +69,7 @@ local function validateSchema(schema, input, errors) -- recursive schema validation for generic tables errors[key] = errors[key] or {} validateSchema(field_schema.schema, input_value, errors[key]) - if not errors[key] then + if next(errors[key]) == nil then errors[key] = nil end elseif wanted_type == 'list' then diff --git a/tests/schema_validation.lua b/tests/schema_validation.lua index 7b4d8f8..74ead89 100644 --- a/tests/schema_validation.lua +++ b/tests/schema_validation.lua @@ -1,4 +1,4 @@ -TestConfigSchemaValidator = {} +TestSchemaValidator = {} local config = require('config') @@ -25,7 +25,7 @@ function pprint(t, ident) end end -function TestConfigSchemaValidator:testBasicFields() +function TestSchemaValidator:testBasicFields() local errors = config.validateSchema({a={type='string'}}, {a='test'}) lu.assertIs(len(errors), 0) local errors = config.validateSchema({a={type='number'}}, {a=123}) @@ -34,10 +34,29 @@ function TestConfigSchemaValidator:testBasicFields() lu.assertIs(len(errors), 1) end -function TestConfigSchemaValidator:testList() +function TestSchemaValidator:testList() local errors = config.validateSchema({a={type='list', schema={type='number'}}}, {a={1,2,3}}) lu.assertIs(len(errors), 0) local errors = config.validateSchema({a={type='list', schema={type='number'}}}, {a={1,2,3,'asd'}}) lu.assertIs(len(errors), 1) end + +function TestSchemaValidator:testTable() + local errors = config.validateSchema( + { + a={ + type='table', + schema={ + b={ + type='number' + } + } + } + }, + {a= + {b=2} + } + ) + lu.assertIs(len(errors), 0) +end