Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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

Merged
nicolas-grekas merged 1 commit intosymfony:masterfromxabbuh:issue-14943
Oct 24, 2018

Conversation

@xabbuh
Copy link
Member

@xabbuhxabbuh commentedJul 10, 2018
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets#12312
LicenseMIT
Doc PR

gmponos and devrck reacted with thumbs up emoji
@xabbuh
Copy link
MemberAuthor

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
Copy link
Contributor

Shouldnt we, for consistency, raise a violation per case:

if (!is_numeric($value) && !$valueinstanceof \DateTimeInterface) {
$this->context->buildViolation($constraint->invalidMessage)
->setParameter('{{ value }}',$this->formatValue($value,self::PRETTY_DATE))
->setCode(Range::INVALID_CHARACTERS_ERROR)
->addViolation();
return;
}

and basically get rid ofUnexpectedTypeException for this case.

yceruto, ogizanagi, and stoccc reacted with thumbs up emoji

@stoccc
Copy link
Contributor

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
Copy link
Contributor

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).
Also we cannot just catchUnexpectedTypeException as this can also be logic exceptions due to a developer mistake that should be fixed (i.e the constraint class check).

yceruto and stoccc reacted with thumbs up emoji

@ro0NL
Copy link
Contributor

ro0NL commentedJul 11, 2018
edited
Loading

Also i was wondering if we can somehow leverage theType constraint/validator, in terms of forwarding this validation. To do all the work basically, more concise and to avoid a new error code/message for each constraint i guess.

Maybe with some helper, it could also ease the migration of custom constraint validators.

@xabbuh
Copy link
MemberAuthor

@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
Copy link
Contributor

Oh wow :)@webmozart created all the tickets already :D

His suggestion indeed looks very good! Except im not sure about proposed strict/loose varying inType.. we dont really need that IMHO (now we have normalizers as well).

We still agree a constraint violation should be favored over an exception right?

Your example in#27917 (comment) is IMO not good in terms of DX

Still.., we need to trigger type validationsomewhere right?

@xabbuh
Copy link
MemberAuthor

@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):

Or perhaps we should introduce a newUnexpectedValueTypeException extendingUnexpectedTypeException that'll be caught and transformed into a violation by the component.
We should also inspect more thoroughly other validators.

@ro0NL
Copy link
Contributor

👍 forUnexpectedValueTypeException

@xabbuhxabbuhforce-pushed theissue-14943 branch 2 times, most recently from9d30a98 tofac281dCompareSeptember 25, 2018 08:48
@xabbuhxabbuh changed the title[Validator] catch any UnexpectedTypeException on validation[Validator] catch any UnexpectedValueException on validationSep 25, 2018
@xabbuh
Copy link
MemberAuthor

PR updated with a newUnexpectedValueException which will be caught by the validator and transformed into a constraint violation

{
private$expectedType;

publicfunction__construct($value,string$expectedType)
Copy link
Contributor

@ro0NLro0NLSep 25, 2018
edited
Loading

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 :)

Copy link
Contributor

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

Copy link
Contributor

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
Copy link
Contributor

ro0NL commentedOct 4, 2018
edited
Loading

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.abstract SimpleConstraintValidator:

getConstraintClasses(): array { return [Some::class]; }getValidTypes(): array { return ['string']; }allowsNull(): bool { return true; }

E.g.getConstraintClasses() could be guessed, similar like bundle extension class is guessed.

Also#27917 (comment) is still considerable IMHO, that solves translation messages, complex type validation and overall consistency.

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

I think we can simplify it by adding some util to the base class, e.g.$this->validType().

@xabbuh
Copy link
MemberAuthor

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
Copy link
Contributor

Well, the main idea for this PR is not to centralise logic.

But it does exactly that, inferring the constraint violation thru exception inRecursiveContextualValidator. Meaning the constraint might behave differently between validator implementations? It feels a bit like a trick, compared to raising a pure violation from the constraint validator (one way or another).

@nicolas-grekasnicolas-grekas removed this from thenext milestoneOct 20, 2018
@nicolas-grekasnicolas-grekas added this to the4.2 milestoneOct 20, 2018
@nicolas-grekas
Copy link
Member

Do you think we can resolve this before the end of the month to have it in 4.2?

@xabbuh
Copy link
MemberAuthor

xabbuh commentedOct 21, 2018
edited
Loading

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');
Copy link
Contributor

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

Copy link
Contributor

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 accessible
  • This value should be iterable
  • This value should be a countable

Copy link
Member

@nicolas-grekasnicolas-grekasOct 23, 2018
edited
Loading

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)?

Copy link
Contributor

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.

Copy link
Contributor

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

Copy link
Contributor

@ro0NLro0NLOct 23, 2018
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

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');
Copy link
Contributor

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
Copy link
Member

Thank you@xabbuh.

@nicolas-grekasnicolas-grekas merged commitfa35860 intosymfony:masterOct 24, 2018
nicolas-grekas added a commit that referenced this pull requestOct 24, 2018
…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
@xabbuhxabbuh deleted the issue-14943 branchOctober 24, 2018 07:55
This was referencedNov 3, 2018
chalasr added a commit that referenced this pull requestJan 2, 2024
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
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@dunglasdunglasdunglas approved these changes

+3 more reviewers

@ostroluckyostroluckyostrolucky left review comments

@ro0NLro0NLro0NL left review comments

@ogizanagiogizanagiogizanagi approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

4.2

Development

Successfully merging this pull request may close these issues.

8 participants

@xabbuh@ro0NL@stoccc@ogizanagi@nicolas-grekas@dunglas@ostrolucky@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp