Skip to content

[Validator] Add new json Validator Documentation #10351

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions reference/constraints.rst
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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