Skip to content

Commit 236053b

Browse files
committed
bugfix: the input data should be empty table if the field required is an empty table too.
local rule = { type = "object", properties = { username = { type = "string" }, passwd = { type = "string" }, }, oneOf = { {required = {"username", "passwd"}}, {required = {}} } } local conf = {passwd = "passwd", username = "name"} -- valid local conf = {} -- valid local conf = {passwd = "passwd"} -- invalid
1 parent f0a20ad commit 236053b

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
OR_EXEC ?= $(shell which openresty)
22
LUA_JIT_DIR ?= $(shell ${OR_EXEC} -V 2>&1 | grep prefix | grep -Eo 'prefix=(.*?)/nginx' | grep -Eo '/.*/')luajit
33
LUAROCKS_VER ?= $(shell luarocks --version | grep -E -o "luarocks [0-9]+.")
4-
LUA_PATH ?= $(shell lua -e "print(package.path)")
5-
LUA_CPATH ?= $(shell lua -e "print(package.cpath)")
4+
LUA_PATH ?= $(shell luajit -e "print(package.path)")
5+
LUA_CPATH ?= $(shell luajit -e "print(package.cpath)")
66

77

88
### help: Show Makefile rules.
@@ -16,12 +16,12 @@ help:
1616
### dev: Create a development ENV
1717
.PHONY: dev
1818
dev:
19-
ifeq ($(UNAME),Darwin)
19+
ifeq ($(LUAROCKS_VER),luarocks 3.)
2020
luarocks install --lua-dir=$(LUA_JIT_DIR) rockspec/jsonschema-master-0.rockspec --only-deps
21-
else ifneq ($(LUAROCKS_VER),'luarocks 3.')
22-
luarocks install rockspec/jsonschema-master-0.rockspec --only-deps
21+
luarocks install --lua-dir=$(LUA_JIT_DIR) luaposix
2322
else
24-
luarocks install --lua-dir=/usr/local/openresty/luajit jsonschema-master-0.rockspec --only-deps
23+
luarocks install rockspec/jsonschema-master-0.rockspec --only-deps
24+
luarocks install luaposix
2525
endif
2626

2727

lib/jsonschema.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,13 @@ generate_validator = function(ctx, schema)
653653
end
654654

655655
ctx:stmt('end') -- if object
656+
657+
if schema.required and #schema.required == 0 then
658+
-- return false if the input data is not empty
659+
ctx:stmt(sformat('if %s ~= 1 then', datakind))
660+
ctx:stmt( ' return false, "the input data should be an empty table"')
661+
ctx:stmt( 'end')
662+
end
656663
end
657664

658665
-- array checks

t/default.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,34 @@ if not conf.rule then
2828
ngx.say("fail: missing default value")
2929
return
3030
end
31+
32+
rule = {
33+
type = "object",
34+
properties = {
35+
username = { type = "string" },
36+
passwd = { type = "string" },
37+
},
38+
oneOf = {
39+
{required = {"username", "passwd"}},
40+
{required = {}}
41+
}
42+
}
43+
44+
validator = jsonschema.generate_validator(rule)
45+
46+
local ok, err = validator({passwd = "passwd", username = "name"})
47+
if not ok then
48+
ngx.say("fail: check default value: ", err)
49+
end
50+
51+
ok, err = validator({})
52+
if not ok then
53+
ngx.say("fail: check default value: ", err)
54+
end
55+
56+
ok = validator({passwd = "passwd"})
57+
if ok then
58+
ngx.say("fail: expect to fail")
59+
end
60+
3161
ngx.say("passed: table value as default value")

0 commit comments

Comments
 (0)