CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-yamale

A schema and validator for YAML with comprehensive data type validation and constraint support.

Overview
Eval results
Files

cli.mddocs/

Command Line Interface

Command-line tools for validating YAML files in scripts and automated workflows. The Yamale CLI provides powerful file validation capabilities with support for batch processing, custom schemas, and flexible output options.

Capabilities

Basic Command Usage

The yamale command validates YAML files against schemas with automatic schema discovery.

yamale [options] [PATH ...]

Command Line Options

# Core options
-h, --help              # Show help message and exit
-s SCHEMA, --schema SCHEMA    # Schema filename (default: schema.yaml)
-V, --version          # Show program version and exit

# File processing
-e PATTERN, --exclude PATTERN # Python regex to exclude files (can be used multiple times)
-p PARSER, --parser PARSER    # YAML parser: "pyyaml" (default) or "ruamel"

# Validation modes  
-x, --no-strict        # Disable strict mode (allow unexpected elements)
-v, --verbose          # Show verbose output

# Performance
-n CPU_NUM, --cpu-num CPU_NUM # Number of processes for validation (default: 4, or 'auto')

Basic Usage Examples

Single File Validation

# Validate single file with default schema (schema.yaml)
yamale data.yaml

# Use custom schema file
yamale -s user-schema.yaml user-data.yaml

# Validate with verbose output
yamale -v config.yaml

Directory Validation

# Validate all YAML files in current directory
yamale

# Validate all YAML files in specific directory  
yamale ./configs/

# Validate multiple directories
yamale ./api-specs/ ./configurations/ ./test-data/

Schema Discovery

Yamale automatically searches for schema files:

  1. Same directory as the data file
  2. Parent directories (recursive search up the tree)
  3. Uses first schema file found
  4. Reports error if no schema found
# Directory structure:
# project/
#   ├── schema.yaml          # Found for any file in project/
#   ├── config.yaml
#   └── data/
#       ├── local-schema.yaml # Found for files in data/
#       ├── users.yaml        # Uses data/local-schema.yaml
#       └── settings.yaml     # Uses data/local-schema.yaml

yamale project/config.yaml      # Uses project/schema.yaml
yamale project/data/users.yaml  # Uses project/data/local-schema.yaml

Advanced Usage

Pattern Exclusion

# Exclude test files
yamale -e "test.*\.yaml$" ./configs/

# Exclude multiple patterns
yamale -e "\.tmp$" -e "backup.*" -e "draft.*" ./data/

# Exclude sensitive files
yamale -e "secret" -e "private" -e "\.key$" ./

Parser Selection

# Use ruamel.yaml for YAML 1.2 support
yamale -p ruamel schema.yaml data.yaml

# Default PyYAML parser
yamale -p pyyaml schema.yaml data.yaml

Performance Tuning

# Use all available CPU cores
yamale -n auto ./large-dataset/

# Limit to 2 processes
yamale -n 2 ./configs/

# Single-threaded processing
yamale -n 1 ./data/

Strict Mode Control

# Allow extra fields in data (non-strict mode)
yamale -x config.yaml

# Default strict mode (reject extra fields)
yamale config.yaml

Practical Examples

CI/CD Integration

#!/bin/bash
# validate-configs.sh - CI validation script

echo "Validating configuration files..."

