How do I lint YAML files, without having to upload it to http://yamllint.com ?
For example, if I have
people:
1:
:name: John Smith
:name: Jane Smith
How do I make it warn me that the last :name
over-writes the first :name
?
I'm using Ruby 2.1, and Ubuntu 12.04.
A linter for YAML files. yamllint does not only check for syntax validity, but for weirdnesses like key repetition and cosmetic problems such as lines length, trailing spaces, indentation, etc.
Writing configuration files typically involves using the YAML data serialization language. YAML is an abbreviation that is a mark-up language, not a document. It is frequently employed in programs that store or transport data and configuration files. Data serialization is a feature of the YAML module in Ruby.
The yamllint command-line tool does what you want:
sudo pip install yamllint
Specifically, it has a rule key-duplicates
that detects repetitions and keys
over-writing one another:
$ yamllint test.yml
test.yml
1:1 warning missing document start "---" (document-start)
4:5 error duplication of key ":name" in mapping (key-duplicates)
(It has many other rules that you can enable/disable or tweak.)
Is this what you're after?
require 'yaml'
def check_yaml(filename)
unless YAML.dump(YAML.load_file(filename)) == File.read(filename).gsub(/\s*#.*/, '')
raise 'problem'
end
end
check_yaml 'somefile.yml'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With