Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support if-then-else #37 #53

Merged
merged 22 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b6ff3b1
use JSONPointer backend
YongHee-Kim Oct 3, 2020
45f9c04
fix test for exceptions
YongHee-Kim Oct 3, 2020
5c87171
fix uri fragment pointer
YongHee-Kim Oct 4, 2020
31ea926
fix index shfit bug
YongHee-Kim Oct 4, 2020
29cdf20
revert unnessary line chagne
YongHee-Kim Oct 4, 2020
1e4c162
remove `isnothing` for julia 1.0
YongHee-Kim Oct 6, 2020
270bc76
Merge branch 'master' of https://github.com/YongHee-Kim/JSONSchema.jl
YongHee-Kim Jul 11, 2021
d66b48d
Merge branch 'master' of https://github.com/fredo-dedup/JSONSchema.jl…
YongHee-Kim Sep 12, 2021
d5674e3
Merge remote-tracking branch 'upstream/master'
YongHee-Kim Jun 24, 2024
efc6bd0
added JSONPointer dependency
YongHee-Kim Jun 26, 2024
064ab5b
Replaced `get_element` methods with JSONPointer package
YongHee-Kim Jun 26, 2024
7a481ec
added `_if_then_esle` validation for draft7 #37
YongHee-Kim Jun 27, 2024
e9b81d1
Included draft 7 in the testt
YongHee-Kim Jun 27, 2024
6ee43bd
fixed docstring for _if_then_else
YongHee-Kim Jun 27, 2024
b758a35
Applied JuliadFormatter
YongHee-Kim Jun 27, 2024
e2e551a
Revert "Replaced `get_element` methods with JSONPointer package"
YongHee-Kim Jun 28, 2024
f30ca92
cleanup _if_then_else
YongHee-Kim Jun 28, 2024
6191fb6
`return nothing` -> `return` to keep the same rule with orginal codes
YongHee-Kim Jun 28, 2024
2c21138
Update validation.jl
odow Jun 29, 2024
24e52d1
Update validation.jl
odow Jun 29, 2024
7891e71
Update validation.jl
odow Jun 29, 2024
6ccd592
Update src/validation.jl
odow Jun 29, 2024
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
54 changes: 54 additions & 0 deletions src/validation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,64 @@ function _validate(x, schema, ::Val{:not}, val, path::String)
end

# 9.2.2.1: if
function _validate(x, schema, ::Val{:if}, val, path::String)
# ignore if without then or else
if haskey(schema, "then") || haskey(schema, "else")
return _if_then_else(x, schema, path)
end
return
end

# 9.2.2.2: then
function _validate(x, schema, ::Val{:then}, val, path::String)
# ignore then without if
if haskey(schema, "if")
return _if_then_else(x, schema, path)
end
return
end

# 9.2.2.3: else
function _validate(x, schema, ::Val{:else}, val, path::String)
# ignore else without if
if haskey(schema, "if")
return _if_then_else(x, schema, path)
end
return
end

"""
_if_then_else(x, schema, path)

The if, then and else keywords allow the application of a subschema based on the
outcome of another schema. Details are in the link and the truth table is as
follows:

```
┌─────┬──────┬──────┬────────┐
│ if │ then │ else │ result │
├─────┼──────┼──────┼────────┤
│ T │ T │ n/a │ T │
│ T │ F │ n/a │ F │
│ F │ n/a │ T │ T │
│ F │ n/a │ F │ F │
│ n/a │ n/a │ n/a │ T │
└─────┴──────┴──────┴────────┘
```

See https://json-schema.org/understanding-json-schema/reference/conditionals#ifthenelse
for details.
"""
function _if_then_else(x, schema, path)
if _validate(x, schema["if"], path) !== nothing
if haskey(schema, "else")
return _validate(x, schema["else"], path)
end
elseif haskey(schema, "then")
return _validate(x, schema["then"], path)
end
return
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

I might have made a mistake, but I think you can write this code as:

function _if_then_else(x, schema, path)
    if _validate(x, schema["if"], path) !== nothing
        if haskey(schema, "else")
            return _validate(x, schema["else"], path)
        end
    elseif haskey(schema, "then")
        return _validate(x, schema["then"], path)
    end
    return
end

but what about the case where you have a if-then and the if fails to validate?


###
### Checks for Arrays.
Expand Down
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ write(
}]""",
)

@testset "Draft 4/6" begin
@testset "Draft 4/6/7" begin
# Note(odow): I didn't want to use a mutable reference like this for the web-server.
# The obvious thing to do is to start a new server for each value of `draft_folder`,
# however, shutting down the webserver asynchronously doesn't play well with
Expand All @@ -148,6 +148,7 @@ write(
@testset "$(draft_folder)" for draft_folder in [
"draft4",
"draft6",
"draft7",
basename(abspath(LOCAL_TEST_DIR)),
]
test_dir = joinpath(SCHEMA_TEST_DIR, draft_folder)
Expand Down Expand Up @@ -190,6 +191,7 @@ end
@testset "$(draft_folder)" for draft_folder in [
"draft4",
"draft6",
"draft7",
basename(abspath(LOCAL_TEST_DIR)),
]
test_dir = joinpath(SCHEMA_TEST_DIR, draft_folder)
Expand Down
Loading