Skip to content

Commit

Permalink
Add support for abstract dict and vector types (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
acdupont authored Mar 31, 2023
1 parent 7670325 commit 00e0e9e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
10 changes: 7 additions & 3 deletions src/schema.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function _recurse_get_element(schema::AbstractDict, element::String)
return schema[element]
end

function _recurse_get_element(schema::Vector, element::String)
function _recurse_get_element(schema::AbstractVector, element::String)
index = tryparse(Int, element) # Remember that `index` is 0-indexed!
if index === nothing
error("expected integer array index instead of '$(element)'.")
Expand Down Expand Up @@ -115,7 +115,7 @@ end
resolve_refs!(::Any, ::URIs.URI, ::AbstractDict, ::String) = nothing

function resolve_refs!(
schema::Vector,
schema::AbstractVector,
uri::URIs.URI,
id_map::AbstractDict,
parent_dir::String,
Expand Down Expand Up @@ -162,7 +162,11 @@ end

build_id_map!(::AbstractDict, ::Any, ::URIs.URI) = nothing

function build_id_map!(id_map::AbstractDict, schema::Vector, uri::URIs.URI)
function build_id_map!(
id_map::AbstractDict,
schema::AbstractVector,
uri::URIs.URI,
)
build_id_map!.(Ref(id_map), schema, Ref(uri))
return
end
Expand Down
50 changes: 36 additions & 14 deletions src/validation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ _validate(::Any, ::Any, ::Val, ::Any, ::String) = nothing
###

# 9.2.1.1
function _validate(x, schema, ::Val{:allOf}, val::Vector, path::String)
function _validate(x, schema, ::Val{:allOf}, val::AbstractVector, path::String)
for v in val
ret = _validate(x, v, path)
if ret !== nothing
Expand All @@ -121,7 +121,7 @@ function _validate(x, schema, ::Val{:allOf}, val::Vector, path::String)
end

# 9.2.1.2
function _validate(x, schema, ::Val{:anyOf}, val::Vector, path::String)
function _validate(x, schema, ::Val{:anyOf}, val::AbstractVector, path::String)
for v in val
if _validate(x, v, path) === nothing
return
Expand All @@ -131,7 +131,7 @@ function _validate(x, schema, ::Val{:anyOf}, val::Vector, path::String)
end

# 9.2.1.3
function _validate(x, schema, ::Val{:oneOf}, val::Vector, path::String)
function _validate(x, schema, ::Val{:oneOf}, val::AbstractVector, path::String)
found_match = false
for v in val
if _validate(x, v, path) === nothing
Expand Down Expand Up @@ -162,7 +162,7 @@ end

# 9.3.1.1
function _validate(
x::Vector,
x::AbstractVector,
schema,
::Val{:items},
val::AbstractDict,
Expand All @@ -180,7 +180,13 @@ function _validate(
return _additional_items(x, schema, items, additionalItems, path)
end

function _validate(x::Vector, schema, ::Val{:items}, val::Vector, path::String)
function _validate(
x::AbstractVector,
schema,
::Val{:items},
val::AbstractVector,
path::String,
)
items = fill(false, length(x))
for (i, xi) in enumerate(x)
if i > length(val)
Expand All @@ -196,7 +202,13 @@ function _validate(x::Vector, schema, ::Val{:items}, val::Vector, path::String)
return _additional_items(x, schema, items, additionalItems, path)
end

function _validate(x::Vector, schema, ::Val{:items}, val::Bool, path::String)
function _validate(
x::AbstractVector,
schema,
::Val{:items},
val::Bool,
path::String,
)
return val || (!val && length(x) == 0) ? nothing :
SingleIssue(x, path, "items", val)
end
Expand All @@ -223,7 +235,7 @@ _additional_items(x, schema, items, val::Nothing, path) = nothing

# 9.3.1.2
function _validate(
x::Vector,
x::AbstractVector,
schema,
::Val{:additionalItems},
val,
Expand All @@ -235,7 +247,13 @@ end
# 9.3.1.3: unevaluatedProperties

# 9.3.1.4
function _validate(x::Vector, schema, ::Val{:contains}, val, path::String)
function _validate(
x::AbstractVector,
schema,
::Val{:contains},
val,
path::String,
)
for (i, xi) in enumerate(x)
ret = _validate(xi, val, path * "[$(i)]")
if ret === nothing
Expand Down Expand Up @@ -365,7 +383,7 @@ function _validate(x, schema, ::Val{:type}, val::String, path::String)
SingleIssue(x, path, "type", val) : nothing
end

function _validate(x, schema, ::Val{:type}, val::Vector, path::String)
function _validate(x, schema, ::Val{:type}, val::AbstractVector, path::String)
if !any(v -> _is_type(x, Val{Symbol(v)}()), val)
return SingleIssue(x, path, "type", val)
end
Expand Down Expand Up @@ -528,7 +546,7 @@ end

# 6.4.1
function _validate(
x::Vector,
x::AbstractVector,
schema,
::Val{:maxItems},
val::Integer,
Expand All @@ -539,7 +557,7 @@ end

# 6.4.2
function _validate(
x::Vector,
x::AbstractVector,
schema,
::Val{:minItems},
val::Integer,
Expand All @@ -550,7 +568,7 @@ end

# 6.4.3
function _validate(
x::Vector,
x::AbstractVector,
schema,
::Val{:uniqueItems},
val::Bool,
Expand Down Expand Up @@ -600,7 +618,7 @@ function _validate(
x::AbstractDict,
schema,
::Val{:required},
val::Vector,
val::AbstractVector,
path::String,
)
return any(v -> !haskey(x, v), val) ?
Expand All @@ -625,7 +643,11 @@ function _validate(
return
end

function _dependencies(x::AbstractDict, path::String, val::Union{Bool,Dict})
function _dependencies(
x::AbstractDict,
path::String,
val::Union{Bool,AbstractDict},
)
return _validate(x, val, path) === nothing
end

Expand Down

0 comments on commit 00e0e9e

Please sign in to comment.