Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/jsonschema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ generate_validator = function(ctx, schema)
end
if schema.pattern then
ctx:stmt(sformat(' if not %s(%s, %q) then', ctx:libfunc('custom.match_pattern'), ctx:param(1), schema.pattern))
ctx:stmt(sformat(' return false, %s([[failed to match pattern %q with %%q]], %s)', ctx:libfunc('string.format'), schema.pattern, ctx:param(1)))
ctx:stmt(sformat(' return false, %s([[failed to match pattern %q with %%q]], %s)', ctx:libfunc('string.format'), string.gsub(schema.pattern, "%%", "%%%%"), ctx:param(1)))
ctx:stmt( ' end')
end
ctx:stmt('end') -- if string
Expand Down
20 changes: 20 additions & 0 deletions t/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,23 @@ for i, case in ipairs(cases) do
assert(ok, string.format("fail: validate case %d, err: %s, ", i, err))
end
ngx.say("passed: check string len")

----------------------------------------------------- test case 8
-- test pattern with `%`
local host_pattern = [[^http(s)?:\/\/[a-zA-Z0-9-_.:\@%]+$]]
local rule = {
type = "object",
properties = {
foo = {
pattern = host_pattern,
},
},
}

local validator = jsonschema.generate_validator(rule)
local t = {
foo = "http://#baidu.com"
}
local ok, err = validator(t)
assert(ok~=nil, ("pattern: failed to check pattern with `%%`: %s"):format(err))
ngx.say("passed: pass check pattern with `%`")