本文整理汇总了Python中jsonschema.exceptions.ValidationError方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.ValidationError方法的具体用法?Python exceptions.ValidationError怎么用?Python exceptions.ValidationError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jsonschema.exceptions
的用法示例。
在下文中一共展示了exceptions.ValidationError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _test_validate
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import ValidationError [as 别名]
def _test_validate(self, schema, expect_failure, input_files, input):
"""validates input yaml against schema.
:param schema: schema yaml file
:param expect_failure: should the validation pass or fail.
:param input_files: pytest fixture used to access the test input files
:param input: test input yaml doc filename"""
schema_dir = pkg_resources.resource_filename('drydock_provisioner',
'schemas')
schema_filename = os.path.join(schema_dir, schema)
schema_file = open(schema_filename, 'r')
schema = yaml.safe_load(schema_file)
input_file = input_files.join(input)
instance_file = open(str(input_file), 'r')
instance = yaml.safe_load(instance_file)
if expect_failure:
with pytest.raises(ValidationError):
jsonschema.validate(instance['spec'], schema['data'])
else:
jsonschema.validate(instance['spec'], schema['data'])
示例2: additionalItems
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import ValidationError [as 别名]
def additionalItems(validator, aI, instance, schema):
if (
not validator.is_type(instance, "array") or
validator.is_type(schema.get("items", {}), "object")
):
return
len_items = len(schema.get("items", []))
if validator.is_type(aI, "object"):
for index, item in enumerate(instance[len_items:], start=len_items):
for error in validator.descend(item, aI, path=index):
yield error
elif not aI and len(instance) > len(schema.get("items", [])):
error = "Additional items are not allowed (%s %s unexpected)"
yield ValidationError(
error %
_utils.extras_msg(instance[len(schema.get("items", [])):])
)
示例3: dependencies
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import ValidationError [as 别名]
def dependencies(validator, dependencies, instance, schema):
if not validator.is_type(instance, "object"):
return
for property, dependency in iteritems(dependencies):
if property not in instance:
continue
if validator.is_type(dependency, "object"):
for error in validator.descend(
instance, dependency, schema_path=property,
):
yield error
else:
dependencies = _utils.ensure_list(dependency)
for dependency in dependencies:
if dependency not in instance:
yield ValidationError(
"%r is a dependency of %r" % (dependency, property)
)
示例4: type_draft3
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import ValidationError [as 别名]
def type_draft3(validator, types, instance, schema):
types = _utils.ensure_list(types)
all_errors = []
for index, type in enumerate(types):
if type == "any":
return
if validator.is_type(type, "object"):
errors = list(validator.descend(instance, type, schema_path=index))
if not errors:
return
all_errors.extend(errors)
else:
if validator.is_type(instance, type):
return
else:
yield ValidationError(
_utils.types_msg(instance, types), context=all_errors,
)
示例5: properties_draft3
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import ValidationError [as 别名]
def properties_draft3(validator, properties, instance, schema):
if not validator.is_type(instance, "object"):
return
for property, subschema in iteritems(properties):
if property in instance:
for error in validator.descend(
instance[property],
subschema,
path=property,
schema_path=property,
):
yield error
elif subschema.get("required", False):
error = ValidationError("%r is a required property" % property)
error._set(
validator="required",
validator_value=subschema["required"],
instance=instance,
schema=schema,
)
error.path.appendleft(property)
error.schema_path.extend([property, "required"])
yield error
示例6: oneOf_draft4
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import ValidationError [as 别名]
def oneOf_draft4(validator, oneOf, instance, schema):
subschemas = enumerate(oneOf)
all_errors = []
for index, subschema in subschemas:
errs = list(validator.descend(instance, subschema, schema_path=index))
if not errs:
first_valid = subschema
break
all_errors.extend(errs)
else:
yield ValidationError(
"%r is not valid under any of the given schemas" % (instance,),
context=all_errors,
)
more_valid = [s for i, s in subschemas if validator.is_valid(instance, s)]
if more_valid:
more_valid.append(first_valid)
reprs = ", ".join(repr(schema) for schema in more_valid)
yield ValidationError(
"%r is valid under each of %s" % (instance, reprs)
)
示例7: test_unset_error
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import ValidationError [as 别名]
def test_unset_error(self):
error = exceptions.ValidationError("message")
self.assertEqual(str(error), "message")
kwargs = {
"validator": "type",
"validator_value": "string",
"instance": 5,
"schema": {"type": "string"},
}
# Just the message should show if any of the attributes are unset
for attr in kwargs:
k = dict(kwargs)
del k[attr]
error = exceptions.ValidationError("message", **k)
self.assertEqual(str(error), "message")
示例8: test_str_works_with_instances_having_overriden_eq_operator
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import ValidationError [as 别名]
def test_str_works_with_instances_having_overriden_eq_operator(self):
"""
Check for https://github.com/Julian/jsonschema/issues/164 which
rendered exceptions unusable when a `ValidationError` involved
instances with an `__eq__` method that returned truthy values.
"""
instance = mock.MagicMock()
error = exceptions.ValidationError(
"a message",
validator="foo",
instance=instance,
validator_value="some",
schema="schema",
)
str(error)
self.assertFalse(instance.__eq__.called)
示例9: validate_image_detections
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import ValidationError [as 别名]
def validate_image_detections(image_detections):
for i, image_detection in enumerate(image_detections):
try:
validate_schema(image_detection, IMAGE_DETECTION_SCHEMA)
except SchemaError as se:
raise Exception(f"at index {i}") from se
image = image_detection['image']
for detection in image_detection['detections']:
if detection['right'] >= image['width'] or detection['bottom'] >= image['height']:
raise ValueError(f"Image {image} has out of bounds bounding box {detection}")
if detection['right'] <= detection['left'] or detection['bottom'] <= detection['top']:
raise ValueError(f"Image {image} has zero dimension bbox {detection}")
示例10: additionalProperties
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import ValidationError [as 别名]
def additionalProperties(validator, aP, instance, schema):
if not validator.is_type(instance, "object"):
return
extras = set(_utils.find_additional_properties(instance, schema))
if validator.is_type(aP, "object"):
for extra in extras:
for error in validator.descend(instance[extra], aP, path=extra):
yield error
elif not aP and extras:
if "patternProperties" in schema:
patterns = sorted(schema["patternProperties"])
if len(extras) == 1:
verb = "does"
else:
verb = "do"
error = "%s %s not match any of the regexes: %s" % (
", ".join(map(repr, sorted(extras))),
verb,
", ".join(map(repr, patterns)),
)
yield ValidationError(error)
else:
error = "Additional properties are not allowed (%s %s unexpected)"
yield ValidationError(error % _utils.extras_msg(extras))
示例11: minimum
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import ValidationError [as 别名]
def minimum(validator, minimum, instance, schema):
if not validator.is_type(instance, "number"):
return
if schema.get("exclusiveMinimum", False):
failed = instance <= minimum
cmp = "less than or equal to"
else:
failed = instance < minimum
cmp = "less than"
if failed:
yield ValidationError(
"%r is %s the minimum of %r" % (instance, cmp, minimum)
)
示例12: maximum
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import ValidationError [as 别名]
def maximum(validator, maximum, instance, schema):
if not validator.is_type(instance, "number"):
return
if schema.get("exclusiveMaximum", False):
failed = instance >= maximum
cmp = "greater than or equal to"
else:
failed = instance > maximum
cmp = "greater than"
if failed:
yield ValidationError(
"%r is %s the maximum of %r" % (instance, cmp, maximum)
)
示例13: multipleOf
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import ValidationError [as 别名]
def multipleOf(validator, dB, instance, schema):
if not validator.is_type(instance, "number"):
return
if isinstance(dB, float):
quotient = instance / dB
failed = int(quotient) != quotient
else:
failed = instance % dB
if failed:
yield ValidationError("%r is not a multiple of %r" % (instance, dB))
示例14: maxItems
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import ValidationError [as 别名]
def maxItems(validator, mI, instance, schema):
if validator.is_type(instance, "array") and len(instance) > mI:
yield ValidationError("%r is too long" % (instance,))
示例15: uniqueItems
# 需要导入模块: from jsonschema import exceptions [as 别名]
# 或者: from jsonschema.exceptions import ValidationError [as 别名]
def uniqueItems(validator, uI, instance, schema):
if (
uI and
validator.is_type(instance, "array") and
not _utils.uniq(instance)
):
yield ValidationError("%r has non-unique elements" % (instance,))