Skip to content

Commit 791a412

Browse files
committed
minor #10351 [Validator] Add new json Validator Documentation (zairigimad)
This PR was squashed before being merged into the master branch (closes #10351). Discussion ---------- [Validator] Add new json Validator Documentation Hi, introduce ~~IsJson~~ `Json` Constraint . this is a documentation for the new feature in the pull request symfony/symfony#28477 cordially, Commits ------- 01ea746 [Validator] Add new json Validator Documentation
2 parents 543ae67 + 01ea746 commit 791a412

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

reference/constraints.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Validation Constraints Reference
1919
constraints/Regex
2020
constraints/Ip
2121
constraints/Uuid
22+
constraints/Json
2223

2324
constraints/EqualTo
2425
constraints/NotEqualTo

reference/constraints/Json.rst

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
Json
2+
3+
Validates that a value is valid ``json``. Specifically, this checks to see if
4+
the value is a valid ``json`` or not.
5+
6+
+----------------+-----------------------------------------------------------------------+
7+
| Applies to | :ref:`property or method <validation-property-target>` |
8+
+----------------+-----------------------------------------------------------------------+
9+
| Options | - `message`_ |
10+
| | - `payload`_ |
11+
+----------------+-----------------------------------------------------------------------+
12+
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\Json` |
13+
+----------------+-----------------------------------------------------------------------+
14+
| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\JsonValidator` |
15+
+----------------+-----------------------------------------------------------------------+
16+
17+
Basic Usage
18+
-----------
19+
20+
The ``Json`` constraint can be applied to a property or a "getter" method,
21+
but is most commonly useful in the latter case. For example, suppose that
22+
you want to guarantee that some ``jsonString`` property is valid JSON.
23+
24+
.. configuration-block::
25+
26+
.. code-block:: php-annotations
27+
28+
// src/Entity/Book.php
29+
namespace App\Entity;
30+
31+
use Symfony\Component\Validator\Constraints as Assert;
32+
33+
class Book
34+
{
35+
/**
36+
* @Assert\Json(
37+
* message = "You've entered an invalid Json."
38+
* )
39+
*/
40+
private $chapters;
41+
}
42+
43+
.. code-block:: yaml
44+
45+
# config/validator/validation.yaml
46+
App\Entity\Book:
47+
properties:
48+
chapters:
49+
- Json:
50+
message: You've entered an invalid Json.
51+
52+
.. code-block:: xml
53+
54+
<!-- config/validator/validation.xml -->
55+
<?xml version="1.0" encoding="UTF-8" ?>
56+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
57+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
58+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
59+
60+
<class name="App\Entity\Book">
61+
<property name="chapters">
62+
<constraint name="Json">
63+
<option name"message">You've entered an invalid Json.</option>
64+
</constraint>
65+
</property>
66+
</class>
67+
</constraint-mapping>
68+
69+
.. code-block:: php
70+
71+
// src/Entity/Book.php
72+
namespace App\Entity;
73+
74+
use Symfony\Component\Validator\Mapping\ClassMetadata;
75+
use Symfony\Component\Validator\Constraints as Assert;
76+
77+
class Book
78+
{
79+
private $chapters;
80+
81+
public static function loadValidatorMetadata(ClassMetadata $metadata)
82+
{
83+
$metadata->addPropertyConstraint('chapters', new Assert\Json(array(
84+
'message' => 'You\'ve entered an invalid Json.',
85+
)));
86+
}
87+
}
88+
89+
Options
90+
-------
91+
92+
message
93+
~~~~~~~
94+
95+
**type**: ``string`` **default**: ``This value should be valid JSON.``
96+
97+
This message is shown if the underlying data is not a valid JSON.
98+
99+
.. include:: /reference/constraints/_payload-option.rst.inc

reference/constraints/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ String Constraints
2121
* :doc:`Regex </reference/constraints/Regex>`
2222
* :doc:`Ip </reference/constraints/Ip>`
2323
* :doc:`Uuid</reference/constraints/Uuid>`
24+
* :doc:`Json</reference/constraints/Json>`
2425

2526
Comparison Constraints
2627
~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)