-
Notifications
You must be signed in to change notification settings - Fork 774
Create ShortCircuit validator and ShortCircuitCapable interface #1663
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
Draft
henriquemoody
wants to merge
1
commit into
Respect:main
Choose a base branch
from
henriquemoody:validator/short-circuit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| <!-- | ||
| SPDX-FileCopyrightText: (c) Respect Project Contributors | ||
| SPDX-License-Identifier: MIT | ||
| --> | ||
|
|
||
| # ShortCircuit | ||
|
|
||
| - `ShortCircuit()` | ||
| - `ShortCircuit(Validator ...$validators)` | ||
|
|
||
| Validates the input against a series of validators, stopping at the first failure. | ||
|
|
||
| Like PHP's `&&` operator, it uses short-circuit evaluation: once the outcome is determined, remaining validators are | ||
| skipped. Unlike [AllOf](AllOf.md), which evaluates all validators and collects all failures, `ShortCircuit` returns | ||
| immediately. | ||
|
|
||
| ```php | ||
| v::shortCircuit(v::intVal(), v::positive())->assert(15); | ||
| // Validation passes successfully | ||
| ``` | ||
|
|
||
| This is useful when: | ||
|
|
||
| - You want only the first error message instead of all of them | ||
| - Later validators depend on earlier ones passing (e.g., checking a format before checking a value) | ||
| - You want to avoid unnecessary validation work | ||
|
|
||
| This validator is particularly useful in combination with [Factory](Factory.md) when later validations depend on earlier | ||
| results. For example, validating a subdivision code that depends on a valid country code: | ||
|
|
||
| ```php | ||
| $validator = v::shortCircuit( | ||
| v::key('countryCode', v::countryCode()), | ||
| v::factory(static fn($input) => v::key('subdivisionCode', v::subdivisionCode($input['countryCode']))), | ||
| ); | ||
|
|
||
| $validator->assert([]); | ||
| // → `.countryCode` must be present | ||
|
|
||
| $validator->assert(['countryCode' => 'US']); | ||
| // → `.subdivisionCode` must be present | ||
|
|
||
| $validator->assert(['countryCode' => 'US', 'subdivisionCode' => 'ZZ']); | ||
| // → `.subdivisionCode` must be a subdivision code of United States | ||
|
|
||
| $validator->assert(['countryCode' => 'US', 'subdivisionCode' => 'CA']); | ||
| // Validation passes successfully | ||
| ``` | ||
|
|
||
| Because [SubdivisionCode](SubdivisionCode.md) requires a valid country code, it only makes sense to validate the | ||
| subdivision after the country code passes. You could achieve this with [When](When.md), but you would have to repeat | ||
| `v::key('countryCode', v::countryCode())` twice. | ||
|
|
||
| ## Templates | ||
|
|
||
| This validator does not have templates of its own. It returns the result of the first failing validator, or the result | ||
| of the last validator when all pass. | ||
|
|
||
| ## Categorization | ||
|
|
||
| - Composite | ||
| - Conditions | ||
| - Nesting | ||
|
|
||
| ## Changelog | ||
|
|
||
| | Version | Description | | ||
| | ------: | :---------- | | ||
| | 3.0.0 | Created | | ||
|
|
||
| ## See Also | ||
|
|
||
| - [AllOf](AllOf.md) | ||
| - [AnyOf](AnyOf.md) | ||
| - [NoneOf](NoneOf.md) | ||
| - [OneOf](OneOf.md) | ||
| - [SubdivisionCode](SubdivisionCode.md) | ||
| - [When](When.md) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * SPDX-License-Identifier: MIT | ||
| * SPDX-FileCopyrightText: (c) Respect Project Contributors | ||
| * SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com> | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Respect\Validation\Helpers; | ||
|
|
||
| use Respect\Validation\Result; | ||
| use Respect\Validation\Validator; | ||
| use Respect\Validation\Validators\Core\ShortCircuitable; | ||
|
|
||
| trait CanEvaluateShortCircuit | ||
| { | ||
| private function evaluateShortCircuitWith(Validator $validator, mixed $input): Result | ||
| { | ||
| if ($validator instanceof ShortCircuitable) { | ||
| return $validator->evaluateShortCircuit($input); | ||
| } | ||
|
|
||
| return $validator->evaluate($input); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 should compare somewhere what
v::shortCircuit()does (localized stop-on-first-error behavior) and what->check()does (global stop-on-first-error behavior) and their differences, with helpful examples.