6
6
import copy
7
7
import jsonschema
8
8
import numbers
9
+ import numpy
9
10
import sys
10
11
11
12
import jsonsubschema ._constants as definitions
16
17
JSONtop ,
17
18
JSONbot
18
19
)
20
+ from jsonsubschema .exceptions import UnexpectedCanonicalization
19
21
20
22
TOP = {}
21
23
BOT = {"not" : {}}
22
24
23
25
24
26
def canonicalize_schema (obj ):
25
27
# First, make sure the given json is a valid json schema.
26
- utils .validate_schema (obj )
28
+ utils .validate_schema (obj ) # should throw jsonschema.SchemaError on unknown types
27
29
28
30
# Second, canonicalize the schema.
29
31
if utils .is_dict (obj ):
@@ -41,24 +43,33 @@ def canonicalize_dict(d, outer_key=None):
41
43
if d == {} or d == {"not" : {}}:
42
44
return d
43
45
44
- # Ignore (a.k.a drop) any other validatoin keyword
45
- # when there is a $ref
46
- if d .get ("$ref" ):
47
- for k in list (d .keys ()):
48
- if k != "$ref" and k not in definitions .JNonValidation :
49
- del d [k ]
46
+ # Ignore (drop) any other validatoin keyword when there is a $ref
47
+ # Currently, jsonref handles this case properly,
48
+ # We might need to handle it again on out own when
49
+ # we handle recursive $ref independently from jsonref.
50
+ # if d.get("$ref"):
51
+ # for k in list(d.keys()):
52
+ # if k != "$ref" and k not in definitions.JNonValidation:
53
+ # del d[k]
50
54
51
55
# Skip normal dict canonicalization
52
- # for object.properties/patternProperties
56
+ # for object.properties;
57
+ # patternProperties;
58
+ # dependencies
53
59
# because these should be usual dict containers.
54
60
if outer_key in ["properties" , "patternProperties" ]:
55
61
for k , v in d .items ():
56
62
d [k ] = canonicalize_dict (v )
57
63
return d
64
+ if outer_key == "dependencies" :
65
+ for k , v in d .items ():
66
+ if utils .is_dict (v ):
67
+ d [k ] = canonicalize_dict (v )
68
+ return d
58
69
59
70
# here, start dict canonicalization
60
71
if not definitions .Jkeywords .intersection (d .keys ()):
61
- return TOP
72
+ return d
62
73
63
74
t = d .get ("type" )
64
75
has_connectors = definitions .Jconnectors .intersection (d .keys ())
@@ -91,10 +102,10 @@ def canonicalize_single_type(d):
91
102
elif utils .is_list (v ):
92
103
if k == "enum" :
93
104
v = utils .get_typed_enum_vals (v , t )
94
- if not v :
95
- return BOT
96
- else :
97
- d [k ] = v
105
+ # if not v:
106
+ # return BOT
107
+ # else:
108
+ d [k ] = v
98
109
elif k == "required" :
99
110
d [k ] = sorted (set (v ))
100
111
else :
@@ -104,13 +115,14 @@ def canonicalize_single_type(d):
104
115
return rewrite_enum (d )
105
116
else :
106
117
return d
107
-
108
- else :
109
- # TODO: or just return?
110
- print ("Unknown schema type {} at:" .format (t ))
111
- print (d )
112
- print ("Exiting..." )
113
- sys .exit (1 )
118
+
119
+ # jsonschema validation in the begining prevents
120
+ # reaching this case. So we don't need this.
121
+ # else:
122
+ # print("Unknown schema type {} at:".format(t))
123
+ # print(d)
124
+ # print("Exiting...")
125
+ # sys.exit(1)
114
126
115
127
116
128
def canonicalize_list_of_types (d ):
@@ -122,13 +134,18 @@ def canonicalize_list_of_types(d):
122
134
s_i ["type" ] = t_i
123
135
s_i = canonicalize_single_type (s_i )
124
136
anyofs .append (s_i )
125
- else :
126
- # TODO: or just return?
127
- print ("Unknown schema type {} at: {}" .format (t_i , t ))
128
- print (d )
129
- print ("Exiting..." )
130
- sys .exit (1 )
131
-
137
+
138
+ # jsonschema validation in the begining prevents
139
+ # reaching this case. So we don't need this.
140
+ # else:
141
+ # print("Unknown schema type {} at: {}".format(t_i, t))
142
+ # print(d)
143
+ # print("Exiting...")
144
+ # sys.exit(1)
145
+
146
+ # if len(anyofs) == 1:
147
+ # return anyofs[0]
148
+ # elif len(anyofs) > 1:
132
149
return {"anyOf" : anyofs }
133
150
134
151
0 commit comments