Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I validate my YAML file from command line?

Tags:

yaml

People also ask

What is Yaml validator?

YAML Validator works well on Windows, MAC, Linux, Chrome, Firefox, Edge, and Safari. This YAML Linter helps a developer who works with JSON data to test and verify.

How do I validate a YAML file in Python?

Validate the YAML with its corresponding schema for basic data type check. Custom validations like IP address, random strings can be added in schema.

What is valid YAML syntax?

All YAML files (regardless of their association with Ansible or not) can optionally begin with --- and end with ... . This is part of the YAML format and indicates the start and end of a document. All members of a list are lines beginning at the same indentation level starting with a "- " (a dash and a space):


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

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


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.


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.