Skip to content

Commit 6466d07

Browse files
committed
Get these out of the way.
1 parent ce47781 commit 6466d07

File tree

3 files changed

+180
-171
lines changed

3 files changed

+180
-171
lines changed

jsonschema/_legacy_validators.py

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
from jsonschema import _utils
2+
from jsonschema.compat import iteritems
3+
from jsonschema.exceptions import ValidationError
4+
5+
6+
def allOf_draft4(validator, allOf, instance, schema):
7+
for index, subschema in enumerate(allOf):
8+
for error in validator.descend(instance, subschema, schema_path=index):
9+
yield error
10+
11+
12+
def anyOf_draft4(validator, anyOf, instance, schema):
13+
all_errors = []
14+
for index, subschema in enumerate(anyOf):
15+
errs = list(validator.descend(instance, subschema, schema_path=index))
16+
if not errs:
17+
break
18+
all_errors.extend(errs)
19+
else:
20+
yield ValidationError(
21+
"%r is not valid under any of the given schemas" % (instance,),
22+
context=all_errors,
23+
)
24+
25+
26+
def oneOf_draft4(validator, oneOf, instance, schema):
27+
subschemas = enumerate(oneOf)
28+
all_errors = []
29+
for index, subschema in subschemas:
30+
errs = list(validator.descend(instance, subschema, schema_path=index))
31+
if not errs:
32+
first_valid = subschema
33+
break
34+
all_errors.extend(errs)
35+
else:
36+
yield ValidationError(
37+
"%r is not valid under any of the given schemas" % (instance,),
38+
context=all_errors,
39+
)
40+
41+
more_valid = [s for i, s in subschemas if validator.is_valid(instance, s)]
42+
if more_valid:
43+
more_valid.append(first_valid)
44+
reprs = ", ".join(repr(schema) for schema in more_valid)
45+
yield ValidationError(
46+
"%r is valid under each of %s" % (instance, reprs)
47+
)
48+
49+
50+
def disallow_draft3(validator, disallow, instance, schema):
51+
for disallowed in _utils.ensure_list(disallow):
52+
if validator.is_valid(instance, {"type": [disallowed]}):
53+
yield ValidationError(
54+
"%r is disallowed for %r" % (disallowed, instance)
55+
)
56+
57+
58+
def extends_draft3(validator, extends, instance, schema):
59+
if validator.is_type(extends, "object"):
60+
for error in validator.descend(instance, extends):
61+
yield error
62+
return
63+
for index, subschema in enumerate(extends):
64+
for error in validator.descend(instance, subschema, schema_path=index):
65+
yield error
66+
67+
68+
def items_draft3_draft4(validator, items, instance, schema):
69+
if not validator.is_type(instance, "array"):
70+
return
71+
72+
if validator.is_type(items, "object"):
73+
for index, item in enumerate(instance):
74+
for error in validator.descend(item, items, path=index):
75+
yield error
76+
else:
77+
for (index, item), subschema in zip(enumerate(instance), items):
78+
for error in validator.descend(
79+
item, subschema, path=index, schema_path=index,
80+
):
81+
yield error
82+
83+
84+
def minimum_draft3_draft4(validator, minimum, instance, schema):
85+
if not validator.is_type(instance, "number"):
86+
return
87+
88+
if schema.get("exclusiveMinimum", False):
89+
failed = instance <= minimum
90+
cmp = "less than or equal to"
91+
else:
92+
failed = instance < minimum
93+
cmp = "less than"
94+
95+
if failed:
96+
yield ValidationError(
97+
"%r is %s the minimum of %r" % (instance, cmp, minimum)
98+
)
99+
100+
101+
def maximum_draft3_draft4(validator, maximum, instance, schema):
102+
if not validator.is_type(instance, "number"):
103+
return
104+
105+
if schema.get("exclusiveMaximum", False):
106+
failed = instance >= maximum
107+
cmp = "greater than or equal to"
108+
else:
109+
failed = instance > maximum
110+
cmp = "greater than"
111+
112+
if failed:
113+
yield ValidationError(
114+
"%r is %s the maximum of %r" % (instance, cmp, maximum)
115+
)
116+
117+
118+
def properties_draft3(validator, properties, instance, schema):
119+
if not validator.is_type(instance, "object"):
120+
return
121+
122+
for property, subschema in iteritems(properties):
123+
if property in instance:
124+
for error in validator.descend(
125+
instance[property],
126+
subschema,
127+
path=property,
128+
schema_path=property,
129+
):
130+
yield error
131+
elif subschema.get("required", False):
132+
error = ValidationError("%r is a required property" % property)
133+
error._set(
134+
validator="required",
135+
validator_value=subschema["required"],
136+
instance=instance,
137+
schema=schema,
138+
)
139+
error.path.appendleft(property)
140+
error.schema_path.extend([property, "required"])
141+
yield error
142+
143+
144+
def type_draft3(validator, types, instance, schema):
145+
types = _utils.ensure_list(types)
146+
147+
all_errors = []
148+
for index, type in enumerate(types):
149+
if validator.is_type(type, "object"):
150+
errors = list(validator.descend(instance, type, schema_path=index))
151+
if not errors:
152+
return
153+
all_errors.extend(errors)
154+
else:
155+
if validator.is_type(instance, type):
156+
return
157+
else:
158+
yield ValidationError(
159+
_utils.types_msg(instance, types), context=all_errors,
160+
)

