Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_errors_dict_interface():
"""
`validated.errors` should present a dict-like interface.
"""
validator = Object(properties={"example": Integer()})
value, error = validator.validate_or_error({"example": "abc"})
assert dict(error) == {"example": "Must be a number."}
validator = Object(properties={"example": Integer()})
value, error = validator.validate_or_error({"example": "abc"})
assert error["example"] == "Must be a number."
validator = Object(additional_properties=Object(additional_properties=Integer()))
value, error = validator.validate_or_error({"example": {"nested": "abc"}})
assert dict(error) == {"example": {"nested": "Must be a number."}}
validator = Integer()
value, error = validator.validate_or_error("abc")
assert error[""] == "Must be a number."
validator = Integer()
value, error = validator.validate_or_error("abc")
assert dict(error) == {"": "Must be a number."}
value, error = validator.validate_or_error({"example": 123})
assert value == {"example": 123}
validator = Object(required=["example"])
value, error = validator.validate_or_error({})
assert dict(error) == {"example": "This field is required."}
validator = Object(properties={"example": Integer()})
value, error = validator.validate_or_error({"example": "123"})
assert value == {"example": 123}
validator = Object(properties={"example": Integer()})
value, error = validator.validate_or_error({"example": "abc"})
assert dict(error) == {"example": "Must be a number."}
validator = Object(pattern_properties={"^x-.*$": Integer()})
value, error = validator.validate_or_error({"x-example": "123"})
assert value == {"x-example": 123}
validator = Object(pattern_properties={"^x-.*$": Integer()})
value, error = validator.validate_or_error({"x-example": "abc"})
assert dict(error) == {"x-example": "Must be a number."}
validator = Object(properties={"example": Integer(default=0)})
value, error = validator.validate_or_error({"example": "123"})
assert value == {"example": 123}
validator = Object(properties={"example": Integer(default=0)})
value, error = validator.validate_or_error({})
assert value == {"example": 0}
validator = Object(additional_properties=False)
value, error = validator.validate_or_error({})
assert dict(error) == {"": "Must have at least 2 properties."}
validator = Object(min_properties=2)
value, error = validator.validate_or_error({"a": 1, "b": 2})
assert value == {"a": 1, "b": 2}
validator = Object(max_properties=2)
value, error = validator.validate_or_error({})
assert value == {}
validator = Object(max_properties=2)
value, error = validator.validate_or_error({"a": 1, "b": 2, "c": 3})
assert dict(error) == {"": "Must have no more than 2 properties."}
validator = Object(required=["example"])
value, error = validator.validate_or_error({"example": 123})
assert value == {"example": 123}
validator = Object(required=["example"])
value, error = validator.validate_or_error({})
assert dict(error) == {"example": "This field is required."}
validator = Object(properties={"example": Integer()})
value, error = validator.validate_or_error({"example": "123"})
assert value == {"example": 123}
validator = Object(properties={"example": Integer()})
value, error = validator.validate_or_error({"example": "abc"})
assert dict(error) == {"example": "Must be a number."}
validator = Object(pattern_properties={"^x-.*$": Integer()})
value, error = validator.validate_or_error(None)
assert dict(error) == {"": "May not be null."}
validator = Object()
value, error = validator.validate_or_error(123)
assert dict(error) == {"": "Must be an object."}
validator = Object()
value, error = validator.validate_or_error({1: 123})
assert dict(error) == {1: "All object keys must be strings."}
validator = Object(property_names=String(min_length=3))
value, error = validator.validate_or_error({"a": 123})
assert dict(error) == {"a": "Invalid property name."}
validator = Object(allow_null=True)
value, error = validator.validate_or_error(None)
assert value is None
assert error is None
validator = Object(min_properties=1)
value, error = validator.validate_or_error({})
assert dict(error) == {"": "Must not be empty."}
validator = Object(min_properties=1)
value, error = validator.validate_or_error({"a": 1})
assert value == {"a": 1}
validator = Object(min_properties=2)
value, error = validator.validate_or_error({})
assert dict(error) == {"": "Must have at least 2 properties."}
def test_errors_dict_interface():
"""
`validated.errors` should present a dict-like interface.
"""
validator = Object(properties={"example": Integer()})
value, error = validator.validate_or_error({"example": "abc"})
assert dict(error) == {"example": "Must be a number."}
validator = Object(properties={"example": Integer()})
value, error = validator.validate_or_error({"example": "abc"})
assert error["example"] == "Must be a number."
validator = Object(additional_properties=Object(additional_properties=Integer()))
value, error = validator.validate_or_error({"example": {"nested": "abc"}})
assert dict(error) == {"example": {"nested": "Must be a number."}}
validator = Integer()
value, error = validator.validate_or_error("abc")
assert error[""] == "Must be a number."
validator = Integer()
value, error = validator.validate_or_error("abc")
assert dict(error) == {"": "Must be a number."}
assert dict(error) == {1: "All object keys must be strings."}
validator = Object(property_names=String(min_length=3))
value, error = validator.validate_or_error({"a": 123})
assert dict(error) == {"a": "Invalid property name."}
validator = Object(allow_null=True)
value, error = validator.validate_or_error(None)
assert value is None
assert error is None
validator = Object(min_properties=1)
value, error = validator.validate_or_error({})
assert dict(error) == {"": "Must not be empty."}
validator = Object(min_properties=1)
value, error = validator.validate_or_error({"a": 1})
assert value == {"a": 1}
validator = Object(min_properties=2)
value, error = validator.validate_or_error({})
assert dict(error) == {"": "Must have at least 2 properties."}
validator = Object(min_properties=2)
value, error = validator.validate_or_error({"a": 1, "b": 2})
assert value == {"a": 1, "b": 2}
validator = Object(max_properties=2)
value, error = validator.validate_or_error({})
assert value == {}
validator = Object(max_properties=2)
"minimum",
"multipleOf",
"pattern",
"patternProperties",
"properties",
"propertyNames",
"required",
"type",
"uniqueItems",
}
definitions = SchemaDefinitions()
JSONSchema = (
Object(
properties={
"$ref": String(),
"type": String() | Array(items=String()),
"enum": Array(unique_items=True, min_items=1),
"definitions": Object(
additional_properties=Reference("JSONSchema", definitions=definitions)
),
# String
"minLength": Integer(minimum=0),
"maxLength": Integer(minimum=0),
"pattern": String(format="regex"),
"format": String(),
# Numeric
"minimum": Number(),
"maximum": Number(),
"exclusiveMinimum": Number(),
property_names_argument = from_json_schema(
property_names, definitions=definitions
)
kwargs = {
"allow_null": allow_null,
"properties": properties_argument,
"pattern_properties": pattern_properties_argument,
"additional_properties": additional_properties_argument,
"property_names": property_names_argument,
"min_properties": data.get("minProperties", None),
"max_properties": data.get("maxProperties", None),
"required": data.get("required", None),
"default": data.get("default", NO_DEFAULT),
}
return Object(**kwargs)
assert False, f"Invalid argument type_string={type_string!r}" # pragma: no cover