本文整理汇总了Python中fastjsonschema.JsonSchemaException方法的典型用法代码示例。如果您正苦于以下问题:Python fastjsonschema.JsonSchemaException方法的具体用法?Python fastjsonschema.JsonSchemaException怎么用?Python fastjsonschema.JsonSchemaException使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fastjsonschema
的用法示例。
在下文中一共展示了fastjsonschema.JsonSchemaException方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fast_validate
# 需要导入模块: import fastjsonschema [as 别名]
# 或者: from fastjsonschema import JsonSchemaException [as 别名]
def fast_validate(
schema: RawSchema, raw_items: RawItems, keys: pd.Index
) -> Dict[str, set]:
"""Verify items one by one. It stops after the first error in an item in most cases.
Faster than jsonschema validation
Args:
schema: a JSON schema
raw_items: a raw data to validate one by one
keys: keys corresponding to raw_items index
Returns:
A dictionary of errors with message and item keys
"""
errors: DefaultDict = defaultdict(set)
validate = fastjsonschema.compile(schema)
for i, raw_item in enumerate(tqdm(raw_items, desc="Fast Schema Validation")):
raw_item.pop("_type", None)
raw_item.pop("_key", None)
try:
validate(raw_item)
except fastjsonschema.JsonSchemaException as error:
errors[str(error)].add(keys[i])
return dict(errors)
示例2: asserter
# 需要导入模块: import fastjsonschema [as 别名]
# 或者: from fastjsonschema import JsonSchemaException [as 别名]
def asserter():
def f(definition, value, expected, formats={}):
# When test fails, it will show up code.
code_generator = CodeGeneratorDraft07(definition, formats=formats)
print(code_generator.func_code)
pprint(code_generator.global_state)
# By default old tests are written for draft-04.
definition.setdefault('$schema', 'http://json-schema.org/draft-04/schema')
validator = compile(definition, formats=formats)
if isinstance(expected, JsonSchemaException):
with pytest.raises(JsonSchemaException) as exc:
validator(value)
assert exc.value.message == expected.message
assert exc.value.value == (value if expected.value == '{data}' else expected.value)
assert exc.value.name == expected.name
assert exc.value.definition == (definition if expected.definition == '{definition}' else expected.definition)
assert exc.value.rule == expected.rule
else:
assert validator(value) == expected
return f
示例3: _validate
# 需要导入模块: import fastjsonschema [as 别名]
# 或者: from fastjsonschema import JsonSchemaException [as 别名]
def _validate(req_schema: Dict = None, resp_schema: Dict = None):
def decorator(func):
@wraps(func)
def wrapper(self, req, resp, *args, **kwargs):
if req_schema is not None:
try:
schema = fastjsonschema.compile(req_schema)
schema(req.media)
except fastjsonschema.JsonSchemaException as e:
msg = "Request data failed validation: {}".format(e.message)
raise HTTPError(falcon.HTTP_BAD_REQUEST, msg)
result = func(self, req, resp, *args, **kwargs)
if resp_schema is not None:
try:
schema = fastjsonschema.compile(resp_schema)
schema(resp.media)
except fastjsonschema.JsonSchemaException:
raise HTTPError(
falcon.HTTP_INTERNAL_SERVER_ERROR,
"Response data failed validation",
)
# Do not return 'e.message' in the response to
# prevent info about possible internal response
# formatting bugs from leaking out to users.
return result
return wrapper
return decorator
示例4: test_sane_json_invalid_not_list
# 需要导入模块: import fastjsonschema [as 别名]
# 或者: from fastjsonschema import JsonSchemaException [as 别名]
def test_sane_json_invalid_not_list():
with pytest.raises(JsonSchemaException) as e:
SaneJson(get_mock('invalid/bad_sane_json_1.json'))
assert 'data must be array' in str(e.value)
示例5: test_sane_json_invalid_no_layout
# 需要导入模块: import fastjsonschema [as 别名]
# 或者: from fastjsonschema import JsonSchemaException [as 别名]
def test_sane_json_invalid_no_layout():
with pytest.raises(JsonSchemaException) as e:
SaneJson(get_mock('invalid/bad_sane_json_2.json'))
assert "data[0] must contain ['type', 'data', 'layout'] properties" in str(
e.value)
示例6: test_sane_json_invalid_no_col_key
# 需要导入模块: import fastjsonschema [as 别名]
# 或者: from fastjsonschema import JsonSchemaException [as 别名]
def test_sane_json_invalid_no_col_key():
with pytest.raises(JsonSchemaException) as e:
SaneJson(get_mock('invalid/bad_sane_json_3.json'))
assert "data[0].layout must contain ['rowPos', 'columnPos', 'h', 'w']" + \
" properties" in str(e.value)
示例7: test_sane_json_invalid_no_row_key
# 需要导入模块: import fastjsonschema [as 别名]
# 或者: from fastjsonschema import JsonSchemaException [as 别名]
def test_sane_json_invalid_no_row_key():
with pytest.raises(JsonSchemaException) as e:
SaneJson(get_mock('invalid/bad_sane_json_4.json'))
assert "data[0].layout must contain ['rowPos', 'columnPos', 'h', 'w']" + \
" properties" in str(e.value)
示例8: test_sane_json_invalid_no_width_key
# 需要导入模块: import fastjsonschema [as 别名]
# 或者: from fastjsonschema import JsonSchemaException [as 别名]
def test_sane_json_invalid_no_width_key():
with pytest.raises(JsonSchemaException) as e:
SaneJson(get_mock('invalid/bad_sane_json_5.json'))
assert "data[0].layout must contain ['rowPos', 'columnPos', 'h', 'w']" + \
" properties" in str(e.value)
示例9: test_sane_json_invalid_2ndpage_no_height_key
# 需要导入模块: import fastjsonschema [as 别名]
# 或者: from fastjsonschema import JsonSchemaException [as 别名]
def test_sane_json_invalid_2ndpage_no_height_key():
with pytest.raises(JsonSchemaException) as e:
SaneJson(get_mock('invalid/bad_sane_json_7.json'))
assert "data[1].layout must contain ['rowPos', 'columnPos', 'h', 'w']" + \
" properties" in str(e.value)
示例10: test_sane_json_invalid_layout_keys
# 需要导入模块: import fastjsonschema [as 别名]
# 或者: from fastjsonschema import JsonSchemaException [as 别名]
def test_sane_json_invalid_layout_keys():
with pytest.raises(JsonSchemaException) as e:
SaneJson(get_mock('invalid/invalid_layout_keys.json'))
assert 'data[0].layout.h must be bigger than or equal to 1' in str(e.value)
示例11: template_test
# 需要导入模块: import fastjsonschema [as 别名]
# 或者: from fastjsonschema import JsonSchemaException [as 别名]
def template_test(schema_version, schema, data, is_valid):
"""
Test function to be used (imported) in final test file to run the tests
which are generated by `pytest_generate_tests` hook.
"""
# For debug purposes. When test fails, it will print stdout.
resolver = RefResolver.from_schema(schema, handlers={'http': remotes_handler})
debug_generator = _get_code_generator_class(schema_version)(schema, resolver=resolver)
print(debug_generator.global_state_code)
print(debug_generator.func_code)
# JSON schema test suits do not contain schema version.
# Our library needs to know that or it would use always the latest implementation.
if isinstance(schema, dict):
schema.setdefault('$schema', schema_version)
validate = compile(schema, handlers={'http': remotes_handler})
try:
result = validate(data)
print('Validate result:', result)
except JsonSchemaException:
if is_valid:
raise
else:
if not is_valid:
pytest.fail('Test should not pass')
示例12: test_benchmark_bad_values
# 需要导入模块: import fastjsonschema [as 别名]
# 或者: from fastjsonschema import JsonSchemaException [as 别名]
def test_benchmark_bad_values(benchmark, value):
@benchmark
def f():
try:
fastjsonschema_validate(value)
except fastjsonschema.JsonSchemaException:
pass
else:
pytest.fail('Exception is not raised')
示例13: test_exception_variable_path
# 需要导入模块: import fastjsonschema [as 别名]
# 或者: from fastjsonschema import JsonSchemaException [as 别名]
def test_exception_variable_path(value, expected):
exc = JsonSchemaException('msg', name=value)
assert exc.path == expected
示例14: test_exception_rule_definition
# 需要导入模块: import fastjsonschema [as 别名]
# 或者: from fastjsonschema import JsonSchemaException [as 别名]
def test_exception_rule_definition(definition, rule, expected_rule_definition):
exc = JsonSchemaException('msg', definition=definition, rule=rule)
assert exc.rule_definition == expected_rule_definition
示例15: test_integer_is_not_number
# 需要导入模块: import fastjsonschema [as 别名]
# 或者: from fastjsonschema import JsonSchemaException [as 别名]
def test_integer_is_not_number(asserter, value):
asserter({
'type': 'integer',
}, value, JsonSchemaException('data must be integer', value='{data}', name='data', definition='{definition}', rule='type'))