Yamale

6.1.0 · active · verified Thu Apr 09

Yamale (ya·ma·lē) is a schema and validator for YAML. It allows for defining schemas in YAML files and validating other YAML data against them, supporting a wide range of data types and custom validators. The current version is 6.1.0, and it generally follows a regular release cadence with several minor and major releases each year.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a simple YAML schema and then validate a YAML data file against it using Yamale's API. It creates temporary schema and data files, performs validation, and prints the result.

import yamale
import os

# Create dummy schema.yaml and data.yaml files for demonstration
# In a real scenario, these would be pre-existing files.
with open('schema.yaml', 'w') as f:
    f.write('name: str()\nage: int(max=200)\nawesome: bool()')

with open('data.yaml', 'w') as f:
    f.write('name: Bill\nage: 26\nawesome: True')

try:
    # Import Yamale and make a schema object:
    schema = yamale.make_schema('./schema.yaml')

    # Create a Data object
    data = yamale.make_data('./data.yaml')

    # Validate data against the schema. Throws a ValueError if data is invalid.
    yamale.validate(schema, data)
    print('Validation success! Data is valid against the schema.')

except ValueError as e:
    print(f'Validation failed!\n{e}')
except FileNotFoundError as e:
    print(f'Error: {e}. Ensure schema.yaml and data.yaml exist.')
finally:
    # Clean up dummy files
    if os.path.exists('schema.yaml'):
        os.remove('schema.yaml')
    if os.path.exists('data.yaml'):
        os.remove('data.yaml')

view raw JSON →