本文整理汇总了Python中notario.validate函数的典型用法代码示例。如果您正苦于以下问题:Python validate函数的具体用法?Python validate怎么用?Python validate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了validate函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_nested_raises_nested_invalid
def test_nested_raises_nested_invalid(self):
data = {'a': [{'a': ['a']}, {'b': 'c'}]}
nested_schema = iterables.MultiIterable(('a', ['b']), ('b', 'c'))
schema = ('a', iterables.AllItems(nested_schema))
with raises(Invalid) as exc:
validate(data, schema)
assert exc.value.reason == 'expected a list but got dict'
示例2: test_all_items_fail
def test_all_items_fail(self):
def fail(value): raise AssertionError
data = {'a': 'some string'}
schema = ('a', chainable.AnyIn(types.boolean, fail))
with raises(Invalid) as exc:
validate(data, schema)
assert exc.value.args[0] == '-> a did not pass validation against callable: AnyIn'
示例3: test_one_item_fails
def test_one_item_fails(self):
data = {'a': 'some string'}
schema = ('a', chainable.AllIn(types.string, types.boolean))
with raises(Invalid) as exc:
validate(data, schema)
error = exc.value.args[0]
assert '-> a -> some string did not pass validation against callable: AllIn -> boolean' in error
示例4: test_all_objects_fail_non_callable
def test_all_objects_fail_non_callable(self):
data = {'a': {'a': 1, 'b': 1, 'c': 1}}
schema = ('a', recursive.AllObjects((types.string, 2)))
with raises(Invalid) as exc:
validate(data, schema)
error = exc.value.args[0]
assert '-> a -> a -> 1 did not match 2' in error
示例5: test_all_items_fail_length
def test_all_items_fail_length(self):
data = {"a": [{"a": 2}, {"b": {"a": "b"}}]}
schema = ("a", iterables.AllItems((types.string, 2)))
with raises(SchemaError) as exc:
validate(data, schema)
error = exc.value.args[0]
assert "-> a -> b has less items in schema than in data" in error
示例6: wrapped
def wrapped(*args, **kwargs):
request.validation_error = None
if request.method in ('POST', 'PUT'):
try:
body = request.body.decode()
if not body:
raise ValueError('No JSON object could be decoded')
data = json.loads(body)
notario.validate(data, schema)
except (Invalid, ValueError) as error:
# TODO: It would be nice if we could have a sane default
# here where we set the response body instead of requiring
# a handler
request.validation_error = error
if handler:
redirect_to_handler(error, handler)
# a controller can say `handler=False` to signal they don't
# want to delegate, not even to the fallback
if handler is None:
headers = {'Content-Type': 'application/json'}
raise JSONValidationException(
detail=error,
headers=headers
)
return f(*args, **kwargs)
示例7: test_multi_pair_non_nested_third_key
def test_multi_pair_non_nested_third_key(self):
data = {'a': 'a', 'b':'b', 'c':'c', 'd':'d'}
schema = (('a', 'a'), ('b', 'b'), ('f', 'c'), ('d', 'a'))
with raises(Invalid) as exc:
validate(data, schema)
assert exc.value.args[0] == "-> c key did not match 'f'"
示例8: test_multi_pair_non_nested_second
def test_multi_pair_non_nested_second(self):
data = {'a': 'a', 'b':'b', 'c':'c', 'd':'d'}
schema = (('a', 'a'), ('b', 'a'), ('c', 'c'), ('d', 'd'))
with raises(Invalid) as exc:
validate(data, schema)
assert exc.value.args[0] == "-> b -> b did not match 'a'"
示例9: test_all_objects_fail
def test_all_objects_fail(self):
data = {'a': {'a': 1, 'b': 'a string', 'c': 3}}
schema = ('a', recursive.AllObjects((types.string, types.integer)))
with raises(Invalid) as exc:
validate(data, schema)
error = exc.value.args[0]
assert '-> a -> b -> a string did not pass validation against callable: integer' in error
assert 'not of type int' in error
示例10: test_any_item_with_dictionaries
def test_any_item_with_dictionaries(self):
data = {'a': [{'a': 1}, {'b': 2}]}
schema = ('a', iterables.AnyItem(('c', 4)))
with raises(Invalid) as exc:
validate(data, schema)
error = exc.value.args[0]
assert "-> a -> list[] did not contain any valid items matching ('c', 4)" in error
示例11: test_any_items_fail
def test_any_items_fail(self):
data = {'a': [1, 2, 3, 4, 5]}
schema = ('a', iterables.AnyItem(types.string))
with raises(Invalid) as exc:
validate(data, schema)
error = exc.value.args[0]
assert '-> a -> list[] did not contain any valid items against callable: string' in error
示例12: test_all_objects_fail_length
def test_all_objects_fail_length(self):
data = {"a": {"a": 2, "b": {"a": "b"}}}
schema = ("a", recursive.AllObjects((types.string, 2)))
with raises(SchemaError) as exc:
validate(data, schema)
error = exc.value.args[0]
assert "-> a -> b has less items in schema than in data" in error
示例13: test_all_items_fail_non_callable
def test_all_items_fail_non_callable(self):
data = {'a': [1, 2, '3', 4, 5]}
schema = ('a', iterables.AllItems('foo'))
with raises(Invalid) as exc:
validate(data, schema)
error = exc.value.args[0]
assert "-> a -> list[0] item did not match 'foo'" in error
示例14: test_any_objects_fail_non_callable
def test_any_objects_fail_non_callable(self):
data = {"a": {"a": 1, "b": 4, "c": 3}}
schema = ("a", recursive.AnyObject(("a", "a")))
with raises(Invalid) as exc:
validate(data, schema)
error = exc.value.args[0]
assert "-> a did not contain any valid objects against callable: AnyObject" in error
示例15: test_any_items_fail_non_callable
def test_any_items_fail_non_callable(self):
data = {'a': [1, 2, 3, 4, 5]}
schema = ('a', iterables.AnyItem('foo'))
with raises(Invalid) as exc:
validate(data, schema)
error = exc.value.args[0]
assert "-> a -> list[] did not contain any valid items matching 'foo'" in error