-
Notifications
You must be signed in to change notification settings - Fork 356
Custom constraints #198
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
Custom constraints #198
Conversation
Please explain the need for custom constraints outside of the JSON Schema spec? Wouldn't a custom constraint in a given schema only work for this validator and not others? |
The official spec is too concrete for normal usage; the change is mostly based on (fe) https://github.com/acornejo/jjv#custom-checks
Also slightly based on the "format" spec:
For optimal schema usage you should be able to define own constraints, outside of the spec;
And be able to test your constraint ... this allows this. Even if it is not spec.. it just opens up the possibilities. Yes you need to create a validator for the required schema validation... This may be different than a previous global validator..(if this is what you meant)? So also enhances creating a new validator specific to the requirement. |
The PR is slightly superfluous as you can add your own factory (with own custom constraints) per validator instance. |
@AubreyHewes thanks for your patience and insight. Would you please rebase and squash this PR down to a single commit? |
@bighappyface I am leaning towards this being superfluous. As via the factory you can do your own stuff anyway.. Also directly supporting custom attributes/stuff does not help the json spec. |
Allows adding custom constraints via
Background
I ran in to the problem of custom constraints and via pr #143 by @Maks3w (the factory) the possiblity was available to easily accomplish adding custom constraints.. (this is moved from a @Maks3w fork)
Implementation
This allows applying custom constraints, by name, as either a callable/className/instance.
I am not saying this is how it should be --- all input welcome.
i.e. schema
Can be constrained via
$validator->addConstraint('myCustomCheck', $constraint)
Add a callable constraint
ConstraintInterface->check
signaturefunction ($value, $schema = null, $path = null, $i = null)
Add by Constraint class-name
FQCN
must be of typeJsonSchema\Constraints\ConstraintInterface
Add by Constraint instance
MyCustomConstraint
must be of typeJsonSchema\Constraints\ConstraintInterface