106

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.

2

6 Answers 6

58

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.

4
  • How can we achieve the validation of yaml in java. I am using github.com/networknt/json-schema-validator which works fine if validating against a json file, however, does not work against yaml file. Commented Nov 18, 2019 at 15:39
  • 2
    Just leaving a note to readers following along, that you'll probably want to use ajv to validate JSON, and js-yaml to turn your yaml into JSON. These are the healthiest node libs for these purposes as of 2020.
    – Steven Lu
    Commented Apr 25, 2020 at 7:31
  • 1
    I am still a little confused. @cheesus just to clarify, when you say "works like a charm" are you saying you converted YAML in to JSON and then validated it with JSON schema tools (like ajv) or are those tools somehow meant to work directly with YAML? (They don't in my experience, and I would not expect them either. Just the phrase "JSON Schema can be used with most YAML documents" reads like they should?!?) Could someone please clarify. Thank you.
    – McKrassy
    Commented May 26, 2020 at 11:26
  • 2
    @McKrassy, yes, JSON Schema can indeed be used with YAML files, without converting them to JSON. When thinking about this, you should regard both JSON and YAML files as tree structures. JSON Schema place constraints on these tree structures, so they apply to both JSON and YAML files. Here's an example of a JSON schema used for YAML: marketplace.visualstudio.com/items?itemName=redhat.vscode-yaml Commented May 27, 2020 at 13:22
19

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

3
  • Kwalify will not let you do Mapping of Mappings, like: Joey: age: 22 gender: M Ann: age: 34 gender: F Commented Jun 23, 2015 at 21:50
  • 8
    Kwalify was a good alternative unfortunately it is not maintained anymore. pykwalify looks promising
    – nowox
    Commented Oct 28, 2015 at 12:30
  • 5
    Using something that is not a standard does not seem like a good solution. I would rather use a jsonschema and then validate YAML using it since YAML has one to one mapping to JSON. Swagger does something similar.
    – Lucas
    Commented Aug 8, 2016 at 22:21
2

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.

1

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.

2
  • Hello, I want to create a YAML validator. what should I do. I mean what is the best approach to do that? Commented Sep 19, 2020 at 8:56
  • @AkashPatel, most common approach is to have a schema. Then you need something that will validate the whole YAML tree against that schema. The approach of validation itself depends on how potent your schema is - ranging from a simple strict trees with no recursion, to complex stuff like XSD has. That's why I suggested the approach in my answer. Commented Sep 23, 2020 at 7:45
1

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
0

If your project is in C++, you can also use the yaml-schema-cpp library. It allows to validate (and complete) the .yaml input files using schema files (YAML files with extension .schema) from your C++ code.

1
  • Unfortunately it is not a simply to use library, lacking of documentations too. It also looks there is no work for the open-source aspect of it, but it is mostly an internal project made public. so not sure it is a robust library yet.
    – Raffaello
    Commented Aug 31, 2023 at 12:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.