DEV Community

loading...

YAML Validations

Maxime Guilbert
Just a dev who touches a lot of things and wants to share his experiences. (or just want to note somewhere things to don't forget)
・2 min read

In some pipelines or when a project build, we want to check if some files are build correctly like our YAML files!

So here is YamlLint!

You can find on their website all that you will need to use it, but here is a quick look!

Quick look

Setup

Depending your project/machine, you can :

  • install it directly on your machine if you are on Linux
sudo apt-get install yamllint
Enter fullscreen mode Exit fullscreen mode
  • install it with brew
brew install yamllint
Enter fullscreen mode Exit fullscreen mode
  • add it in your Python project
python setup.py sdist
pip install --user dist/yamllint-*.tar.gz
Enter fullscreen mode Exit fullscreen mode

Then you can use it directly with the default configs, or override it with your configs.

Overiding configs

To do it, create a .yamllint file at the top level of your project. Then, in this file, you can enable/disable rules or override rules ...

example

---

yaml-files:
  - '*.yaml'
  - '*.yml'
  - '.yamllint'

rules:
  braces:
    level: warning
    max-spaces-inside: 1
  brackets: enable
  colons: enable
  commas: enable
  comments:
    level: warning
  comments-indentation:
    level: warning
  document-end: disable
  document-start:
    level: warning
  empty-lines: enable
  empty-values: disable
  hyphens: enable
  indentation: enable
  key-duplicates: enable
  key-ordering: disable
  line-length: enable
  new-line-at-end-of-file: enable
  new-lines: enable
  octal-values: disable
  quoted-strings: disable
  trailing-spaces: enable
  truthy:
    level: warning
Enter fullscreen mode Exit fullscreen mode

Documentation links

Run

Then, you can run the yaml lint to test it!

When you want to check some files

yamllint file.yml other-file.yaml
Enter fullscreen mode Exit fullscreen mode

When you want to check all the files in the folder

yamllint .
Enter fullscreen mode Exit fullscreen mode

Alternatives

If you are working with a Maven project, maybe you want to check it during your build! In this case you can use this library:

GitHub logo sbaudoin / yamllint

YAML Linter written in Java

YAML Lint

Apache License, Version 2.0, January 2004 Maven Central Build Status Sonarcloud Status Sonarcloud Status javadoc

YAML lint written in Java Its main purpose is to provide an API and scripts to analyze YAML documents The YAML documents are syntactically checked as well as against rules. To get the list of rules, please refer to the classes of the com.github.sbaudoin.yamllint.rules package. Among other, we have a rule to check the presence of the start and end YAML document marker, the correct and consistent indentation, etc.

API usage

Maven dependency:

<dependency>
    <groupId>com.github.sbaudoin</groupId>
    <artifactId>yamllint</artifactId>
    <version>1.4.0</version>
</dependency>

For use, please refer to the JavaDoc.

The class that will mostly interest you is com.github.sbaudoin.yamllint.Linter: it contains static methods that can be used to analyze a YAML string or a file.

3 errors levels have been defined: info, warning and error.

The linter can return only one syntax error per file (once a syntax error has been met we cannot expect a lot from the rest of…


Links


I hope it will help you!

Please don't hesitate to give me a feedback about this post to improuve my writing skills. Thanks!

Discussion (0)