diff --git a/lib/jsonschema.lua b/lib/jsonschema.lua index 1422632..c147f80 100644 --- a/lib/jsonschema.lua +++ b/lib/jsonschema.lua @@ -1025,6 +1025,29 @@ generate_validator = function(ctx, schema) ctx:stmt('end') end + if schema['if'] then + ctx:stmt( 'do') + local validator = ctx:validator({ 'if' }, schema['if']) + ctx:stmt(sformat( ' local matched = %s(%s)', validator, ctx:param(1))) + if schema['then'] then + ctx:stmt( ' if matched then') + validator = ctx:validator({ 'then' }, schema['then']) + ctx:stmt(sformat(' if not %s(%s) then', validator, ctx:param(1))) + ctx:stmt( ' return false, "then clause did not match"') + ctx:stmt( ' end') + ctx:stmt( ' end') + end + if schema['else'] then + ctx:stmt( ' if not matched then') + validator = ctx:validator({ 'else' }, schema['else']) + ctx:stmt(sformat(' if not %s(%s) then', validator, ctx:param(1))) + ctx:stmt( ' return false, "else clause did not match"') + ctx:stmt( ' end') + ctx:stmt( ' end') + end + ctx:stmt( 'end') + end + if schema['not'] then local validator = ctx:validator({ 'not' }, schema['not']) ctx:stmt(sformat('if %s(%s) then', validator, ctx:param(1))) diff --git a/t/draft7.lua b/t/draft7.lua index fafbff8..8677c23 100644 --- a/t/draft7.lua +++ b/t/draft7.lua @@ -92,15 +92,13 @@ local supported = { 'spec/JSON-Schema-Test-Suite/tests/draft7/format.json', 'spec/JSON-Schema-Test-Suite/tests/draft7/const.json', 'spec/JSON-Schema-Test-Suite/tests/draft7/contains.json', + 'spec/JSON-Schema-Test-Suite/tests/draft7/if-then-else.json', -- ref 'spec/JSON-Schema-Test-Suite/tests/draft7/ref.json', -- not support: an external resolver is required -- 'spec/JSON-Schema-Test-Suite/tests/draft7/refRemote.json', -- 'spec/JSON-Schema-Test-Suite/tests/draft7/definitions.json', - - -- not support: todo - -- 'spec/JSON-Schema-Test-Suite/tests/draft7/if-then-else.json', } -- supported = { -- 'spec/JSON-Schema-Test-Suite/tests/draft7/dependencies.json',