Difference between revisions of "Yamllint"
(add that you have to manually fix errors) |
(add content on how to use comment directives in yamllint) |
||
Line 1: | Line 1: | ||
Use an online validator like https://www.yamllint.com/ or https://jsonformatter.org/yaml-validator | Use an online validator like https://www.yamllint.com/ or https://jsonformatter.org/yaml-validator | ||
− | Of course, you should have one in your local tools and CI pipeline to ensure that your [[YAML]] is always correct. | + | Of course, you should have one in your local tools and CI pipeline to ensure that your [[YAML]] is always correct. With the yamllint, there is both a script and a Python module; meaning you can write your own linting tool in Python by invoking (importing) the yamllint module<ref>https://yamllint.readthedocs.io/en/stable/development.html</ref>. See the caveat section below about "using the right tool for the job" - meaning use the right linter for the language/project you are linting. |
− | == Manually fix errors== | + | ==Manually fix errors== |
Unfortunately, <tt>yamllint</tt> does not fix your file for you. There could be ambiguities about the proper fix, so you need to do the fix(es) yourself. | Unfortunately, <tt>yamllint</tt> does not fix your file for you. There could be ambiguities about the proper fix, so you need to do the fix(es) yourself. | ||
− | == Many linters == | + | == Tips == |
+ | You can use '''[https://yamllint.readthedocs.io/en/stable/disable_with_comments.html comment directives]''' to control the behavior of <code>yamllint</code> | ||
+ | |||
+ | With <code>disable-line</code> you put the directive in-line, or on the line above.<syntaxhighlight lang="yaml"> | ||
+ | # The following mapping contains the same key twice, | ||
+ | # but I know what I'm doing: | ||
+ | key: value 1 | ||
+ | # yamllint disable-line rule:key-duplicates | ||
+ | key: value 2 | ||
+ | |||
+ | # yamllint disable-line rule:line-length | ||
+ | - This line is waaaaaaaaaay too long but yamllint will not report anything about it. | ||
+ | This line will be checked by yamllint. | ||
+ | </syntaxhighlight>With <code>disable</code> you can disable multiple [https://yamllint.readthedocs.io/en/stable/rules.html rules] for the entire file.<syntaxhighlight lang="yaml"> | ||
+ | # yamllint disable rule:hyphens rule:commas rule:indentation | ||
+ | </syntaxhighlight>Or even use <code>disable-file</code><syntaxhighlight lang="yaml"> | ||
+ | # yamllint disable-file | ||
+ | # This file is not valid YAML because it is a Jinja template | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==Many linters== | ||
You can't just use "one" solution either<ref>https://phabricator.wikimedia.org/T95890</ref>. The leading GPL linter is based on Python, so depending on your code repo, you may instead want to use a JavaScript or PHP implementation. Thus, tools like [[wp:Grunt (software)]] may be used to automate JSHint linting in [[JavaScript]] projects<ref>https://www.codereadability.com/jshint-with-grunt/</ref>. | You can't just use "one" solution either<ref>https://phabricator.wikimedia.org/T95890</ref>. The leading GPL linter is based on Python, so depending on your code repo, you may instead want to use a JavaScript or PHP implementation. Thus, tools like [[wp:Grunt (software)]] may be used to automate JSHint linting in [[JavaScript]] projects<ref>https://www.codereadability.com/jshint-with-grunt/</ref>. | ||
− | == Source, Docs and Reading == | + | ==Source, Docs and Reading== |
− | * https://github.com/adrienverge/yamllint | + | |
− | * https://yamllint.readthedocs.io/en/stable/ | + | *https://github.com/adrienverge/yamllint |
− | * https://www.redhat.com/sysadmin/check-yaml-yamllint | + | *https://yamllint.readthedocs.io/en/stable/ |
+ | *https://www.redhat.com/sysadmin/check-yaml-yamllint | ||
{{References}} | {{References}} |
Revision as of 10:17, 20 January 2024
Use an online validator like https://www.yamllint.com/ or https://jsonformatter.org/yaml-validator
Of course, you should have one in your local tools and CI pipeline to ensure that your Yaml is always correct. With the yamllint, there is both a script and a Python module; meaning you can write your own linting tool in Python by invoking (importing) the yamllint module[1]. See the caveat section below about "using the right tool for the job" - meaning use the right linter for the language/project you are linting.
Manually fix errors[edit | edit source]
Unfortunately, yamllint does not fix your file for you. There could be ambiguities about the proper fix, so you need to do the fix(es) yourself.
Tips[edit | edit source]
You can use comment directives to control the behavior of yamllint
With disable-line
you put the directive in-line, or on the line above.
# The following mapping contains the same key twice,
# but I know what I'm doing:
key: value 1
# yamllint disable-line rule:key-duplicates
key: value 2
# yamllint disable-line rule:line-length
- This line is waaaaaaaaaay too long but yamllint will not report anything about it.
This line will be checked by yamllint.
With disable
you can disable multiple rules for the entire file.
# yamllint disable rule:hyphens rule:commas rule:indentation
Or even use disable-file
# yamllint disable-file
# This file is not valid YAML because it is a Jinja template
Many linters[edit | edit source]
You can't just use "one" solution either[2]. The leading GPL linter is based on Python, so depending on your code repo, you may instead want to use a JavaScript or PHP implementation. Thus, tools like wp:Grunt (software) may be used to automate JSHint linting in JavaScript projects[3].
Source, Docs and Reading[edit | edit source]
- https://github.com/adrienverge/yamllint
- https://yamllint.readthedocs.io/en/stable/
- https://www.redhat.com/sysadmin/check-yaml-yamllint