-
-
Notifications
You must be signed in to change notification settings - Fork 39
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
Add UUID validation rule, and test #754
Draft
DikoIbragimov
wants to merge
4
commits into
yiisoft:master
Choose a base branch
from
uzdevid:master
base: master
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Validator\Rule; | ||
|
||
use Attribute; | ||
use Closure; | ||
use Yiisoft\Validator\DumpedRuleInterface; | ||
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait; | ||
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait; | ||
use Yiisoft\Validator\Rule\Trait\WhenTrait; | ||
use Yiisoft\Validator\SkipOnEmptyInterface; | ||
use Yiisoft\Validator\SkipOnErrorInterface; | ||
use Yiisoft\Validator\WhenInterface; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] | ||
class Uuid implements DumpedRuleInterface, SkipOnEmptyInterface, SkipOnErrorInterface, WhenInterface { | ||
use SkipOnEmptyTrait; | ||
use SkipOnErrorTrait; | ||
use WhenTrait; | ||
|
||
/** | ||
* @param bool $replaceChars | ||
* @param string $message | ||
* @param string $notPassedMessage | ||
* @param bool $skipOnError | ||
* @param Closure|null $when | ||
*/ | ||
public function __construct( | ||
private bool $replaceChars = false, | ||
private string $message = 'The value of {property} does not conform to the UUID format.', | ||
private string $notPassedMessage = '{Property} not passed.', | ||
private bool $skipOnError = false, | ||
private Closure|null $when = null, | ||
) { | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function getReplaceChars(): bool { | ||
return $this->replaceChars; | ||
} | ||
|
||
/** | ||
* Gets error message used when validation fails because the validated value is empty. | ||
* | ||
* @return string Error message / template. | ||
* | ||
* @see $message | ||
*/ | ||
public function getMessage(): string { | ||
return $this->message; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName(): string { | ||
return self::class; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getOptions(): array { | ||
return []; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getHandler(): string { | ||
return UuidHandler::class; | ||
} | ||
} |
This file contains 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 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Validator\Rule; | ||
|
||
use Yiisoft\Validator\Exception\UnexpectedRuleException; | ||
use Yiisoft\Validator\Result; | ||
use Yiisoft\Validator\RuleHandlerInterface; | ||
use Yiisoft\Validator\RuleInterface; | ||
use Yiisoft\Validator\ValidationContext; | ||
|
||
class UuidHandler implements RuleHandlerInterface { | ||
private const PATTERN = '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i'; | ||
private const NIL = '00000000-0000-0000-0000-000000000000'; | ||
|
||
/** | ||
* @param mixed $value | ||
* @param RuleInterface $rule | ||
* @param ValidationContext $context | ||
* @return Result | ||
*/ | ||
public function validate(mixed $value, RuleInterface $rule, ValidationContext $context): Result { | ||
if (!$rule instanceof Uuid) { | ||
throw new UnexpectedRuleException(Uuid::class, $rule); | ||
} | ||
|
||
$result = new Result(); | ||
|
||
if ($this->validateUuid($value, $rule->getReplaceChars())) { | ||
Check failure on line 30 in src/Rule/UuidHandler.php
|
||
return $result; | ||
} | ||
|
||
return $result->addError($rule->getMessage(), [ | ||
'property' => $context->getTranslatedProperty(), | ||
'Property' => $context->getCapitalizedTranslatedProperty(), | ||
]); | ||
} | ||
|
||
/** | ||
* @param string $uuid | ||
* @param bool $replaceChars | ||
* @return bool | ||
*/ | ||
protected function validateUuid(string $uuid, bool $replaceChars): bool { | ||
if ($replaceChars) { | ||
$uuid = str_replace(['urn:', 'uuid:', 'URN:', 'UUID:', '{', '}'], '', $uuid); | ||
} | ||
|
||
return $uuid === self::NIL || preg_match('/' . self::PATTERN . '/Dms', $uuid); | ||
} | ||
} |
This file contains 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,70 @@ | ||
<?php | ||
|
||
namespace Yiisoft\Validator\Tests\Rule; | ||
|
||
use Yiisoft\Validator\Rule\Url; | ||
use Yiisoft\Validator\Rule\Uuid; | ||
use Yiisoft\Validator\Rule\UuidHandler; | ||
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait; | ||
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase; | ||
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait; | ||
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait; | ||
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait; | ||
|
||
class UuidTest extends RuleTestCase { | ||
use DifferentRuleInHandlerTestTrait; | ||
use RuleWithOptionsTestTrait; | ||
use SkipOnErrorTestTrait; | ||
use WhenTestTrait; | ||
|
||
public function testGetName(): void { | ||
$rule = new Uuid(); | ||
$this->assertSame(Uuid::class, $rule->getName()); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
protected function getDifferentRuleInHandlerItems(): array { | ||
return [Uuid::class, UuidHandler::class]; | ||
} | ||
|
||
/** | ||
* @return array[] | ||
*/ | ||
public function dataValidationPassed(): array { | ||
return [ | ||
['0193ba64-5ef5-7ba3-90c1-2915349a60d3', [new Uuid()]], | ||
['89ffcfc5-d452-4eb9-8949-d35fb577f09d', [new Uuid()]], | ||
|
||
['289cef4c-b873-11ef-9cd2-0242ac120002', [new Uuid()]], | ||
]; | ||
} | ||
|
||
public function dataValidationFailed(): array { | ||
$errors = ['' => ['The value of value does not conform to the UUID format.']]; | ||
|
||
return [ | ||
['not uuid value', [new Uuid()], $errors], | ||
['ea20aba6-1fb2-45de-8582-6ed15f94501', [new Uuid()], $errors], | ||
]; | ||
} | ||
|
||
public function dataOptions(): array { | ||
return [ | ||
[new Uuid(), []], | ||
]; | ||
} | ||
|
||
public function testSkipOnError(): void { | ||
$this->testSkipOnErrorInternal(new Url(), new Url(skipOnError: true)); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testWhen(): void { | ||
$when = static fn(mixed $value): bool => $value !== null; | ||
$this->testWhenInternal(new Uuid(), new Uuid(when: $when)); | ||
} | ||
} |
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.
This property is not used.