Skip to content

Commit 841dd45

Browse files
committed
[Validator] Add the Yaml constrain
1 parent 23f0059 commit 841dd45

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed

components/yaml.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ During the parsing of the YAML contents, all the ``_`` characters are removed
214214
from the numeric literal contents, so there is not a limit in the number of
215215
underscores you can include or the way you group contents.
216216

217+
.. _yaml-flags:
218+
217219
Advanced Usage: Flags
218220
---------------------
219221

reference/constraints/Yaml.rst

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
Yaml
2+
====
3+
4+
Validates that a value has valid `YAML`_ syntax.
5+
6+
.. versionadded:: 7.2
7+
8+
The ``Yaml`` constraint was introduced in Symfony 7.2.
9+
10+
========== ===================================================================
11+
Applies to :ref:`property or method <validation-property-target>`
12+
Class :class:`Symfony\\Component\\Validator\\Constraints\\Yaml`
13+
Validator :class:`Symfony\\Component\\Validator\\Constraints\\YamlValidator`
14+
========== ===================================================================
15+
16+
Basic Usage
17+
-----------
18+
19+
The ``Yaml`` constraint can be applied to a property or a "getter" method:
20+
21+
.. configuration-block::
22+
23+
.. code-block:: php-attributes
24+
25+
// src/Entity/Report.php
26+
namespace App\Entity;
27+
28+
use Symfony\Component\Validator\Constraints as Assert;
29+
30+
class Report
31+
{
32+
#[Assert\Yaml(
33+
message: "Your configuration doesn't have valid YAML syntax."
34+
)]
35+
private string $customConfiguration;
36+
}
37+
38+
.. code-block:: yaml
39+
40+
# config/validator/validation.yaml
41+
App\Entity\Report:
42+
properties:
43+
customConfiguration:
44+
- Yaml:
45+
message: Your configuration doesn't have valid YAML syntax.
46+
47+
.. code-block:: xml
48+
49+
<!-- config/validator/validation.xml -->
50+
<?xml version="1.0" encoding="UTF-8" ?>
51+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
52+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
53+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
54+
55+
<class name="App\Entity\Report">
56+
<property name="customConfiguration">
57+
<constraint name="Yaml">
58+
<option name="message">Your configuration doesn't have valid YAML syntax.</option>
59+
</constraint>
60+
</property>
61+
</class>
62+
</constraint-mapping>
63+
64+
.. code-block:: php
65+
66+
// src/Entity/Report.php
67+
namespace App\Entity;
68+
69+
use Symfony\Component\Validator\Constraints as Assert;
70+
use Symfony\Component\Validator\Mapping\ClassMetadata;
71+
72+
class Report
73+
{
74+
public static function loadValidatorMetadata(ClassMetadata $metadata): void
75+
{
76+
$metadata->addPropertyConstraint('customConfiguration', new Assert\Yaml([
77+
'message' => 'Your configuration doesn\'t have valid YAML syntax.',
78+
]));
79+
}
80+
}
81+
82+
Options
83+
-------
84+
85+
``flags``
86+
~~~~~~~~~
87+
88+
**type**: ``int`` **default**: ``0``
89+
90+
This option enables optional features of the YAML parser when validating contents.
91+
Its value is a combination of one or more of the :ref:`flags defined by the Yaml component <yaml-flags>`:
92+
93+
.. configuration-block::
94+
95+
.. code-block:: php-attributes
96+
97+
// src/Entity/Report.php
98+
namespace App\Entity;
99+
100+
use Symfony\Component\Validator\Constraints as Assert;
101+
use Symfony\Component\Yaml\Yaml;
102+
103+
class Report
104+
{
105+
#[Assert\Yaml(
106+
message: "Your configuration doesn't have valid YAML syntax.",
107+
flags: Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_DATETIME,
108+
)]
109+
private string $customConfiguration;
110+
}
111+
112+
.. code-block:: yaml
113+
114+
# config/validator/validation.yaml
115+
App\Entity\Report:
116+
properties:
117+
customConfiguration:
118+
- Yaml:
119+
message: Your configuration doesn't have valid YAML syntax.
120+
flags: YamlParser::PARSE_CONSTANT | YamlParser::PARSE_CUSTOM_TAGS | Yaml::PARSE_DATETIME
121+
122+
.. code-block:: xml
123+
124+
<!-- config/validator/validation.xml -->
125+
<?xml version="1.0" encoding="UTF-8" ?>
126+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
127+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
128+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
129+
130+
<class name="App\Entity\Report">
131+
<property name="customConfiguration">
132+
<constraint name="Yaml">
133+
<option name="message">Your configuration doesn't have valid YAML syntax.</option>
134+
<option name="flags">Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_DATETIME</option>
135+
</constraint>
136+
</property>
137+
</class>
138+
</constraint-mapping>
139+
140+
.. code-block:: php
141+
142+
// src/Entity/Report.php
143+
namespace App\Entity;
144+
145+
use Symfony\Component\Validator\Constraints as Assert;
146+
use Symfony\Component\Validator\Mapping\ClassMetadata;
147+
use Symfony\Component\Yaml\Yaml;
148+
149+
class Report
150+
{
151+
public static function loadValidatorMetadata(ClassMetadata $metadata): void
152+
{
153+
$metadata->addPropertyConstraint('customConfiguration', new Assert\Yaml([
154+
'message' => 'Your configuration doesn\'t have valid YAML syntax.',
155+
'flags' => Yaml::PARSE_CONSTANT | Yaml::PARSE_CUSTOM_TAGS | Yaml::PARSE_DATETIME,
156+
]));
157+
}
158+
}
159+
160+
``message``
161+
~~~~~~~~~~~
162+
163+
**type**: ``string`` **default**: ``This value is not valid YAML.``
164+
165+
This message shown if the underlying data is not a valid YAML value.
166+
167+
You can use the following parameters in this message:
168+
169+
=============== ==============================================================
170+
Parameter Description
171+
=============== ==============================================================
172+
``{{ error }}`` The full error message from the YAML parser
173+
``{{ line }}`` The line where the YAML syntax error happened
174+
=============== ==============================================================
175+
176+
.. include:: /reference/constraints/_groups-option.rst.inc
177+
178+
.. include:: /reference/constraints/_payload-option.rst.inc
179+
180+
.. _`YAML`: https://en.wikipedia.org/wiki/YAML

0 commit comments

Comments
 (0)