Skip to content

Commit d59691b

Browse files
committed
Add new json Validator
1 parent 5b08019 commit d59691b

File tree

4 files changed

+166
-1
lines changed

4 files changed

+166
-1
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ CHANGELOG
1010
* made `ValidatorBuilder` final
1111
* marked `format` the default option in `DateTime` constraint
1212
* deprecated validating instances of `\DateTimeInterface` in `DateTimeValidator`, `DateValidator` and `TimeValidator`.
13-
13+
* added `Json` constraint
14+
1415
4.1.0
1516
-----
1617

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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
if (!$this->isJson($value)) {
43+
$this->context->buildViolation($constraint->message)
44+
->setParameter('{{ value }}', $this->formatValue($value))
45+
->setCode(Json::NOT_JSON_ERROR)
46+
->addViolation();
47+
}
48+
}
49+
50+
protected function isJson(string $string): bool
51+
{
52+
json_decode($string);
53+
54+
return JSON_ERROR_NONE === json_last_error();
55+
}
56+
}
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)