diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index 43ead5b182ba7..509f3317672b6 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -7,7 +7,8 @@ CHANGELOG * added options `iban` and `ibanPropertyPath` to Bic constraint * added UATP cards support to `CardSchemeValidator` * added option `allowNull` to NotBlank constraint - + * added `Json` constraint + 4.2.0 ----- diff --git a/src/Symfony/Component/Validator/Constraints/Json.php b/src/Symfony/Component/Validator/Constraints/Json.php new file mode 100755 index 0000000000000..74d55f775d7a2 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/Json.php @@ -0,0 +1,31 @@ + + * + * 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; + +/** + * @Annotation + * @Target({"PROPERTY", "METHOD", "ANNOTATION"}) + * + * @author Imad ZAIRIG + */ +class Json extends Constraint +{ + const INVALID_JSON_ERROR = '0789c8ad-2d2b-49a4-8356-e2ce63998504'; + + protected static $errorNames = [ + self::INVALID_JSON_ERROR => 'INVALID_JSON_ERROR', + ]; + + public $message = 'This value should be valid JSON.'; +} diff --git a/src/Symfony/Component/Validator/Constraints/JsonValidator.php b/src/Symfony/Component/Validator/Constraints/JsonValidator.php new file mode 100755 index 0000000000000..850bbfbcbf908 --- /dev/null +++ b/src/Symfony/Component/Validator/Constraints/JsonValidator.php @@ -0,0 +1,51 @@ + + * + * 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; + +/** + * @author Imad ZAIRIG + */ +class JsonValidator extends ConstraintValidator +{ + /** + * {@inheritdoc} + */ + public function validate($value, Constraint $constraint) + { + if (!$constraint instanceof Json) { + throw new UnexpectedTypeException($constraint, Json::class); + } + + if (null === $value || '' === $value) { + return; + } + + if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) { + throw new UnexpectedTypeException($value, 'string'); + } + + $value = (string) $value; + + json_decode($value); + + if (JSON_ERROR_NONE !== json_last_error()) { + $this->context->buildViolation($constraint->message) + ->setParameter('{{ value }}', $this->formatValue($value)) + ->setCode(Json::INVALID_JSON_ERROR) + ->addViolation(); + } + } +} diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf index 30e6804c7b83e..bb997332aecb6 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.en.xlf @@ -330,6 +330,10 @@ This Business Identifier Code (BIC) is not associated with IBAN {{ iban }}. This Business Identifier Code (BIC) is not associated with IBAN {{ iban }}. + + This value should be valid JSON. + This value should be valid JSON. + diff --git a/src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf b/src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf index 67441127cda1a..bdee03a0004c2 100644 --- a/src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf +++ b/src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf @@ -330,6 +330,10 @@ This Business Identifier Code (BIC) is not associated with IBAN {{ iban }}. Ce BIC n'est pas associé à l'IBAN {{ iban }}. + + This value should be valid JSON. + Cette valeur doit être un JSON valide. + diff --git a/src/Symfony/Component/Validator/Tests/Constraints/JsonValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/JsonValidatorTest.php new file mode 100644 index 0000000000000..6c94c1d8df6cc --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/Constraints/JsonValidatorTest.php @@ -0,0 +1,77 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Validator\Tests\Constraints; + +use Symfony\Component\Validator\Constraints\Json; +use Symfony\Component\Validator\Constraints\JsonValidator; +use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; + +class JsonValidatorTest extends ConstraintValidatorTestCase +{ + protected function createValidator() + { + return new JsonValidator(); + } + + /** + * @dataProvider getValidValues + */ + public function testJsonIsValid($value) + { + $this->validator->validate($value, new Json()); + + $this->assertNoViolation(); + } + + /** + * @dataProvider getInvalidValues + */ + public function testInvalidValues($value) + { + $constraint = new Json([ + 'message' => 'myMessageTest', + ]); + + $this->validator->validate($value, $constraint); + + $this->buildViolation('myMessageTest') + ->setParameter('{{ value }}', '"'.$value.'"') + ->setCode(Json::INVALID_JSON_ERROR) + ->assertRaised(); + } + + public function getValidValues() + { + return [ + ['{"planet":"earth", "country": "Morocco","city": "Rabat" ,"postcode" : 10160, "is_great": true, + "img" : null, "area": 118.5, "foo": 15 }'], + [null], + [''], + ['"null"'], + ['null'], + ['"string"'], + ['1'], + ['true'], + [1], + ]; + } + + public function getInvalidValues() + { + return [ + ['{"foo": 3 "bar": 4}'], + ['{"foo": 3 ,"bar": 4'], + ['{foo": 3, "bar": 4}'], + ['foo'], + ]; + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy