-
Notifications
You must be signed in to change notification settings - Fork 774
Create "Formatted" validator #1657
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
Open
henriquemoody
wants to merge
1
commit into
Respect:main
Choose a base branch
from
henriquemoody:validator/formatted
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.
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| <!-- | ||
| SPDX-FileCopyrightText: (c) Respect Project Contributors | ||
| SPDX-License-Identifier: MIT | ||
| --> | ||
|
|
||
| # Formatted | ||
|
|
||
| - `Formatted(Formatter $formatter, Validator $validator)` | ||
|
|
||
| Decorates a validator to format input values in error messages while still validating the original input. | ||
|
|
||
| ```php | ||
| use Respect\StringFormatter\FormatterBuilder as f; | ||
|
|
||
| v::formatted(f::mask('1-4'), v::email())->assert('foo@example.com'); | ||
| // Validation passes successfully | ||
|
|
||
| v::formatted(f::mask('1-4'), v::email())->assert('not an email'); | ||
| // → "****an email" must be a valid email address | ||
|
|
||
| v::formatted(f::pattern('#### #### #### ####'), v::creditCard())->assert('4111111111111111'); | ||
| // Validation passes successfully | ||
|
|
||
| v::formatted(f::pattern('#### #### #### ####'), v::creditCard())->assert('1234123412341234'); | ||
| // → "1234 1234 1234 1234" must be a valid credit card number | ||
| ``` | ||
|
|
||
| This validator is useful for displaying formatted values in error messages, making them more readable for end users. For example, showing credit card numbers with spaces or phone numbers with proper formatting. | ||
|
|
||
| It uses [respect/string-formatter](https://github.com/Respect/StringFormatter) as the underlying formatting engine. See the [StringFormatter documentation](https://github.com/Respect/StringFormatter) for available formatters. | ||
|
|
||
| ## Behavior | ||
|
|
||
| The validator first ensures the input is a valid string using `StringVal`. If the input passes string validation, it validates the original input using the inner validator. The formatted version is only used for display in error messages. | ||
|
|
||
| ## Categorization | ||
|
|
||
| - Display | ||
| - Transformations | ||
|
|
||
| ## Changelog | ||
|
|
||
| | Version | Description | | ||
| | ------: | :---------- | | ||
| | 3.0.0 | Created | | ||
|
|
||
| ## See Also | ||
|
|
||
| - [Call](Call.md) | ||
| - [Masked](Masked.md) | ||
| - [Named](Named.md) | ||
| - [Templated](Templated.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
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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * SPDX-License-Identifier: MIT | ||
| * SPDX-FileCopyrightText: (c) Respect Project Contributors | ||
| * SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com> | ||
| * SPDX-FileContributor: Danilo Benevides <danilobenevides01@gmail.com> | ||
| * SPDX-FileContributor: Graham Campbell <graham@mineuk.com> | ||
| * SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com> | ||
| * SPDX-FileContributor: Jayson Reis <santosdosreis@gmail.com> | ||
| * SPDX-FileContributor: Nick Lombard <github@jigsoft.co.za> | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Respect\Validation\Validators; | ||
|
|
||
| use Attribute; | ||
| use Respect\StringFormatter\Formatter; | ||
| use Respect\Validation\Result; | ||
| use Respect\Validation\Validator; | ||
|
|
||
| #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] | ||
| final readonly class Formatted implements Validator | ||
| { | ||
| public function __construct( | ||
| private Formatter $formatter, | ||
| private Validator $validator, | ||
| ) { | ||
| } | ||
|
|
||
| public function evaluate(mixed $input): Result | ||
| { | ||
| $stringVal = new StringVal(); | ||
| $stringValResult = $stringVal->evaluate($input); | ||
| if (!$stringValResult->hasPassed) { | ||
| return $stringValResult->withNameFrom($this->validator)->withIdFrom($this->validator); | ||
| } | ||
|
|
||
| return $this->validator->evaluate($input)->withInput($this->formatter->format((string) $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.
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.
I see GitHub marking this file as newly added. Is this a moved file from somewhere else that GitHub didn't caught?
If it's a new file, perhaps we should review its FileContributor entries.