# curl "http://127.0.0.1:6000/wrap" -d "a=123&b=123&c=spam&d=123&e=1236&f=123&g=spa1&h=11%&i=12&j=bar&k=32&l=abc&m=123"
{
"code": 200,
"data": {
"a": "123",
"b": "123",
"c": "spam",
"d": "123",
"e": "1236",
"f": "123",
"g": "spa1",
"h": "11%",
"i": "12",
"j": "bar",
"k": "32",
"l": "abc",
"m": "123"
},
"err": null
}
# curl "http://127.0.0.1:5000/wrap" -d "a=123&b=123&c=spam&d=123&e=1236&f=123&g=spa1&h=11%&i=12&j=bar&k=32&l=abc&m=123 "
{"code":500,"data":null,"err":"m should not contain spaces"}
# curl "http://127.0.0.1:5000/wrap" -d "a=123&b=123&c=spam&d=13&e=1236&f=123&g=spa1&h=11%&i=12&j=bar&k=32&l=abc&m=123"
{"code":500,"data":null,"err":{"d":["must not fall between 1 and 100"]}}
# curl "http://127.0.0.1:5000/wrap" -d "a=1234&b=&c=nospam&d=13&e=123456&f=123&g=spa1&h=11%&i=12&j=bar&k=3a2&l=1abc&m=123a"
{"code":500,"data":null,"err":{"a":["must be equal to '123'"],"b":["must be True-equivalent value"],"c":["must be one of ['spam', 'eggs', 'bacon']"],"d":["must not fall between 1 and 100"],"e":["must be at most 5 elements in length"],"l":["must be all letters"],"m":["must be all numbers"]}}
from validator import Required, Not, Truthy, Blank, Range, Equals, In, validate
# let's say that my dictionary needs to meet the following rules...
rules = {
"foo": [Required, Equals(123)], # foo must be exactly equal to 123
"bar": [Required, Truthy()], # bar must be equivalent to True
"baz": [In(["spam", "eggs", "bacon"])], # baz must be one of these options
"qux": [Not(Range(1, 100))] # qux must not be a number between 1 and 100 inclusive
}
# then this following dict would pass:
passes = {
"foo": 123,
"bar": True, # or a non-empty string, or a non-zero int, etc...
"baz": "spam",
"qux": 101
}
>>> validate(rules, passes)
(True, {})
# but this one would fail
fails = {
"foo": 321,
"bar": False, # or 0, or [], or an empty string, etc...
"baz": "barf",
"qux": 99
}
>>> validate(rules, fails)
(False, {
'foo': ["must be equal to 123"],
'bar': ['must be True-equivalent value'],
'baz': ["must be one of ['spam', 'eggs', 'bacon']"],
'qux': ['must not fall between 1 and 100']
})