Penny
Penny

Reputation: 1280

Bulk validating yaml files

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?

Upvotes: 3

Views: 1241

Answers (2)

Penny
Penny

Reputation: 1280

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!

Upvotes: 0

Jordan Running
Jordan Running

Reputation: 106077

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

Upvotes: 2

Related Questions