Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
),
DataTest(
name="Wrong type for A",
data={"A": 1, "B": False},
valid=False,
expected_errors={
"": [messages.VALID_FOR_SEVERAL_EXCLUSIVE_SCHEMAS],
"A": [messages.INVALID_BOOLEAN],
},
),
DataTest(
name="Wrong type for both",
data={"A": 1, "B": 1},
valid=False,
expected_errors={
"": [messages.VALID_FOR_SEVERAL_EXCLUSIVE_SCHEMAS],
"A": [messages.INVALID_BOOLEAN],
"B": [messages.INVALID_BOOLEAN],
},
),
]
class Pattern(SchemaTest):
schema = {"pattern": "^a*$"}
data_tests = [
DataTest(name="valid", data="aaa", valid=True),
DataTest(
name="invalid",
data="abc",
valid=False,
expected_errors={"": [messages.REGEX_NOT_MATCHED]},
),
]
class OneOf(SchemaTest):
schema = {
"type": "object",
"properties": {"A": {"type": "boolean"}, "B": {"type": "boolean"}},
"oneOf": [{"required": ["A"]}, {"required": ["B"]}],
}
data_tests = [
DataTest(
name="Empty object",
data={},
valid=False,
expected_errors={"": [messages.NOT_VALID_UNDER_ANY_SCHEMA]},
),
DataTest(name="Mismatch B", data={"A": True}, valid=True),
DataTest(name="Mismatch A", data={"B": True}, valid=True),
DataTest(
name="Both values",
data={"A": False, "B": False},
valid=False,
expected_errors={"": [messages.VALID_FOR_SEVERAL_EXCLUSIVE_SCHEMAS]},
),
DataTest(
name="Wrong type for A",
data={"A": 1, "B": False},
valid=False,
expected_errors={
"": [messages.VALID_FOR_SEVERAL_EXCLUSIVE_SCHEMAS],
"A": [messages.INVALID_BOOLEAN],
"xn--hxajbheg2az3al.xn--jxalpdlp",
"a" * 63,
]
},
valid=True,
),
DataTest(
name="hostname. invalid",
data={"hostnames": ["...", "a" * 64, "-hi-", "_hi_", "*hi*"]},
valid=False,
expected_errors={
"hostnames.0": [messages.INVALID_HOSTNAME],
"hostnames.1": [messages.INVALID_HOSTNAME],
"hostnames.2": [messages.INVALID_HOSTNAME],
"hostnames.3": [messages.INVALID_HOSTNAME],
"hostnames.4": [messages.INVALID_HOSTNAME],
},
),
DataTest(
name="urls. valid",
data={
"urls": [
"http://www.domain",
"http://www.com",
"http://www.domain.com.",
"http://www.domain.com/.",
"http://www.domain.com/..",
"http://www.domain.com//cataglog//index.html",
"http://www.domain.net/",
"http://www.domain.com/level2/leafnode-L2.xhtml/",
"http://www.domain.com/level2/level3/leafnode-L3.xhtml/",
"http://www.domain.com?pageid=123&testid=1524",
expected_errors={"": [messages.FIELD_TOO_LONG]},
),
DataTest(name="ignores non-arrays", data="foobar", valid=True),
]
class MaxLength(SchemaTest):
schema = {"maxLength": 2}
data_tests = [
DataTest(name="shorter is valid", data="f", valid=True),
DataTest(name="exact length is valid", data="fo", valid=True),
DataTest(
name="too long is invalid",
data="foo",
valid=False,
expected_errors={"": [messages.FIELD_TOO_LONG]},
),
DataTest(name="ignores non-strings", data=100, valid=True),
]
class MaxProperties(SchemaTest):
schema = {"maxProperties": 2}
data_tests = [
DataTest(name="shorter is valid", data={"foo": 1}, valid=True),
DataTest(name="exact length is valid", data={"foo": 1, "bar": 2}, valid=True),
DataTest(
name="too long is invalid",
data={"foo": 1, "bar": 2, "baz": 3},
valid=False,
expected_errors={"": [messages.TOO_MANY_PROPERTIES]},
),
("number", None, messages.INVALID_NUMBER),
("number", "1", messages.INVALID_NUMBER),
("number", True, messages.INVALID_NUMBER),
# null
("null", None, None),
("null", [], messages.NOT_NULL),
("null", {}, messages.NOT_NULL),
("null", 0, messages.NOT_NULL),
("null", "", messages.NOT_NULL),
# object
("object", {}, None),
("object", {"a": 1}, None),
("object", None, messages.INVALID_OBJECT),
("object", [], messages.INVALID_OBJECT),
("object", 0, messages.INVALID_OBJECT),
("object", "", messages.INVALID_OBJECT),
("object", "abc", messages.INVALID_OBJECT),
# string
("string", "", None),
("string", "abc", None),
("string", "-", None),
("string", "...", None),
("string", u"", None),
("string", u"abc", None),
("string", u"EspaƱa", None),
("string", "1", None),
("string", None, messages.INVALID_STRING),
("string", 1, messages.INVALID_STRING),
("string", [], messages.INVALID_STRING),
("string", {}, messages.INVALID_STRING),
]
data_tests = [
expected_errors={"": [messages.MISSING_DEPENDENT_FIELD]},
),
DataTest(
name="multiple, missing other dependency",
schema=schema_multiple,
data={"bar": 1, "quux": 2},
valid=False,
expected_errors={"": [messages.MISSING_DEPENDENT_FIELD]},
),
DataTest(
name="multiple, missing both dependencies",
schema=schema_multiple,
data={"quux": 1},
valid=False,
expected_errors={
"": [messages.MISSING_DEPENDENT_FIELD, messages.MISSING_DEPENDENT_FIELD]
},
),
DataTest(
name="multiple subschema, valid",
schema=schema_multiple_subschema,
data={"foo": 1, "bar": 2},
valid=True,
),
DataTest(
name="multiple subschema, no dependency",
schema=schema_multiple_subschema,
data={"foo": "quux"},
valid=True,
),
DataTest(
name="multiple subschema, wrong type",
("integer", 1.2, messages.INVALID_INT),
("integer", "1", messages.INVALID_INT),
# number
("number", 0, None),
("number", 1, None),
("number", -500, None),
("number", 1000000, None),
("number", 1.2, None),
("number", -34.7, None),
("number", None, messages.INVALID_NUMBER),
("number", "1", messages.INVALID_NUMBER),
("number", True, messages.INVALID_NUMBER),
# null
("null", None, None),
("null", [], messages.NOT_NULL),
("null", {}, messages.NOT_NULL),
("null", 0, messages.NOT_NULL),
("null", "", messages.NOT_NULL),
# object
("object", {}, None),
("object", {"a": 1}, None),
("object", None, messages.INVALID_OBJECT),
("object", [], messages.INVALID_OBJECT),
("object", 0, messages.INVALID_OBJECT),
("object", "", messages.INVALID_OBJECT),
("object", "abc", messages.INVALID_OBJECT),
# string
("string", "", None),
("string", "abc", None),
("string", "-", None),
("string", "...", None),
("string", u"", None),
type_tests = [
# -------------------------------------------------------
# type data expected error
# -------------------------------------------------------
# array
("array", [], None),
("array", [1, 2], None),
("array", None, messages.INVALID_ARRAY),
("array", {}, messages.INVALID_ARRAY),
("array", "[]", messages.INVALID_ARRAY),
("array", "abc", messages.INVALID_ARRAY),
("array", 1, messages.INVALID_ARRAY),
# boolean
("boolean", True, None),
("boolean", False, None),
("boolean", None, messages.INVALID_BOOLEAN),
("boolean", "True", messages.INVALID_BOOLEAN),
("boolean", "False", messages.INVALID_BOOLEAN),
("boolean", 0, messages.INVALID_BOOLEAN),
("boolean", 1, messages.INVALID_BOOLEAN),
# integer
("integer", 0, None),
("integer", 1, None),
("integer", -500, None),
("integer", 1000000, None),
("integer", None, messages.INVALID_INT),
("integer", 1.2, messages.INVALID_INT),
("integer", "1", messages.INVALID_INT),
# number
("number", 0, None),
("number", 1, None),
("number", -500, None),
schema=schema_properties,
data={"foo": "foo", "bar": "bar"},
valid=True,
),
DataTest(
name="properties, missing optional valid",
schema=schema_properties,
data={"bar": "bar"},
valid=True,
),
DataTest(
name="properties, missing required",
schema=schema_properties,
data={"foo": "foo"},
valid=False,
expected_errors={"bar": [messages.MISSING_REQUIRED_FIELD]},
),
DataTest(
name="properties, missing all",
schema=schema_properties,
data={},
valid=False,
expected_errors={"bar": [messages.MISSING_REQUIRED_FIELD]},
),
]
class Format(SchemaTest):
schema = {
"type": "object",
"properties": {
"datetimes": {
class AdditionalItems(SchemaTest):
data_tests = [
DataTest(
name="additionalItems as schema, additional items match schema",
schema={"items": [{}], "additionalItems": {"type": "integer"}},
data=[None, 2, 3, 4],
valid=True,
),
DataTest(
name="additionalItems as schema, additional items do not match schema",
schema={"items": [{}], "additionalItems": {"type": "integer"}},
data=[None, 2, 3, "foo"],
valid=False,
expected_errors={"3": [messages.INVALID_INT]},
),
DataTest(
name="no additionalItems, all items match schema",
schema={"items": {}, "additionalItems": False},
data=[1, 2, 3, 4, 5],
valid=True,
),
DataTest(
name="array of items with no additionalItems, no additional items present",
schema={"items": [{}, {}, {}], "additionalItems": False},
data=[1, 2, 3],
valid=True,
),
DataTest(
name="array of items with no additionalItems, additional items are not permitted",
schema={"items": [{}, {}, {}], "additionalItems": False},