diff --git a/components/yaml.rst b/components/yaml.rst index e26f7c70736..a04ad7afe35 100644 --- a/components/yaml.rst +++ b/components/yaml.rst @@ -293,6 +293,51 @@ flag:: // Line // String +Syntax Validation +~~~~~~~~~~~~~~~~~ + +The syntax of YAML contents can be validated through the CLI using the +:class:`Symfony\\Component\\Yaml\\Command\\LintCommand` command. + +First, install the Console component: + +.. code-block:: terminal + + $ composer require symfony/console + +Create a console application with ``lint:yaml`` as its only command:: + + // lint.php + + use Symfony\Component\Console\Application; + use Symfony\Component\Yaml\Command\LintCommand; + + (new Application('yaml/lint')) + ->add(new LintCommand()) + ->getApplication() + ->setDefaultCommand('lint:yaml', true) + ->run(); + +Then, execute the script for validating contents: + +.. code-block:: terminal + + # validates a single file + $ php lint.php path/to/file.yml + + # or all the files in a directory + $ php lint.php path/to/directory + + # or contents passed to STDIN + $ cat path/to/file.yml | php lint.php + +The result is written to STDOUT and uses a plain text format by default. +Add the ``--format`` option to get the output in JSON format: + +.. code-block:: terminal + + $ php lint.php path/to/file.yml --format json + Learn More ----------