Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
value, error = validator.validate_or_error(["1", 2, "3"])
assert value == [1, 2, 3]
validator = Array(items=Integer())
value, error = validator.validate_or_error(["a", 2, "c"])
assert dict(error) == {0: "Must be a number.", 2: "Must be a number."}
validator = Array(items=[String(), Integer()])
value, error = validator.validate_or_error(["a", "b", "c"])
assert error == ValidationError(text="Must have 2 items.", code="exact_items")
validator = Array(items=[String(), Integer()])
value, error = validator.validate_or_error(["a", "123"])
assert value == ["a", 123]
validator = Array(items=[String(), Integer()], additional_items=Integer())
value, error = validator.validate_or_error(["a", "123", "456"])
assert value == ["a", 123, 456]
validator = Array(items=String(), unique_items=True)
value, error = validator.validate_or_error(["a", "b"])
assert value == ["a", "b"]
validator = Array(items=String(), unique_items=True)
value, error = validator.validate_or_error(["a", "a"])
assert dict(error) == {1: "Items must be unique."}
validator = Array(items=[String(), Integer(), Boolean()], min_items=1)
value, error = validator.validate_or_error(["a"])
assert value == ["a"]
validator = Array(items=[String(), Integer(), Boolean()], min_items=1)
assert error == ValidationError(text="May not be null.", code="null")
validator = Array()
value, error = validator.validate_or_error(123)
assert error == ValidationError(text="Must be an array.", code="type")
validator = Array(allow_null=True)
value, error = validator.validate_or_error(None)
assert value is None
assert error is None
validator = Array(min_items=1)
value, error = validator.validate_or_error([])
assert error == ValidationError(text="Must not be empty.", code="empty")
validator = Array(min_items=1)
value, error = validator.validate_or_error([1])
assert value == [1]
validator = Array(min_items=2)
value, error = validator.validate_or_error([])
assert error == ValidationError(
text="Must have at least 2 items.", code="min_items"
)
validator = Array(min_items=2)
value, error = validator.validate_or_error([1, 2])
assert value == [1, 2]
validator = Array(max_items=2)
value, error = validator.validate_or_error([])
assert value == []
value, error = validator.validate_or_error(["a", "123"])
assert value == ["a", 123]
validator = Array(items=[String(), Integer()], additional_items=Integer())
value, error = validator.validate_or_error(["a", "123", "456"])
assert value == ["a", 123, 456]
validator = Array(items=String(), unique_items=True)
value, error = validator.validate_or_error(["a", "b"])
assert value == ["a", "b"]
validator = Array(items=String(), unique_items=True)
value, error = validator.validate_or_error(["a", "a"])
assert dict(error) == {1: "Items must be unique."}
validator = Array(items=[String(), Integer(), Boolean()], min_items=1)
value, error = validator.validate_or_error(["a"])
assert value == ["a"]
validator = Array(items=[String(), Integer(), Boolean()], min_items=1)
value, error = validator.validate_or_error(["a", "123"])
assert value == ["a", 123]
validator = Array(items=[String(), Integer(), Boolean()], min_items=1)
value, error = validator.validate_or_error([])
assert error == ValidationError(text="Must not be empty.", code="empty")
validator = Array(items=[String(), Integer(), Boolean()], min_items=2)
value, error = validator.validate_or_error([])
assert error == ValidationError(
text="Must have at least 2 items.", code="min_items"
)
def test_array():
validator = Array()
value, error = validator.validate_or_error([])
assert value == []
validator = Array()
value, error = validator.validate_or_error(None)
assert error == ValidationError(text="May not be null.", code="null")
validator = Array()
value, error = validator.validate_or_error(123)
assert error == ValidationError(text="Must be an array.", code="type")
validator = Array(allow_null=True)
value, error = validator.validate_or_error(None)
assert value is None
assert error is None
validator = Array(min_items=1)
value, error = validator.validate_or_error([])
assert error == ValidationError(text="Must not be empty.", code="empty")
validator = Array(min_items=1)
value, error = validator.validate_or_error([1])
assert value == [1]
validator = Array(min_items=2)
value, error = validator.validate_or_error([])
assert error == ValidationError(
text="Must have at least 2 items.", code="min_items"
"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(),
"exclusiveMaximum": Number(),
"multipleOf": Number(exclusive_minimum=0),
# Object
"properties": Object(
additional_properties=Reference("JSONSchema", definitions=definitions)
),
"minProperties": Integer(minimum=0),
"maxProperties": Integer(minimum=0),
"patternProperties": Object(
additional_properties=Reference("JSONSchema", definitions=definitions)
),
"additionalProperties": (
Reference("JSONSchema", definitions=definitions) | Boolean()
),
"required": Array(items=String(), unique_items=True),
# Array
"items": (
Reference("JSONSchema", definitions=definitions)
| Array(
items=Reference("JSONSchema", definitions=definitions), min_items=1
)
),
"additionalItems": (
Reference("JSONSchema", definitions=definitions) | Boolean()
),
"minItems": Integer(minimum=0),
"maxItems": Integer(minimum=0),
"uniqueItems": Boolean(),
}
)
| Boolean()
)
definitions["JSONSchema"] = JSONSchema
"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(),
"exclusiveMaximum": Number(),
"multipleOf": Number(exclusive_minimum=0),
# Object
"properties": Object(
"exclusiveMinimum": Number(),
"exclusiveMaximum": Number(),
"multipleOf": Number(exclusive_minimum=0),
# Object
"properties": Object(
additional_properties=Reference("JSONSchema", definitions=definitions)
),
"minProperties": Integer(minimum=0),
"maxProperties": Integer(minimum=0),
"patternProperties": Object(
additional_properties=Reference("JSONSchema", definitions=definitions)
),
"additionalProperties": (
Reference("JSONSchema", definitions=definitions) | Boolean()
),
"required": Array(items=String(), unique_items=True),
# Array
"items": (
Reference("JSONSchema", definitions=definitions)
| Array(
items=Reference("JSONSchema", definitions=definitions), min_items=1
)
),
"additionalItems": (
Reference("JSONSchema", definitions=definitions) | Boolean()
),
"minItems": Integer(minimum=0),
"maxItems": Integer(minimum=0),
"uniqueItems": Boolean(),
}
)
| Boolean()
def set_definitions(field: Field, definitions: SchemaDefinitions) -> None:
"""
Recursively set the definitions that string-referenced `Reference` fields
should use.
"""
if isinstance(field, Reference) and field.definitions is None:
field.definitions = definitions
elif isinstance(field, Array):
if field.items is not None:
if isinstance(field.items, (tuple, list)):
for child in field.items:
set_definitions(child, definitions)
else:
set_definitions(field.items, definitions)
elif isinstance(field, Object):
for child in field.properties.values():
set_definitions(child, definitions)