-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Add a constraint to sequentially validate a set of constraints #34456
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
Conversation
…n value to mocked validate method calls (ogizanagi) This PR was merged into the 3.4 branch. Discussion ---------- [Validator] ConstraintValidatorTestCase: add missing return value to mocked validate method calls | Q | A | ------------- | --- | Branch? | 3.4 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | N/A <!-- prefix each issue number with "Fix #", if any --> | License | MIT | Doc PR | N/A Spotted while working on #34456. Not sure it should really qualify as a bugfix, but the `ContextualValidatorInterface::validate` method is expected to return the instance. If [chaining in a validator](https://github.com/symfony/symfony/pull/34456/files#diff-0e6e3106aa637d750d47e86a14cef8d4R43), trying to use this test methods would throw an error, trying to call a method on `null`. Commits ------- 8d1f326 [Validator] ConstraintValidatorTestCase: add missing return value to mocked validate method calls
I like this idea a lot (and the PR explains it perfectly! thanks for that). Just as a very minor comment: given that we already use
|
@javiereguiluz : I hesitated to explain this point in the PR, but I preferred to avoid naming it |
src/Symfony/Component/Validator/Constraints/SequentiallyValidator.php
Outdated
Show resolved
Hide resolved
rebase needed |
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.
Already used in one of my projects so far no issue detected.
Rebased |
Thank you @ogizanagi. |
…a set of constraints (ogizanagi) This PR was merged into the 5.1-dev branch. Discussion ---------- [Validator] Add a constraint to sequentially validate a set of constraints | Q | A | ------------- | --- | Branch? | master <!-- see below --> | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | N/A <!-- prefix each issue number with "Fix #", if any --> | License | MIT | Doc PR | Todo Follows #20017 (comment) given some feedbacks about the suggested feature. ```php /** * @var string * * @Assert\Sequentially({ * @Assert\Type("string"), * @Assert\Length(min="4"), * @Assert\Regex("[a-z]"), * @SomeCustomConstraintWithHeavyExternalCalls(), * }) */ public $foo; ``` This new `Sequentially` constraint solves - with less power but better DX - some of the use-cases of the `GroupSequence` feature, allowing to interrupt the validation of some constraints if a previous one in the list failed before. Constraints are validated in given order, and the first violation raised will prevent other constraint validators to be executed. It can either prevent unexpected type exceptions thrown by further constraints or heavy & unnecessary calls to a database or external services if the value to validate already doesn't match some of the basic requirements. Commits ------- dfd9038 [Validator] Add a constraint to sequentially validate a set of constraints
Sorry if this feedback is too late, but I think it would be helpful to have a non-sequential constraint also. Using the original example of checking the type, prior to check the length and then the pattern, and finally using the value in a query -- it is technically acceptable to check the pattern despite having an incorrect length. The following is to illustrate what I think should be ideally supported, by introducing a
Not that the I was looking to introduce a PR for a breakable constraint before discovering this one already existed. With that, I had in mind to name the constraints |
@courtney-miles : That makes sense. With the /**
* @var string
*
* @Assert\Sequentially({
* @Assert\Type("string"),
* @MyFooFormatRequirements(), <-- Length + Regex in a custom Compound constraint
* @SomeCustomConstraintWithHeavyExternalCalls(),
* })
*/
public $foo; If you think this It'll basically work like the Compound one, allowing to provide the |
Follows #20017 (comment) given some feedbacks about the suggested feature.
This new
Sequentially
constraint solves - with less power but better DX - some of the use-cases of theGroupSequence
feature, allowing to interrupt the validation of some constraints if a previous one in the list failed before. Constraints are validated in given order, and the first violation raised will prevent other constraint validators to be executed.It can either prevent unexpected type exceptions thrown by further constraints or heavy & unnecessary calls to a database or external services if the value to validate already doesn't match some of the basic requirements.