From 5453f002e7c2a65ceb8295cca69ba1b0d1b08888 Mon Sep 17 00:00:00 2001 From: Michael Martin Date: Thu, 16 Apr 2020 10:30:32 -0700 Subject: [PATCH] feat: implement if/then/else --- lib/jsonschema.lua | 23 +++++++++++++++++++++++ t/draft7.lua | 4 +--- 2 files changed, 24 insertions(+), 3 deletions(-) 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',