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 src/validation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ _is_type(::Any, ::Val) = false
_is_type(::Array, ::Val{:array}) = true
_is_type(::Bool, ::Val{:boolean}) = true
_is_type(::Integer, ::Val{:integer}) = true
_is_type(x::Float64, ::Val{:integer}) = isinteger(x)
_is_type(x::AbstractFloat, ::Val{:integer}) = isinteger(x)
_is_type(::Real, ::Val{:number}) = true
_is_type(::Nothing, ::Val{:null}) = true
_is_type(::Missing, ::Val{:null}) = true
Expand Down
41 changes: 41 additions & 0 deletions test/integer_floats.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@testset "Integer-valued floating-point instances" begin
integer = JSONSchema.Schema(Dict("type" => "integer"))
nullable = JSONSchema.Schema(Dict("type" => ["integer", "null"]))
number = JSONSchema.Schema(Dict("type" => "number"))
object = JSONSchema.Schema(
Dict("properties" => Dict("count" => Dict("type" => "integer"))),
)
array = JSONSchema.Schema(Dict("items" => Dict("type" => "integer")))

for T in (Float16, Float32, Float64, BigFloat)
for value in (zero(T), -zero(T), T(7), T(-2))
@test isvalid(integer, value)
@test isvalid(nullable, value)
end
for value in (T(0.5), T(-2.5), T(NaN), T(Inf), T(-Inf))
@test !isvalid(integer, value)
@test !isvalid(nullable, value)
end
@test isvalid(number, T(0.5))
@test isvalid(object, Dict("count" => T(2)))
issue = JSONSchema.validate(object, Dict("count" => T(0.5)))
@test issue.reason == "type"
@test JSONSchema.json_pointer(issue) == "#/count"
@test isvalid(array, T[1, 2])
issue = JSONSchema.validate(array, T[1, 1.5])
@test issue.reason == "type"
@test JSONSchema.json_pointer(issue) == "#/1"
end

@test isvalid(integer, BigFloat(big(2)^200))
@test isvalid(integer, big(2)^200)
@test isvalid(integer, 7)
@test isvalid(nullable, nothing)
@test isvalid(nullable, missing)
for value in (true, false, "7", nothing)
@test !isvalid(integer, value)
end
for value in (true, false)
@test !isvalid(number, value)
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import OrderedCollections
import ZipFile

include("additional_patterns.jl")
include("integer_floats.jl")

const TEST_SUITE_URL = "https://github.com/json-schema-org/JSON-Schema-Test-Suite/archive/23.1.0.zip"

Expand Down
Loading