102

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.

dreftymac
  • 31,404
  • 26
  • 119
  • 182
Klaim
  • 67,274
  • 36
  • 133
  • 188

6 Answers6

56

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.

vossad01
  • 11,552
  • 8
  • 56
  • 109
  • How can we achieve the validation of yaml in java. I am using https://github.com/networknt/json-schema-validator which works fine if validating against a json file, however, does not work against yaml file. – Piyush Kumar Nov 18 '19 at 15:39
  • 2
    Just leaving a note to readers following along, that you'll probably want to use [`ajv`](https://www.npmjs.com/package/ajv) to validate JSON, and [`js-yaml`](https://www.npmjs.com/package/js-yaml) to turn your yaml into JSON. These are the healthiest node libs for these purposes as of 2020. – Steven Lu Apr 25 '20 at 07: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 May 26 '20 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: https://marketplace.visualstudio.com/items?itemName=redhat.vscode-yaml – Jonas Sourlier May 27 '20 at 13:22
19

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

Raffi Khatchadourian
  • 3,042
  • 3
  • 31
  • 37
leebriggs
  • 3,187
  • 1
  • 20
  • 17
  • Kwalify will not let you do Mapping of Mappings, like: Joey: age: 22 gender: M Ann: age: 34 gender: F – Toddius Zho Jun 23 '15 at 21:50
  • 8
    `Kwalify` was a good alternative unfortunately it is not maintained anymore. [pykwalify](https://github.com/Grokzen/pykwalify) looks promising – nowox Oct 28 '15 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 Aug 08 '16 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.

Yuri Astrakhan
  • 8,808
  • 6
  • 63
  • 97
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.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
  • Hello, I want to create a YAML validator. what should I do. I mean what is the best approach to do that? – Akash Patel Sep 19 '20 at 08: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. – Ondra Žižka Sep 23 '20 at 07: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
Yonah Dissen
  • 1,197
  • 1
  • 8
  • 16
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.

joanvallve
  • 11
  • 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 Aug 31 '23 at 12:17