From 131febc7be8392d408a1afb8798d40e52877308b Mon Sep 17 00:00:00 2001 From: zairig imad Date: Sun, 16 Sep 2018 01:09:38 +0200 Subject: [PATCH] [Validator] Add new json Validator --- src/Symfony/Component/Validator/CHANGELOG.md | 3 +- .../Component/Validator/Constraints/Json.php | 31 ++++++++ .../Validator/Constraints/JsonValidator.php | 51 ++++++++++++ .../Resources/translations/validators.en.xlf | 4 + .../Resources/translations/validators.fr.xlf | 4 + .../Tests/Constraints/JsonValidatorTest.php | 77 +++++++++++++++++++ 6 files changed, 169 insertions(+), 1 deletion(-) create mode 100755 src/Symfony/Component/Validator/Constraints/Json.php create mode 100755 src/Symfony/Component/Validator/Constraints/JsonValidator.php create mode 100644 src/Symfony/Component/Validator/Tests/Constraints/JsonValidatorTest.php 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'], + ]; + } +}