Skip to content

Instantly share code, notes, and snippets.

@mansam
Created July 3, 2014 17:44
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 mansam/f1e2c56a40d07379a95c to your computer and use it in GitHub Desktop.
Save mansam/f1e2c56a40d07379a95c to your computer and use it in GitHub Desktop.
def test_each_validator(self):
passes = {
"foo": [1, 2, 3, 4, 5, 6],
"bar": [{"qux": 1}, {"qux": 2}]
}
fails = {
"foo": [1, 2, 3, 4, 5, 11],
"bar": [{"qux": 3}, {"qux": 4, "zot": 5}]
}
validation = {
"foo": [Required, Each([Range(0, 10)])],
"bar": [Required, Each({
"qux": [Required, Range(0, 2)],
"zot": [In([1, 2, 3])]
})
]
}
valid, errors = validate(validation, passes)
assert valid
assert len(errors) == 0
valid, errors = validate(validation, fails)
assert not valid
assert len(errors) == 2
assert errors == {
"foo": ["all values must fall between 0 and 10"],
"bar": [{
0: {"qux": ["must fall between 0 and 2"]},
1: {
"qux": ["must fall between 0 and 2"],
"zot": ["must be one of [1, 2, 3]"]
}
}]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment