YAML Schema Validation?

ValidationXsdSchemaYaml

Validation Problem Overview


Is there a schema validation language for YAML? I've googled but couldn't find anything useful.

Something like XSD format, using the language itself to describe the schema, would be the best choice in my case.

Validation Solutions


Solution 1 - Validation

JSON Schema can be used with most YAML documents resulting in a more portable and better documented solution than Rx or Kwalify. JSON Schema is the only of the three for which I have been able to find editor support.

More information on using YAML and JSON Schema including tools and editor support is tracked on this page. At the time of writing, there is editor support in Visual Studio Code and a command-line based validation tool available via npm.


Full Disclosure: I authored the web site linked above to try to make the application of JSON Schema to YAML more discoverable. I also wrote an editor extension for VS Code that has since been superseded by the RedHat extension linked above.

Solution 2 - Validation

Try Kwalify (Ruby and Java only), or Rx (many languages)

Solution 3 - Validation

I wonder if it would make sense to reuse JSON schema for this. YAML can be easily converted to JSON without loosing any information (?), so in theory YAML could be validated by the same tool chain, allowing open source community to concentrate on one good schema tool chain. The schema itself could also be written in YAML and converted to JSON.

Solution 4 - Validation

Good idea. Googled this up because I was looking for the same.

It's possible to convert YAML to XML in a defined manner (similarly to JSON <-> XML) and validate with a standard XML validator.

Depending on your platform, there are tools or snippets for that conversion: JavaScript (NPM), Ruby, Java (Jackson), Java (TestNG) (you'll need to see the source for what parameters it wants).

If done using an API, the error positions can even be mapped back to the original YAML file.

Solution 5 - Validation

You can use this python ysd project to validate your yaml files. https://github.com/yonahd/ysd Super simple to use

python yaml-validator/main.py -v yamls/example-values.yaml -r yamls/example-rules.yaml 

Example rule file:

required: // field must exist and type must match
  env: str
  enabled: bool
  replicas: int
optional: // if field exists type must match
  disk: str

Example yaml file (helm values file):

network:
  service:
    port: 8060
enabled: true
image:
  app: my-app:build
replicas: 1
env: dev
disk: local

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKlaimView Question on Stackoverflow
Solution 1 - Validationvossad01View Answer on Stackoverflow
Solution 2 - ValidationleebriggsView Answer on Stackoverflow
Solution 3 - ValidationYuri AstrakhanView Answer on Stackoverflow
Solution 4 - ValidationOndra ŽižkaView Answer on Stackoverflow
Solution 5 - ValidationYonah DissenView Answer on Stackoverflow