116
votes

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?

6
Try: travis lint .travis.ymlkenorb
python -c "from yaml import load, Loader; load(open('.travis.yml'), Loader=Loader)"Natim
This question should not be closed. Perhaps re-word it to "How do I validate my YAML file from command line". This is a valid and useful questionHanxue
Seconded @hanxue - This comes up as the first result when searching the topic and should be a useful reference when google lands us here.brice
Yeah, this question should not be closed. I don't think the answers are opinionated or spam.Joey Novak

6 Answers

167
votes

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
27
votes

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.

26
votes

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

18
votes

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.

-2
votes

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.

-11
votes

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.