8000 [Validator] Add new json Validator by zairigimad · Pull Request #28477 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Add new json Validator #28477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conve 10000 rsations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ CHANGELOG
* added options `iban` and `ibanPropertyPath` to Bic constraint
* added UATP cards support to `CardSchemeValidator`
* added option `allowNull` to NotBlank constraint

* added `Json` constraint

4.2.0
-----

Expand Down
31 changes: 31 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Json.php
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 src/Symfony/Component/Validator/Constraints/JsonValidator.php
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@
<source>This Business Identifier Code (BIC) is not associated with IBAN {{ iban }}.</source>
<target>This Business Identifier Code (BIC) is not associated with IBAN {{ iban }}.</target>
</trans-unit>
<trans-unit id="85">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be 86.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see 21b8efc

<source>This value should be valid JSON.</source>
<target>This value should be valid JSON.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@
<source>This Business Identifier Code (BIC) is not associated with IBAN {{ iban }}.</source>
<target>Ce BIC n'est pas associé à l'IBAN {{ iban }}.</target>
</trans-unit>
<trans-unit id="85">
<source>This value should be valid JSON.</source>
<target>Cette valeur doit être un JSON valide.</target>
</trans-unit>
</body>
</file>
</xliff>
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'],
];
}
}
0