Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Validator] Add new Schema validation constraint #58560

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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add new Schema validation constraint.
This constraint is like an extension of `Json` and `Yaml` constraints. It checks if given value is valid for the given format, then, if you declare `constraints` option, it will try to apply those constraints to the deserialized data.
  • Loading branch information
WedgeSama committed Oct 15, 2024
commit 9da376260779ed8c19364febdfe5b4d41cc57832
74 changes: 74 additions & 0 deletions 74 src/Symfony/Component/Validator/Constraints/Schema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\InvalidArgumentException;
use Symfony\Component\Validator\Exception\LogicException;
use Symfony\Component\Yaml\Parser;

/**
* @author Benjamin Georgeault <bgeorgeault@wedgesama.fr>
*/
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class Schema extends Composite
{
public const JSON = 'JSON';
public const YAML = 'YAML';

public const INVALID_ERROR = 'f8925b90-edfd-4364-a17a-f34a60f24b26';

public string $format;

protected const ERROR_NAMES = [
self::INVALID_ERROR => 'INVALID_ERROR',
];

private static array $allowedTypes = [
self::JSON,
self::YAML,
];

/**
* @param array<Constraint>|Constraint $constraints
*/
#[HasNamedArguments]
public function __construct(
string $format,
public array|Constraint $constraints = [],
public ?string $invalidMessage = 'Cannot apply schema validation, this value does not respect format.',
?array $groups = null,
mixed $payload = null,
public int $flags = 0,
public ?int $depth = null,
) {
$this->format = $format = strtoupper($format);

if (!\in_array($format, static::$allowedTypes)) {
throw new InvalidArgumentException(\sprintf('The "format" parameter value is not valid. It must contain one or more of the following values: "%s".', implode(', ', self::$allowedTypes)));
}

if (self::YAML === $format && !class_exists(Parser::class)) {
throw new LogicException('The Yaml component is required to use the Yaml constraint. Try running "composer require symfony/yaml".');
}

parent::__construct([
'constraints' => $constraints,
], $groups, $payload);
}

protected function getCompositeOption(): string
{
return 'constraints';
}
}
87 changes: 87 additions & 0 deletions 87 src/Symfony/Component/Validator/Constraints/SchemaValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?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;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
use Symfony\Component\Validator\Exception\ValidatorException;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser;

/**
* @author Benjamin Georgeault <bgeorgeault@wedgesama.fr>
*/
class SchemaValidator extends ConstraintValidator
{
public function validate(mixed $value, Constraint $constraint): void
{
if (!$constraint instanceof Schema) {
throw new UnexpectedTypeException($constraint, Schema::class);
}

if (null === $value || '' === $value) {
return;
}

if (!\is_scalar($value) && !$value instanceof \Stringable) {
throw new UnexpectedValueException($value, 'string');
}

$value = (string) $value;

try {
$data = match ($constraint->format) {
Schema::YAML => $this->validateAndGetYaml($value, $constraint),
Schema::JSON => $this->validateAndGetJson($value, $constraint),
};
} catch (ValidatorException $e) {
$this->context->buildViolation($constraint->invalidMessage)
->setParameter('{{ error }}', $e->getMessage())
->setParameter('{{ format }}', $constraint->format)
->setCode(Schema::INVALID_ERROR)
->addViolation();

return;
}

if (empty($constraint->constraints)) {
return;
}

$validator = ($context = $this->context)
->getValidator()->inContext($context);

$validator->validate($data, $constraint->constraints);
}

private function validateAndGetYaml(string $value, Schema $constraint): mixed
{
try {
return (new Parser())->parse($value, $constraint->flags);
} catch (ParseException $e) {
throw new ValidatorException(\sprintf('Invalid YAML with message "%s".', $e->getMessage()));
} finally {
restore_error_handler();
}
}

private function validateAndGetJson(string $value, Schema $constraint): mixed
{
if (!json_validate($value, $constraint->depth ?? 512, $constraint->flags)) {
throw new ValidatorException('Invalid JSON.');
}

return json_decode($value, true, $constraint->depth ?? 512, $constraint->flags);
}
}
40 changes: 40 additions & 0 deletions 40 src/Symfony/Component/Validator/Tests/Constraints/SchemaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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 PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\Schema;

class SchemaTest extends TestCase
{
public function testEmptyFieldsInOptions()
{
$constraint = new Schema(
format: 'YAML',
invalidMessage: 'fooo',
);

$this->assertSame([], $constraint->constraints);
$this->assertSame('YAML', $constraint->format);
$this->assertSame('fooo', $constraint->invalidMessage);
$this->assertSame(0, $constraint->flags);
}

public function testUpperFormat()
{
$constraint = new Schema(
format: 'yaml',
);

$this->assertSame('YAML', $constraint->format);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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\NotNull;
use Symfony\Component\Validator\Constraints\Schema;
use Symfony\Component\Validator\Constraints\SchemaValidator;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

class SchemaValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator(): SchemaValidator
{
return new SchemaValidator();
}

/**
* @dataProvider getValidValues
*/
public function testFormatIsValid(string $format, string $value)
{
$this->validator->validate($value, new Schema($format));

$this->assertNoViolation();
}

/**
* @dataProvider getInvalidValues
*/
public function testFormatIsInvalid(string $format, string $value, string $errorMsg)
{
$this->validator->validate($value, new Schema($format));

$this->buildViolation('Cannot apply schema validation, this value does not respect format.')
->setParameter('{{ error }}', $errorMsg)
->setParameter('{{ format }}', $format)
->setCode(Schema::INVALID_ERROR)
->assertRaised();
}

public function testValidWithConstraint()
{
$constraint = new Schema(
format: 'yaml',
constraints: new NotNull(),
);

$this->validator->validate('foo: "bar"', $constraint);
$this->assertNoViolation();
}

public static function getValidValues(): array
{
return [
['yaml', 'foo: "bar"'],
['json', '{"foo":"bar"}'],
];
}

public static function getInvalidValues(): array
{
return [
['YAML', 'foo: ["bar"', 'Invalid YAML with message "Malformed inline YAML string at line 1 (near "foo: ["bar"").".'],
['JSON', '{"foo:"bar"}', 'Invalid JSON.'],
];
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.