10000 Add new json Validator · symfony/symfony@cc65f3c · GitHub
[go: up one dir, main page]

Skip to content

Commit cc65f3c

Browse files
committed
Add new json Validator
1 parent 2cad97b commit cc65f3c

File tree

6 files changed

+169
-1
lines changed
10000

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 NOT_JSON_ERROR = '0789c8ad-2d2b-49a4-8356-e2ce63998504';
25+
26+
protected static $errorNames = array(
27+
self::NOT_JSON_ERROR => 'NOT_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::NOT_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 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(array(
41+
'message' => 'myMessageTest',
42+
));
43+
44+
$this->validator->validate($value, $constraint);
45+
46+
$this->buildViolation('myMessageTest')
47+
->setParameter('{{ value }}', '"'.$value.'"')
48+
->setCode(Json::NOT_JSON_ERROR)
49+
->assertRaised();
50+
}
51+
52+
public function getValidValues()
53+
{
54+
return array(
55+
array('{"planet":"earth", "country": "Morocco","city": "Rabat" ,"postcode" : 10160, "is_great": true,
56+
"img" : null, "area": 118.5, "foo": 15 }'),
57+
array(null),
58+
array(''),
59+
array('"null"'),
60+
array('null'),
61+
array('"string"'),
62+
array('1'),
63+
array('true'),
64+
array(1),
65+
);
66+
}
67+
68+
public function getInvalidValues()
69+
{
70+
return array(
71+
array('{"foo": 3 "bar": 4}'),
72+
array('{"foo": 3 ,"bar": 4'),
73+
array('{foo": 3, "bar": 4}'),
74+
array('foo'),
75+
);
76+
}
77+
}

0 commit comments

Comments
 (0)
0