-
Notifications
You must be signed in to change notification settings - Fork 7
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
Null or shape? #10
Comments
Hey @Enchiridion , yeah good point. |
I'm working with an new API and just came across another issue. Some pieces of data may be multiple types, for example it can be either not present, null, false, or an array with a specific shape (the API isn't the greatest). Maybe these complex cases could be handled by a specifying a function? Then I can implement the custom logic needed and also utilize the library where possible. I think only a new rule would be needed and none of the modifiers need to be touched. |
I think you can have a new rule with your custom logic right now. Try this:
See this example for more: |
Thanks! I was able to use that to create a custom rule that's working great. Now that I've dug into the code more, I realized that the detailed error messages aren't being passed on, which would of been really helpful earlier when I was building the rules. For example: try {
$pattern = array( 'name' => ':number' );
$content = array( 'name' => 'foo' );
$builder = \PASVL\Validation\ValidatorBuilder::forArray( $pattern );
$builder->build()->validate( $content );
} catch ( \Throwable $e ) {
echo $e->getMessage();
} The Exception's message should be |
Good question, I remember that error messages were not that simple. To put it shortly it only can tell you that patterns did not match, but it can't tell you what exact pattern did not match. That is because keys are specified as patterns in general, exact key is just a special case. So the system does not know what pattern was meant for the value. So here is an example:
Both data items match both key patterns. The system just applies different combination of patterns to the data and stops whenever there is a match. Otherwise it halts with a message like the one you posted. p.s. I get your confusion and I had the same question in the past, but I am yet to come to a general solution that allows to specify keys as patterns as well as gives better messages. p.p.s. Hence the regular expressions function |
First, great library!
Is it possible to specify that a value can be NULL or an array with a particular shape?
In this example,
roadInfo
can be either NULL or the shape shown below.The text was updated successfully, but these errors were encountered: