summaryrefslogtreecommitdiff
path: root/jsonschema/tests/test_exceptions.py
blob: 92976b37f3cfd6196f49d600369c2e318054195b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
from jsonschema import Draft4Validator, exceptions
from jsonschema.tests.compat import mock, unittest


class TestValidationErrorSorting(unittest.TestCase):
    def test_shallower_errors_are_better_matches(self):
        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "minProperties" : 2,
                        "properties" : {"bar" : {"type" : "object"}},
                    }
                }
            }
        )
        errors = sorted(validator.iter_errors({"foo" : {"bar" : []}}))
        self.assertEqual(
            [list(error.path) for error in errors],
            [["foo", "bar"], ["foo"]],
        )

    def test_global_errors_are_even_better_matches(self):
        validator = Draft4Validator(
            {
                "minProperties" : 2,
                "properties" : {"foo" : {"type" : "array"}},
            }
        )
        errors = sorted(validator.iter_errors({"foo" : {"bar" : []}}))
        self.assertEqual(
            [list(error.path) for error in errors],
            [["foo"], []],
        )

    def test_oneOf_and_anyOf_are_weak_matches(self):
        """
        A property you *must* match is probably better than one you have to
        match a part of.

        """

        validator = Draft4Validator(
            {
                "minProperties" : 2,
                "oneOf" : [{"type" : "string"}, {"type" : "number"}],
            }
        )
        errors = sorted(validator.iter_errors({}))
        self.assertEqual(
            [error.validator for error in errors], ["oneOf", "minProperties"],
        )

    def test_cannot_sort_errors_of_mixed_types(self):
        with self.assertRaises(TypeError):
            v = exceptions.ValidationError("Oh", instance=3)
            s = exceptions.SchemaError("No!", instance=3)
            v < s


class TestBestMatch(unittest.TestCase):
    def test_for_errors_without_context_it_returns_the_max(self):
        """
        The ``max`` will be the error which is most "shallow" in the instance.

        """

        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "minProperties" : 2,
                        "properties" : {"bar" : {"type" : "object"}},
                    },
                },
            }
        )
        errors = sorted(validator.iter_errors({"foo" : {"bar" : []}}))
        self.assertIs(exceptions.best_match(errors), errors[-1])

    def test_context_for_anyOf(self):
        """
        For the anyOf validator, we use the min, to assume the least.

        Other errors are not necessarily relevant, since only one needs to
        match.

        """

        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "anyOf" : [
                            {"type" : "string"},
                            {"properties" : {"bar" : {"type" : "array"}}},
                        ],
                    },
                },
            },
        )
        errors = validator.iter_errors({"foo" : {"bar" : 12}})
        best = exceptions.best_match(errors)
        self.assertEqual(best.validator_value, "array")

    def test_context_for_oneOf(self):
        """
        For the oneOf validator, we use the min, to assume the least.

        Other errors are not necessarily relevant, since only one needs to
        match.

        """

        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "oneOf" : [
                            {"type" : "string"},
                            {"properties" : {"bar" : {"type" : "array"}}},
                        ],
                    },
                },
            },
        )
        errors = validator.iter_errors({"foo" : {"bar" : 12}})
        best = exceptions.best_match(errors)
        self.assertEqual(best.validator_value, "array")

    def test_context_for_allOf(self):
        """
        allOf just yields all the errors globally, so each should be considered

        """

        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "allOf" : [
                            {"type" : "string"},
                            {"properties" : {"bar" : {"type" : "array"}}},
                        ],
                    },
                },
            },
        )
        errors = validator.iter_errors({"foo" : {"bar" : 12}})
        best = exceptions.best_match(errors)
        self.assertEqual(best.validator_value, "string")

    def test_nested_context_for_oneOf(self):
        validator = Draft4Validator(
            {
                "properties" : {
                    "foo" : {
                        "oneOf" : [
                            {"type" : "string"},
                            {
                                "oneOf" : [
                                    {"type" : "string"},
                                    {
                                        "properties" : {
                                            "bar" : {"type" : "array"}
                                        },
                                    },
                                ],
                            },
                        ],
                    },
                },
            },
        )
        errors = validator.iter_errors({"foo" : {"bar" : 12}})
        best = exceptions.best_match(errors)
        self.assertEqual(best.validator_value, "array")

    def test_one_error(self):
        validator = Draft4Validator({"minProperties" : 2})
        error, = validator.iter_errors({})
        self.assertEqual(
            exceptions.best_match(validator.iter_errors({})), error,
        )

    def test_no_errors(self):
        validator = Draft4Validator({})
        self.assertIsNone(exceptions.best_match(validator.iter_errors({})))