valitron icon indicating copy to clipboard operation
valitron copied to clipboard

Implement JSON Schema validation

Open alexweissman opened this issue 10 years ago • 6 comments

So for a while, I've been thinking about how validation plugins for different languages/platforms each have their own particular "language" for specifying validation rules and parameters. For example, FormValidation has the greaterThan validator, while Valitron has the min rule, Symfony2 has the GreaterThan constraint, Djanjo has MinValueValidator, etc.

The lack of a standard for validation rules is a problem, because it limits interoperability between client- and server-side code, as well as among different client- and server-side languages/frameworks/plugins.

I was going to try to develop my own standard, but I found out there is already an effort called JSON Schema. Would you consider extending Valitron to be able to load validation rules from a schema in this format?

alexweissman avatar Feb 08 '15 23:02 alexweissman

I can't commit time to it right now, but I would happily accept a PR that implements this.

vlucas avatar Feb 09 '15 15:02 vlucas

Ok. I've been developing an adapter class that extends Validator. Would you want that as part of the repo, or would you rather I just modify the base class?

alexweissman avatar Feb 09 '15 16:02 alexweissman

The extension will be fine. Valitron will be undergoing a large re-organization for v2 (just in planning phase - no code yet), so it will be easier to keep the functionality separate that way.

vlucas avatar Feb 09 '15 19:02 vlucas

Cool. Is there a way to contribute to the planning? I have a few suggestions ;-)

alexweissman avatar Feb 09 '15 20:02 alexweissman

This might be problematic because associative arrays cannot be validated 100% with JsonSchema rules

http://json-guard.thephpleague.com/faq/

Why can’t I json_decode to an array? If you decoded your json as an array (json_decode($data, true)), an empty object is decoded the same >as an empty array; they both decode to []. This would make it impossible to validate “type”: “object” or >“type”: “array” when the object/array is empty.

RoboSparrow avatar Feb 07 '17 05:02 RoboSparrow

Oh cool, I haven't looked at this in a while, but I guess JSON Schema is starting to become active again?

Seems like this could be solved just by implementing a custom version of json_decode that can distinguish objects and arrays.

alexweissman avatar Feb 16 '17 00:02 alexweissman