valitron
valitron copied to clipboard
Implement JSON Schema validation
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?
I can't commit time to it right now, but I would happily accept a PR that implements this.
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?
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.
Cool. Is there a way to contribute to the planning? I have a few suggestions ;-)
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.
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.