aproxy/tests/schema_validation.lua

63 lines
1.6 KiB
Lua
Raw Normal View History

2022-12-07 18:12:43 +00:00
TestSchemaValidator = {}
2022-12-07 17:57:07 +00:00
local config = require('config')
local function len(t)
local count = 0
for _ in pairs(t) do count = count + 1 end
return count
end
function pprint(t, ident)
local ident = ident or 0
if type(t) == 'table' then
local count = 0
for k, v in pairs(t) do
print(string.rep('\t', ident) .. k)
pprint(v, ident + 1)
count = count + 1
end
if count == 0 then
print('<empty table>')
end
else
print(string.rep('\t', ident) .. tostring(t))
end
end
2022-12-07 18:12:43 +00:00
function TestSchemaValidator:testBasicFields()
2022-12-07 17:57:07 +00:00
local errors = config.validateSchema({a={type='string'}}, {a='test'})
lu.assertIs(len(errors), 0)
local errors = config.validateSchema({a={type='number'}}, {a=123})
lu.assertIs(len(errors), 0)
local errors = config.validateSchema({a={type='string'}}, {a=123})
lu.assertIs(len(errors), 1)
end
2022-12-07 18:12:43 +00:00
function TestSchemaValidator:testList()
2022-12-07 17:57:07 +00:00
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
2022-12-07 18:12:43 +00:00
function TestSchemaValidator:testTable()
local errors = config.validateSchema(
{
a={
type='table',
schema={
b={
type='number'
}
}
}
},
{a=
{b=2}
}
)
lu.assertIs(len(errors), 0)
end