Skip to content

Enhance oneOf validation errors #45

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 15 additions & 11 deletions src/Json/Schema/Validation.elm
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ type ValidationError
| Enum
| Const
| InvalidType String
| OneOfNoneSucceed
| OneOfManySucceed Int
| OneOfNoneSucceed (List (Result (List Error) Value))
| OneOfManySucceed (List (Result (List Error) Value))
| Not
| UnresolvableReference String
| AlwaysFail
Expand Down Expand Up @@ -947,18 +947,22 @@ validate validationOptions pool value rootSchema schema =
Decode.value
(\oneOf val ->
let
validSubschema schemaLocal =
validateSchema validationOptionsLocal jsonPointer val schemaLocal == Ok val
validationResults =
oneOf |> List.map (validateSchema validationOptionsLocal jsonPointer val)

successfulValidations =
validationResults
|> List.filterMap Result.toMaybe
in
case oneOf |> List.filter validSubschema |> List.length of
1 ->
Ok val
case successfulValidations of
v :: [] ->
Ok v

0 ->
Err [ Error jsonPointer OneOfNoneSucceed ]
[] ->
Err [ Error jsonPointer <| OneOfNoneSucceed validationResults ]

len ->
Err [ Error jsonPointer <| OneOfManySucceed len ]
_ ->
Err [ Error jsonPointer <| OneOfManySucceed validationResults ]
)

validateNot : ValidationOptions -> JsonPointer -> Value -> SubSchema -> Result (List Error) Value
Expand Down