Skip to content

Commit 24a15c2

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

File tree

4 files changed

+141
-1
lines changed

4 files changed

+141
-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 `NotJson` 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 NotJson extends Constraint
23+
{
24+
const NOT_JSON_ERROR = '60d2f30b-8cfa-4372-b155-9656634de120';
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 NotJsonValidator extends ConstraintValidator
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function validate($value, Constraint $constraint)
27+
{
28+
if (!$constraint instanceof NotJson) {
29+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\NotJson');
30+
}
31+
32+
if (!$this->isJson($value)) {
33+
$this->context->buildViolation($constraint->message)
34+
->setParameter('{{ value }}', $this->formatValue($value))
35+
->setCode(NotJson::NOT_JSON_ERROR)
36+
->addViolation();
37+
}
38+
}
39+
40+
/**
41+
* @param string $string
42+
*
43+
* @return bool
44+
*/
45+
protected function isJson($string)
46+
{
47+
json_decode($string);
48+
49+
return JSON_ERROR_NONE == json_last_error();
50+
}
51+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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\NotJson;
15+
use Symfony\Component\Validator\Constraints\NotJsonValidator;
16+
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
17+
18+
class NotJsonValidatorTest extends ConstraintValidatorTestCase
19+
{
20+
protected function createValidator()
21+
{
22+
return new NotJsonValidator();
23+
}
24+
25+
public function testJsonIsValid()
26+
{
27+
$this->validator->validate('{ "planet":"earth", "country": "Morocco", "city": "Rabat" }', new NotJson());
28+
29+
$this->assertNoViolation();
30+
}
31+
32+
/**
33+
* @dataProvider getInvalidValues
34+
*/
35+
public function testInvalidValues($value)
36+
{
37+
$constraint = new NotJson(array(
38+
'message' => 'myMessageTest',
39+
));
40+
41+
$this->validator->validate($value, $constraint);
42+
43+
$this->buildViolation('myMessageTest')
44+
->setParameter('{{ value }}', '"'.$value.'"')
45+
->setCode(NotJson::NOT_JSON_ERROR)
46+
->assertRaised();
47+
}
48+
49+
public function getInvalidValues()
50+
{
51+
return array(
52+
array('{"foo": 3 "bar": 4}'),
53+
array('{"foo": 3 ,"bar": 4'),
54+
array('{foo": 3, "bar": 4}'),
55+
);
56+
}
57+
}

0 commit comments

Comments
 (0)
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