-
Notifications
You must be signed in to change notification settings - Fork 774
Create "Patterned" validator #1654
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
Closed
Closed
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| <!-- | ||
| SPDX-FileCopyrightText: (c) Respect Project Contributors | ||
| SPDX-License-Identifier: MIT | ||
| --> | ||
|
|
||
| # Patterned | ||
|
|
||
| - `Patterned(string $pattern, Validator $validator)` | ||
|
|
||
| Decorates a validator to format input values using a pattern in error messages while still validating the original unformatted input. | ||
|
|
||
| ```php | ||
| v::patterned('0000 0000 0000 0000', v::creditCard())->assert('4111111111111111'); | ||
| // Validation passes successfully | ||
|
|
||
| v::patterned('0000 0000 0000 0000', v::creditCard())->assert('4111111111111112'); | ||
| // → "4111 1111 1111 1112" must be a valid credit card number | ||
|
|
||
| v::patterned('0{3}.0{3}.0{3}-0{2}', v::cpf())->assert('12345678900'); | ||
| // → "123.456.789-00" must be a valid CPF number | ||
|
|
||
| v::patterned('(0{2}) 0{5}-0{4}', v::phone())->assert('11987654321'); | ||
| // → "(11) 98765-4321" must be a valid telephone number | ||
| ``` | ||
|
|
||
| This validator is useful for displaying formatted values in error messages when the original input lacks formatting characters. | ||
|
|
||
| It uses [respect/string-formatter](https://github.com/Respect/StringFormatter) as the underlying formatting engine. See the documentation of [PatternFormatter](https://github.com/Respect/StringFormatter/blob/main/docs/PatternFormatter.md) for more information about the pattern syntax. | ||
|
|
||
| ## Categorization | ||
|
|
||
| - Display | ||
| - Miscellaneous | ||
|
|
||
| ## Behavior | ||
|
|
||
| The validator first ensures the input is a valid string using `StringVal`. If the input passes string validation, it validates the original unformatted input using the inner validator. If validation fails, it applies the pattern formatting to the input value shown in error messages. | ||
|
|
||
| ## Changelog | ||
|
|
||
| | Version | Description | | ||
| | ------: | :---------- | | ||
| | 3.0.0 | Created | | ||
|
|
||
| ## See Also | ||
|
|
||
| - [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
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,46 @@ | ||
| <?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\Validators; | ||
|
|
||
| use Attribute; | ||
| use Respect\StringFormatter\InvalidFormatterException; | ||
| use Respect\StringFormatter\PatternFormatter; | ||
| use Respect\Validation\Exceptions\InvalidValidatorException; | ||
| use Respect\Validation\Result; | ||
| use Respect\Validation\Validator; | ||
|
|
||
| #[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] | ||
| final readonly class Patterned implements Validator | ||
| { | ||
| private PatternFormatter $patternFormatter; | ||
|
|
||
| public function __construct( | ||
| private string $pattern, | ||
| private Validator $validator, | ||
| ) { | ||
| try { | ||
| $this->patternFormatter = new PatternFormatter($this->pattern); | ||
| } catch (InvalidFormatterException $exception) { | ||
| throw new InvalidValidatorException($exception->getMessage()); | ||
| } | ||
| } | ||
|
|
||
| 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->patternFormatter->format((string) $input)); | ||
| } | ||
| } |
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,25 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * SPDX-License-Identifier: MIT | ||
| * SPDX-FileCopyrightText: (c) Respect Project Contributors | ||
| * SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com> | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| test('input is not a string', catchAll( | ||
| fn() => v::patterned('0{3}.0{3}.0{3}-0{2}', v::cpf())->assert(new stdClass()), | ||
| fn(string $message, string $fullMessage, array $messages) => expect() | ||
| ->and($message)->toBe('`stdClass {}` must be a string value') | ||
| ->and($fullMessage)->toBe('- `stdClass {}` must be a string value') | ||
| ->and($messages)->toBe(['cpf' => '`stdClass {}` must be a string value']), | ||
| )); | ||
|
|
||
| test('failed validator', catchAll( | ||
| fn() => v::patterned('0{3}.0{3}.0{3}-0{2}', v::cpf())->assert('12345678900'), | ||
| fn(string $message, string $fullMessage, array $messages) => expect() | ||
| ->and($message)->toBe('"123.456.789-00" must be a valid CPF number') | ||
| ->and($fullMessage)->toBe('- "123.456.789-00" must be a valid CPF number') | ||
| ->and($messages)->toBe(['cpf' => '"123.456.789-00" must be a valid CPF number']), | ||
| )); | ||
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,58 @@ | ||
| <?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\Validators; | ||
|
|
||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\Attributes\Test; | ||
| use Respect\StringFormatter\PatternFormatter; | ||
| use Respect\Validation\Exceptions\InvalidValidatorException; | ||
| use Respect\Validation\Test\TestCase; | ||
| use Respect\Validation\Test\Validators\Stub; | ||
|
|
||
| #[CoversClass(Patterned::class)] | ||
| final class PatternedTest extends TestCase | ||
| { | ||
| #[Test] | ||
| public function shouldNotAllowCreatingValidatorWithAnInvalidPattern(): void | ||
| { | ||
| $pattern = ''; | ||
|
|
||
| $this->expectException(InvalidValidatorException::class); | ||
|
|
||
| new Patterned($pattern, Stub::daze()); | ||
| } | ||
|
|
||
| #[Test] | ||
| #[DataProvider('providerForNonStringValues')] | ||
| public function shouldNotValidateWhenInputIsNotStringValue(mixed $input): void | ||
| { | ||
| $this->assertInvalidInput(new Patterned('0{3}.0{3}.0{3}-0{2}', Stub::any(1)), $input); | ||
| } | ||
|
|
||
| #[Test] | ||
| #[DataProvider('providerForStringValues')] | ||
| public function shouldFormatTheInputWhenInputIsStringValue(mixed $input): void | ||
| { | ||
| $patternFormatter = new PatternFormatter('0{3}.0{3}.0{3}-0{2}'); | ||
|
|
||
| $stub = Stub::pass(2); | ||
| $comparableResult = $stub->evaluate($input); | ||
|
|
||
| $validator = new Patterned('0{3}.0{3}.0{3}-0{2}', $stub); | ||
|
|
||
| $result = $validator->evaluate($input); | ||
|
|
||
| self::assertSame($patternFormatter->format((string) $input), $result->input); | ||
| self::assertSame($comparableResult->hasPassed, $result->hasPassed); | ||
| self::assertSame($comparableResult->validator, $result->validator); | ||
| } | ||
| } |
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.
This example is misleading. The use of
$implies a monterary value, but the validator used is a phone number.Suggestion: Keep the phone number, and instead add examples for the most popular use cases: