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
9 changes: 9 additions & 0 deletions src/validation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,8 @@ end
### Checks for numbers.
###

# Bool is a Julia Number, but JSON numeric assertions do not apply to booleans.

# 6.2.1
function _validate(
x::Number,
Expand All @@ -617,6 +619,7 @@ function _validate(
val::Number,
path::_InstancePath,
)
x isa Bool && return
y = x / val
if !isfinite(y) || !isapprox(y, round(y))
return SingleIssue(x, path, "multipleOf", val)
Expand All @@ -632,6 +635,7 @@ function _validate(
val::Number,
path::_InstancePath,
)
x isa Bool && return
if x > val
return SingleIssue(x, path, "maximum", val)
end
Expand All @@ -646,6 +650,7 @@ function _validate(
val::Number,
path::_InstancePath,
)
x isa Bool && return
if x >= val
return SingleIssue(x, path, "exclusiveMaximum", val)
end
Expand All @@ -659,6 +664,7 @@ function _validate(
val::Bool,
path::_InstancePath,
)
x isa Bool && return
if val && x >= get(schema, "maximum", Inf)
return SingleIssue(x, path, "exclusiveMaximum", val)
end
Expand All @@ -673,6 +679,7 @@ function _validate(
val::Number,
path::_InstancePath,
)
x isa Bool && return
if x < val
return SingleIssue(x, path, "minimum", val)
end
Expand All @@ -687,6 +694,7 @@ function _validate(
val::Number,
path::_InstancePath,
)
x isa Bool && return
if x <= val
return SingleIssue(x, path, "exclusiveMinimum", val)
end
Expand All @@ -700,6 +708,7 @@ function _validate(
val::Bool,
path::_InstancePath,
)
x isa Bool && return
if val && x <= get(schema, "minimum", -Inf)
return SingleIssue(x, path, "exclusiveMinimum", val)
end
Expand Down
67 changes: 67 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -882,3 +882,70 @@ end
@test JSONSchema.validate(schema, "bad") isa JSONSchema.SingleIssue
end
end

@testset "Boolean instances ignore numeric assertions" begin
cases = (
("multipleOf", 2, 4, 3),
("maximum", 0, 0, 1),
("minimum", 1, 1, 0),
("exclusiveMaximum", 1, 0, 1),
("exclusiveMinimum", 0, 1, 0),
)
for (keyword, limit, good, bad) in cases
raw = Dict(keyword => limit)
schema = JSONSchema.Schema(raw)
for value in (true, false)
@test JSONSchema.validate(schema, value) === nothing
@test isvalid(schema, value)
typed =
JSONSchema.Schema(Dict("type" => "boolean", keyword => limit))
@test JSONSchema.validate(typed, value) === nothing
array = JSONSchema.Schema(Dict("items" => raw))
@test JSONSchema.validate(array, [value]) === nothing
object =
JSONSchema.Schema(Dict("properties" => Dict("flag" => raw)))
@test JSONSchema.validate(object, Dict("flag" => value)) === nothing
end
@test JSONSchema.validate(schema, good) === nothing
issue = JSONSchema.validate(schema, bad)
@test issue isa JSONSchema.SingleIssue
@test issue.reason == keyword
@test issue.path == ""
@test JSONSchema.validate(schema, Float64(good)) === nothing
@test JSONSchema.validate(schema, Float64(bad)) isa
JSONSchema.SingleIssue
for value in ("text", nothing, [], Dict("a" => 1))
@test JSONSchema.validate(schema, value) === nothing
end
end
for (keyword, boundkey, bound) in (
("exclusiveMaximum", "maximum", 0),
("exclusiveMinimum", "minimum", 1),
),
exclusive in (true, false)

schema =
JSONSchema.Schema(Dict(keyword => exclusive, boundkey => bound))
for value in (true, false)
@test JSONSchema.validate(schema, value) === nothing
end
@test (JSONSchema.validate(schema, bound) === nothing) == !exclusive
end
for typ in ("number", "integer"), value in (true, false)
@test JSONSchema.validate(
JSONSchema.Schema(Dict("type" => typ)),
value,
) isa JSONSchema.SingleIssue
end
for value in (true, false)
schema = JSONSchema.Schema(
Dict(
"anyOf" => [
Dict("type" => "boolean", "minimum" => 2),
Dict("type" => "integer", "minimum" => 2),
],
),
)
@test JSONSchema.validate(schema, value) === nothing
end
end
Loading