Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import voluptuous
import yaml
schema = voluptuous.Schema({
voluptuous.Required("name"): str,
"sex": voluptuous.Any("Male", "Female", "N/A"),
"class": str,
"title": str,
"hp": [int],
"sp": [int],
"gold": int,
"inventory": [str],
})
instance = yaml.load("""
name: Vorlin Laruknuzum
sex: Male
class: Priest
title: Acolyte
hp: [32, 71]
sp: [1, 13]
gold: 423
inventory:
- a Holy Book of Prayers (Words of Wisdom)
- an Azure Potion of Cure Light Wounds
- a Silver Wand of Wonder
""")
# calling the validation function
# this function will return instance if it's valid
# and will raise a MulitpleInvalid exception otherwise
assert schema(instance) == instance
# more info at https://github.com/alecthomas/voluptuous
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment