コード例 #1
0
def test_readable_schema_wrong_naming():
    document = {"produce": ["Apple", 5, "High"]}
    schema = {
        "unknown": {
            "type":
            "list",
            "name":
            "fruits",
            "items": [
                {
                    "type": "string"
                },
                {
                    "type": "integer",
                    "min": 0
                },
                {
                    "type": "string"
                },
            ],
        }
    }
    v = Validator(schema)
    assert v.validate(document) is False
    assert v.errors == {"produce": ["unknown field"]}
    assert v.normalized_as_dict(document) == {
        "produce": {
            0: "Apple",
            1: 5,
            2: "High"
        }
    }
    assert v.errors == {}
コード例 #2
0
def test_simple_list_validation_works():
    document = ["test1", 600, "test2"]
    schema = get_simple_list_schema()

    v = Validator(schema)
    assert v.validate(document) is True
    assert v.errors == {}
コード例 #3
0
def test_simple_dict_validation_breaks():
    document = {"list_of_values": [40, 600, "test2"]}
    schema = get_simple_dict_schema()

    v = Validator(schema)
    assert v.validate(document) is False
    assert v.errors == {"list_of_values": [{0: ["must be of string type"]}]}
コード例 #4
0
def test_simple_dict_validation_list_len_breaks():
    document = {"list_of_values": ["test1", 600]}
    schema = get_simple_dict_schema()

    v = Validator(schema)
    assert v.validate(document) is False
    assert v.errors == {"list_of_values": ["length of list should be 3, it is 2"]}
コード例 #5
0
def test_simple_dict_validation_min_number_not_compliant():
    document = {"list_of_values": ["test1", 400, "test2"]}
    schema = get_simple_dict_schema()

    v = Validator(schema)
    assert v.validate(document) is False
    assert v.errors == {"list_of_values": [{1: ["min value is 500"]}]}
コード例 #6
0
def test_simple_dict_validation_list_len_works():
    document = {"list_of_values": ["test1", 600]}
    schema = get_simple_dict_schema()

    v = Validator(schema, allow_list_missing=True)
    assert v.validate(document) is True
    assert v.errors == {}
コード例 #7
0
def test_simple_dict_validation_works():
    document = {"list_of_values": ["test1", 600, "test2"]}
    schema = get_simple_dict_schema()

    v = Validator(schema)
    assert v.validate(document) is True
    assert v.errors == {}
コード例 #8
0
def test_extended_list_validation_list_len_breaks():
    document = [
        ["test1", 501, ["test2", 1, 2, 55]],
        600,
        ["test3", 1, 2, {"field1": "testfield", "field2": 5}],
        ["test10", 600],
    ]
    schema = get_extended_list_schema()

    v = Validator(schema)
    assert v.validate(document) is False
    assert v.errors == {"_schema": [{3: ["length of list should be 3, it is 2"]}]}
コード例 #9
0
def test_extended_list_validation_works_no_schema_init():
    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()
    assert v.validate(document, schema) is True
    assert v.errors == {}
コード例 #10
0
def test_extended_list_validation_list_len_works():
    document = [
        ["test1", 501, ["test2", 1, 2, 55]],
        600,
        ["test3", 1, 2, {"field1": "testfield", "field2": 5}],
        ["test10", 600],
    ]
    schema = get_extended_list_schema()

    v = Validator(schema, allow_list_missing=True)
    assert v.validate(document) is True
    assert v.errors == {}
コード例 #11
0
def test_extended_list_validation_min_number_not_compliant():
    document = [
        ["test1", 499, ["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.validate(document) is False
    assert v.errors == {"_schema": [{0: [{1: ["min value is 500"]}]}]}
コード例 #12
0
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}
コード例 #13
0
def test_extended_dict_validation_too_many_fields_raise():
    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, "hello"]},
        "list_of_values_3": ["test10", 600, "test11"],
    }
    schema = get_extended_dict_schema()

    v = Validator(schema, allow_list_missing=True)
    assert v.validate(document) is False
    assert v.errors == {"list_of_values_2": [{"3": [{4: ["unknown field"]}]}]}
コード例 #14
0
def test_extended_dict_validation_list_len_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]},
        "list_of_values_3": ["test10", 600, "test11"],
    }
    schema = get_extended_dict_schema()

    v = Validator(schema, allow_list_missing=True)
    assert v.validate(document) is True
    assert v.errors == {}
コード例 #15
0
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}
コード例 #16
0
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}
コード例 #17
0
def test_extended_dict_validation_works_no_schema_init():
    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()
    print(v.errors)
    assert v.validate(document, schema) is True
    assert v.errors == {}
コード例 #18
0
def test_extended_dict_validation_list_len_breaks():
    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]},
        "list_of_values_3": ["test10", 600, "test11"],
    }
    schema = get_extended_dict_schema()

    v = Validator(schema)
    assert v.validate(document) is False
    assert v.errors == {
        "list_of_values_2": [{"3": ["length of list should be 4, it is 3"]}]
    }
コード例 #19
0
def test_extended_dict_validation_min_number_not_compliant():
    document = {
        "list_of_values": [
            "test1",
            600,
            ["test3", 1, 2, {"field1": "testfield", "field2": 5}],
        ],
        "list_of_values_2": {"1": "test1", "2": 499, "3": ["test2", 1, 2, 55, "hello"]},
        "list_of_values_3": ["test10", 600, "test11"],
    }
    schema = get_extended_dict_schema()

    v = Validator(schema)
    assert v.validate(document) is False
    assert v.errors == {
        "list_of_values_2": [
            {"2": ["min value is 500"], "3": ["length of list should be 4, it is 5"]}
        ]
    }
コード例 #20
0
def test_extended_list_validation_too_many_fields_raise():
    document = [
        ["test1", 501, ["test2", 1, 2, 55]],
        600,
        ["test3", 1, 2, {"field1": "testfield", "field2": 5, "field3": 4}],
        ["test10", 600],
    ]
    schema = get_extended_list_schema()

    v = Validator(schema)
    assert v.validate(document) is False
    assert v.errors == {
        "_schema": [
            {
                2: [{3: [{"field3": ["unknown field"]}]}],
                3: ["length of list should be 3, it is 2"],
            }
        ]
    }
コード例 #21
0
def test_extended_list_validation_breaks():
    document = [
        ["test1", 501, ["test", 1, 2, "testwrongtype"]],
        600,
        ["test3", 1, 2, {"field3": "testfield", "field2": 5}],
        ["test10", 600, "test11"],
    ]
    schema = get_extended_list_schema()

    v = Validator(schema)
    assert v.validate(document) is False
    assert v.errors == {
        "_schema": [
            {
                0: [{2: [{3: ["must be of integer type"]}]}],
                2: [{3: [{"field3": ["unknown field"]}]}],
            }
        ]
    }
コード例 #22
0
def test_coerce_rules_work_in_list():

    schema = {
        "list_of_values": {
            "type": "list",
            "items": [
                {"type": "string", "coerce": str},
                {"type": "integer", "coerce": int},
            ],
        }
    }

    document = {"list_of_values": [123, "987"]}

    v = Validator()
    assert v.validate(document, schema) is True
    assert v.errors == {}
    assert v.normalized_as_dict(document, schema) == {
        "list_of_values": {0: "123", 1: 987}
    }
コード例 #23
0
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