Skip to content

Commit 7ff3366

Browse files
authored
fix: allow injecting false default value (#47)
1 parent c5b46c7 commit 7ff3366

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/jsonschema.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ generate_validator = function(ctx, schema)
626626
end
627627
ctx:stmt( ' end') -- if prop
628628

629-
if type(subschema) == "table" and subschema.default and
629+
if type(subschema) == "table" and subschema.default ~= nil and
630630
(type(subschema.default) == "number" or
631631
type(subschema.default) == "string" or
632632
type(subschema.default) == "boolean" or
@@ -964,7 +964,7 @@ generate_validator = function(ctx, schema)
964964
end
965965
end
966966
ctx:stmt(') then')
967-
ctx:stmt(' return false, "matches non of the enum values"')
967+
ctx:stmt(' return false, "matches none of the enum values"')
968968
ctx:stmt('end')
969969
end
970970

t/default.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,20 @@ local rule = {
133133

134134
local validator = jsonschema.generate_validator(rule)
135135
assert(rule.id == "root:/", "fail: schema id is removed")
136+
137+
----------------------------------------------------- test case 6
138+
local rule = {
139+
type = "object",
140+
properties = {
141+
foo = {type = "boolean", default = false}
142+
}
143+
}
144+
145+
local validator = jsonschema.generate_validator(rule)
146+
local t = {}
147+
local ok, err = validator(t)
148+
if not ok then
149+
ngx.say("fail: inject default false value: ", err)
150+
return
151+
end
152+
assert(t.foo == false, "fail: inject default false value")

0 commit comments

Comments
 (0)