How do I validate my YAML file from command line?

Yaml

Yaml Problem Overview


I am having issues pulling from a YAML config file:

> Fatal error: while parsing a block mapping; expected <block end>, but found block entry

While there are plenty of online YAML validators, which I have tried and have helped, I'd like to validate my YAML files from the command line and integrate this into my continuous integration pipeline.

How can I validate the syntax of a YAML file on the command line?

Yaml Solutions


Solution 1 - Yaml

With basic Ruby installation this should work:

ruby -ryaml -e "p YAML.load(STDIN.read)" < data.yaml

Python version (thx @Murphy):

pip install pyyaml
python -c 'import yaml, sys; print(yaml.safe_load(sys.stdin))' < data.yaml

Solution 2 - Yaml

You could use yamllint. It's available in Homebrew, etc. It can be used for syntax validation as well as for linting.

Solution 3 - Yaml

Given that you have a perl install on the server you are working on, and it has some of the basic YAML tools, you can use...

perl -MYAML -e 'use YAML;YAML::LoadFile("./file.yaml")'

It should be noted that this will be strict in it's interpretation of the file, but useful.

Solution 4 - Yaml

To correct your .yaml files I recommend the tool yamllint. It can be launched easily from the local console.

The package yamllint is available for all major operating systems.

It's installable from the system's package sources. (e.g. sudo apt-get install yamllint). See documentation for quick start and installation.

Solution 5 - Yaml

My preferd way is yamllint -d "{extends: default, rules: {quoted-strings: enable}}" .

Since I really want to catch quote errors, e.g. validate: bash -c ' ' \""

This is valid yaml, since yaml will just quote the string and turn it into: validate: "bash -c ' ' \\\"\""

Whilst there was just clearly a quote missing at the beginning of the validate comand.

So a normal yaml checker will not detect this, yamllint wil not even detect this in it's default configuration, so turn on quoted-strings checker.

Solution 6 - Yaml

If you got no interpreter installed in your environment but still got a curl, then you could use an online linter project like Lint-Trilogy:

curl -X POST  --data "data=$(cat myfile.yml)" https://www.lint-trilogy.com/lint/yaml/json

It delivers the validation result incl. error descriptions (if any) as json or csv or, where sufficient, as plain text true or false.

It's available as docker file, too. So if you often need a REST based linter, perhaps in a CI/CD pipeline, it may be handy to host an own instance on your site.

Solution 7 - Yaml

Or alternately installed (free) Eclipse IDE and then YEdit yaml editor and see your yaml with syntax highlighting, error flags, and outline views. One time setup cost works pretty well for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser375566View Question on Stackoverflow
Solution 1 - YamlTombartView Answer on Stackoverflow
Solution 2 - YamlJames HiewView Answer on Stackoverflow
Solution 3 - YamlCS MarshallView Answer on Stackoverflow
Solution 4 - YamlkinafuView Answer on Stackoverflow
Solution 5 - YamlJens TimmermanView Answer on Stackoverflow
Solution 6 - YamlDocView Answer on Stackoverflow
Solution 7 - YamlErnieView Answer on Stackoverflow