YAML Schema is a technology for validating the structure and content of YAML files, helping to ensure the correctness and consistency of configuration files.
Basic Concepts of YAML Schema
What is YAML Schema
YAML Schema is a document that defines the expected structure of a YAML file, similar to JSON Schema. It describes:
- Allowed fields
- Data types of fields
- Required fields
- Field constraints
- Default values
Why Need YAML Schema
- Validate Configuration: Ensure configuration files meet expected structure
- Documentation: Automatically generate configuration documentation
- IDE Support: Provide autocomplete and error hints
- Team Collaboration: Unify configuration standards
- Error Prevention: Discover configuration errors before runtime
Common YAML Schema Formats
1. JSON Schema
JSON Schema is the most commonly used YAML validation format because YAML is a superset of JSON.
yaml# config.yaml server: host: localhost port: 8080 ssl: true
json// schema.json { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "server": { "type": "object", "properties": { "host": { "type": "string", "format": "hostname" }, "port": { "type": "integer", "minimum": 1, "maximum": 65535 }, "ssl": { "type": "boolean" } }, "required": ["host", "port"] } }, "required": ["server"] }
2. K8s OpenAPI Schema
Kubernetes uses OpenAPI Schema to validate resource configurations.
yamlapiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.21
3. Custom Schema Format
Some tools use custom Schema formats.
yaml# schema.yaml type: object properties: server: type: object properties: host: type: string pattern: '^[a-zA-Z0-9.-]+$' port: type: integer min: 1 max: 65535 ssl: type: boolean default: false required: - server - server.host - server.port
YAML Schema Validation Tools
1. Python Validation
pythonimport yaml from jsonschema import validate, ValidationError # Load YAML file with open('config.yaml', 'r') as f: config = yaml.safe_load(f) # Load Schema with open('schema.json', 'r') as f: schema = yaml.safe_load(f) # Validate try: validate(instance=config, schema=schema) print("YAML validation passed") except ValidationError as e: print(f"YAML validation failed: {e.message}")
2. JavaScript Validation
javascriptconst yaml = require('js-yaml'); const Ajv = require('ajv'); const fs = require('fs'); // Load YAML file const config = yaml.safeLoad(fs.readFileSync('config.yaml', 'utf8')); // Load Schema const schema = JSON.parse(fs.readFileSync('schema.json', 'utf8')); // Validate const ajv = new Ajv(); const validate = ajv.compile(schema); if (validate(config)) { console.log('YAML validation passed'); } else { console.log('YAML validation failed:', validate.errors); }
3. Command Line Tools
bash# Use yamllint yamllint -d relaxed config.yaml # Use kubeval (Kubernetes) kubeval deployment.yaml # Use spectral (OpenAPI) spectral lint openapi.yaml
Common Validation Scenarios
1. Type Validation
yaml# config.yaml server: host: localhost port: 8080 ssl: true
json// schema.json { "type": "object", "properties": { "server": { "type": "object", "properties": { "host": { "type": "string" }, "port": { "type": "integer" }, "ssl": { "type": "boolean" } } } } }
2. Required Field Validation
json{ "type": "object", "required": ["server", "database"], "properties": { "server": { "type": "object", "required": ["host", "port"], "properties": { "host": { "type": "string" }, "port": { "type": "integer" } } }, "database": { "type": "object", "required": ["url"], "properties": { "url": { "type": "string" } } } } }
3. Numeric Range Validation
json{ "type": "object", "properties": { "port": { "type": "integer", "minimum": 1, "maximum": 65535 }, "timeout": { "type": "integer", "minimum": 0, "maximum": 3600 } } }
4. String Format Validation
json{ "type": "object", "properties": { "email": { "type": "string", "format": "email" }, "url": { "type": "string", "format": "uri" }, "ip": { "type": "string", "format": "ipv4" } } }
5. Enum Value Validation
json{ "type": "object", "properties": { "environment": { "type": "string", "enum": ["development", "staging", "production"] }, "log_level": { "type": "string", "enum": ["debug", "info", "warn", "error"] } } }
6. Array Validation
json{ "type": "object", "properties": { "servers": { "type": "array", "items": { "type": "object", "properties": { "host": { "type": "string" }, "port": { "type": "integer" } }, "required": ["host", "port"] }, "minItems": 1, "maxItems": 10 } } }
7. Regex Validation
json{ "type": "object", "properties": { "version": { "type": "string", "pattern": "^\\d+\\.\\d+\\.\\d+$" }, "hostname": { "type": "string", "pattern": "^[a-zA-Z0-9.-]+$" } } }
Advanced Validation Features
1. Conditional Validation
json{ "type": "object", "properties": { "ssl": { "type": "boolean" }, "cert_path": { "type": "string" }, "key_path": { "type": "string" } }, "if": { "properties": { "ssl": { "const": true } } }, "then": { "required": ["cert_path", "key_path"] } }
2. Default Values
json{ "type": "object", "properties": { "timeout": { "type": "integer", "default": 30 }, "retry": { "type": "integer", "default": 3 } } }
3. Custom Validation
pythonimport yaml from jsonschema import validate, ValidationError def custom_validator(validator, value, instance, schema): if validator.is_type(instance, "object"): for property, subschema in schema.get("properties", {}).items(): if "custom" in subschema: if not subschema["custom"](instance.get(property)): yield ValidationError( f"Custom validation failed for {property}" ) # Use custom validation schema = { "type": "object", "properties": { "port": { "type": "integer", "custom": lambda x: 1024 <= x <= 65535 } } }
Practical Application Examples
Kubernetes Configuration Validation
pythonfrom kubernetes import client, config from jsonschema import validate # Kubernetes Deployment Schema k8s_deployment_schema = { "type": "object", "required": ["apiVersion", "kind", "metadata", "spec"], "properties": { "apiVersion": {"enum": ["apps/v1"]}, "kind": {"const": "Deployment"}, "metadata": { "type": "object", "required": ["name"] }, "spec": { "type": "object", "required": ["replicas", "selector", "template"] } } } # Validate Deployment configuration with open('deployment.yaml', 'r') as f: deployment = yaml.safe_load(f) validate(instance=deployment, schema=k8s_deployment_schema)
Application Configuration Validation
yaml# app-config.yaml server: host: api.example.com port: 443 ssl: true database: type: postgresql host: db.example.com port: 5432 name: myapp features: - authentication - logging - monitoring
json// app-config-schema.json { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "required": ["server", "database"], "properties": { "server": { "type": "object", "required": ["host", "port"], "properties": { "host": { "type": "string", "format": "hostname" }, "port": { "type": "integer", "minimum": 1, "maximum": 65535 }, "ssl": { "type": "boolean", "default": false } } }, "database": { "type": "object", "required": ["type", "host", "port", "name"], "properties": { "type": { "type": "string", "enum": ["postgresql", "mysql", "mongodb"] }, "host": { "type": "string", "format": "hostname" }, "port": { "type": "integer", "minimum": 1, "maximum": 65535 }, "name": { "type": "string", "minLength": 1 } } }, "features": { "type": "array", "items": { "type": "string", "enum": ["authentication", "logging", "monitoring", "caching"] }, "uniqueItems": true } } }
Best Practices
1. Schema Design Principles
- Keep it simple and clear
- Use appropriate constraints
- Provide meaningful error messages
- Document the Schema
2. Validation Timing
- Validate in CI/CD pipelines
- Validate at application startup
- Validate during configuration file editing (IDE integration)
3. Error Handling
pythontry: validate(instance=config, schema=schema) except ValidationError as e: print(f"Validation failed: {e.message}") print(f"Path: {' -> '.join(str(p) for p in e.path)}") print(f"Value: {e.instance}")
4. Schema Version Management
json{ "$schema": "http://json-schema.org/draft-07/schema#", "$id": "https://example.com/schemas/config-v1.json", "title": "Application Configuration", "version": "1.0.0" }
Recommended Tools
- yamllint: YAML syntax checking
- kubeval: Kubernetes configuration validation
- spectral: OpenAPI specification validation
- ajv: JavaScript JSON Schema validator
- jsonschema: Python JSON Schema validator
YAML Schema validation is an important means of ensuring configuration file quality and should be widely used in the development process.