Skip to content

Commit ddd76d7

Browse files
committed
feature: supported table value as default value.
1 parent e6b06b7 commit ddd76d7

File tree

6 files changed

+74
-5
lines changed

6 files changed

+74
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
test*.lua
22
go
3+
\.*

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ test:
3030
LUA_PATH="./lib/?.lua;$(LUA_PATH)" LUA_CPATH="$(LUA_CPATH)" resty t/draft4.lua
3131
LUA_PATH="./lib/?.lua;$(LUA_PATH)" LUA_CPATH="$(LUA_CPATH)" resty t/draft6.lua
3232
LUA_PATH="./lib/?.lua;$(LUA_PATH)" LUA_CPATH="$(LUA_CPATH)" resty t/draft7.lua
33+
LUA_PATH="./lib/?.lua;$(LUA_PATH)" LUA_CPATH="$(LUA_CPATH)" resty t/default.lua
3334

3435

3536
### help: Show Makefile rules.

lib/jsonschema.lua

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,22 @@ local function str_filter(s)
390390
return s
391391
end
392392

393+
local function to_lua_code(var)
394+
if type(var) == "string" then
395+
return sformat("%q", var)
396+
end
397+
398+
if type(var) ~= "table" then
399+
return var
400+
end
401+
402+
local code = "{"
403+
for k, v in pairs(var) do
404+
code = code .. string.format("[%s] = %s,", to_lua_code(k), to_lua_code(v))
405+
end
406+
return code .. "}"
407+
end
408+
393409
generate_validator = function(ctx, schema)
394410
-- get type informations as they will be necessary anyway
395411
local datatype = ctx:localvar(sformat('%s(%s)',
@@ -495,11 +511,9 @@ generate_validator = function(ctx, schema)
495511
if type(subschema) == "table" and subschema.default and
496512
(type(subschema.default) == "number" or
497513
type(subschema.default) == "string" or
498-
type(subschema.default) == "boolean") then
499-
local default = subschema.default
500-
if type(subschema.default) == "string" then
501-
default = sformat("%q", default)
502-
end
514+
type(subschema.default) == "boolean" or
515+
type(subschema.default) == "table") then
516+
local default = to_lua_code(subschema.default)
503517
ctx:stmt( ' if propvalue == nil then')
504518
ctx:stmt(sformat(' %s[%s] = %s', ctx:param(1), str_filter(prop), default))
505519
ctx:stmt( ' end')

spec/extra/default.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[
2+
{
3+
"description": "table value as default",
4+
"schema": {
5+
"type": "object",
6+
"properties": {
7+
"rule": {
8+
"type": "array",
9+
"default": [1, 2, 3]
10+
}
11+
}
12+
},
13+
"tests": [
14+
{
15+
"description": "valid ip address",
16+
"data": {},
17+
"valid": true
18+
}
19+
]
20+
}
21+
]

t/default.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
local jsonschema = require 'jsonschema'
2+
3+
local rule = {
4+
type = "object",
5+
properties = {
6+
rule = {
7+
type = "array",
8+
default = {1, 2, 3},
9+
},
10+
base = {type = "string", default = "xxxxxxxx"}
11+
}
12+
}
13+
14+
-- local code = jsonschema.generate_validator_code(rule)
15+
-- print(code)
16+
17+
local validator = jsonschema.generate_validator(rule)
18+
19+
local conf = {}
20+
local ok = validator(conf)
21+
22+
if not ok then
23+
ngx.say("fail: check default value")
24+
return
25+
end
26+
27+
if not conf.rule then
28+
ngx.say("fail: missing default value")
29+
return
30+
end
31+
ngx.say("passed: table value as default value")

t/draft7.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ local supported = {
5252
"spec/extra/table.json",
5353
"spec/extra/ref.json",
5454
"spec/extra/format.json",
55+
"spec/extra/default.json",
5556

5657
'spec/JSON-Schema-Test-Suite/tests/draft7/type.json',
5758
'spec/JSON-Schema-Test-Suite/tests/draft7/default.json',

0 commit comments

Comments
 (0)