Skip to content

Commit 72d26fe

Browse files
authored
feat: Add support of LuaJIT 64bit integer types (#66)
1 parent 62c1408 commit 72d26fe

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

lib/jsonschema.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,8 @@ local function typeexpr(ctx, jsontype, datatype, tablekind)
479479
elseif jsontype == 'table' then
480480
return sformat(' %s == "table" ', datatype)
481481
elseif jsontype == 'integer' then
482-
return sformat(' (%s == "number" and %s(%s, 1.0) == 0.0) ',
483-
datatype, ctx:libfunc('math.fmod'), ctx:param(1))
482+
return sformat(' ((%s == "number" or (%s == "cdata" and tonumber(%s) ~= nil)) and %s %% 1.0 == 0.0) ',
483+
datatype, datatype, ctx:param(1), ctx:param(1))
484484
elseif jsontype == 'string' or jsontype == 'boolean' or jsontype == 'number' then
485485
return sformat('%s == %q', datatype, jsontype)
486486
elseif jsontype == 'null' then

t/default.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local ffi = require('ffi')
12
local jsonschema = require 'jsonschema'
23
----------------------------------------------------- test case 1
34
local rule = {
@@ -150,3 +151,39 @@ if not ok then
150151
return
151152
end
152153
assert(t.foo == false, "fail: inject default false value")
154+
155+
----------------------------------------------------- test int64
156+
local rule = {
157+
type = "object",
158+
properties = {
159+
foo = "integer"
160+
}
161+
}
162+
163+
local validator = jsonschema.generate_validator(rule)
164+
local t = {
165+
foo = 1ULL
166+
}
167+
local ok, err = validator(t)
168+
assert(ok, ("fail: failed to check uint64: %s"):format(err))
169+
ngx.say("passed: pass check uint64")
170+
171+
local t = {
172+
foo = -2LL
173+
}
174+
local ok, err = validator(t)
175+
assert(ok, ("fail: failed to check int64: %s"):format(err))
176+
ngx.say("passed: pass check int64")
177+
178+
---cdata format
179+
ffi.cdef[[
180+
union bar { int i;};
181+
]]
182+
183+
local t = {
184+
foo = ffi.new("union bar", {})
185+
}
186+
187+
local ok = validator(t)
188+
assert(ok~=nil, "fail: failed to negative check of int64")
189+
ngx.say("passed: pass negative check of int64")

0 commit comments

Comments
 (0)