One of the benefits of XML is being able to validate a document against an XSD. YAML doesn't have this feature, so how can I validate that the YAML document I open is in the format expected by my application?
One of the benefits of XML is being able to validate a document against an XSD. YAML doesn't have this feature, so how can I validate that the YAML document I open is in the format expected by my application?
I find Cerberus to be very reliable with great documentation and straightforward to use.
Here is a basic implementation example:
my_yaml.yaml:
name: 'my_name'
date: 2017-10-01
metrics:
percentage:
value: 87
trend: stable
Defining the validation schema in schema.py:
{
'name': {
'required': True,
'type': 'string'
},
'date': {
'required': True,
'type': 'date'
},
'metrics': {
'required': True,
'type': 'dict',
'schema': {
'percentage': {
'required': True,
'type': 'dict',
'schema': {
'value': {
'required': True,
'type': 'number',
'min': 0,
'max': 100
},
'trend': {
'type': 'string',
'nullable': True,
'regex': '^(?i)(down|equal|up)$'
}
}
}
}
}
}
Using the PyYaml to load a yaml document:
import yaml
def load_doc():
with open('./my_yaml.yaml', 'r') as stream:
try:
return yaml.load(stream)
except yaml.YAMLError as exception:
raise exception
## Now, validating the yaml file is straightforward:
from cerberus import Validator
schema = eval(open('./schema.py', 'r').read())
v = Validator(schema)
doc = load_doc()
print(v.validate(doc, schema))
print(v.errors)
Keep in mind that Cerberus is an agnostic data validation tool, which means that it can support formats other than YAML, such as JSON, XML and so on.
Yes - having support for validation is vital for lots of important use cases. See e.g. YAML and the importance of Schema Validation « Stuart Gunter
As already mentioned, there is Rx, available for various languages, and Kwalify for Ruby and Java.
See also the PyYAML discussion: YAMLSchemaDiscussion.
A related effort is JSON Schema, which even had some IETF standardization activity: draft-zyp-json-schema-03 - A JSON Media Type for Describing the Structure and Meaning of JSON Documents
You can load YAML document as a dict and use library schema to check it:
from schema import Schema, And, Use, Optional, SchemaError
import yaml
schema = Schema(
{
'created': And(datetime.datetime),
'author': And(str),
'email': And(str),
'description': And(str),
Optional('tags'): And(str, lambda s: len(s) >= 0),
'setup': And(list),
'steps': And(list, lambda steps: all('=>' in s for s in steps), error='Steps should be array of string '
'and contain "=>" to separate'
'actions and expectations'),
'teardown': And(list)
}
)
with open(filepath) as f:
data = yaml.load(f)
try:
schema.validate(data)
except SchemaError as e:
print(e)
Pydantic has not been mentioned.
From their example:
from datetime import datetime
from typing import List, Optional
from pydantic import BaseModel
class User(BaseModel):
id: int
name = 'John Doe'
signup_ts: Optional[datetime] = None
friends: List[int] = []
# Parse your YAML into a dictionary, then validate against your model.
external_data = {
'id': '123',
'signup_ts': '2019-06-01 12:22',
'friends': [1, 2, '3'],
}
user = User(**external_data)
I wrapped some existing json-related python libraries aiming for being able to use them with yaml as well.
The resulting python library mainly wraps ...
jsonschema - a validator for json files against json-schema files, being wrapped to support validating yaml files against json-schema files in yaml-format as well.
jsonpath-ng - an implementation of JSONPath for python, being wrapped to support JSONPath selection directly on yaml files.
... and is available on github:
https://github.com/yaccob/ytools
It can be installed using pip:
pip install ytools
Validation example (from https://github.com/yaccob/ytools#validation):
import ytools
ytools.validate("test/sampleschema.yaml", ["test/sampledata.yaml"])
What you don't get out of the box yet, is validating against external schemas that are in yaml format as well.
ytools is not providing anything that hasn't existed before - it just makes the application of some existing solutions more flexible and more convenient.