本文整理汇总了Python中jsonschema.exceptions.SchemaError方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.SchemaError方法的具体用法?Python exceptions.SchemaError怎么用?Python exceptions.SchemaError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jsonschema.exceptions
的用法示例。
在下文中一共展示了exceptions.SchemaError方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_draft3_schema_draft4_validator
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import SchemaError [as 别名]
def test_draft3_schema_draft4_validator(self):
stdout, stderr = StringIO(), StringIO()
with self.assertRaises(SchemaError):
cli.run(
{
"validator": Draft4Validator,
"schema": {
"anyOf": [
{"minimum": 20},
{"type": "string"},
{"required": True},
],
},
"instances": [1],
"error_format": "{error.message}",
},
stdout=stdout,
stderr=stderr,
)
示例2: validate_json
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import SchemaError [as 别名]
def validate_json(self, data):
try:
validator = jsonschema.Draft4Validator(self.schema)
validation_errors = [e for e in validator.iter_errors(data)]
# Iteratre over all errors and raise as single exception
if validation_errors:
exception_msgs = {}
for err in validation_errors:
if err.path:
field = '-'.join([str(e) for e in err.path])
elif err.schema_path:
field = '-'.join([str(e) for e in err.schema_path])
else:
field = 'error'
if field in exception_msgs:
exception_msgs[field].append(err.message)
else:
exception_msgs[field] = [err.message]
raise serializers.ValidationError(exception_msgs)
except (JSONSchemaValidationError, JSONSchemaError) as e:
raise serializers.ValidationError(e.message)
return self.to_internal_value(json.dumps(data))
示例3: _assert_schema
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import SchemaError [as 别名]
def _assert_schema(self, schema, reality):
try:
validate(reality, schema, format_checker=FormatChecker())
except SchemaError as e:
raise RuntimeError(e)
except ValidationError as e:
raise AssertionError(e)
示例4: test_raises_if_schema_is_invalid
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import SchemaError [as 别名]
def test_raises_if_schema_is_invalid(self):
with self.assertRaises(jse.SchemaError):
self.schema = """
{
"not": "valid schema",
"but": "Valid json"
}
"""
self.given_json({
'id': 1
}).validate()
# response_json_matches_defined_schema()
示例5: test_invalid_schema
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import SchemaError [as 别名]
def test_invalid_schema(self):
with pytest.raises(SchemaError):
class TestItem1(JsonSchemaItem):
jsonschema = invalid_schema
示例6: test_invalid_schema
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import SchemaError [as 别名]
def test_invalid_schema(self):
try:
class TestItem1(JsonSchemaItem):
jsonschema = invalid_schema
except SchemaError:
pass
else:
self.fail('SchemaError was not raised')
示例7: validation_errors
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import SchemaError [as 别名]
def validation_errors(schema):
"""
Given a dict, returns any known JSON Schema validation errors. If there are none,
implies that the dict is a valid JSON Schema.
:param schema: dict
:return: [String, ...]
"""
errors = []
if not isinstance(schema, dict):
errors.append('Parameter `schema` is not a dict, instead found: {}'.format(type(schema)))
try:
if not _valid_schema_version(schema):
errors.append('Schema version must be Draft 4. Found: {}'.format('$schema'))
except Exception as ex:
errors = _unexpected_validation_error(errors, ex)
try:
Draft4Validator.check_schema(schema)
except SchemaError as error:
errors.append(str(error))
except Exception as ex:
errors = _unexpected_validation_error(errors, ex)
try:
simplify(schema)
except JSONSchemaError as error:
errors.append(str(error))
except Exception as ex:
errors = _unexpected_validation_error(errors, ex)
return errors
示例8: validate_json
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import SchemaError [as 别名]
def validate_json(json_string, schema):
"""
invokes the validate function of jsonschema
"""
schema_dict = json.loads(schema)
schema_title = schema_dict['title']
try:
validate(json_string, schema_dict)
except ValidationError as err:
title = 'JSON validation failed: {}'.format(err.message)
description = 'Failed validator: {} : {}'.format(
err.validator,
err.validator_value
)
LOG.error(title)
LOG.error(description)
raise InvalidFormatError(
title=title,
description=description,
)
except SchemaError as err:
title = 'SchemaError: Unable to validate JSON: {}'.format(err)
description = 'Invalid Schema: {}'.format(schema_title)
LOG.error(title)
LOG.error(description)
raise AppError(
title=title,
description=description
)
except FormatError as err:
title = 'FormatError: Unable to validate JSON: {}'.format(err)
description = 'Invalid Format: {}'.format(schema_title)
LOG.error(title)
LOG.error(description)
raise AppError(
title=title,
description=description
)
# The action resource structure