JSON Schema can be used to validate YAML documents. YAML Ain't Markup Language (YAML) is a powerful data serialization language that aims to be human friendly. Most JSON is syntactically valid YAML, but idiomatic YAML follows very different conventions. While YAML has advanced features that cannot be directly mapped to JSON, most YAML files use features that can be validated by JSON Schema. JSON Schema is the most portable and broadly supported choice for YAML validation.

JSON

[
    {
        "id": 2,
        "name": "An ice sculpture",
        "price": 12.5,
        "tags": [
            "cold",
            "ice"
        ],
        "dimensions": {
            "length": 7,
            "width": 12,
            "height": 9.5
        },
        "warehouseLocation": {
            "latitude": -78.75,
            "longitude": 20.4
        }
    },
    {
        "id": 3,
        "name": "A blue mouse",
        "price": 25.5,
        "dimensions": {
            "length": 3.1,
            "width": 1,
            "height": 1
        },
        "warehouseLocation": {
            "latitude": 54.4,
            "longitude": -32.7
        }
    }
]

YAML

- id: 2
  name: An ice sculpture
  price: 12.5
  tags:
    - cold
    - ice
  dimensions:
    length: 7
    width: 12
    height: 9.5
  warehouseLocation:
    latitude: -78.75
    longitude: 20.4
- id: 3
  name: A blue mouse
  price: 25.5
  dimensions:
    length: 3.1
    width: 1
    height: 1
  warehouseLocation:
    latitude: 54.4
    longitude: -32.7


Example JSON from the JSON Schema website.

Editor Support

Editor Validation Error Highlighting Completion Suggestions
Visual Studio Code

Tools and Libraries

  • Polyglottal JSON Schema Validator - Written in JavaScript. Released under the MIT license.
    • Polyglottal JSON Schema Validator (pajv) is a command line utility that can be used to validate data in numerous formats against a JSON Schema.

Alternatives

  • Kwalify
    • Portability concerns with only Ruby and Python implementations
    • Only Python has recent development activity
    • No tooling support
  • Rx
    • Supports JavaScript, Perl, PHP, Ruby, Python
    • No recent development activity
    • No tooling support
  • simple-yaml-validator
    • JavaScript only
    • Only structural validation
    • Little adoption. npm shows low download count.
  • Yamale
    • Python only
    • Relatively unknown