Last active
August 29, 2015 13:58
-
-
Save joehakimrahme/9954097 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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