Skip to content

Commit 1319a80

Browse files
committed
optimize: used a better error message when matching failed.
1 parent 537e46f commit 1319a80

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

lib/jsonschema.lua

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local store = require 'jsonschema.store'
2+
local json_encode = require("cjson.safe").encode
23
local loadstring = loadstring
34
local tostring = tostring
45
local pairs = pairs
@@ -844,14 +845,28 @@ generate_validator = function(ctx, schema)
844845

845846
if schema.anyOf then
846847
local lasti = #schema.anyOf
848+
local requires = {}
847849
ctx:stmt('if not (')
848850
for i, subschema in ipairs(schema.anyOf) do
849851
local op = i == lasti and '' or ' or'
850852
local validator = ctx:validator({ 'anyOf', tostring(i-1) }, subschema)
851853
ctx:stmt(sformat(' %s(%s)', validator, ctx:param(1)), op)
854+
if type(subschema) == "table" and subschema.required then
855+
local str = json_encode(subschema.required)
856+
if str then
857+
tab_insert(requires, str)
858+
end
859+
end
860+
end
861+
862+
if #requires > 0 then
863+
requires = ' ": " .. ' .. sformat("%q", tab_concat(requires, " or "))
864+
else
865+
requires = ' ""'
852866
end
867+
853868
ctx:stmt(') then')
854-
ctx:stmt(' return false, "object matches none of the alternatives"')
869+
ctx:stmt(' return false, "object matches none of the requireds" .. ' .. requires)
855870
ctx:stmt('end')
856871
end
857872

0 commit comments

Comments
 (0)