How do I validate my YAML file from command line?

Solution 1:

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:

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

Solution 3:

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:

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.