json-model-compiler 2.0b0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- json_model/README.md +32 -0
- json_model/__init__.py +18 -0
- json_model/analyze.py +409 -0
- json_model/clang.py +563 -0
- json_model/convert.py +430 -0
- json_model/data/clang_entry.c +21 -0
- json_model/data/clang_free.c +10 -0
- json_model/data/clang_init.c +11 -0
- json_model/data/clang_pcre2_fun.c +6 -0
- json_model/data/clang_re2_fun.c +5 -0
- json_model/data/javascript_exe.js +4 -0
- json_model/data/javascript_foot.js +9 -0
- json_model/data/javascript_free.js +9 -0
- json_model/data/javascript_head.js +1 -0
- json_model/data/javascript_init.js +11 -0
- json_model/data/javascript_main.js +5 -0
- json_model/data/plpgsql_entry.sql +16 -0
- json_model/data/python_entry.py +11 -0
- json_model/data/python_exe.py +1 -0
- json_model/data/python_free.py +6 -0
- json_model/data/python_init.py +9 -0
- json_model/data/python_main.py +4 -0
- json_model/javascript.py +310 -0
- json_model/language.py +748 -0
- json_model/model.py +1084 -0
- json_model/mtypes.py +42 -0
- json_model/objmerge.py +165 -0
- json_model/optim.py +486 -0
- json_model/plpgsql.py +422 -0
- json_model/python.py +269 -0
- json_model/recurse.py +127 -0
- json_model/resolver.py +106 -0
- json_model/runtime/META.json +30 -0
- json_model/runtime/Makefile +21 -0
- json_model/runtime/__init__.py +32 -0
- json_model/runtime/json-model.c +1054 -0
- json_model/runtime/json-model.h +183 -0
- json_model/runtime/json_model--2.0.sql +261 -0
- json_model/runtime/json_model.control +2 -0
- json_model/runtime/main.c +473 -0
- json_model/runtime/support.py +485 -0
- json_model/runtime/types.py +16 -0
- json_model/script.py +585 -0
- json_model/url_cache.py +82 -0
- json_model/utils.py +623 -0
- json_model/xstatic.py +1444 -0
- json_model_compiler-2.0b0.dist-info/METADATA +201 -0
- json_model_compiler-2.0b0.dist-info/RECORD +51 -0
- json_model_compiler-2.0b0.dist-info/WHEEL +5 -0
- json_model_compiler-2.0b0.dist-info/entry_points.txt +2 -0
- json_model_compiler-2.0b0.dist-info/top_level.txt +1 -0
json_model/convert.py
ADDED
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from .mtypes import ModelType, ModelPath, JsonSchema, Jsonable, Symbols, JsonModel
|
|
3
|
+
from .utils import is_cst, cst, log, PREDEF_RE
|
|
4
|
+
|
|
5
|
+
#
|
|
6
|
+
# MODEL TO SCHEMA CONVERSION
|
|
7
|
+
#
|
|
8
|
+
|
|
9
|
+
PREDEF_TYPES = {
|
|
10
|
+
"$STRING": "string",
|
|
11
|
+
"$DATE": "string",
|
|
12
|
+
"$TIME": "string",
|
|
13
|
+
"$DATETIME": "string",
|
|
14
|
+
"$REGEX": "string",
|
|
15
|
+
"$EXREG": "string",
|
|
16
|
+
"$UUID": "string",
|
|
17
|
+
"$URL": "string",
|
|
18
|
+
"$URI": "string",
|
|
19
|
+
"$EMAIL": "string",
|
|
20
|
+
"$NUMBER": "number",
|
|
21
|
+
"$BOOL": "boolean",
|
|
22
|
+
"$BOOLEAN": "boolean",
|
|
23
|
+
"$NULL": "null",
|
|
24
|
+
"$INTEGER": "integer",
|
|
25
|
+
"$INT": "integer",
|
|
26
|
+
"$I32": "integer",
|
|
27
|
+
"$I64": "integer",
|
|
28
|
+
"$FLOAT": "number",
|
|
29
|
+
"$F32": "number",
|
|
30
|
+
"$F64": "number",
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
PREDEF_FORMATS = {
|
|
34
|
+
"$DATE": "date",
|
|
35
|
+
"$TIME": "time",
|
|
36
|
+
"$DATETIME": "datetime",
|
|
37
|
+
"$URL": "uri",
|
|
38
|
+
"$REGEX": "regex",
|
|
39
|
+
# not "$EXREG"?
|
|
40
|
+
"$UUID": "uuid",
|
|
41
|
+
"$EMAIL": "email",
|
|
42
|
+
"$URI": "uri",
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
def unExreg(pat: str, opts: str) -> tuple[str, str]:
|
|
46
|
+
"""Convert extended regex to standard regex."""
|
|
47
|
+
opts = re.sub("X", "", opts)
|
|
48
|
+
pat = re.sub(r"\(\$\w+:", "(", pat) # explicit re
|
|
49
|
+
pat = re.sub(r"\$\w+", ".*", pat) # remaining implicit re
|
|
50
|
+
return pat, opts
|
|
51
|
+
|
|
52
|
+
def propRefRegex(jm: JsonModel, model: ModelType, path: ModelPath) -> str|None:
|
|
53
|
+
"""Try to handle a property refererence in some cases as a regex."""
|
|
54
|
+
if isinstance(model, dict) and "|" in model:
|
|
55
|
+
# try to build a regex
|
|
56
|
+
alts = []
|
|
57
|
+
assert isinstance(model["|"], list)
|
|
58
|
+
for index, alt in enumerate(model["|"]):
|
|
59
|
+
lpath = path + ["|", index]
|
|
60
|
+
|
|
61
|
+
# resolve and recurse if needed, with some shortcuts
|
|
62
|
+
changed = True
|
|
63
|
+
while isinstance(alt, str) and changed:
|
|
64
|
+
changed = False
|
|
65
|
+
if alt == "":
|
|
66
|
+
return "" # shortcut for default, any strings are ok
|
|
67
|
+
if alt[0] == "$" and alt not in PREDEF_TYPES:
|
|
68
|
+
ja = jm.resolveRef(alt, path) # type: ignore
|
|
69
|
+
alt, changed = ja._model, True
|
|
70
|
+
|
|
71
|
+
if not isinstance(alt, str):
|
|
72
|
+
salts = propRefRegex(jm, alt, lpath)
|
|
73
|
+
if salts is not None:
|
|
74
|
+
if salts == "":
|
|
75
|
+
return ""
|
|
76
|
+
else:
|
|
77
|
+
alts.append(salts)
|
|
78
|
+
continue
|
|
79
|
+
else: # let caller do a rough approximation
|
|
80
|
+
log.debug(f"cannot handle prop ref at {lpath}")
|
|
81
|
+
return None
|
|
82
|
+
|
|
83
|
+
if alt in PREDEF_RE:
|
|
84
|
+
alts.append(PREDEF_RE[alt])
|
|
85
|
+
continue
|
|
86
|
+
|
|
87
|
+
# string constant and regex
|
|
88
|
+
if re.match(r"[_a-zA-Z0-9]", alt):
|
|
89
|
+
if alt[0] == "_":
|
|
90
|
+
alt = alt[1:]
|
|
91
|
+
alt = re.escape(alt)
|
|
92
|
+
alts.append(f"^{alt}$")
|
|
93
|
+
elif alt[0] == "/":
|
|
94
|
+
alt, opts = alt[1:].rsplit("/", 1)
|
|
95
|
+
if "X" in opts:
|
|
96
|
+
alt, opts = unExreg(alt, opts)
|
|
97
|
+
alts.append(f"(?{opts}){alt}" if opts else alt)
|
|
98
|
+
else:
|
|
99
|
+
log.debug(f"cannot handle prop ref at {lpath}")
|
|
100
|
+
|
|
101
|
+
return "(" + "|".join(alts) + ")"
|
|
102
|
+
else: # not a |
|
|
103
|
+
log.debug(f"cannot handle prop ref at {path}")
|
|
104
|
+
return None
|
|
105
|
+
|
|
106
|
+
def _m2s(model: ModelType, path: ModelPath, defs: Symbols) -> JsonSchema:
|
|
107
|
+
"""Recursive model to schema conversion."""
|
|
108
|
+
|
|
109
|
+
schema: Jsonable = {}
|
|
110
|
+
|
|
111
|
+
match model:
|
|
112
|
+
case None:
|
|
113
|
+
schema["type"] = "null"
|
|
114
|
+
case bool():
|
|
115
|
+
schema["type"] = "boolean"
|
|
116
|
+
case float():
|
|
117
|
+
schema["type"] = "number"
|
|
118
|
+
if model == 0.0:
|
|
119
|
+
schema["minimum"] = 0.0
|
|
120
|
+
elif model == 1.0:
|
|
121
|
+
schema["exclusiveMinimum"] = 0.0
|
|
122
|
+
case int():
|
|
123
|
+
schema["type"] = "integer"
|
|
124
|
+
# FIXME should check for option?
|
|
125
|
+
if model == 0:
|
|
126
|
+
schema["minimum"] = 0
|
|
127
|
+
elif model == 1:
|
|
128
|
+
schema["minimum"] = 1
|
|
129
|
+
case str():
|
|
130
|
+
if model == "":
|
|
131
|
+
schema["type"] = "string"
|
|
132
|
+
elif model == "$ANY": # shortcuts
|
|
133
|
+
return True
|
|
134
|
+
elif model == "$NONE":
|
|
135
|
+
return False
|
|
136
|
+
elif model in PREDEF_TYPES:
|
|
137
|
+
schema["type"] = PREDEF_TYPES[model]
|
|
138
|
+
if model in PREDEF_FORMATS:
|
|
139
|
+
schema["format"] = PREDEF_FORMATS[model]
|
|
140
|
+
elif model[0] == "$":
|
|
141
|
+
if model == "$#":
|
|
142
|
+
schema["$ref"] = "#"
|
|
143
|
+
elif "/" in model or "." in model: # probable URL
|
|
144
|
+
schema["$ref"] = model[1:]
|
|
145
|
+
else:
|
|
146
|
+
schema["$ref"] = "#/$defs/" + model[1:]
|
|
147
|
+
elif model[0] == "/":
|
|
148
|
+
schema["type"] = "string"
|
|
149
|
+
try:
|
|
150
|
+
pattern, ropts = model[1:].rsplit("/", 1)
|
|
151
|
+
assert ropts == "" or ropts.isalpha(), f"invalid regex options: {ropts}"
|
|
152
|
+
if "X" in ropts:
|
|
153
|
+
pattern, ropts = unExreg(pattern, ropts)
|
|
154
|
+
pattern = f"(?{ropts}){pattern}" if ropts else pattern
|
|
155
|
+
except Exception as e:
|
|
156
|
+
raise Exception(f"invalid regex {model}: {e}")
|
|
157
|
+
schema["pattern"] = pattern
|
|
158
|
+
else: # _ = …
|
|
159
|
+
schema["const"] = cst(model)
|
|
160
|
+
case list():
|
|
161
|
+
if len(model) == 1:
|
|
162
|
+
schema["type"] = "array"
|
|
163
|
+
schema["items"] = _m2s(model[0], path + [0], defs)
|
|
164
|
+
elif len(model) > 1:
|
|
165
|
+
# tuple: prefixItems/items?
|
|
166
|
+
schema["type"] = "array"
|
|
167
|
+
pitems = []
|
|
168
|
+
for i, item in enumerate(model):
|
|
169
|
+
pitems.append(_m2s(item, path + [i], defs))
|
|
170
|
+
schema["prefixItems"] = pitems
|
|
171
|
+
schema["items"] = False
|
|
172
|
+
# needed to ensure that all prefix items are there
|
|
173
|
+
schema["minItems"] = len(model)
|
|
174
|
+
else:
|
|
175
|
+
# liste vide : tableau maxItems = 0
|
|
176
|
+
schema["type"] = "array"
|
|
177
|
+
schema["maxItems"] = 0
|
|
178
|
+
|
|
179
|
+
case dict():
|
|
180
|
+
|
|
181
|
+
# commentaires transformés en "description", "$comment" disponible si besoin
|
|
182
|
+
if "#" in model:
|
|
183
|
+
sharp = model["#"]
|
|
184
|
+
assert isinstance(sharp, str)
|
|
185
|
+
schema["description"] = sharp
|
|
186
|
+
|
|
187
|
+
# constraint...
|
|
188
|
+
if "@" in model:
|
|
189
|
+
subschema = _m2s(model["@"], path + ["@"], defs)
|
|
190
|
+
if isinstance(subschema, dict):
|
|
191
|
+
schema.update(**subschema)
|
|
192
|
+
else:
|
|
193
|
+
assert isinstance(subschema, bool)
|
|
194
|
+
if not subschema:
|
|
195
|
+
# schema["not"] = {}
|
|
196
|
+
# no matching schema, no point in checking anything further
|
|
197
|
+
return False
|
|
198
|
+
# if true, we keep the schema as is
|
|
199
|
+
if "type" in schema:
|
|
200
|
+
# contraints on string
|
|
201
|
+
if schema["type"] == "string":
|
|
202
|
+
if "<=" in model:
|
|
203
|
+
schema["maxLength"] = model["<="]
|
|
204
|
+
if ">=" in model:
|
|
205
|
+
schema["minLength"] = model[">="]
|
|
206
|
+
if "=" in model:
|
|
207
|
+
eq = model["="]
|
|
208
|
+
if isinstance(eq, int):
|
|
209
|
+
schema["minLength"] = eq
|
|
210
|
+
schema["maxLength"] = eq
|
|
211
|
+
elif isinstance(eq, str):
|
|
212
|
+
schema["const"] = eq
|
|
213
|
+
else:
|
|
214
|
+
log.warning(f"ignoring '=' constraint at {path}")
|
|
215
|
+
if "!=" in model:
|
|
216
|
+
schema["not"] = {"const": model["!="]}
|
|
217
|
+
# FIXME extension? remove?
|
|
218
|
+
# if "mime" in model:
|
|
219
|
+
# schema["contentEncoding"] = model["mime"]
|
|
220
|
+
# if "encoding" in model:
|
|
221
|
+
# schema["contentMediaType"] = model["encoding"]
|
|
222
|
+
# contraints on number or integer
|
|
223
|
+
if schema["type"] in ("number", "integer"):
|
|
224
|
+
if "<=" in model:
|
|
225
|
+
schema["maximum"] = model["<="]
|
|
226
|
+
if ">=" in model:
|
|
227
|
+
schema["minimum"] = model[">="]
|
|
228
|
+
if "<" in model:
|
|
229
|
+
schema["exclusiveMaximum"] = model["<"]
|
|
230
|
+
if ">" in model:
|
|
231
|
+
schema["exclusiveMinimum"] = model[">"]
|
|
232
|
+
if ".mo" in model: # model extension
|
|
233
|
+
schema["multipleOf"] = model[".mo"]
|
|
234
|
+
if "=" in model:
|
|
235
|
+
eq = model["="]
|
|
236
|
+
if isinstance(eq, (int, float)):
|
|
237
|
+
if "const" in schema:
|
|
238
|
+
if schema["const"] != eq:
|
|
239
|
+
return False
|
|
240
|
+
else:
|
|
241
|
+
pass # redundant
|
|
242
|
+
else:
|
|
243
|
+
schema["const"] = eq
|
|
244
|
+
elif isinstance(eq, str):
|
|
245
|
+
return False
|
|
246
|
+
if "!=" in model:
|
|
247
|
+
assert "not" not in schema # FIXME allOf?
|
|
248
|
+
schema["not"] = {"const": model["!="]}
|
|
249
|
+
# contraints on object
|
|
250
|
+
if schema["type"] == "object":
|
|
251
|
+
if "<=" in model:
|
|
252
|
+
assert isinstance(model["<="], int)
|
|
253
|
+
schema["maxProperties"] = model["<="]
|
|
254
|
+
if "<" in model:
|
|
255
|
+
assert isinstance(model["<"], int)
|
|
256
|
+
schema["maxProperties"] = model["<"] - 1
|
|
257
|
+
if ">=" in model:
|
|
258
|
+
assert isinstance(model[">="], int)
|
|
259
|
+
schema["minProperties"] = model[">="]
|
|
260
|
+
if ">" in model:
|
|
261
|
+
assert isinstance(model[">"], int)
|
|
262
|
+
schema["minProperties"] = model[">"] + 1
|
|
263
|
+
if "=" in model:
|
|
264
|
+
eq = model["="]
|
|
265
|
+
if isinstance(eq, int):
|
|
266
|
+
schema["minProperties"] = eq
|
|
267
|
+
schema["maxProperties"] = eq
|
|
268
|
+
else:
|
|
269
|
+
schema["const"] = eq # probably infeasible
|
|
270
|
+
if "!=" in model:
|
|
271
|
+
assert "not" not in schema # FIXME allOf?
|
|
272
|
+
schema["not"] = {"const": model["!="]}
|
|
273
|
+
# constraints on array
|
|
274
|
+
if schema["type"] == "array":
|
|
275
|
+
if ".in" in model: # model extension
|
|
276
|
+
schema["contains"] = _m2s(model[".in"], path, defs)
|
|
277
|
+
if "<=" in model:
|
|
278
|
+
assert isinstance(model["<="], int)
|
|
279
|
+
schema["maxContains"] = model["<="]
|
|
280
|
+
if "<" in model:
|
|
281
|
+
assert isinstance(model["<"], int)
|
|
282
|
+
schema["maxContains"] = model["<"] - 1
|
|
283
|
+
if ">=" in model:
|
|
284
|
+
assert isinstance(model[">="], int)
|
|
285
|
+
schema["minContains"] = model[">="]
|
|
286
|
+
if ">" in model:
|
|
287
|
+
assert isinstance(model[">"], int)
|
|
288
|
+
schema["minContains"] = model[">"] + 1
|
|
289
|
+
# TODO
|
|
290
|
+
# else:
|
|
291
|
+
# FIXME check comparison value types!
|
|
292
|
+
if "<=" in model:
|
|
293
|
+
assert isinstance(model["<="], int)
|
|
294
|
+
schema["maxItems"] = model["<="]
|
|
295
|
+
if "<" in model:
|
|
296
|
+
assert isinstance(model["<"], int)
|
|
297
|
+
schema["maxItems"] = model["<"] - 1
|
|
298
|
+
if ">=" in model:
|
|
299
|
+
assert isinstance(model[">="], int)
|
|
300
|
+
schema["minItems"] = model[">="]
|
|
301
|
+
if ">" in model:
|
|
302
|
+
assert isinstance(model[">"], int)
|
|
303
|
+
schema["minItems"] = model[">"] + 1
|
|
304
|
+
if "!" in model:
|
|
305
|
+
v = model["!"]
|
|
306
|
+
assert isinstance(v, bool)
|
|
307
|
+
if v:
|
|
308
|
+
schema["uniqueItems"] = True
|
|
309
|
+
else:
|
|
310
|
+
# no type, maybe because there are several, eg a or/xor
|
|
311
|
+
# random attempt at supporting some cases…
|
|
312
|
+
# we could do a better job if we knew here which types are actually possible
|
|
313
|
+
if ">=" in model and isinstance(model[">="], int):
|
|
314
|
+
v = int(model[">="])
|
|
315
|
+
schema["minimum"] = v
|
|
316
|
+
schema["minLength"] = v
|
|
317
|
+
schema["minProperties"] = v
|
|
318
|
+
schema["minItems"] = v
|
|
319
|
+
if ">" in model and isinstance(model[">"], int):
|
|
320
|
+
v = int(model[">"])
|
|
321
|
+
schema["exclusiveMinimum"] = v
|
|
322
|
+
schema["minLength"] = v + 1
|
|
323
|
+
schema["minProperties"] = v + 1
|
|
324
|
+
schema["minItems"] = v + 1
|
|
325
|
+
if "<=" in model and isinstance(model["<="], int):
|
|
326
|
+
v = int(model["<="])
|
|
327
|
+
schema["maximum"] = v
|
|
328
|
+
schema["maxLength"] = v
|
|
329
|
+
schema["maxProperties"] = v
|
|
330
|
+
schema["maxItems"] = v
|
|
331
|
+
if "<" in model and isinstance(model["<"], int):
|
|
332
|
+
v = int(model["<"])
|
|
333
|
+
schema["exclusiveMaximum"] = v
|
|
334
|
+
schema["maxLength"] = v - 1
|
|
335
|
+
schema["maxProperties"] = v - 1
|
|
336
|
+
schema["maxItems"] = v - 1
|
|
337
|
+
if "=" in model and isinstance(model["="], int):
|
|
338
|
+
v = int(model["="])
|
|
339
|
+
schema["minimum"] = v
|
|
340
|
+
schema["maximum"] = v
|
|
341
|
+
schema["minLength"] = v
|
|
342
|
+
schema["maxLength"] = v
|
|
343
|
+
schema["minProperties"] = v
|
|
344
|
+
schema["maxProperties"] = v
|
|
345
|
+
schema["minItems"] = v
|
|
346
|
+
schema["maxItems"] = v
|
|
347
|
+
# combinations
|
|
348
|
+
elif "&" in model:
|
|
349
|
+
models = model["&"]
|
|
350
|
+
assert isinstance(models, list) # pyright hint
|
|
351
|
+
schema["allOf"] = [_m2s(m, path + ["&", i], defs) for i, m in enumerate(models)]
|
|
352
|
+
elif "^" in model:
|
|
353
|
+
models = model["^"]
|
|
354
|
+
assert isinstance(models, list) # pyright hint
|
|
355
|
+
schema["oneOf"] = [_m2s(m, path + ["^", i], defs) for i, m in enumerate(models)]
|
|
356
|
+
elif "|" in model:
|
|
357
|
+
models = model["|"]
|
|
358
|
+
assert isinstance(models, list)
|
|
359
|
+
if all(map(is_cst, models)): # all choices are constants
|
|
360
|
+
schema["enum"] = list(map(cst, models)) # pyright: ignore
|
|
361
|
+
else:
|
|
362
|
+
schema["anyOf"] = [_m2s(m, path + ["|", i], defs)
|
|
363
|
+
for i, m in enumerate(models)]
|
|
364
|
+
elif "+" in model:
|
|
365
|
+
assert False, f"+ must have been removed by preprocessing {path + ['+']}"
|
|
366
|
+
else: # actual object!
|
|
367
|
+
schema["type"] = "object"
|
|
368
|
+
properties, required, addProp = {}, [], False
|
|
369
|
+
|
|
370
|
+
# récupérer properties/required
|
|
371
|
+
for prop, val in model.items():
|
|
372
|
+
assert isinstance(prop, str)
|
|
373
|
+
lpath = path + [prop]
|
|
374
|
+
# skip some properties: already managed before
|
|
375
|
+
if prop in ("~", "#", "$", "%"):
|
|
376
|
+
continue
|
|
377
|
+
if prop == "":
|
|
378
|
+
addProp = _m2s(val, lpath, defs)
|
|
379
|
+
elif prop[0] in ("_", "!"):
|
|
380
|
+
required.append(prop[1:])
|
|
381
|
+
properties[prop[1:]] = _m2s(val, lpath, defs)
|
|
382
|
+
elif prop[0] == "?":
|
|
383
|
+
properties[prop[1:]] = _m2s(val, lpath, defs)
|
|
384
|
+
elif prop[0] == "/":
|
|
385
|
+
try:
|
|
386
|
+
pattern, ropts = prop[1:].rsplit("/", 1)
|
|
387
|
+
assert ropts == "" or ropts.isalpha(), f"invalid regex: {prop}"
|
|
388
|
+
pattern = f"(?{ropts}){pattern}" if ropts else pattern
|
|
389
|
+
except Exception as e:
|
|
390
|
+
raise Exception(f"invalid prop regex {prop}: {e}")
|
|
391
|
+
if "patternProperties" not in schema:
|
|
392
|
+
schema["patternProperties"] = {}
|
|
393
|
+
assert isinstance(schema["patternProperties"], dict)
|
|
394
|
+
schema["patternProperties"][pattern] = _m2s(val, lpath, defs)
|
|
395
|
+
if addProp is None:
|
|
396
|
+
addProp = False
|
|
397
|
+
elif prop[0] == "$":
|
|
398
|
+
# NOTE should be ok if simple enough so that constant propagation removed it
|
|
399
|
+
raise Exception(f"JSON Schema: no support for property ref {prop} at {lpath}")
|
|
400
|
+
else: # standard property
|
|
401
|
+
required.append(prop)
|
|
402
|
+
properties[prop] = _m2s(val, lpath, defs)
|
|
403
|
+
|
|
404
|
+
# merge into schema
|
|
405
|
+
if properties:
|
|
406
|
+
schema["properties"] = properties
|
|
407
|
+
if required:
|
|
408
|
+
schema["required"] = required
|
|
409
|
+
if addProp is not None:
|
|
410
|
+
schema["additionalProperties"] = addProp
|
|
411
|
+
|
|
412
|
+
if ".schema" in model: # model extension, overrides generated stuff
|
|
413
|
+
sub = model[".schema"]
|
|
414
|
+
assert isinstance(sub, dict)
|
|
415
|
+
schema.update(sub)
|
|
416
|
+
|
|
417
|
+
# simplify final schema if possible
|
|
418
|
+
if schema == {}:
|
|
419
|
+
schema = True
|
|
420
|
+
elif "not" in schema and schema["not"] == {}:
|
|
421
|
+
if len(schema) == 1:
|
|
422
|
+
schema = False
|
|
423
|
+
else:
|
|
424
|
+
schema["not"] = True
|
|
425
|
+
|
|
426
|
+
return schema
|
|
427
|
+
|
|
428
|
+
def model2schema(model: JsonModel, path: ModelPath, defs: Symbols) -> JsonSchema:
|
|
429
|
+
"""Entry point to convert a model to a Schema."""
|
|
430
|
+
return _m2s(model, path, defs)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* # API
|
|
3
|
+
*
|
|
4
|
+
* ```c
|
|
5
|
+
* bool error;
|
|
6
|
+
* char *message;
|
|
7
|
+
*
|
|
8
|
+
* bool valid = check(json_value, "model-name", &error, &message);
|
|
9
|
+
*
|
|
10
|
+
* if (message)
|
|
11
|
+
* free(message), message = NULL;
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* - `error` is set of there was an error during initialization or if the model does not exists.
|
|
15
|
+
* - `message` is provided if a non NULL pointer is passed, and must be freed by the caller.
|
|
16
|
+
*/
|
|
17
|
+
bool
|
|
18
|
+
CHECK_FUNCTION_NAME(const json_t *val, const char *name, bool *error, char **reasons)
|
|
19
|
+
{
|
|
20
|
+
return jm_generic_entry(CHECK_FUNCTION_NAME_init, CHECK_FUNCTION_NAME_map, val, name, error, reasons);
|
|
21
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import * as runtime from "json_model_runtime"
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
--
|
|
2
|
+
-- JSON Model checking entry point
|
|
3
|
+
--
|
|
4
|
+
-- TODO INOUT rep?
|
|
5
|
+
CREATE OR REPLACE FUNCTION CHECK_FUNCTION_NAME(val JSONB, name TEXT, rep jm_report_entry[])
|
|
6
|
+
RETURNS BOOLEAN CALLED ON NULL INPUT IMMUTABLE PARALLEL SAFE AS $$
|
|
7
|
+
DECLARE
|
|
8
|
+
fun TEXT;
|
|
9
|
+
BEGIN
|
|
10
|
+
fun := CHECK_FUNCTION_NAME_map(name);
|
|
11
|
+
IF fun IS NULL THEN
|
|
12
|
+
RAISE EXCEPTION 'model for % not found', name;
|
|
13
|
+
END IF;
|
|
14
|
+
RETURN jm_call(fun, val, NULL, rep);
|
|
15
|
+
END;
|
|
16
|
+
$$ LANGUAGE plpgsql;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
def CHECK_FUNCTION_NAME_fun(name: str) -> CheckFun:
|
|
2
|
+
"""Return check function for JSON model name."""
|
|
3
|
+
return CHECK_FUNCTION_NAME_map[name]
|
|
4
|
+
|
|
5
|
+
# entry point for generated checkers
|
|
6
|
+
def CHECK_FUNCTION_NAME(val: Jsonable, name: str = "", rep: Report = None) -> bool:
|
|
7
|
+
"""Check val validity agains JSON Model name."""
|
|
8
|
+
if name not in CHECK_FUNCTION_NAME_map:
|
|
9
|
+
raise Exception(f"unexpected model name: {name}")
|
|
10
|
+
checker = CHECK_FUNCTION_NAME_map[name]
|
|
11
|
+
return checker(val, [], rep)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#! /bin/env python
|