forked from symfony/symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
add 'Yaml', 'Xml, and 'Jwt' constraints #1
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
sfmok
wants to merge
1
commit into
7.2
Choose a base branch
from
feat/format-validator
base: 7.2
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
32 changes: 32 additions & 0 deletions
32
src/Symfony/Component/Validator/Constraints/AbstractFormat.php
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,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
/** | ||
* Used for the format of values. | ||
* | ||
* @author Mokhtar Tlili <tlili.mokhtar@gmail.com> | ||
*/ | ||
abstract class AbstractFormat extends Constraint | ||
{ | ||
public string $format; | ||
public string $message = 'This value should be valid {{ format }}.'; | ||
|
||
public function __construct(?string $message = null, ?array $options = null, ?array $groups = null, mixed $payload = null) | ||
{ | ||
parent::__construct($options, $groups, $payload); | ||
|
||
$this->message = $message ?? $this->message; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/Symfony/Component/Validator/Constraints/AbstractFormatValidator.php
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 | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Constraints; | ||
|
||
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
use Symfony\Component\Validator\Exception\UnexpectedValueException; | ||
|
||
/** | ||
* Provides a base class for the validation of value format. | ||
* | ||
* @author Mokhtar Tlili <tlili.mokhtar@gmail.com> | ||
*/ | ||
abstract class AbstractFormatValidator extends ConstraintValidator | ||
{ | ||
public function validate(mixed $value, Constraint $constraint): void | ||
{ | ||
if (!$constraint instanceof AbstractFormat) { | ||
throw new UnexpectedTypeException($constraint, AbstractFormat::class); | ||
} | ||
|
||
if (null === $value || '' === $value) { | ||
return; | ||
} | ||
|
||
if (!\is_scalar($value) && !$value instanceof \Stringable) { | ||
throw new UnexpectedValueException($value, 'string'); | ||
} | ||
|
||
if ($this->validateFormat((string) $value)) { | ||
return; | ||
} | ||
|
||
$this->context->buildViolation($constraint->message) | ||
->setParameter('{{ value }}', $this->formatValue($value)) | ||
->setParameter('{{ format }}', $constraint->format) | ||
->setCode($this->getErrorCode()) | ||
->addViolation(); | ||
} | ||
|
||
/** | ||
* Validate format value | ||
*/ | ||
abstract protected function validateFormat(string $value): bool; | ||
|
||
abstract protected function getErrorCode(): string; | ||
} |
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,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Constraints; | ||
|
||
/** | ||
* Validates that a value is a valid jwt format. | ||
* | ||
* @author Mokhtar Tlili <tlili.mokhtar@gmail.com> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
class Jwt extends AbstractFormat | ||
{ | ||
public const INVALID_JWT_ERROR = '8f14e45f-e6e5-4c6c-9c17-68f22a1f3dff'; | ||
|
||
protected const ERROR_NAMES = [ | ||
self::INVALID_JWT_ERROR => 'INVALID_JWT_ERROR', | ||
]; | ||
|
||
public string $format = 'JWT'; | ||
} |
48 changes: 48 additions & 0 deletions
48
src/Symfony/Component/Validator/Constraints/JwtValidator.php
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,48 @@ | ||||||||||||||||||
<?php | ||||||||||||||||||
|
||||||||||||||||||
/* | ||||||||||||||||||
* This file is part of the Symfony package. | ||||||||||||||||||
* | ||||||||||||||||||
* (c) Fabien Potencier <fabien@symfony.com> | ||||||||||||||||||
* | ||||||||||||||||||
* For the full copyright and license information, please view the LICENSE | ||||||||||||||||||
* file that was distributed with this source code. | ||||||||||||||||||
*/ | ||||||||||||||||||
|
||||||||||||||||||
namespace Symfony\Component\Validator\Constraints; | ||||||||||||||||||
|
||||||||||||||||||
/** | ||||||||||||||||||
* @author Mokhtar Tlili <tlili.mokhtar@gmail.com> | ||||||||||||||||||
*/ | ||||||||||||||||||
class JwtValidator extends AbstractFormatValidator | ||||||||||||||||||
{ | ||||||||||||||||||
protected function validateFormat(string $value): bool | ||||||||||||||||||
{ | ||||||||||||||||||
// Split the JWT into its three parts | ||||||||||||||||||
$parts = explode('.', $value); | ||||||||||||||||||
|
||||||||||||||||||
// JWT should have exactly three parts | ||||||||||||||||||
if (count($parts) !== 3) { | ||||||||||||||||||
return false; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
[$header, $payload, $signature] = $parts; | ||||||||||||||||||
|
||||||||||||||||||
// Replace URL-safe characters with standard Base64 characters and decode | ||||||||||||||||||
$decodedHeader = base64_decode(strtr($header, '-_', '+/')); | ||||||||||||||||||
$decodedPayload = base64_decode(strtr($payload, '-_', '+/')); | ||||||||||||||||||
|
||||||||||||||||||
// Check if the decoded header and payload are valid JSON | ||||||||||||||||||
if (!json_validate($decodedHeader) || !json_validate($decodedPayload)) { | ||||||||||||||||||
return false; | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
// At this point, it looks like a JWT | ||||||||||||||||||
return true; | ||||||||||||||||||
Comment on lines
+35
to
+41
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.
Suggested change
|
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
protected function getErrorCode(): string | ||||||||||||||||||
{ | ||||||||||||||||||
return Jwt::INVALID_JWT_ERROR; | ||||||||||||||||||
} | ||||||||||||||||||
} |
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,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Constraints; | ||
|
||
/** | ||
* Validates that a value is a valid xml format. | ||
* | ||
* @author Mokhtar Tlili <tlili.mokhtar@gmail.com> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
class Xml extends AbstractFormat | ||
{ | ||
public const INVALID_XML_ERROR = '0355230a-97b8-49da-b8cd-985bf3345bcf'; | ||
|
||
protected const ERROR_NAMES = [ | ||
self::INVALID_XML_ERROR => 'INVALID_XML_ERROR', | ||
]; | ||
|
||
public string $format = 'XML'; | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Symfony/Component/Validator/Constraints/XmlValidator.php
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,28 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Constraints; | ||
|
||
/** | ||
* @author Mokhtar Tlili <tlili.mokhtar@gmail.com> | ||
*/ | ||
class XmlValidator extends AbstractFormatValidator | ||
{ | ||
protected function validateFormat(string $value): bool | ||
{ | ||
return false !== @simplexml_load_string($value); | ||
} | ||
|
||
protected function getErrorCode(): string | ||
{ | ||
return Xml::INVALID_XML_ERROR; | ||
} | ||
} |
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,45 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Constraints; | ||
|
||
use Symfony\Component\Validator\Exception\LogicException; | ||
use Symfony\Component\Yaml\Yaml as SymfonyYaml; | ||
|
||
/** | ||
* Validates that a value is a valid yaml format. | ||
* | ||
* @author Mokhtar Tlili <tlili.mokhtar@gmail.com> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
class Yaml extends AbstractFormat | ||
{ | ||
public const INVALID_YAML_ERROR = '8f14e45f-e6e5-4c6c-9c17-68f22a1f3dff'; | ||
|
||
protected const ERROR_NAMES = [ | ||
self::INVALID_YAML_ERROR => 'INVALID_YAML_ERROR', | ||
]; | ||
|
||
public string $format = 'YAML'; | ||
|
||
/** | ||
* @param string[]|null $groups | ||
* @param array<string,mixed> $options | ||
*/ | ||
public function __construct(?array $options = null, ?string $message = null, ?array $groups = null, mixed $payload = null) | ||
{ | ||
if (!class_exists(SymfonyYaml::class)) { | ||
throw new LogicException('The Yaml component is required to use the Yaml constraint. Try running "composer require symfony/yaml".'); | ||
} | ||
|
||
parent::__construct($message, $options, $groups, $payload); | ||
} | ||
} |
Oops, something went wrong.
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.
$signature is unused