8000 feature #28477 [Validator] Add new json Validator (zairigimad) · symfony/symfony@b951a0f · GitHub
[go: up one dir, main page]

Skip to content

Commit b951a0f

Browse files
committed
feature #28477 [Validator] Add new json Validator (zairigimad)
This PR was squashed before being merged into the 4.3-dev branch (closes #28477). Discussion ---------- [Validator] Add new json Validator | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT | Doc PR | symfony/symfony-docs#10351 Hi, in so many cases we want to test if a value is a valid json or not, so I propose this new ~~NotJson~~ `Json` constraint for this need. cordially, Commits ------- 131febc [Validator] Add new json Validator
2 parents 09275d4 + 131febc commit b951a0f

File tree

6 files changed

+169
-1
lines changed

6 files changed

+169
-1
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ CHANGELOG
77
* added options `iban` and `ibanPropertyPath` to Bic constraint
88
* added UATP cards support to `CardSchemeValidator`
99
* added option `allowNull` to NotBlank constraint
10-
10+
* added `Json` constraint
11+
1112
4.2.0
1213
-----
1314

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
16+
/**
17+
* @Annotation
18+
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
19+
*
20+
* @author Imad ZAIRIG <imadzairig@gmail.com>
21+
*/
22+
class Json extends Constraint
23+
{
24+
const INVALID_JSON_ERROR = '0789c8ad-2d2b-49a4-8356-e2ce63998504';
25+
26+
protected static $errorNames = [
27+
self::INVALID_JSON_ERROR => 'INVALID_JSON_ERROR',
28+
];
29+
30+
public $message = 'This value should be valid JSON.';
31+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Constraints;
13+
14+
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
18+
/**
19+
* @author Imad ZAIRIG <imadzairig@gmail.com>
20+
*/
21+
class JsonValidator extends ConstraintValidator
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function validate($value, Constraint $constraint)
27+
{
28+
if (!$constraint instanceof Json) {
29+
throw new UnexpectedTypeException($constraint, Json::class);
30+
}
31+
32+
if (null === $value || '' === $value) {
33+
return;
34+
}
35+
36+
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
37+
throw new UnexpectedTypeException($value, 'string');
38+
}
39+
40+
$value = (string) $value;
41+
42+
json_decode($value);
43+
44+
if (JSON_ERROR_NONE !== json_last_error()) {
45+
$this->context->buildViolation($constraint->message)
46+
->setParameter('{{ value }}', $this->formatValue($value))
47+
->setCode(Json::INVALID_JSON_ERROR)
48+
->addViolation();
49+
}
50+
}
51+
}

src/Symfony/Component/Validator/Resources/translations/validators.en.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,10 @@
330330
<source>This Business Identifier Code (BIC) is not associated with IBAN {{ iban }}.</source>
331331
<target>This Business Identifier Code (BIC) is not associated with IBAN {{ iban }}.</target>
332332
</trans-unit>
333+
<trans-unit id="85">
334+
<source>This value should be valid JSON.</source>
335+
<target>This value should be valid JSON.</target>
336+
</trans-unit>
333337
</body>
334338
</file>
335339
</xliff>

src/Symfony/Component/Validator/Resources/translations/validators.fr.xlf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,10 @@
330330
<source>This Business Identifier Code (BIC) is not associated with IBAN {{ iban }}.</source>
331331
<target>Ce code d'identification d'entreprise (BIC) n'est pas associé à l'IBAN {{ iban }}.</target>
332332
</trans-unit>
333+
<trans-unit id="85">
334+
<source>This value should be valid JSON.</source>
335+
<target>Cette valeur doit être un JSON valide.</target>
336+
</trans-unit>
333337
</body>
334338
</file>
335339
</xliff>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Constraints;
13+
14+
use Symfony\Component\Validator\Constraints\Json;
15+
use Symfony\Component\Validator\Constraints\JsonValidator;
16+
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
17+
18+
class JsonValidatorTest extends ConstraintValidatorTestCase
19+
{
20+
protected function createValidator()
21+
{
22+
return new JsonValidator();
23+
}
24+
25+
/**
26+
* @dataProvider getValidValues
27+
*/
28+
public function testJsonIsValid($value)
29+
{
30+
$this->validator->validate($value, new Json());
31+
32+
$this->assertNoViolation();
33+
}
34+
35+
/**
36+
* @dataProvider getInvalidValues
37+
*/
38+
public function testInvalidValues($value)
39+
{
40+
$constraint = new Json([
41+
'message' => 'myMessageTest',
42+
]);
43+
44+
$this->validator->validate($value, $constraint);
45+
46+
$this->buildViolation('myMessageTest')
47+
->setParameter('{{ value }}', '"'.$value.'"')
48+
->setCode(Json::INVALID_JSON_ERROR)
49+
->assertRaised();
50+
}
51+
52+
public function getValidValues()
53+
{
54+
return [
55+
['{"planet":"earth", "country": "Morocco","city": "Rabat" ,"postcode" : 10160, "is_great": true,
56+
"img" : null, "area": 118.5, "foo": 15 }'],
57+
[null],
58+
[''],
59+
['"null"'],
60+
['null'],
61+
['"string"'],
62+
['1'],
63+
['true'],
64+
[1],
65+
];
66+
}
67+
68+
public function getInvalidValues()
69+
{
70+
return [
71+
['{"foo": 3 "bar": 4}'],
72+
['{"foo": 3 ,"bar": 4'],
73+
['{foo": 3, "bar": 4}'],
74+
['foo'],
75+
];
76+
}
77+
}

0 commit comments

Comments
 (0)
0