jhthorsen / json-validator

:cop: Validate data against a JSON schema
https://metacpan.org/release/JSON-Validator
56 stars 58 forks source link

if/then/else can be used with other types than object #187

Closed karenetheridge closed 4 years ago

karenetheridge commented 4 years ago

At present, the keywords if, then and else can only be used within the 'object' type. This was probably done because the documentation example (https://json-schema.org/understanding-json-schema/reference/conditionals.html) uses objects, but it should not be limited to that type.

These keywords are described in the specification at https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.6.

Relequestual commented 4 years ago

The values of if/then/else are schemas. The schemas are then applied to the instance location and evaluated. then or else schema is applied depending on the validation result of the if value schema.

karenetheridge commented 4 years ago

@Relequestual Yup, that's how we're treating it, in https://github.com/mojolicious/json-validator/blob/master/lib/JSON/Validator.pm#L871-L876 -- it's a simple matter of adding that check to the other type validator subs as well (or actually, move that check outside of the object sub into the generic _validate sub, to run after the type-specific checks).

karenetheridge commented 4 years ago

(fixed in a branch, not yet PR'd.)

jhthorsen commented 4 years ago

Fixed by #197.