YAML Tools & Validation
Discover essential tools for validating, formatting, and working with YAML files effectively.
Online YAML Tools
Online tools provide quick and easy ways to validate, format, and convert YAML files without installing anything.
🔍 YAML Validator
Validate YAML syntax and catch errors before they cause problems in production.
- Syntax validation
- Error highlighting
- Line-by-line analysis
- Best practice suggestions
✨ YAML Formatter
Format and beautify YAML files with consistent indentation and structure.
- Auto-formatting
- Indentation correction
- Consistent spacing
- Readable output
🔄 YAML to JSON
Convert YAML files to JSON format for use with other tools and systems.
- Bidirectional conversion
- Preserve data structure
- Copy to clipboard
- Download results
Command Line Tools
Command line tools provide powerful YAML processing capabilities for automation and scripting.
yamllint - YAML Linter
# Install yamllint
pip install yamllint
# Basic linting
yamllint config.yml
# Lint with specific rules
yamllint -d relaxed config.yml
# Lint multiple files
yamllint *.yml
# Lint with custom configuration
yamllint -c .yamllint config.yml
# Example .yamllint configuration
rules:
line-length:
max: 120
level: warning
indentation:
spaces: 2
level: error
truthy:
allowed-values: ['true', 'false']
level: error
comments:
min-spaces-from-content: 1
level: warning
yq - YAML Processor
# Install yq
# macOS
brew install yq
# Linux
wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/local/bin/yq
chmod +x /usr/local/bin/yq
# Basic operations
yq eval '.database.host' config.yml
yq eval '.services[0].name' docker-compose.yml
# Modify values
yq eval '.database.host = "newhost"' config.yml -i
# Add new keys
yq eval '.database.port = 5432' config.yml -i
# Delete keys
yq eval 'del(.database.password)' config.yml -i
# Convert to JSON
yq eval -o=json config.yml
# Merge files
yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' file1.yml file2.yml
# Filter arrays
yq eval '.services[] | select(.image | contains("nginx"))' docker-compose.yml
js-yaml - Node.js YAML Library
# Install js-yaml
npm install js-yaml
# Basic usage
const yaml = require('js-yaml');
const fs = require('fs');
// Load YAML file
try {
const doc = yaml.load(fs.readFileSync('config.yml', 'utf8'));
console.log(doc);
} catch (e) {
console.error(e);
}
// Dump object to YAML
const obj = {
name: 'My App',
version: '1.0.0',
database: {
host: 'localhost',
port: 5432
}
};
const yamlStr = yaml.dump(obj, {
indent: 2,
lineWidth: 120,
noRefs: true,
sortKeys: false
});
console.log(yamlStr);
// Validate YAML
function validateYAML(yamlString) {
try {
yaml.load(yamlString);
return { valid: true, error: null };
} catch (e) {
return { valid: false, error: e.message };
}
}
IDE and Editor Support
Modern IDEs and editors provide excellent YAML support with syntax highlighting, validation, and formatting.
Visual Studio Code
Excellent YAML support with extensions and built-in features.
- Red Hat YAML Extension: Syntax highlighting, validation, formatting
- YAML Support by Red Hat: IntelliSense, hover information
- YAML Sort: Sort YAML keys alphabetically
- YAML Lint: Real-time linting and error detection
VS Code Settings
{
"yaml.schemas": {
"https://json.schemastore.org/github-workflow.json": ".github/workflows/*.yml",
"https://json.schemastore.org/docker-compose.json": "docker-compose*.yml"
},
"yaml.format.enable": true,
"yaml.format.singleQuote": false,
"yaml.format.bracketSpacing": true,
"yaml.validate": true,
"yaml.hover": true,
"yaml.completion": true
}
IntelliJ IDEA / PyCharm
Comprehensive YAML support with advanced features.
- Built-in YAML support: Syntax highlighting, validation
- Schema validation: JSON Schema support for YAML
- Refactoring: Rename, move, and extract operations
- Code completion: Smart suggestions and auto-completion
Vim / Neovim
Powerful YAML editing with plugins and configurations.
- vim-yaml: Syntax highlighting and indentation
- coc-yaml: Language server support
- vim-yaml-folds: Code folding for YAML
- ale: Linting and formatting integration
Vim Configuration
" YAML settings
autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab
autocmd FileType yaml setlocal foldmethod=indent
autocmd FileType yaml setlocal foldlevelstart=20
" YAML folding
let g:yaml_fold_level_start = 1
let g:yaml_fold_level_end = 10
Sublime Text
Lightweight editor with good YAML support through packages.
- YAML: Syntax highlighting and snippets
- YAML Lint: Real-time validation
- Pretty YAML: Format and beautify YAML
- YAML Sort: Sort YAML keys
CI/CD Integration
Integrate YAML validation and formatting into your CI/CD pipelines to catch issues early.
GitHub Actions YAML Validation
name: YAML Validation
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
jobs:
validate-yaml:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install yamllint
run: pip install yamllint
- name: Validate YAML files
run: |
find . -name "*.yml" -o -name "*.yaml" | xargs yamllint
- name: Validate Docker Compose
run: |
docker-compose config
- name: Validate Kubernetes manifests
run: |
kubectl apply --dry-run=client -f k8s/
- name: Check YAML formatting
run: |
for file in $(find . -name "*.yml" -o -name "*.yaml"); do
echo "Checking $file"
yamllint -d relaxed "$file"
done
GitLab CI YAML Validation
stages:
- validate
- test
variables:
YAMLLINT_CONFIG: .yamllint
validate_yaml:
stage: validate
image: python:3.9
before_script:
- pip install yamllint
script:
- yamllint -c $YAMLLINT_CONFIG .
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == "main"
validate_docker_compose:
stage: validate
image: docker:latest
services:
- docker:dind
script:
- docker-compose config
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == "main"
validate_kubernetes:
stage: validate
image: bitnami/kubectl:latest
script:
- kubectl apply --dry-run=client -f k8s/
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == "main"
Pre-commit Hooks
# .pre-commit-config.yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
args: ['--unsafe']
- id: check-added-large-files
- repo: https://github.com/adrienverge/yamllint.git
rev: v1.32.0
hooks:
- id: yamllint
args: [-c=.yamllint]
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.0-alpha.9-for-vscode
hooks:
- id: prettier
types: [yaml]
args: [--tab-width=2]
- repo: local
hooks:
- id: yaml-sort
name: Sort YAML keys
entry: yq
args: [eval, '.', -i]
language: system
files: \.(yml|yaml)$
Schema Validation
Use JSON Schema to validate YAML files against predefined schemas for better data integrity.
JSON Schema for YAML
# config-schema.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": ["application", "database", "api"],
"properties": {
"application": {
"type": "object",
"required": ["name", "version", "environment"],
"properties": {
"name": {
"type": "string",
"minLength": 1
},
"version": {
"type": "string",
"pattern": "^\\d+\\.\\d+\\.\\d+$"
},
"environment": {
"type": "string",
"enum": ["development", "staging", "production"]
}
}
},
"database": {
"type": "object",
"required": ["host", "port", "name"],
"properties": {
"host": {
"type": "string"
},
"port": {
"type": "integer",
"minimum": 1,
"maximum": 65535
},
"name": {
"type": "string"
},
"username": {
"type": "string"
},
"password": {
"type": "string"
}
}
},
"api": {
"type": "object",
"required": ["base_url", "timeout"],
"properties": {
"base_url": {
"type": "string",
"format": "uri"
},
"timeout": {
"type": "integer",
"minimum": 1
},
"rate_limit": {
"type": "integer",
"minimum": 1
}
}
}
}
}
Schema Validation Script
#!/usr/bin/env python3
import json
import yaml
import jsonschema
from jsonschema import validate
def validate_yaml_with_schema(yaml_file, schema_file):
"""Validate YAML file against JSON Schema"""
try:
# Load YAML file
with open(yaml_file, 'r') as f:
yaml_data = yaml.safe_load(f)
# Load JSON Schema
with open(schema_file, 'r') as f:
schema = json.load(f)
# Validate
validate(instance=yaml_data, schema=schema)
print(f"✅ {yaml_file} is valid according to schema")
return True
except yaml.YAMLError as e:
print(f"❌ YAML syntax error in {yaml_file}: {e}")
return False
except jsonschema.exceptions.ValidationError as e:
print(f"❌ Schema validation error in {yaml_file}: {e.message}")
return False
except Exception as e:
print(f"❌ Error validating {yaml_file}: {e}")
return False
# Usage
if __name__ == "__main__":
validate_yaml_with_schema("config.yml", "config-schema.json")
Performance Optimization
Optimize YAML files for better performance and faster parsing.
✅ Performance Tips
- Use anchors for repeated values - Reduces file size
- Avoid deeply nested structures - Slower to parse
- Use flow style for simple data - Faster parsing
- Minimize file size - Remove unnecessary whitespace
- Use appropriate data types - Avoid unnecessary type conversion
Performance Comparison
# ❌ Slow - deeply nested, repeated values
application:
database:
production:
host: "prod-db.example.com"
port: 5432
username: "admin"
password: "secret"
pool: 20
timeout: 30
ssl: true
staging:
host: "staging-db.example.com"
port: 5432
username: "admin"
password: "secret"
pool: 10
timeout: 30
ssl: true
development:
host: "dev-db.example.com"
port: 5432
username: "admin"
password: "secret"
pool: 5
timeout: 30
ssl: false
# ✅ Fast - using anchors, flat structure
defaults: &db_defaults
port: 5432
username: "admin"
password: "secret"
timeout: 30
database:
production:
<<: *db_defaults
host: "prod-db.example.com"
pool: 20
ssl: true
staging:
<<: *db_defaults
host: "staging-db.example.com"
pool: 10
ssl: true
development:
<<: *db_defaults
host: "dev-db.example.com"
pool: 5
ssl: false
Debugging YAML Issues
Common YAML issues and how to debug them effectively.
🔧 Common Issues and Solutions
Indentation Errors
Problem: "mapping values are not allowed here"
Solution: Check for consistent indentation (use 2 spaces)
# ❌ Wrong
database:
host: localhost
port: 5432 # Wrong indentation
# ✅ Correct
database:
host: localhost
port: 5432
Missing Colons
Problem: "did not find expected key"
Solution: Add colons after keys
# ❌ Wrong
name John Doe
age 30
# ✅ Correct
name: John Doe
age: 30
Unquoted Special Values
Problem: Values interpreted as wrong type
Solution: Quote values that look like other types
# ❌ Wrong
date: 2023-12-25
phone: 555-1234
status: active
# ✅ Correct
date: "2023-12-25"
phone: "555-1234"
status: "active"
Circular References
Problem: Anchors referencing each other
Solution: Avoid circular references in anchors
# ❌ Wrong
user1: &user1
name: John
friend: *user2
user2: &user2
name: Jane
friend: *user1
# ✅ Correct
user1:
name: John
friend: "Jane"
user2:
name: Jane
friend: "John"
Best Practices Summary
✅ Tool Usage Best Practices
- Always validate YAML files - Use linters and validators
- Use consistent formatting - Apply formatters automatically
- Integrate into CI/CD - Catch issues early
- Use schema validation - Ensure data integrity
- Monitor performance - Optimize large YAML files
- Document your tools - Help team members understand setup
⚠️ Common Tool Mistakes
- Not using validation tools - Relying on manual checking
- Ignoring linter warnings - Not fixing style issues
- Not versioning tool configs - Inconsistent tool settings
- Over-complicating schemas - Making validation too strict
- Not testing tool integration - Assuming tools work correctly