A Python utility for validating user YAML configuration files against a default/template YAML file with support for optional fields
Project description
YAML Validator
A Python utility for validating user YAML configuration files against a default/template YAML file with support for optional fields.
Features
- Validates that required keys from a default YAML are present in a user YAML
- Supports marking keys as optional using
#optionalcomments - Type checking between user and default configurations
- Recursive validation for nested YAML structures
- Clear error messages indicating missing keys or type mismatches
Requirements
pip install -r requirements.txt
Usage
Command Line
./yaml_validator.py <user_config.yaml> <default_config.yaml>
Arguments
user_config.yaml: The YAML file to validatedefault_config.yaml: The template/default YAML file with annotations
Exit Codes
0: Validation successful1: Validation failed (missing required keys or type mismatches)
Importing in Python Projects
from yaml_validator import validate_config
# Validate a user config against a default template
is_valid = validate_config('user_config.yaml', 'default_config.yaml')
if is_valid:
print("Configuration is valid!")
else:
print("Configuration validation failed.")
# Handle the error appropriately
The validate_config function returns True if validation passes, False otherwise. Validation errors are printed to stdout.
Annotation Syntax
In your default YAML file, mark optional keys with #optional:
database:
host: localhost
port: 5432
username: admin
password: secret #optional
ssl_enabled: true #optional
Keys without #optional are treated as required.
Validation Rules
- Required Keys: Keys in the default file without
#optionalmust exist in the user file - Type Matching: When a key exists in both files, the value types must match
- Nested Validation: Nested dictionaries are validated recursively
- Optional Keys: Keys marked with
#optionalcan be omitted from the user file
Example
default.yaml:
app:
name: MyApp
version: 1.0
debug: false #optional
database:
host: localhost
port: 5432
user.yaml:
app:
name: MyApp
version: 1.0
database:
host: db.example.com
port: 5432
Running the validator:
./yaml_validator.py user.yaml default.yaml
# Exit code 0 - validation passes
How It Works
- Annotation Extraction (
extract_annotations): Parses the default YAML file to identify which keys are optional or required based on#optionalcomments - Recursive Validation (
validate_config_recursive): Traverses both YAML structures simultaneously, checking for missing keys and type mismatches - Error Reporting: Prints detailed messages for each validation failure with the full key path
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file yaml_validator-0.3.0.tar.gz.
File metadata
- Download URL: yaml_validator-0.3.0.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8cd22184903c0ecf972039bfe6d0b39af09e76fa1d2e4c0f8ce0f84675ec0ad8
|
|
| MD5 |
b42e706214f1948ddcc680f20d6dcceb
|
|
| BLAKE2b-256 |
ee74f869c33dcf340169886c23882526ca318523cc29084342ae55061868cdf5
|
File details
Details for the file yaml_validator-0.3.0-py3-none-any.whl.
File metadata
- Download URL: yaml_validator-0.3.0-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
162bf8ee3875bafcc564ff53cc5a5ba6af5c9d3ed7ea46242e36399c7e18066b
|
|
| MD5 |
8e9b703757e3c761921eeed9dfecfe6f
|
|
| BLAKE2b-256 |
bc873ed6041d383e92dd13f6d5c5f8394e12ce87ae4c6a8f8f064c59b6c48d53
|