Skip to content

Commit 7bd1fec

Browse files
authored
fix: make sure character % will be handled correctly (#73)
1 parent c067f11 commit 7bd1fec

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

lib/jsonschema.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ generate_validator = function(ctx, schema)
943943
end
944944
if schema.pattern then
945945
ctx:stmt(sformat(' if not %s(%s, %q) then', ctx:libfunc('custom.match_pattern'), ctx:param(1), schema.pattern))
946-
ctx:stmt(sformat(' return false, %s([[failed to match pattern %q with %%q]], %s)', ctx:libfunc('string.format'), schema.pattern, ctx:param(1)))
946+
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)))
947947
ctx:stmt( ' end')
948948
end
949949
ctx:stmt('end') -- if string

t/default.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,23 @@ for i, case in ipairs(cases) do
213213
assert(ok, string.format("fail: validate case %d, err: %s, ", i, err))
214214
end
215215
ngx.say("passed: check string len")
216+
217+
----------------------------------------------------- test case 8
218+
-- test pattern with `%`
219+
local host_pattern = [[^http(s)?:\/\/[a-zA-Z0-9-_.:\@%]+$]]
220+
local rule = {
221+
type = "object",
222+
properties = {
223+
foo = {
224+
pattern = host_pattern,
225+
},
226+
},
227+
}
228+
229+
local validator = jsonschema.generate_validator(rule)
230+
local t = {
231+
foo = "http://#baidu.com"
232+
}
233+
local ok, err = validator(t)
234+
assert(ok~=nil, ("pattern: failed to check pattern with `%%`: %s"):format(err))
235+
ngx.say("passed: pass check pattern with `%`")

0 commit comments

Comments
 (0)