3

I need to validate a whole bunch of YAML files.

I tried the yaml online parser (http://yaml-online-parser.appspot.com/) which works perfect, but it's too much manual work to copy each YAML file content into the box and parse them.

Is there a way to parse/validate YAML files in bulk?

Jordan Running
  • 102,619
  • 17
  • 182
  • 182
Penny
  • 1,218
  • 1
  • 13
  • 32

2 Answers2

2

This is fairly straightforward in any scripting language that has a YAML library. For example, here's how you might do it in Ruby:

#!/usr/bin/env ruby

require "yaml"

def check_file(filename)
  YAML.parse_file(filename)
  puts "OK"
  0
rescue Psych::SyntaxError => ex
  puts "Error#{ex.message[/: .+/]}"
  1
end

exit_code = 0
max_filename_length = ARGV.max_by(&:size).size

ARGV.each do |filename|
  printf "%-*s  ", max_filename_length, filename
  exit_code |= check_file(filename)
end

exit exit_code

Usage:

$ ruby check_yaml.rb *.yml
config-1.yml  OK
config-2.yml  OK
invalid.yml   Error: did not find expected key while parsing a block mapping at line 2 column 3
xyzzy.yml     OK

$ echo $EXIT_CODE
1
Jordan Running
  • 102,619
  • 17
  • 182
  • 182
  • Thank you Jordan! I'm not so familiar with Ruby, but can you explain to me a little bit about how should I use the ruby code? If I understand it correctly, I would: 1. create a .rb file by coping your code into it. 2. cd into the directory that contains all the yaml files. 3. type in `ruby check_yaml.rb *.yml` in the command line. to check all of them. Is it correct? – Penny Dec 08 '15 at 23:18
  • Yes, that's correct. Or you can list each of the files you want to check, e.g. `ruby check_yaml.rb path/to/file.yml path/to/another/file.yml` – Jordan Running Dec 08 '15 at 23:26
  • Note that if `check_yaml.rb` is in a different directory than the one you're in you'll have to supply a relative or absolute path to it. – Jordan Running Dec 08 '15 at 23:28
  • thank you Jordan! It works great! I just have 2 more follow-up question: 1. Is it possible to check yaml files in a folder that has subfolders? 2. Does all YAMLs follow the same syntax? Cause I know that markdown files have different flavors, so are yamls having the same thing? Thanks! – Penny Dec 08 '15 at 23:50
  • 1. Yes. You could use extended globbing e.g. `foo/**/*.yml`. Here's more information: http://stackoverflow.com/questions/3529997/unix-wildcard-selectors-asterisks If you have too many files to use in a single command line you could use something like `find` with the `-exec` option or `xargs`. 2. Yes, there's only one YAML standard, so you shouldn't have any issues with multiple flavors. There are different versions of the official spec, although the differences are minor; most tools support 1.1; some don't support 1.2. You can learn more at http://yaml.org. – Jordan Running Dec 09 '15 at 00:49
0

Just want to share another YAML parser that I found useful too.

https://www.npmjs.com/package/yaml-to-json

Thank you all for helping with this!

Penny
  • 1,218
  • 1
  • 13
  • 32