fix validation for full tables

This commit is contained in:
Luna 2022-12-07 15:12:43 -03:00
parent fd59059101
commit 48917659ca
2 changed files with 23 additions and 4 deletions

View file

@ -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

View file

@ -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