jsonschema/_validators.py

Lines changed: 0 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,6 @@ def additionalProperties(validator, aP, instance, schema):
5959
yield ValidationError(error % _utils.extras_msg(extras))
6060

6161

62-
def items_draft3_draft4(validator, items, instance, schema):
63-
if not validator.is_type(instance, "array"):
64-
return
65-
66-
if validator.is_type(items, "object"):
67-
for index, item in enumerate(instance):
68-
for error in validator.descend(item, items, path=index):
69-
yield error
70-
else:
71-
for (index, item), subschema in zip(enumerate(instance), items):
72-
for error in validator.descend(
73-
item, subschema, path=index, schema_path=index,
74-
):
75-
yield error
76-
77-
7862
def items(validator, items, instance, schema):
7963
if not validator.is_type(instance, "array"):
8064
return
@@ -132,40 +116,6 @@ def contains(validator, contains, instance, schema):
132116
)
133117

134118

135-
def minimum_draft3_draft4(validator, minimum, instance, schema):
136-
if not validator.is_type(instance, "number"):
137-
return
138-
139-
if schema.get("exclusiveMinimum", False):
140-
failed = instance <= minimum
141-
cmp = "less than or equal to"
142-
else:
143-
failed = instance < minimum
144-
cmp = "less than"
145-
146-
if failed:
147-
yield ValidationError(
148-
"%r is %s the minimum of %r" % (instance, cmp, minimum)
149-
)
150-
151-
152-
def maximum_draft3_draft4(validator, maximum, instance, schema):
153-
if not validator.is_type(instance, "number"):
154-
return
155-
156-
if schema.get("exclusiveMaximum", False):
157-
failed = instance >= maximum
158-
cmp = "greater than or equal to"
159-
else:
160-
failed = instance > maximum
161-
cmp = "greater than"
162-
163-
if failed:
164-
yield ValidationError(
165-
"%r is %s the maximum of %r" % (instance, cmp, maximum)
166-
)
167-
168-
169119
def exclusiveMinimum(validator, minimum, instance, schema):
170120
if not validator.is_type(instance, "number"):
171121
return
@@ -319,69 +269,6 @@ def ref(validator, ref, instance, schema):
319269
validator.resolver.pop_scope()
320270

321271

