Skip to content

Instantly share code, notes, and snippets.

@joehakimrahme
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joehakimrahme/9954097 to your computer and use it in GitHub Desktop.
Save joehakimrahme/9954097 to your computer and use it in GitHub Desktop.
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