8000 Add New IsJson Validator Documentation · symfony/symfony-docs@62a408d · GitHub
[go: up one dir, main page]

Skip to content

Commit 62a408d

Browse files
committed
Add New IsJson Validator Documentation
1 parent e08cac0 commit 62a408d

File tree

3 files changed

+102
-0
lines changed

3 files changed

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