Skip to content

Commit 2e9df0e

Browse files
authored
feat: implement if/then/else (#64)
1 parent 782e290 commit 2e9df0e

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/jsonschema.lua

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,29 @@ generate_validator = function(ctx, schema)
10251025
ctx:stmt('end')
10261026
end
10271027

1028+
if schema['if'] then
1029+
ctx:stmt( 'do')
1030+
local validator = ctx:validator({ 'if' }, schema['if'])
1031+
ctx:stmt(sformat( ' local matched = %s(%s)', validator, ctx:param(1)))
1032+
if schema['then'] then
1033+
ctx:stmt( ' if matched then')
1034+
validator = ctx:validator({ 'then' }, schema['then'])
1035+
ctx:stmt(sformat(' if not %s(%s) then', validator, ctx:param(1)))
1036+
ctx:stmt( ' return false, "then clause did not match"')
1037+
ctx:stmt( ' end')
1038+
ctx:stmt( ' end')
1039+
end
1040+
if schema['else'] then
1041+
ctx:stmt( ' if not matched then')
1042+
validator = ctx:validator({ 'else' }, schema['else'])
1043+
ctx:stmt(sformat(' if not %s(%s) then', validator, ctx:param(1)))
1044+
ctx:stmt( ' return false, "else clause did not match"')
1045+
ctx:stmt( ' end')
1046+
ctx:stmt( ' end')
1047+
end
1048+
ctx:stmt( 'end')
1049+
end
1050+
10281051
if schema['not'] then
10291052
local validator = ctx:validator({ 'not' }, schema['not'])
10301053
ctx:stmt(sformat('if %s(%s) then', validator, ctx:param(1)))

t/draft7.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,13 @@ local supported = {
9292
'spec/JSON-Schema-Test-Suite/tests/draft7/format.json',
9393
'spec/JSON-Schema-Test-Suite/tests/draft7/const.json',
9494
'spec/JSON-Schema-Test-Suite/tests/draft7/contains.json',
95+
'spec/JSON-Schema-Test-Suite/tests/draft7/if-then-else.json',
9596

9697
-- ref
9798
'spec/JSON-Schema-Test-Suite/tests/draft7/ref.json',
9899
-- not support: an external resolver is required
99100
-- 'spec/JSON-Schema-Test-Suite/tests/draft7/refRemote.json',
100101
-- 'spec/JSON-Schema-Test-Suite/tests/draft7/definitions.json',
101-
102-
-- not support: todo
103-
-- 'spec/JSON-Schema-Test-Suite/tests/draft7/if-then-else.json',
104102
}
105103
-- supported = {
106104
-- 'spec/JSON-Schema-Test-Suite/tests/draft7/dependencies.json',

0 commit comments

Comments
 (0)