# Validate production configs with strict mode
if yamale -s prod-schema.yaml configs/prod/*.yaml; then
    echo "✓ Production configs valid"
else
    echo "✗ Production config validation failed"
    exit 1
fi

# Validate development configs (allow extra fields)
if yamale -x -s dev-schema.yaml configs/dev/*.yaml; then
    echo "✓ Development configs valid"  
else
    echo "✗ Development config validation failed"
    exit 1
fi

# Validate API specifications
if yamale -s api-schema.yaml -e "\.draft\.yaml$" api-specs/; then
    echo "✓ API specifications valid"
else
    echo "✗ API specification validation failed"
    exit 1
fi

echo "All validations passed!"

Bulk Validation Script

#!/bin/bash
# bulk-validate.sh - Validate multiple projects

PROJECTS=("project-a" "project-b" "project-c")
TOTAL=0
PASSED=0

for project in "${PROJECTS[@]}"; do
    echo "Validating $project..."
    TOTAL=$((TOTAL + 1))
    
    if yamale -s schemas/${project}-schema.yaml data/${project}/; then
        echo "✓ $project passed"
        PASSED=$((PASSED + 1))
    else
        echo "✗ $project failed"
    fi
done

echo "Results: $PASSED/$TOTAL projects passed validation"

if [ $PASSED -eq $TOTAL ]; then
    echo "All projects validated successfully!"
    exit 0
else
    echo "Some projects failed validation"
    exit 1
fi

Pre-commit Hook

#!/bin/bash
# .git/hooks/pre-commit

echo "Running YAML validation..."

# Get list of staged YAML files
YAML_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.ya\?ml$')

if [ -z "$YAML_FILES" ]; then
    echo "No YAML files to validate"
    exit 0
fi

# Validate each file
for file in $YAML_FILES; do
    echo "Validating $file..."
    if ! yamale -s .yamale-schema.yaml "$file"; then
        echo "❌ Validation failed for $file"
        echo "Please fix validation errors before committing"
        exit 1
    fi
done

echo "✅ All YAML files validated successfully"
exit 0

Development Workflow

# Development validation with verbose output
yamale -v -s dev-schema.yaml config-dev.yaml

# Quick validation during development (non-strict)
yamale -x config.yaml

# Validate before deployment (strict + performance)
yamale -n auto -s prod-schema.yaml configs/prod/

# Validate with specific exclusions
yamale -e "\.backup$" -e "\.tmp$" -s schema.yaml ./

Exit Codes

The yamale command returns standard exit codes:

  • 0: All validations passed successfully
  • 1: One or more validations failed
  • 2: Command line argument errors
  • 3: File not found or permission errors
# Check exit code in scripts
yamale config.yaml
if [ $? -eq 0 ]; then
    echo "Validation passed"
else
    echo "Validation failed"
fi

# Use in conditional statements
if yamale -s schema.yaml data.yaml; then
    deploy_application
else
    echo "Cannot deploy: validation failed"
    exit 1
fi

Output Format

Default Output

# Successful validation (no output)
$ yamale config.yaml
$ echo $?
0

# Failed validation
$ yamale invalid.yaml
Error validating data 'invalid.yaml' with 'schema.yaml'
    name: Required field missing
    age: '25.5' is not an integer
$ echo $?
1

Verbose Output

$ yamale -v config.yaml
Loading schema: schema.yaml
Loading data: config.yaml
Validating config.yaml against schema.yaml
Validation successful: config.yaml

Multiple File Output

$ yamale configs/
Error validating data 'configs/invalid.yaml' with 'configs/schema.yaml'
    database.port: '3306.5' is not an integer
    
Error validating data 'configs/incomplete.yaml' with 'configs/schema.yaml'  
    api.key: Required field missing
    
2 of 5 files failed validation

Integration Examples

Make Integration

# Makefile
.PHONY: validate-yaml validate-prod validate-dev

validate-yaml: validate-dev validate-prod

validate-dev:
	@echo "Validating development configs..."
	yamale -x -s schemas/dev-schema.yaml configs/dev/

validate-prod:
	@echo "Validating production configs..."
	yamale -s schemas/prod-schema.yaml configs/prod/

deploy: validate-prod
	@echo "Configs validated, deploying..."
	# deployment commands here

Docker Integration

# Dockerfile
FROM python:3.11-slim

# Install yamale
RUN pip install yamale

# Copy schemas and data
COPY schemas/ /app/schemas/
COPY data/ /app/data/

WORKDIR /app

# Validate on container start
CMD ["yamale", "-s", "schemas/app-schema.yaml", "data/"]

GitHub Actions

# .github/workflows/validate.yml
name: YAML Validation

on: [push, pull_request]

jobs:
  validate:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.11'
    
    - name: Install yamale
      run: pip install yamale
    
    - name: Validate YAML files
      run: |
        yamale -s .github/schemas/config-schema.yaml configs/
        yamale -s .github/schemas/api-schema.yaml api-specs/

Install with Tessl CLI

npx tessl i tessl/pypi-yamale

docs

cli.md

core-functions.md

exceptions.md

index.md

schema-management.md

validators.md

tile.json