From aebe852f49b8721876bad17d6b9abc1febb8f395 Mon Sep 17 00:00:00 2001 From: spacewander Date: Fri, 11 Dec 2020 09:49:20 +0800 Subject: [PATCH] fix: allow injecting false default value --- lib/jsonschema.lua | 4 ++-- t/default.lua | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/jsonschema.lua b/lib/jsonschema.lua index 03d6464..d76e9b4 100644 --- a/lib/jsonschema.lua +++ b/lib/jsonschema.lua @@ -626,7 +626,7 @@ generate_validator = function(ctx, schema) end ctx:stmt( ' end') -- if prop - if type(subschema) == "table" and subschema.default and + if type(subschema) == "table" and subschema.default ~= nil and (type(subschema.default) == "number" or type(subschema.default) == "string" or type(subschema.default) == "boolean" or @@ -964,7 +964,7 @@ generate_validator = function(ctx, schema) end end ctx:stmt(') then') - ctx:stmt(' return false, "matches non of the enum values"') + ctx:stmt(' return false, "matches none of the enum values"') ctx:stmt('end') end diff --git a/t/default.lua b/t/default.lua index db3df9b..16eec5c 100644 --- a/t/default.lua +++ b/t/default.lua @@ -133,3 +133,20 @@ local rule = { local validator = jsonschema.generate_validator(rule) assert(rule.id == "root:/", "fail: schema id is removed") + +----------------------------------------------------- test case 6 +local rule = { + type = "object", + properties = { + foo = {type = "boolean", default = false} + } +} + +local validator = jsonschema.generate_validator(rule) +local t = {} +local ok, err = validator(t) +if not ok then + ngx.say("fail: inject default false value: ", err) + return +end +assert(t.foo == false, "fail: inject default false value")