Bulk validating yaml files

Penny

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related