Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[Validator] catch any UnexpectedValueException on validation#27917
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
xabbuh commentedJul 10, 2018
This change would fix issues like#14943 (and many other related issues). I didn't add any tests yet, but would like to get some feedback first as setting them up seems to be quite complex. I am not sure if anyone could have actually relied on the previous behaviour. If we think that this was a supported use case, we will probably have to make this feature opt-in. |
ro0NL commentedJul 10, 2018
Shouldnt we, for consistency, raise a violation per case:
and basically get rid of |
stoccc commentedJul 10, 2018
Imho a validator should not throw exceptions if you ask to validate unexpected data types, that could come from user or whatever untrusted, it should just add violations. |
ogizanagi commentedJul 11, 2018
I agree with@ro0NL , we should handle it per case in each validator. Relates to#26477 (comment) too where I also suggested this. Handling this properly in each validator would not interrupt the validation and keep adding other violations (and answer the AllValidator issue too). |
ro0NL commentedJul 11, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Also i was wondering if we can somehow leverage the Maybe with some helper, it could also ease the migration of custom constraint validators. |
xabbuh commentedSep 24, 2018
@ro0NL Your example in#27917 (comment) is IMO not good in terms of DX. The goal of this PR is precisely to lower the burden when writing constraint validators. But@ogizanagi has a valid point in the exception could as well have been thrown because of the wrong constraint being passed. What do you think about a different approach like the one@webmozart suggested in#12312? |
ro0NL commentedSep 24, 2018
Oh wow :)@webmozart created all the tickets already :D His suggestion indeed looks very good! Except im not sure about proposed strict/loose varying in We still agree a constraint violation should be favored over an exception right?
Still.., we need to trigger type validationsomewhere right? |
xabbuh commentedSep 24, 2018
@ro0NL I still think it's okay to throw an exception in the validator which will then be transformed into a proper constraint violation. @ogizanagi made another good suggestion in#26477 (comment):
|
ro0NL commentedSep 24, 2018
👍 for |
9d30a98 tofac281dComparexabbuh commentedSep 25, 2018
PR updated with a new |
| { | ||
| private$expectedType; | ||
| publicfunction__construct($value,string$expectedType) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
what aboutarray $expectedTypes. Right nowarray or Traversable is not really a friendly translation parameter. We should leverage the new message formatter for this :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
That assumes it's all unions. It will prevent consumers to use more complicated rules in future, e.g. intersection types. Also, I'm interested how would you improve messagearray or Traversable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
maybe
throw UnexpectedValueException::notUnionOf($value, $type, ...$type);throw UnexpectedValueException::notIntersectionOf($value, $type, ...$type);ro0NL commentedOct 4, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
From#28645 (comment) not sure the current approach is blocking for@nicolas-grekas Alternatively, what about exposing the variables and pre-validate as such in i.e. E.g. Also#27917 (comment) is still considerable IMHO, that solves translation messages, complex type validation and overall consistency.
I think we can simplify it by adding some util to the base class, e.g. |
xabbuh commentedOct 4, 2018
Well, the main idea for this PR is not to centralise logic. I mean I would be fine with adding more code to our core constraint validators to achieve the same. The code added here is more important for individual constraint validators written for applications where users do not want to deal with more code. |
ro0NL commentedOct 4, 2018
But it does exactly that, inferring the constraint violation thru exception in |
nicolas-grekas commentedOct 20, 2018
Do you think we can resolve this before the end of the month to have it in 4.2? |
xabbuh commentedOct 21, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
From my point of view the PR is finished. Do you think we should take another direction? |
| if (!\is_array($value) && !($valueinstanceof \Traversable &&$valueinstanceof \ArrayAccess)) { | ||
| thrownewUnexpectedTypeException($value,'array or Traversable and ArrayAccess'); | ||
| thrownewUnexpectedValueException($value,'array or Traversable and ArrayAccess'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
i still think we need to tackle this being translator friendly, it'll only work for english
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
e.g. different messages like
This value should be of type {{ type }}(generic)This value should be array accessibleThis value should be iterableThis value should be a countable
nicolas-grekasOct 23, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
what about usingarray|(Traversable&ArrayAccess)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
sounds like a plan yes :) the messages dont have to be pretty (tailored), but should be correct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
We don't translate exception messages
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
the issue happens here:https://github.com/symfony/symfony/pull/27917/files#diff-d46c902789b437519780ee8110ecb5e9R807 (inRecursiveContextualValidator)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
| if (!\is_array($value) && !$valueinstanceof \Traversable) { | ||
| thrownewUnexpectedTypeException($value,'array or Traversable'); | ||
| thrownewUnexpectedValueException($value,'array or Traversable'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
lets useiterable here
nicolas-grekas commentedOct 24, 2018
Thank you@xabbuh. |
…dation (xabbuh)This PR was merged into the 4.2-dev branch.Discussion----------[Validator] catch any UnexpectedValueException on validation| Q | A| ------------- | ---| Branch? | master| Bug fix? | no| New feature? | yes| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets |#12312| License | MIT| Doc PR |Commits-------fa35860 catch any UnexpectedValueException on validation
This PR was merged into the 5.4 branch.Discussion----------[Validator] fix the exception being thrown| Q | A| ------------- | ---| Branch? | 5.4| Bug fix? | yes| New feature? | no| Deprecations? | no| Issues || License | MITAn `UnexpectedValueException` is caught and transformed into a violation (`UnexpectedTypeException` indicates a misconfiguration of a constraint and is not caught).see also#27917Commits-------3a8f10b fix the exception being thrown
Uh oh!
There was an error while loading.Please reload this page.