Skip to content
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

[Validator] Add Yaml constraint for validating Yaml content #53749

Open
wants to merge 14 commits into
base: 7.2
Choose a base branch
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* Add the `Yaml` constraint for validating Yaml content

7.1
---

Expand Down
44 changes: 44 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Yaml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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\LogicException;
use Symfony\Component\Yaml\Parser;

/**
* @author Kev <https://github.com/symfonyaml>
*/
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class Yaml extends Constraint
{
public const INVALID_YAML_ERROR = '63313a31-837c-42bb-99eb-542c76aacc48';

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

#[HasNamedArguments]
public function __construct(
symfonyaml marked this conversation as resolved.
Show resolved Hide resolved
public string $message = 'This value is not valid YAML.',
public int $flags = 0,
?array $groups = null,
mixed $payload = null,
) {
if (!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(null, $groups, $payload);
}
}
63 changes: 63 additions & 0 deletions src/Symfony/Component/Validator/Constraints/YamlValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser;

/**
* @author Kev <https://github.com/symfonyaml>
*/
class YamlValidator extends ConstraintValidator
{
public function validate(mixed $value, Constraint $constraint): void
{
if (!$constraint instanceof Yaml) {
throw new UnexpectedTypeException($constraint, Yaml::class);
}

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

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

$value = (string) $value;

/** @see \Symfony\Component\Yaml\Command\LintCommand::validate() */
$prevErrorHandler = set_error_handler(function ($level, $message, $file, $line) use (&$prevErrorHandler) {

Check failure on line 43 in src/Symfony/Component/Validator/Constraints/YamlValidator.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedVariable

src/Symfony/Component/Validator/Constraints/YamlValidator.php:43:95: UndefinedVariable: Cannot find referenced variable $prevErrorHandler (see https://psalm.dev/024)

Check failure on line 43 in src/Symfony/Component/Validator/Constraints/YamlValidator.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedVariable

src/Symfony/Component/Validator/Constraints/YamlValidator.php:43:95: UndefinedVariable: Cannot find referenced variable $prevErrorHandler (see https://psalm.dev/024)
if (\E_USER_DEPRECATED === $level) {
throw new ParseException($message, $this->getParser()->getRealCurrentLineNb() + 1);
}
Comment on lines +44 to +46
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the purpose of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ro0NL Taken from the Yaml LintCommand.php


return $prevErrorHandler ? $prevErrorHandler($level, $message, $file, $line) : false;
});

try {
(new Parser())->parse($value, $constraint->flags);
} catch (ParseException $e) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ error }}', $e->getMessage())
->setParameter('{{ line }}', $e->getParsedLine())
->setCode(Yaml::INVALID_YAML_ERROR)
->addViolation();
} finally {
restore_error_handler();
}
}
}
57 changes: 57 additions & 0 deletions src/Symfony/Component/Validator/Tests/Constraints/YamlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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\Yaml;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;
use Symfony\Component\Yaml\Yaml as YamlParser;

/**
* @author Kev <https://github.com/symfonyaml>
*/
class YamlTest extends TestCase
{
public function testAttributes()
{
$metadata = new ClassMetadata(YamlDummy::class);
$loader = new AttributeLoader();
self::assertTrue($loader->loadClassMetadata($metadata));

[$bConstraint] = $metadata->properties['b']->getConstraints();
self::assertSame('myMessage', $bConstraint->message);
self::assertSame(['Default', 'YamlDummy'], $bConstraint->groups);

[$cConstraint] = $metadata->properties['c']->getConstraints();
self::assertSame(['my_group'], $cConstraint->groups);
self::assertSame('some attached data', $cConstraint->payload);

[$cConstraint] = $metadata->properties['d']->getConstraints();
self::assertSame(YamlParser::PARSE_CONSTANT | YamlParser::PARSE_CUSTOM_TAGS, $cConstraint->flags);
}
}

class YamlDummy
{
#[Yaml]
private $a;

#[Yaml(message: 'myMessage')]
private $b;

#[Yaml(groups: ['my_group'], payload: 'some attached data')]
private $c;

#[Yaml(flags: YamlParser::PARSE_CONSTANT | YamlParser::PARSE_CUSTOM_TAGS)]
private $d;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?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\Yaml;
use Symfony\Component\Validator\Constraints\YamlValidator;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
use Symfony\Component\Yaml\Yaml as YamlParser;

/**
* @author Kev <https://github.com/symfonyaml>
*/
class YamlValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator(): YamlValidator
{
return new YamlValidator();
}

/**
* @dataProvider getValidValues
*/
public function testYamlIsValid($value)
{
$this->validator->validate($value, new Yaml());

$this->assertNoViolation();
}

public function testYamlWithFlags()
{
$this->validator->validate('date: 2023-01-01', new Yaml(flags: YamlParser::PARSE_DATETIME));
$this->assertNoViolation();
}

/**
* @dataProvider getInvalidValues
*/
public function testInvalidValues($value, $message, $line)
{
$constraint = new Yaml(
message: 'myMessageTest',
);

$this->validator->validate($value, $constraint);

$this->buildViolation('myMessageTest')
->setParameter('{{ error }}', $message)
->setParameter('{{ line }}', $line)
->setCode(Yaml::INVALID_YAML_ERROR)
->assertRaised();
}

public function testInvalidFlags()
{
$value = 'tags: [!tagged app.myclass]';
$this->validator->validate($value, new Yaml());
$this->buildViolation('This value is not valid YAML.')
->setParameter('{{ error }}', 'Tags support is not enabled. Enable the "Yaml::PARSE_CUSTOM_TAGS" flag to use "!tagged" at line 1 (near "tags: [!tagged app.myclass]").')
->setParameter('{{ line }}', 1)
->setCode(Yaml::INVALID_YAML_ERROR)
->assertRaised();
}

public static function getValidValues()
symfonyaml marked this conversation as resolved.
Show resolved Hide resolved
{
return [
['planet_diameters: {earth: 12742, mars: 6779, saturn: 116460, mercury: 4879}'],
["key:\n value"],
[null],
[''],
['"null"'],
['null'],
['"string"'],
['1'],
['true'],
[1],
];
}

public static function getInvalidValues(): array
{
return [
['{:INVALID]', 'Malformed unquoted YAML string at line 1 (near "{:INVALID]").', 1],
["key:\nvalue", 'Unable to parse at line 2 (near "value").', 2],
];
}
}