★ YAML: enforce schema linting
• 216 words • 2 min • updated
YAML files can have a schema associated to them.
Schema store is a popular source for schemas. Or you could write your own.
JSON Schemas work on YAML files just fine.
Let’s say you found a schema. For example, for JSON Patch.
Assume this sample .yaml file:
- op: remove
path: /spec/syncPolicy/automatedHow to enforce the schema above in CI?
One approach I like is with YAML Language Server by Red Hat.
You start by annotating the file with the desired schema:
# yaml-language-server: $schema=https://json.schemastore.org/json-patch.json
- op: remove
path: /spec/syncPolicy/automatedAnd then you integrate it with a CI tool that is aware of it. I like pre-commit.com. Assuming you follow the pre-commit setup, integrate the hook:
repos:
- repo: https://github.com/jmlrt/check-yamlschema
rev: v0.0.4
hooks:
- id: check-yamlschemaThen pre-commit run [--all-files] check-yamlschema shall perform its job.
https://github.com/jmlrt/check-yamlschema:
A CLI and pre-commit hook for jsonschema validation in YAML files with multiple documents
Parse multi-documents YAML files, look for inline schema comments, and validate the documents according to their schema.
The ‘any’ schema1 can be handy as an opt-out mechanism:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://json.schemastore.org/any.json",
"anyOf": [
{ "type": "object" },
{ "type": "array" },
{ "type": "string" },
{ "type": "number" },
{ "type": "boolean" },
{ "type": "null" }
]
}