def test_simple_list_validation_works(): document = ["test1", 600, "test2"] schema = get_simple_list_schema() v = Validator(schema) assert v.normalized(document) == ["test1", 600, "test2"] assert v.errors == {}
def test_simple_dict_validation_works(): document = {"list_of_values": ["test1", 600, "test2"]} schema = get_simple_dict_schema() v = Validator(schema) assert v.normalized(document) == { "list_of_values": ["test1", 600, "test2"] } assert v.errors == {}
def test_list_custom_coerce_rules_work_in_list(): def multiply(value): return value * 2 schema = {"type": "list", "items": [{"type": "integer", "coerce": multiply}]} document = [2] v = Validator(schema) assert v.validate(document) is True assert v.errors == {} assert v.normalized(document) == [4] assert v.normalized_as_dict(document) == {0: 4}
def test_extended_list_validation_works(): document = [ ["test1", 501, ["test2", 1, 2, 55]], 600, ["test3", 1, 2, { "field1": "testfield", "field2": 5 }], ["test10", 600, "test11"], ] schema = get_extended_list_schema() v = Validator(schema) assert v.normalized(document) == document assert v.errors == {}
def test_coerce_rules_work_in_list_thats_exceeded_len(): schema = { "type": "list", "items": [ {"type": "integer", "coerce": lambda x: x * 2}, {"type": "integer", "coerce": lambda x: int(x * 6)}, ], } document = [2] v = Validator(schema, allow_list_missing=True) assert v.validate(document) is True assert v.errors == {} assert v.normalized(document) == [4] assert v.normalized_as_dict(document) == {0: 4}
def test_list_lambda_coerce_rules_work_in_list(): schema = { "type": "list", "items": [ {"type": "integer", "coerce": lambda x: x * 2}, {"type": "integer", "coerce": lambda x: int(x * 6)}, ], } document = [2, 1.5] v = Validator(schema) assert v.validate(document) is True assert v.errors == {} assert v.normalized(document) == [4, 9] assert v.normalized_as_dict(document) == {0: 4, 1: 9}
def test_normalization_rules_work(): document = { "list_of_values": [ "test1", 600, [ None, "1", 2, { "field1": "testfield", "field2": 5, "field3": "purge_me" } ], ], "list_of_values_2": { "1": "test1", "2": 501, "3": ["test2", 1, 2, 55] }, "list_of_values_3": ["test10", 600, "test11"], } expected_document = { "list_of_values": [ "test1", 600, ["test_default", 1, 2, { "field1": "testfield", "field2": 5 }], ], "list_of_values_2": { "1": "test1", "2": 501, "3": ["test2", 1, 2, 55] }, "list_of_values_3": ["test10", 600, "test11"], } schema = get_extended_dict_schema() v = Validator(schema, purge_unknown=True) assert v.normalized(document) == expected_document assert v.errors == {}
def test_extended_dict_validation_works(): document = { "list_of_values": [ "test1", 600, ["test3", 1, 2, { "field1": "testfield", "field2": 5 }], ], "list_of_values_2": { "1": "test1", "2": 501, "3": ["test2", 1, 2, 55] }, "list_of_values_3": ["test10", 600, "test11"], } schema = get_extended_dict_schema() v = Validator(schema) assert v.normalized(document) == document assert v.errors == {}
def test_multiuse_validation_works(): document = { "list_of_values": [ "rawr", 1000, [ "sfsdf", 1, 4, { "field1": "dasdsa", "field2": 3, "field3": "sfd" } ], ], "list_of_values_2": ["fd", 1000, ["sfsdf", 1, 5434, 4, 4]], } schema = get_multiuse_schema() v = Validator(schema, allow_list_missing=True) for i in range(3): # ------------------------------------------------------------------------------------------------------- expected_value = { "list_of_values": [{ 2: [{ 3: [{ "field3": ["unknown field"] }] }] }], "list_of_values_2": [{ 2: [{ 3: ["min value is 50"], 4: ["unknown field"] }] }], } assert v.validate(document) is False assert v.errors == expected_value assert v.validate(document, schema) is False assert v.errors == expected_value # ------------------------------------------------------------------------------------------------------- expected_value = { "list_of_values": [ "rawr", 1000, [ "sfsdf", 1, 4, { "field1": "dasdsa", "field2": 3, "field3": "sfd" } ], ], "list_of_values_2": ["fd", 1000, ["sfsdf", 1, 5434, 4, 4]], } assert v.normalized(document) == expected_value assert v.normalized(document, schema) == expected_value # ------------------------------------------------------------------------------------------------------- expected_value = { "hello": { 0: "rawr", 1: 1000, 2: { 0: "sfsdf", 1: 1, "hi": 4, 3: { "field01231": "dasdsa", "field2": 3, "field3": "sfd" }, }, }, "list_of_values_2": { 0: "fd", 1: 1000, 2: { 0: "sfsdf", "nd_int": 1, 2: 5434, 3: 4, 4: 4 }, }, } assert v.normalized_as_dict(document) == expected_value assert v.normalized_as_dict(document, schema) == expected_value a = v.normalized_as_object(document) b = v.normalized_as_object(document, schema) assert a.hello[2].hi == 4 assert b.hello[2].hi == 4 assert a.list_of_values_2[2].nd_int == 1 assert a.list_of_values_2[2].nd_int == 1 assert a.list_of_values_2[2][2] == 5434 assert a.list_of_values_2[2][2] == 5434