-
-
Notifications
You must be signed in to change notification settings - Fork 250
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
Url validation may cause an error if an object was submitted #273
Comments
I have created this issue over a week ago. Had anyone time to look at this? I can also create a PR if its being merged soonish then. |
The problem is that valitron right now will, as you point out, throw an exception if the wrong kind of input is given. See also #182 There are a few options (also outlined there):
I think the original use-case for valitron was checking submitted data from forms, so most rules assume that the input will be a string. If it's not, it's usually (in my experience) a programming error, not an input error by the user. I think that for programming errors, exceptions are the best way to notify the program that something unexpected happened, but I'm open to have you (or anyone else) change my mind on this :) |
Thank you for your answer, sorry that I am a bit late. I thought a bit about this and also discussed with other people on their opinion about this behavior. It would be great if it was possible to check if it is a string and may throw an Also I do not think that it is good to trust I'm curious for your answer and opinion on this :). If you also agree, I am up to create a PR with all the changes. Probably a big change in my eyes since we might need a Class where we can tell what type of variable we accept for each available rule. EDIT: |
I did a bit of thinking and I think the fastest way to have maximum flexibility without changing the current behavior might be something like this It's just a concept for now. There are 3 new options you could set: $validator->guardInputTypes(); If we do, there are 2 options for dealing with invalid input: //can we try to cast invalid input to the expected type?
$validator->castInputTypes();
//should we throw an exception if the type is wrong (and casting failed)?
$validator->throwInvalidArgumentException(); So to get the behavior you'd like to see, you could do this: $validator = new Validator(['someUrl' => new \stdClass()]);
$validator->guardInputTypes(true);
$validator->throwInvalidArgumentException(false);
$validator->rule('url', 'someUrl');
if (!$validator->validate()) {
die('You are a bad person!');
} What do you think? |
I think this concept is a good idea. Other than that it looks very promising. Nice 👍! |
How to reproduce
When you try to validate an URL containing an object like this example:
src/bestFileName.php
Expected Behavior
You should get the error message:
You are a bad person!
.Actual Behavior
You will get an exception message:
strpos() expects parameter 1 to be string, object given
.Stacktrace:
The text was updated successfully, but these errors were encountered: