Skip to content

Commit f34cc60

Browse files
committed
Several improvements to boolean connectors, and many more test cases
1 parent 6d97478 commit f34cc60

7 files changed

+967
-11
lines changed

jsonsubschema/_canonicalization.py

+19-11
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525

2626
def canonicalize_schema(obj):
2727
# First, make sure the given json is a valid json schema.
28-
utils.validate_schema(obj) # should throw jsonschema.SchemaError on unknown types
28+
# should throw jsonschema.SchemaError on unknown types
29+
utils.validate_schema(obj)
2930

3031
# Second, canonicalize the schema.
3132
if utils.is_dict(obj):
@@ -45,15 +46,15 @@ def canonicalize_dict(d, outer_key=None):
4546

4647
# Ignore (drop) any other validatoin keyword when there is a $ref
4748
# Currently, jsonref handles this case properly,
48-
# We might need to handle it again on out own when
49+
# We might need to handle it again on out own when
4950
# we handle recursive $ref independently from jsonref.
5051
# if d.get("$ref"):
5152
# for k in list(d.keys()):
5253
# if k != "$ref" and k not in definitions.JNonValidation:
5354
# del d[k]
5455

5556
# Skip normal dict canonicalization
56-
# for object.properties;
57+
# for object.properties;
5758
# patternProperties;
5859
# dependencies
5960
# because these should be usual dict containers.
@@ -115,8 +116,8 @@ def canonicalize_single_type(d):
115116
return rewrite_enum(d)
116117
else:
117118
return d
118-
119-
# jsonschema validation in the begining prevents
119+
120+
# jsonschema validation in the begining prevents
120121
# reaching this case. So we don't need this.
121122
# else:
122123
# print("Unknown schema type {} at:".format(t))
@@ -134,8 +135,8 @@ def canonicalize_list_of_types(d):
134135
s_i["type"] = t_i
135136
s_i = canonicalize_single_type(s_i)
136137
anyofs.append(s_i)
137-
138-
# jsonschema validation in the begining prevents
138+
139+
# jsonschema validation in the begining prevents
139140
# reaching this case. So we don't need this.
140141
# else:
141142
# print("Unknown schema type {} at: {}".format(t_i, t))
@@ -190,9 +191,14 @@ def canonicalize_connectors(d):
190191
allofs = one + nots
191192
anyofs.append({"allOf": allofs})
192193
return canonicalize_connectors({"anyOf": anyofs})
194+
195+
# Here, the connector is either allOf or oneOf
196+
# So we better simplify them before proceeding more.
193197
else:
194198
d[c] = [canonicalize_dict(i) for i in d[c]]
195-
return d
199+
# return d
200+
simplified = simplify_schema_and_embed_checkers(d)
201+
return simplified
196202

197203
# Connector + other keywords. Combine them first.
198204
else:
@@ -201,8 +207,10 @@ def canonicalize_connectors(d):
201207
allofs.append(canonicalize_dict({c: d[c]}))
202208
del d[c]
203209
if lhs_kw_without_connectors:
204-
allofs.append(canonicalize_dict({k: d[k] for k in lhs_kw_without_connectors}))
210+
allofs.append(canonicalize_dict(
211+
{k: d[k] for k in lhs_kw_without_connectors}))
205212
return {"allOf": allofs}
213+
# return simplify_schema_and_embed_checkers({"allOf": allofs})
206214

207215

208216
def canonicalize_not(d):
@@ -225,7 +233,7 @@ def canonicalize_not(d):
225233
if c == "not":
226234
return negated_schema["not"]
227235

228-
if c == "anyOf":
236+
elif c == "anyOf":
229237
allofs = []
230238
for i in negated_schema["anyOf"]:
231239
allofs.append(canonicalize_not({"not": i}))
@@ -272,7 +280,7 @@ def rewrite_enum(d):
272280
{"type": "number", "minimum": i, "maximum": i})
273281

274282
if t == "boolean":
275-
# booleans are allowed to keep enums,
283+
# booleans are allowed to keep enums,
276284
# since there are only two values.
277285
return d
278286

0 commit comments

Comments
 (0)