/
schema_utils.py
366 lines (308 loc) · 11.1 KB
/
schema_utils.py
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# Copyright 2019 IBM Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any, Dict, List, Optional, Set, Union
# Type definitions
JsonSchema = Dict[str, Any]
SchemaEnum = Set[Any]
STrue: JsonSchema = {}
SFalse: JsonSchema = {"not": STrue}
forOptimizerConstant: str = "forOptimizer"
forOptimizerConstantSuffix: str = "ForOptimizer"
def is_true_schema(s: JsonSchema) -> bool:
return s is True or s == STrue
def is_false_schema(s: JsonSchema) -> bool:
return s is False or s == SFalse
def is_lale_any_schema(s: JsonSchema) -> bool:
if isinstance(s, dict):
t = s.get("laleType", None)
return t == "Any"
else:
return False
def getForOptimizer(obj, prop: str):
return obj.get(prop + forOptimizerConstantSuffix, None)
def getMinimum(obj):
prop = "minimum"
m = obj.get(prop)
mfo = getForOptimizer(obj, prop)
if mfo is None:
return m
else:
if m is not None and mfo < m:
raise ValueError(
f"A minimum ({m}) and a *smaller* minimumForOptimizer ({mfo}) was specified in {obj}"
)
return mfo
def getMaximum(obj):
prop = "maximum"
m = obj.get(prop)
mfo = getForOptimizer(obj, prop)
if mfo is None:
return m
else:
if m is not None and mfo > m:
raise ValueError(
f"A maximum ({m}) and a *greater* maximumForOptimizer ({mfo}) was specified in {obj}"
)
return mfo
def getExclusiveMinimum(obj):
prop = "exclusveMinimum"
m = obj.get(prop)
mfo = getForOptimizer(obj, prop)
if mfo is None:
return m
else:
return mfo
def getExclusiveMaximum(obj):
prop = "exclusiveMaximum"
m = obj.get(prop)
mfo = getForOptimizer(obj, prop)
if mfo is None:
return m
else:
return mfo
def isForOptimizer(s: JsonSchema) -> bool:
if isinstance(s, dict):
return s.get(forOptimizerConstant, True)
else:
return True
def makeSingleton_(k: str, schemas: List[JsonSchema]) -> JsonSchema:
if len(schemas) == 0:
return {}
if len(schemas) == 1:
return schemas[0]
else:
return {k: schemas}
def makeAllOf(schemas: List[JsonSchema]) -> JsonSchema:
return makeSingleton_("allOf", schemas)
def makeAnyOf(schemas: List[JsonSchema]) -> JsonSchema:
return makeSingleton_("anyOf", schemas)
def makeOneOf(schemas: List[JsonSchema]) -> JsonSchema:
return makeSingleton_("oneOf", schemas)
def forOptimizer(schema: JsonSchema) -> Optional[JsonSchema]:
if schema is None or schema is True or schema is False:
return schema
if not isForOptimizer(schema):
return None
if "anyOf" in schema:
subs = schema["anyOf"]
sch = [forOptimizer(s) for s in subs]
sch_nnil = [s for s in sch if s is not None]
if sch_nnil:
return makeAnyOf(sch_nnil)
else:
return None
if "allOf" in schema:
subs = schema["allOf"]
sch = [forOptimizer(s) for s in subs]
sch_nnil = [s for s in sch if s is not None]
filtered_sch = sch_nnil
if len(sch_nnil) != len(sch):
# Questionable semantics here (aka HACK!!!!)
# Since we removed something from the schema
# we will also remove negated schemas
filtered_sch = [
s for s in sch_nnil if not isinstance(s, dict) or "not" not in s
]
if filtered_sch:
return makeAllOf(filtered_sch)
else:
return None
if "oneOf" in schema:
subs = schema["oneOf"]
sch = [forOptimizer(s) for s in subs]
sch_nnil = [s for s in sch if s is not None]
if sch_nnil:
return makeOneOf(sch_nnil)
else:
return None
if "not" in schema:
s = forOptimizer(schema["not"])
if s is None:
return None
else:
return {"not": s}
transformedSchema: JsonSchema = {}
for k, v in schema.items():
if k.endswith(forOptimizerConstantSuffix):
base: str = k[: -len(forOptimizerConstantSuffix)]
transformedSchema[base] = v
elif k not in transformedSchema:
transformedSchema[k] = v
schema = transformedSchema
if "type" in schema and schema["type"] == "object" and "properties" in schema:
# required = schema.get("required", None)
props = {}
for k, v in schema["properties"].items():
s = forOptimizer(v)
if s is None:
# if required and k in required:
# if this field is required (and has now been filtered)
# filter the whole object schema
return None
else:
props[k] = s
ret = schema.copy()
ret["properties"] = props
return ret
return schema
def has_operator(schema: JsonSchema) -> bool:
to = schema.get("laleType", None)
if to == "operator":
return True
if "not" in schema:
if has_operator(schema["not"]):
return True
if "anyOf" in schema:
if any(has_operator(s) for s in schema["anyOf"]):
return True
if "allOf" in schema:
if any(has_operator(s) for s in schema["allOf"]):
return True
if "oneOf" in schema:
if any(has_operator(s) for s in schema["oneOf"]):
return True
if "items" in schema:
it = schema["items"]
if isinstance(it, list):
if any(has_operator(s) for s in it):
return True
else:
if has_operator(it):
return True
if "properties" in schema:
props = schema["properties"]
if any(has_operator(s) for s in props.values()):
return True
if "patternProperties" in schema:
pattern_props = schema["patternProperties"]
if any(has_operator(s) for s in pattern_props.values()):
return True
if "additionalProperties" in schema:
add_props = schema["additionalProperties"]
if not isinstance(add_props, bool):
if has_operator(add_props):
return True
if "dependencies" in schema:
depends = schema["dependencies"]
for d in depends.values():
if not isinstance(d, list):
if has_operator(d):
return True
# if we survived all of the checks, then we
return False
def atomize_schema_enumerations(
schema: Union[None, JsonSchema, List[JsonSchema]]
) -> None:
"""Given a schema, converts structured enumeration values (records, arrays)
into schemas where the structured part is specified as a schema, with the
primitive as the enum.
"""
if schema is None:
return
if isinstance(schema, list):
for s in schema:
atomize_schema_enumerations(s)
return
if not isinstance(schema, dict):
return
for key in ["anyOf", "allOf", "oneOf", "items", "additionalProperties", "not"]:
atomize_schema_enumerations(schema.get(key, None))
for key in ["properties", "patternProperties", "dependencies"]:
v = schema.get(key, None)
if v is not None:
atomize_schema_enumerations(list(v.values()))
# now that we have handled all the recursive cases
ev = schema.get("enum", None)
if ev is not None:
simple_evs: List[Any] = []
complex_evs: List[JsonSchema] = []
for e in ev:
if isinstance(e, dict):
required: List[str] = []
props: Dict[str, JsonSchema] = {}
for k, v in e.items():
required.append(k)
vs = {"enum": [v]}
atomize_schema_enumerations(vs)
props[k] = vs
ds = {
"type": "object",
"additionalProperties": False,
"required": list(e.keys()),
"properties": props,
}
complex_evs.append(ds)
elif isinstance(e, (list, tuple)):
is_tuple = isinstance(e, tuple)
items_len = len(e)
items: List[JsonSchema] = []
for v in e:
vs = {"enum": [v]}
atomize_schema_enumerations(vs)
items.append(vs)
ls = {
"type": "array",
"items": items,
"additionalItems": False,
"minItems": items_len,
"maxItems": items_len,
}
if is_tuple:
ls["laleType"] = "tuple"
complex_evs.append(ls)
else:
simple_evs.append(ev)
if complex_evs:
del schema["enum"]
if simple_evs:
complex_evs.append({"enum": simple_evs})
if len(complex_evs) == 1:
# special case, just update in place
schema.update(complex_evs[0])
else:
schema["anyOf"] = complex_evs
def check_operators_schema(
schema: Optional[Union[List[JsonSchema], JsonSchema]], warnings: List[str]
) -> None:
"""Given a schema, collect warnings if there
are any enumeration with all Operator values
that are not marked as `'laleType':'operator'`.
This should be called after simplification.
"""
if schema is None:
return
if isinstance(schema, list):
for s in schema:
check_operators_schema(s, warnings)
return
if not isinstance(schema, dict):
return
for key in ["anyOf", "allOf", "oneOf", "items", "additionalProperties", "not"]:
v = schema.get(key, None)
if v is not None:
check_operators_schema(v, warnings)
for key in ["properties", "patternProperties", "dependencies"]:
v = schema.get(key, None)
if v is not None:
check_operators_schema(list(v.values()), warnings)
if "enum" in schema:
es = schema["enum"]
if es:
to = schema.get("laleType", None)
if to != "operator":
from lale.operators import Operator
if all(isinstance(e, Operator) for e in es):
warnings.append(
f"The enumeration {[e.name() for e in es]} is all lale operators, but the schema fragment {schema} it is part of does not stipulate that it should be 'laleType':'operator'. While legal, this likely indicates either an omission in the schema or a bug in the schema simplifier"
)