JSON Schema for YAML Validation
JSON Schema validates YAML files by first parsing YAML to a data structure, then validating against the schema. Use tools like ajv, Pydantic, or VS Code YAML extension for validation. This guide covers JSON Schema to YAML conversion, validation workflows, and IDE integration. Need to convert? Try our free online converter.
How JSON Schema Validates YAML
Since YAML is a superset of JSON, both parse to the same data structures. JSON Schema validates the data, not the file format.
| Step | Process |
|---|---|
| 1 | Parse YAML file → Data structure (dict/object) |
| 2 | Load JSON Schema |
| 3 | Validate data against schema |
| 4 | Report errors or success |
JSON Schema Example
Schema (JSON)
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 1
},
"version": {
"type": "string",
"pattern": "^\\d+\\.\\d+\\.\\d+$"
},
"dependencies": {
"type": "array",
"items": { "type": "string" }
}
},
"required": ["name", "version"]
}
Valid YAML File
# config.yaml - validates against schema
name: my-project
version: "1.0.0"
dependencies:
- express
- lodash
Same Schema in YAML
# schema.yaml (same schema, YAML format)
$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
name:
type: string
minLength: 1
version:
type: string
pattern: "^\\d+\\.\\d+\\.\\d+$"
dependencies:
type: array
items:
type: string
required:
- name
- version
Validation Tools
Python with jsonschema
import yaml
import json
from jsonschema import validate, ValidationError
# Load YAML file
with open('config.yaml') as f:
data = yaml.safe_load(f)
# Load JSON Schema
with open('schema.json') as f:
schema = json.load(f)
# Validate
try:
validate(instance=data, schema=schema)
print("✓ Valid!")
except ValidationError as e:
print(f"✗ Invalid: {e.message}")
Node.js with ajv
import Ajv from 'ajv';
import yaml from 'js-yaml';
import fs from 'fs';
const ajv = new Ajv();
// Load files
const schema = JSON.parse(fs.readFileSync('schema.json'));
const data = yaml.load(fs.readFileSync('config.yaml'));
// Validate
const validate = ajv.compile(schema);
if (validate(data)) {
console.log('✓ Valid!');
} else {
console.log('✗ Errors:', validate.errors);
}
CLI with yq + ajv-cli
# Convert YAML to JSON, pipe to ajv
yq -o=json config.yaml | ajv validate -s schema.json -d -
# Or use check-jsonschema (supports YAML directly)
pip install check-jsonschema
check-jsonschema --schemafile schema.json config.yaml
VS Code Integration
The YAML extension by Red Hat validates YAML files using JSON Schema:
Method 1: Modeline in File
# yaml-language-server: $schema=./schema.json
name: my-project
version: "1.0.0"
Method 2: VS Code Settings
// .vscode/settings.json
{
"yaml.schemas": {
"./schema.json": "config.yaml",
"https://json.schemastore.org/package.json": "package.json"
}
}
Method 3: File Patterns
{
"yaml.schemas": {
"./schema.json": ["*.config.yaml", "config/*.yaml"]
}
}
Frequently Asked Questions
Can JSON Schema validate YAML files?
Yes, JSON Schema can validate YAML files. Since YAML is a superset of JSON, you first convert YAML to JSON (or parse to a data structure), then validate against the JSON Schema. Many tools like ajv, Pydantic, and IDE extensions support this workflow.
How do I convert JSON Schema to YAML?
Use yq to convert: yq -P schema.json > schema.yaml. You can
write your JSON Schema in YAML format since it's just data. However,
JSON is the standard format for JSON Schema, so keep the original in
JSON for tooling compatibility.
Is there a YAML Schema standard?
There's no official YAML Schema standard like JSON Schema. Most YAML validation uses JSON Schema after converting YAML to JSON. Some tools use custom schema formats specific to their domain (like Kubernetes CRDs or OpenAPI).
How do I validate YAML in VS Code?
Install the YAML extension by Red Hat. Add a modeline to
your YAML file: # yaml-language-server: $schema=path/to/schema.json.
The extension validates in real-time and provides autocomplete based on
the JSON Schema.
Need to convert between formats?
Use our free online converter for instant JSON ↔ YAML conversion.
Open Converter Tool →