jhthorsen / json-validator

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

custom constraints #131

Closed beyondcreed closed 5 years ago

beyondcreed commented 5 years ago

I am still reviewing json schema drafts, but was wondering if there was a way to specify custom validator rule or constraint.

e.g. lets say you wanna validate email in backend and return error if one is found, is there a way to specify property so that validator could execute custom handler for it. In this instance, we would query db to see if such record exists.

jhthorsen commented 5 years ago

I can't come up with a nice way to do this, without bringing JSON::Validator out of scope for what it's designed to do.

beyondcreed commented 5 years ago

yeah, was just curious how they dealt (or if they even did) with this in the drafts. it would be nice to have definitions for lang neutral custom validators in addition to prebuilt constructs.

beyondcreed commented 5 years ago

another thing, while we are on the subject, is chained validators. If we can make a chain of rules that must all pass or at least some to satisfy constraint imposed on param.

jhthorsen commented 5 years ago

Can you show me where in the drafts they talk about this?

Can't you use "allOf":[...] for chained validation? https://github.com/jhthorsen/json-validator/blob/master/t/jv-allof.t

beyondcreed commented 5 years ago

never saw it in the drafts, that is why I was asking above, thought maybe you knew of anything. ah, allOf, anyOf should work for that use case, thanks.

so its just the custom validators at this point.