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
4 changes: 2 additions & 2 deletions lib/jsonschema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,8 @@ local function typeexpr(ctx, jsontype, datatype, tablekind)
elseif jsontype == 'table' then
return sformat(' %s == "table" ', datatype)
elseif jsontype == 'integer' then
return sformat(' (%s == "number" and %s(%s, 1.0) == 0.0) ',
datatype, ctx:libfunc('math.fmod'), ctx:param(1))
return sformat(' ((%s == "number" or (%s == "cdata" and tonumber(%s) ~= nil)) and %s %% 1.0 == 0.0) ',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need a test case to check it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a couple of tests

datatype, datatype, ctx:param(1), ctx:param(1))
elseif jsontype == 'string' or jsontype == 'boolean' or jsontype == 'number' then
return sformat('%s == %q', datatype, jsontype)
elseif jsontype == 'null' then
Expand Down
37 changes: 37 additions & 0 deletions t/default.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local ffi = require('ffi')
local jsonschema = require 'jsonschema'
----------------------------------------------------- test case 1
local rule = {
Expand Down Expand Up @@ -150,3 +151,39 @@ if not ok then
return
end
assert(t.foo == false, "fail: inject default false value")

----------------------------------------------------- test int64
local rule = {
type = "object",
properties = {
foo = "integer"
}
}

local validator = jsonschema.generate_validator(rule)
local t = {
foo = 1ULL
}
local ok, err = validator(t)
assert(ok, ("fail: failed to check uint64: %s"):format(err))
ngx.say("passed: pass check uint64")

local t = {
foo = -2LL
}
local ok, err = validator(t)
assert(ok, ("fail: failed to check int64: %s"):format(err))
ngx.say("passed: pass check int64")

---cdata format
ffi.cdef[[
union bar { int i;};
]]

local t = {
foo = ffi.new("union bar", {})
}

local ok = validator(t)
assert(ok~=nil, "fail: failed to negative check of int64")
ngx.say("passed: pass negative check of int64")