322-
def type_draft3(validator, types, instance, schema):
323-
types = _utils.ensure_list(types)
324-
325-
all_errors = []
326-
for index, type in enumerate(types):
327-
if validator.is_type(type, "object"):
328-
errors = list(validator.descend(instance, type, schema_path=index))
329-
if not errors:
330-
return
331-
all_errors.extend(errors)
332-
else:
333-
if validator.is_type(instance, type):
334-
return
335-
else:
336-
yield ValidationError(
337-
_utils.types_msg(instance, types), context=all_errors,
338-
)
339-
340-
341-
def properties_draft3(validator, properties, instance, schema):
342-
if not validator.is_type(instance, "object"):
343-
return
344-
345-
for property, subschema in iteritems(properties):
346-
if property in instance:
347-
for error in validator.descend(
348-
instance[property],
349-
subschema,
350-
path=property,
351-
schema_path=property,
352-
):
353-
yield error
354-
elif subschema.get("required", False):
355-
error = ValidationError("%r is a required property" % property)
356-
error._set(
357-
validator="required",
358-
validator_value=subschema["required"],
359-
instance=instance,
360-
schema=schema,
361-
)
362-
error.path.appendleft(property)
363-
error.schema_path.extend([property, "required"])
364-
yield error
365-
366-
367-
def disallow_draft3(validator, disallow, instance, schema):
368-
for disallowed in _utils.ensure_list(disallow):
369-
if validator.is_valid(instance, {"type": [disallowed]}):
370-
yield ValidationError(
371-
"%r is disallowed for %r" % (disallowed, instance)
372-
)
373-
374-
375-
def extends_draft3(validator, extends, instance, schema):
376-
if validator.is_type(extends, "object"):
377-
for error in validator.descend(instance, extends):
378-
yield error
379-
return
380-
for index, subschema in enumerate(extends):
381-
for error in validator.descend(instance, subschema, schema_path=index):
382-
yield error
383-
384-
385272
def type(validator, types, instance, schema):
386273
types = _utils.ensure_list(types)
387274

@@ -426,56 +313,12 @@ def maxProperties(validator, mP, instance, schema):
426313
yield ValidationError("%r has too many properties" % (instance,))
427314

428315

429-
def allOf_draft4(validator, allOf, instance, schema):
430-
for index, subschema in enumerate(allOf):
431-
for error in validator.descend(instance, subschema, schema_path=index):
432-
yield error
433-
434-
435316
def allOf(validator, allOf, instance, schema):
436317
for index, subschema in enumerate(allOf):
437318
for error in validator.descend(instance, subschema, schema_path=index):
438319
yield error
439320

440321

441-
def oneOf_draft4(validator, oneOf, instance, schema):
442-
subschemas = enumerate(oneOf)
443-
all_errors = []
444-
for index, subschema in subschemas:
445-
errs = list(validator.descend(instance, subschema, schema_path=index))
446-
if not errs:
447-
first_valid = subschema
448-
break
449-
all_errors.extend(errs)
450-
else:
451-
yield ValidationError(
452-
"%r is not valid under any of the given schemas" % (instance,),
453-
context=all_errors,
454-
)
455-
456-
more_valid = [s for i, s in subschemas if validator.is_valid(instance, s)]
457-
if more_valid:
458-
more_valid.append(first_valid)
459-
reprs = ", ".join(repr(schema) for schema in more_valid)
460-
yield ValidationError(
461-
"%r is valid under each of %s" % (instance, reprs)
462-
)
463-
464-
465-
def anyOf_draft4(validator, anyOf, instance, schema):
466-
all_errors = []
467-
for index, subschema in enumerate(anyOf):
468-
errs = list(validator.descend(instance, subschema, schema_path=index))
469-
if not errs:
470-
break
471-
all_errors.extend(errs)
472-
else:
473-
yield ValidationError(
474-
"%r is not valid under any of the given schemas" % (instance,),
475-
context=all_errors,
476-
)
477-
478-
479322
def anyOf(validator, anyOf, instance, schema):
480323
all_errors = []
481324
for index, subschema in enumerate(anyOf):

0 commit comments

Comments
 (0)