Skip to content

Commit

Permalink
[Validator] Add new json Validator Documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
zairigimad authored and javiereguiluz committed Mar 4, 2019
1 parent e08cac0 commit 01ea746
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions reference/constraints.rst
Expand Up @@ -19,6 +19,7 @@ Validation Constraints Reference
constraints/Regex
constraints/Ip
constraints/Uuid
constraints/Json

constraints/EqualTo
constraints/NotEqualTo
Expand Down
99 changes: 99 additions & 0 deletions reference/constraints/Json.rst
@@ -0,0 +1,99 @@
Json

Validates that a value is valid ``json``. Specifically, this checks to see if
the value is a valid ``json`` or not.

+----------------+-----------------------------------------------------------------------+
| Applies to | :ref:`property or method <validation-property-target>` |
+----------------+-----------------------------------------------------------------------+
| Options | - `message`_ |
| | - `payload`_ |
+----------------+-----------------------------------------------------------------------+
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\Json` |
+----------------+-----------------------------------------------------------------------+
| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\JsonValidator` |
+----------------+-----------------------------------------------------------------------+

Basic Usage
-----------

The ``Json`` constraint can be applied to a property or a "getter" method,
but is most commonly useful in the latter case. For example, suppose that
you want to guarantee that some ``jsonString`` property is valid JSON.

.. configuration-block::

.. code-block:: php-annotations
// src/Entity/Book.php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Book
{
/**
* @Assert\Json(
* message = "You've entered an invalid Json."
* )
*/
private $chapters;
}
.. code-block:: yaml
# config/validator/validation.yaml
App\Entity\Book:
properties:
chapters:
- Json:
message: You've entered an invalid Json.
.. code-block:: xml
<!-- config/validator/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<class name="App\Entity\Book">
<property name="chapters">
<constraint name="Json">
<option name"message">You've entered an invalid Json.</option>
</constraint>
</property>
</class>
</constraint-mapping>
.. code-block:: php
// src/Entity/Book.php
namespace App\Entity;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;
class Book
{
private $chapters;
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('chapters', new Assert\Json(array(
'message' => 'You\'ve entered an invalid Json.',
)));
}
}
Options
-------

message
~~~~~~~

**type**: ``string`` **default**: ``This value should be valid JSON.``

This message is shown if the underlying data is not a valid JSON.

.. include:: /reference/constraints/_payload-option.rst.inc
1 change: 1 addition & 0 deletions reference/constraints/map.rst.inc
Expand Up @@ -21,6 +21,7 @@ String Constraints
* :doc:`Regex </reference/constraints/Regex>`
* :doc:`Ip </reference/constraints/Ip>`
* :doc:`Uuid</reference/constraints/Uuid>`
* :doc:`Json</reference/constraints/Json>`

Comparison Constraints
~~~~~~~~~~~~~~~~~~~~~~
Expand Down

0 comments on commit 01ea746

Please sign in to comment.