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
22 changes: 17 additions & 5 deletions src/validation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ function _validate(
for (k_val, v_val) in val
r = Regex(k_val)
for (k_x, v_x) in x
if match(r, k_x) === nothing
if !occursin(r, k_x)
continue
end
ret = _validate_child(v_x, v_val, path, string(k_x))
Expand All @@ -489,9 +489,15 @@ function _validate(
)
properties = get(schema, "properties", Dict{String,Any}())
patternProperties = get(schema, "patternProperties", Dict{String,Any}())
patterns = nothing
for (k, v) in x
if k in keys(properties) ||
any(r -> match(Regex(r), k) !== nothing, keys(patternProperties))
if k in keys(properties)
continue
end
if patterns === nothing
patterns = [Regex(r) for r in keys(patternProperties)]
end
if any(r -> occursin(r, k), patterns)
continue
end
ret = _validate_child(v, val, path, string(k))
Expand All @@ -514,9 +520,15 @@ function _validate(
end
properties = get(schema, "properties", Dict{String,Any}())
patternProperties = get(schema, "patternProperties", Dict{String,Any}())
patterns = nothing
for (k, v) in x
if k in keys(properties) ||
any(r -> match(Regex(r), k) !== nothing, keys(patternProperties))
if k in keys(properties)
continue
end
if patterns === nothing
patterns = [Regex(r) for r in keys(patternProperties)]
end
if any(r -> occursin(r, k), patterns)
continue
end
return SingleIssue(x, path, "additionalProperties", val)
Expand Down
78 changes: 78 additions & 0 deletions test/additional_patterns.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
@testset "Additional properties with pattern coverage" begin
properties = Dict("literal" => Dict("type" => "integer"))
patterns = Dict(
"^covered_" => Dict("type" => "string"),
"middle" => Dict("type" => "boolean"),
"^π" => true,
)
for extra in (false, true, Dict("type" => "integer"))
schema = JSONSchema.Schema(
Dict(
"properties" => properties,
"patternProperties" => patterns,
"additionalProperties" => extra,
),
)
for data in (
Dict{String,Any}(),
Dict("literal" => 2),
Dict("covered_name" => "ok"),
Dict("xmiddley" => true),
Dict("πname" => nothing),
Dict{String,Any}(
"literal" => 2,
"covered_name" => "ok",
"xmiddley" => true,
),
)
@test JSONSchema.validate(schema, data) === nothing
end
@test JSONSchema.validate(schema, Dict("covered_middle" => "ok")) isa
JSONSchema.SingleIssue
@test JSONSchema.validate(schema, Dict("literal" => false)) isa
JSONSchema.SingleIssue
@test isvalid(schema, Dict("unknown" => 2)) == (extra !== false)
@test isvalid(schema, Dict("unknown" => false)) == (extra === true)
if extra isa AbstractDict
issue = JSONSchema.validate(schema, Dict("unknown" => false))
@test issue.reason == "type"
@test issue.path == "[unknown]"
@test JSONSchema.json_pointer(issue) == "#/unknown"
elseif extra === false
issue = JSONSchema.validate(schema, Dict("unknown" => 2))
@test issue.reason == "additionalProperties"
@test JSONSchema.json_pointer(issue) == "#"
end
end

schema = JSONSchema.Schema(
Dict(
"items" => Dict(
"patternProperties" => Dict("^covered_" => true),
"additionalProperties" => Dict("type" => "integer"),
),
),
)
@test isvalid(schema, [Dict("covered_name" => nothing), Dict("other" => 3)])
issue = JSONSchema.validate(
schema,
[Dict("covered_name" => nothing), Dict("other" => "bad")],
)
@test issue.reason == "type"
@test JSONSchema.json_pointer(issue) == "#/1/other"

data = Dict{String,Any}("bucket_$(mod1(i, 5))_$i" => i for i in 1:2500)
for extra in (false, Dict("type" => "integer"))
schema = JSONSchema.Schema(
Dict(
"patternProperties" =>
Dict("^bucket_$(i)_" => true for i in 1:5),
"additionalProperties" => extra,
),
)
for _ in 1:3
@test JSONSchema.validate(schema, data) === nothing
end
@test (@allocated JSONSchema.validate(schema, data)) < 800_000
end
end
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import JSON3
import OrderedCollections
import ZipFile

include("additional_patterns.jl")

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

const SCHEMA_TEST_DIR = let
Expand Down
Loading