Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2cad97b
commit 131febc
Showing
6 changed files
with
169 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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 <imadzairig@gmail.com> | ||
*/ | ||
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.'; | ||
} |
51 changes: 51 additions & 0 deletions
51
src/Symfony/Component/Validator/Constraints/JsonValidator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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 <imadzairig@gmail.com> | ||
*/ | ||
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/Symfony/Component/Validator/Tests/Constraints/JsonValidatorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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'], | ||
]; | ||
} | ||
} |