teptris 0.3.19.1-arm-linux → 0.3.21.0-arm-linux
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.
- checksums.yaml +4 -4
- data/Steepfile +1 -0
- data/ext/teptris_ext/engine/README.md +1 -1
- data/ext/teptris_ext/engine/src/include/teptris/schema.h +107 -0
- data/ext/teptris_ext/engine/src/include/teptris/version.h +2 -2
- data/ext/teptris_ext/engine/src/teptris/schema/formats.c +656 -0
- data/ext/teptris_ext/engine/src/teptris/schema/json_read.c +440 -0
- data/ext/teptris_ext/engine/src/teptris/schema/json_read.h +34 -0
- data/ext/teptris_ext/engine/src/teptris/schema/regex.c +1090 -0
- data/ext/teptris_ext/engine/src/teptris/schema/regex.h +25 -0
- data/ext/teptris_ext/engine/src/teptris/schema/schema.c +734 -0
- data/ext/teptris_ext/engine/src/teptris/schema/schema_internal.h +221 -0
- data/ext/teptris_ext/engine/src/teptris/schema/validate.c +1034 -0
- data/ext/teptris_ext/materialize.c +87 -1
- data/lib/teptris/3.3/teptris_ext.so +0 -0
- data/lib/teptris/3.4/teptris_ext.so +0 -0
- data/lib/teptris/4.0/teptris_ext.so +0 -0
- data/lib/teptris/schema.rb +64 -0
- data/lib/teptris/version.rb +1 -1
- data/lib/teptris.rb +1 -0
- data/sig/teptris.rbs +32 -0
- metadata +12 -2
|
@@ -0,0 +1,734 @@
|
|
|
1
|
+
/* Schema compilation: JSON Schema text -> a compiled sch_node graph.
|
|
2
|
+
* See schema.h for the policy contract. Unsupported keywords that
|
|
3
|
+
* would silently change results are rejected here, at compile. */
|
|
4
|
+
#include <math.h>
|
|
5
|
+
#include <stdio.h>
|
|
6
|
+
#include <stdlib.h>
|
|
7
|
+
#include <string.h>
|
|
8
|
+
|
|
9
|
+
#include "schema_internal.h"
|
|
10
|
+
#include "teptris/teptris.h"
|
|
11
|
+
|
|
12
|
+
static void sfail(sctx *c, const char *msg, const char *where) {
|
|
13
|
+
if (c->err != NULL && c->err[0] == '\0') {
|
|
14
|
+
snprintf(c->err, c->err_cap, "%s%s%s", msg,
|
|
15
|
+
where != NULL && where[0] != '\0' ? " at " : "",
|
|
16
|
+
where != NULL ? where : "");
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static sch_node *sch_node_new(sctx *c) {
|
|
21
|
+
sch_node *n = teptris_arena_alloc(c->a, sizeof(sch_node));
|
|
22
|
+
memset(n, 0, sizeof(*n));
|
|
23
|
+
n->types = ST_ANY;
|
|
24
|
+
return n;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/* ------------------------------------------------------------- memo -- */
|
|
28
|
+
|
|
29
|
+
static sch_node *memo_get(sctx *c, const jv *v, bool *found, bool *busy) {
|
|
30
|
+
*found = false;
|
|
31
|
+
*busy = false;
|
|
32
|
+
for (size_t i = 0; i < c->n_memo; i++) {
|
|
33
|
+
if (c->memo[i].src == v) {
|
|
34
|
+
*found = true;
|
|
35
|
+
*busy = c->memo[i].in_progress;
|
|
36
|
+
return c->memo[i].node;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return NULL;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static void memo_put(sctx *c, const jv *v, sch_node *n) {
|
|
43
|
+
if (c->n_memo == c->cap_memo) {
|
|
44
|
+
c->cap_memo = c->cap_memo == 0 ? 16 : c->cap_memo * 2;
|
|
45
|
+
void *nm = teptris_arena_alloc(c->a, c->cap_memo * sizeof(*c->memo));
|
|
46
|
+
if (c->n_memo > 0) memcpy(nm, c->memo, c->n_memo * sizeof(*c->memo));
|
|
47
|
+
c->memo = nm;
|
|
48
|
+
}
|
|
49
|
+
c->memo[c->n_memo].src = v;
|
|
50
|
+
c->memo[c->n_memo].node = n;
|
|
51
|
+
c->memo[c->n_memo].in_progress = true;
|
|
52
|
+
c->n_memo++;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static void memo_done(sctx *c, const jv *v) {
|
|
56
|
+
for (size_t i = 0; i < c->n_memo; i++) {
|
|
57
|
+
if (c->memo[i].src == v) {
|
|
58
|
+
c->memo[i].in_progress = false;
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/* --------------------------------------------------- JSON Pointer -- */
|
|
65
|
+
|
|
66
|
+
/* resolve "#", "#/a/b", or "#anchor" to a jv subtree */
|
|
67
|
+
static jv *resolve_local(sctx *c, const char *ref) {
|
|
68
|
+
if (ref[0] != '#') return NULL;
|
|
69
|
+
if (ref[1] == '\0') return c->root;
|
|
70
|
+
if (ref[1] == '/') {
|
|
71
|
+
/* JSON Pointer with ~0/~1 unescaping */
|
|
72
|
+
jv *cur = c->root;
|
|
73
|
+
const char *p = ref + 2;
|
|
74
|
+
while (*p != '\0') {
|
|
75
|
+
if (cur == NULL || jv_kind_of(cur) != JV_OBJECT) return NULL;
|
|
76
|
+
size_t tlen = 0;
|
|
77
|
+
const char *tok_end = strchr(p, '/');
|
|
78
|
+
tlen = tok_end != NULL ? (size_t)(tok_end - p) : strlen(p);
|
|
79
|
+
char tok[256];
|
|
80
|
+
if (tlen >= sizeof tok) return NULL;
|
|
81
|
+
size_t o = 0;
|
|
82
|
+
for (size_t i = 0; i < tlen; i++) {
|
|
83
|
+
if (p[i] == '~' && i + 1 < tlen) {
|
|
84
|
+
tok[o++] = p[i + 1] == '1' ? '/' : '~';
|
|
85
|
+
i++;
|
|
86
|
+
} else {
|
|
87
|
+
tok[o++] = p[i];
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
tok[o] = '\0';
|
|
91
|
+
cur = jv_member_get(cur, tok);
|
|
92
|
+
if (cur == NULL) return NULL;
|
|
93
|
+
p += tlen;
|
|
94
|
+
if (*p == '/') p++;
|
|
95
|
+
}
|
|
96
|
+
return cur;
|
|
97
|
+
}
|
|
98
|
+
/* plain-name fragment: an $anchor */
|
|
99
|
+
for (size_t i = 0; i < c->n_anchors; i++) {
|
|
100
|
+
if (strcmp(c->anchors[i].name, ref + 1) == 0) {
|
|
101
|
+
return c->anchors[i].target;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return NULL;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
sch_node *sch_resolve_pointer(sctx *c, const char *pointer) {
|
|
108
|
+
jv *target = resolve_local(c, pointer);
|
|
109
|
+
if (target == NULL) return NULL;
|
|
110
|
+
return sch_compile_node(c, target);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/* -------------------------------------------------- anchor collection */
|
|
114
|
+
|
|
115
|
+
static void collect_anchors(sctx *c, jv *v) {
|
|
116
|
+
if (v == NULL) return;
|
|
117
|
+
if (jv_kind_of(v) == JV_OBJECT) {
|
|
118
|
+
for (size_t i = 0; i < jv_member_count(v); i++) {
|
|
119
|
+
const char *k = jv_member_key(v, i);
|
|
120
|
+
jv *val = jv_member_val(v, i);
|
|
121
|
+
if (strcmp(k, "$anchor") == 0 && jv_kind_of(val) == JV_STRING) {
|
|
122
|
+
if (c->n_anchors == c->cap_anchors) {
|
|
123
|
+
c->cap_anchors = c->cap_anchors == 0 ? 8 : c->cap_anchors * 2;
|
|
124
|
+
void *na =
|
|
125
|
+
teptris_arena_alloc(c->a, c->cap_anchors * sizeof(*c->anchors));
|
|
126
|
+
if (c->n_anchors > 0) {
|
|
127
|
+
memcpy(na, c->anchors, c->n_anchors * sizeof(*c->anchors));
|
|
128
|
+
}
|
|
129
|
+
c->anchors = na;
|
|
130
|
+
}
|
|
131
|
+
c->anchors[c->n_anchors].name = jv_str(val);
|
|
132
|
+
c->anchors[c->n_anchors].target = v;
|
|
133
|
+
c->n_anchors++;
|
|
134
|
+
}
|
|
135
|
+
collect_anchors(c, val);
|
|
136
|
+
}
|
|
137
|
+
} else if (jv_kind_of(v) == JV_ARRAY) {
|
|
138
|
+
for (size_t i = 0; i < jv_item_count(v); i++) {
|
|
139
|
+
collect_anchors(c, jv_item(v, i));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/* ----------------------------------------------------------- compile -- */
|
|
145
|
+
|
|
146
|
+
static int type_bit(const char *name) {
|
|
147
|
+
if (strcmp(name, "string") == 0) return ST_STRING;
|
|
148
|
+
if (strcmp(name, "integer") == 0) return ST_INTEGER;
|
|
149
|
+
if (strcmp(name, "number") == 0) return ST_NUMBER;
|
|
150
|
+
if (strcmp(name, "boolean") == 0) return ST_BOOLEAN;
|
|
151
|
+
if (strcmp(name, "object") == 0) return ST_OBJECT;
|
|
152
|
+
if (strcmp(name, "array") == 0) return ST_ARRAY;
|
|
153
|
+
return 0;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
static int format_kind(const char *name) {
|
|
157
|
+
if (strcmp(name, "date-time") == 0) return FMT_DATETIME;
|
|
158
|
+
if (strcmp(name, "date") == 0) return FMT_DATE;
|
|
159
|
+
if (strcmp(name, "time") == 0) return FMT_TIME;
|
|
160
|
+
if (strcmp(name, "email") == 0) return FMT_EMAIL;
|
|
161
|
+
if (strcmp(name, "idn-email") == 0) return FMT_IDN_EMAIL;
|
|
162
|
+
if (strcmp(name, "hostname") == 0) return FMT_HOSTNAME;
|
|
163
|
+
if (strcmp(name, "idn-hostname") == 0) return FMT_IDN_HOSTNAME;
|
|
164
|
+
if (strcmp(name, "ipv4") == 0) return FMT_IPV4;
|
|
165
|
+
if (strcmp(name, "ipv6") == 0) return FMT_IPV6;
|
|
166
|
+
if (strcmp(name, "uri") == 0) return FMT_URI;
|
|
167
|
+
if (strcmp(name, "iri") == 0) return FMT_IRI;
|
|
168
|
+
if (strcmp(name, "uri-reference") == 0) return FMT_URI_REFERENCE;
|
|
169
|
+
if (strcmp(name, "iri-reference") == 0) return FMT_IRI_REFERENCE;
|
|
170
|
+
if (strcmp(name, "json-pointer") == 0) return FMT_JSON_POINTER;
|
|
171
|
+
if (strcmp(name, "relative-json-pointer") == 0)
|
|
172
|
+
return FMT_RELATIVE_JSON_POINTER;
|
|
173
|
+
if (strcmp(name, "regex") == 0) return FMT_REGEX;
|
|
174
|
+
if (strcmp(name, "uri-template") == 0) return FMT_URI_TEMPLATE;
|
|
175
|
+
if (strcmp(name, "uuid") == 0) return FMT_UUID;
|
|
176
|
+
if (strcmp(name, "duration") == 0) return FMT_DURATION;
|
|
177
|
+
if (strcmp(name, "semver") == 0) return FMT_SEMVER;
|
|
178
|
+
if (strcmp(name, "semver-requirement") == 0) return FMT_SEMVER_REQUIREMENT;
|
|
179
|
+
return FMT_NONE; /* other formats: annotations (host's choice) */
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
static bool compile_type(sctx *c, sch_node *n, jv *tv, const char *where) {
|
|
183
|
+
if (jv_kind_of(tv) == JV_STRING) {
|
|
184
|
+
int bit = type_bit(jv_str(tv));
|
|
185
|
+
if (bit == 0) {
|
|
186
|
+
sfail(c, "unknown type name", where);
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
n->types = (unsigned)bit;
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
if (jv_kind_of(tv) == JV_ARRAY) {
|
|
193
|
+
unsigned mask = 0;
|
|
194
|
+
bool had_null = false;
|
|
195
|
+
for (size_t i = 0; i < jv_item_count(tv); i++) {
|
|
196
|
+
jv *e = jv_item(tv, i);
|
|
197
|
+
if (jv_kind_of(e) == JV_NULL) {
|
|
198
|
+
had_null = true; /* policy 1: null matches no TOML value */
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
if (jv_kind_of(e) != JV_STRING) {
|
|
202
|
+
sfail(c, "type entries must be strings", where);
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
int bit = type_bit(jv_str(e));
|
|
206
|
+
if (bit == 0) {
|
|
207
|
+
sfail(c, "unknown type name", where);
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
mask |= (unsigned)bit;
|
|
211
|
+
}
|
|
212
|
+
n->types = mask != 0 ? mask : 0;
|
|
213
|
+
if (had_null && mask == 0) {
|
|
214
|
+
n->is_false = true; /* "type": "null" alone: unsatisfiable */
|
|
215
|
+
}
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
sfail(c, "type must be a string or array of strings", where);
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
static bool grow_dep_list(sctx *c, sch_kschema **list, size_t *n, size_t *cap) {
|
|
223
|
+
if (*n == *cap) {
|
|
224
|
+
*cap = *cap == 0 ? 4 : *cap * 2;
|
|
225
|
+
sch_kschema *nl =
|
|
226
|
+
teptris_arena_alloc(c->a, *cap * sizeof(**list));
|
|
227
|
+
if (*n > 0) memcpy(nl, *list, *n * sizeof(**list));
|
|
228
|
+
*list = nl;
|
|
229
|
+
}
|
|
230
|
+
return true;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/* one "key": [names...] pair (dependentRequired, or dependencies'
|
|
234
|
+
* array form): the fake node reuses the required storage shape */
|
|
235
|
+
static bool sch_compile_dep_req(sctx *c, sch_node *n, const char *key,
|
|
236
|
+
jv *list) {
|
|
237
|
+
grow_dep_list(c, &n->dep_req, &n->n_dep_req, &c->cap_dep_req);
|
|
238
|
+
sch_node *fake = sch_node_new(c);
|
|
239
|
+
size_t dn = jv_item_count(list);
|
|
240
|
+
fake->required = teptris_arena_alloc(c->a, (dn ? dn : 1) * sizeof(char *));
|
|
241
|
+
for (size_t d = 0; d < dn; d++) {
|
|
242
|
+
jv *e = jv_item(list, d);
|
|
243
|
+
if (jv_kind_of(e) != JV_STRING) {
|
|
244
|
+
sfail(c, "dependency entries must be strings", key);
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
size_t slen = jv_str_len(e);
|
|
248
|
+
char *rb = teptris_arena_alloc(c->a, slen + 1);
|
|
249
|
+
memcpy(rb, jv_str(e), slen);
|
|
250
|
+
rb[slen] = '\0';
|
|
251
|
+
fake->required[d] = rb;
|
|
252
|
+
}
|
|
253
|
+
fake->n_req = dn;
|
|
254
|
+
n->dep_req[n->n_dep_req].key = key;
|
|
255
|
+
n->dep_req[n->n_dep_req].schema = fake;
|
|
256
|
+
n->n_dep_req++;
|
|
257
|
+
return true;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/* one "key": {schema} pair (dependentSchemas, or dependencies'
|
|
261
|
+
* schema form; a boolean is a schema) */
|
|
262
|
+
static bool sch_compile_dep_schema(sctx *c, sch_node *n, const char *key,
|
|
263
|
+
jv *dv) {
|
|
264
|
+
grow_dep_list(c, &n->dep_schemas, &n->n_dep_schemas,
|
|
265
|
+
&c->cap_dep_schemas);
|
|
266
|
+
sch_node *s = sch_compile_node(c, dv);
|
|
267
|
+
if (s == NULL && c->err[0] != '\0') return false;
|
|
268
|
+
n->dep_schemas[n->n_dep_schemas].key = key;
|
|
269
|
+
n->dep_schemas[n->n_dep_schemas].schema = s;
|
|
270
|
+
n->n_dep_schemas++;
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
sch_node *sch_compile_node(sctx *c, const jv *v) {
|
|
275
|
+
bool found, busy;
|
|
276
|
+
sch_node *memo_hit = memo_get(c, v, &found, &busy);
|
|
277
|
+
if (found) {
|
|
278
|
+
return memo_hit; /* cycles resolve: the node fills in place */
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (jv_kind_of(v) == JV_BOOL) {
|
|
282
|
+
sch_node *n = sch_node_new(c);
|
|
283
|
+
memo_put(c, v, n);
|
|
284
|
+
n->is_false = !jv_bool(v);
|
|
285
|
+
memo_done(c, v);
|
|
286
|
+
return n;
|
|
287
|
+
}
|
|
288
|
+
if (jv_kind_of(v) != JV_OBJECT) {
|
|
289
|
+
sfail(c, "a schema must be an object or boolean", NULL);
|
|
290
|
+
return NULL;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
sch_node *n = sch_node_new(c);
|
|
294
|
+
memo_put(c, v, n);
|
|
295
|
+
|
|
296
|
+
const char *unsupported[] = {
|
|
297
|
+
"unevaluatedProperties", "unevaluatedItems", "$dynamicRef",
|
|
298
|
+
"$dynamicAnchor", "$recursiveRef", "$recursiveAnchor", NULL
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
for (size_t i = 0; i < jv_member_count(v); i++) {
|
|
302
|
+
const char *k = jv_member_key(v, i);
|
|
303
|
+
jv *val = jv_member_val(v, i);
|
|
304
|
+
|
|
305
|
+
/* unsupported KNOWN keywords: reject (ignoring them would
|
|
306
|
+
* silently accept documents that should fail) */
|
|
307
|
+
for (int u = 0; unsupported[u] != NULL; u++) {
|
|
308
|
+
if (strcmp(k, unsupported[u]) == 0) {
|
|
309
|
+
sfail(c, "unsupported keyword", k);
|
|
310
|
+
return NULL;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if (strcmp(k, "type") == 0) {
|
|
315
|
+
if (!compile_type(c, n, val, k)) return NULL;
|
|
316
|
+
} else if (strcmp(k, "$ref") == 0) {
|
|
317
|
+
if (jv_kind_of(val) != JV_STRING) {
|
|
318
|
+
sfail(c, "$ref must be a string", k);
|
|
319
|
+
return NULL;
|
|
320
|
+
}
|
|
321
|
+
const char *r = jv_str(val);
|
|
322
|
+
if (r[0] != '#') {
|
|
323
|
+
sfail(c, "remote $ref is not supported (a parser library never touches the network)", r);
|
|
324
|
+
return NULL;
|
|
325
|
+
}
|
|
326
|
+
n->ref = sch_resolve_pointer(c, r);
|
|
327
|
+
if (n->ref == NULL) {
|
|
328
|
+
sfail(c, "unresolvable $ref", r);
|
|
329
|
+
return NULL;
|
|
330
|
+
}
|
|
331
|
+
} else if (strcmp(k, "enum") == 0) {
|
|
332
|
+
if (jv_kind_of(val) != JV_ARRAY) {
|
|
333
|
+
sfail(c, "enum must be an array", k);
|
|
334
|
+
return NULL;
|
|
335
|
+
}
|
|
336
|
+
n->enum_vals = teptris_arena_alloc(
|
|
337
|
+
c->a, (jv_item_count(val) ? jv_item_count(val) : 1) * sizeof(jv *));
|
|
338
|
+
n->n_enum = jv_item_count(val);
|
|
339
|
+
for (size_t e = 0; e < jv_item_count(val); e++) {
|
|
340
|
+
n->enum_vals[e] = jv_item(val, e);
|
|
341
|
+
}
|
|
342
|
+
} else if (strcmp(k, "const") == 0) {
|
|
343
|
+
n->const_val = val;
|
|
344
|
+
n->has_const = true;
|
|
345
|
+
} else if (strcmp(k, "properties") == 0 &&
|
|
346
|
+
jv_kind_of(val) == JV_OBJECT) {
|
|
347
|
+
size_t cnt = jv_member_count(val);
|
|
348
|
+
n->props = teptris_arena_alloc(c->a, (cnt ? cnt : 1) * sizeof(sch_prop));
|
|
349
|
+
for (size_t p = 0; p < cnt; p++) {
|
|
350
|
+
n->props[p].name = jv_member_key(val, p);
|
|
351
|
+
n->props[p].schema = sch_compile_node(c, jv_member_val(val, p));
|
|
352
|
+
if (n->props[p].schema == NULL && c->err[0] != '\0') return NULL;
|
|
353
|
+
}
|
|
354
|
+
n->n_props = cnt;
|
|
355
|
+
} else if (strcmp(k, "patternProperties") == 0 &&
|
|
356
|
+
jv_kind_of(val) == JV_OBJECT) {
|
|
357
|
+
size_t cnt = jv_member_count(val);
|
|
358
|
+
n->pprops = teptris_arena_alloc(c->a, (cnt ? cnt : 1) * sizeof(sch_pprop));
|
|
359
|
+
for (size_t p = 0; p < cnt; p++) {
|
|
360
|
+
char rerr[128] = {0};
|
|
361
|
+
struct sch_regex *re = sch_regex_compile(
|
|
362
|
+
c->a, jv_member_key(val, p), strlen(jv_member_key(val, p)),
|
|
363
|
+
rerr, sizeof rerr);
|
|
364
|
+
if (re == NULL) {
|
|
365
|
+
sfail(c, rerr[0] != '\0' ? rerr : "invalid pattern", k);
|
|
366
|
+
return NULL;
|
|
367
|
+
}
|
|
368
|
+
n->pprops[p].re = re;
|
|
369
|
+
n->pprops[p].schema = sch_compile_node(c, jv_member_val(val, p));
|
|
370
|
+
if (n->pprops[p].schema == NULL && c->err[0] != '\0') return NULL;
|
|
371
|
+
}
|
|
372
|
+
n->n_pprops = cnt;
|
|
373
|
+
} else if (strcmp(k, "additionalProperties") == 0) {
|
|
374
|
+
n->additional = sch_compile_node(c, val);
|
|
375
|
+
if (n->additional == NULL && c->err[0] != '\0') return NULL;
|
|
376
|
+
n->additional_set = true;
|
|
377
|
+
} else if (strcmp(k, "required") == 0 &&
|
|
378
|
+
jv_kind_of(val) == JV_ARRAY) {
|
|
379
|
+
size_t cnt = jv_item_count(val);
|
|
380
|
+
n->required = teptris_arena_alloc(c->a, (cnt ? cnt : 1) * sizeof(char *));
|
|
381
|
+
for (size_t r = 0; r < cnt; r++) {
|
|
382
|
+
jv *e = jv_item(val, r);
|
|
383
|
+
if (jv_kind_of(e) != JV_STRING) {
|
|
384
|
+
sfail(c, "required entries must be strings", k);
|
|
385
|
+
return NULL;
|
|
386
|
+
}
|
|
387
|
+
/* NUL-terminated key for strcmp lookups */
|
|
388
|
+
size_t slen = jv_str_len(e);
|
|
389
|
+
char *rb = teptris_arena_alloc(c->a, slen + 1);
|
|
390
|
+
memcpy(rb, jv_str(e), slen);
|
|
391
|
+
rb[slen] = '\0';
|
|
392
|
+
n->required[r] = rb;
|
|
393
|
+
}
|
|
394
|
+
n->n_req = cnt;
|
|
395
|
+
} else if (strcmp(k, "propertyNames") == 0) {
|
|
396
|
+
n->prop_names = sch_compile_node(c, val);
|
|
397
|
+
if (n->prop_names == NULL && c->err[0] != '\0') return NULL;
|
|
398
|
+
} else if (strcmp(k, "minProperties") == 0 &&
|
|
399
|
+
jv_kind_of(val) == JV_INT) {
|
|
400
|
+
n->min_props = (long)jv_int(val);
|
|
401
|
+
n->has_min_props = true;
|
|
402
|
+
} else if (strcmp(k, "maxProperties") == 0 &&
|
|
403
|
+
jv_kind_of(val) == JV_INT) {
|
|
404
|
+
n->max_props = (long)jv_int(val);
|
|
405
|
+
n->has_max_props = true;
|
|
406
|
+
} else if (strcmp(k, "dependencies") == 0 &&
|
|
407
|
+
jv_kind_of(val) == JV_OBJECT) {
|
|
408
|
+
/* draft-07 dependencies: an array value is a
|
|
409
|
+
* dependentRequired pair, a schema value a
|
|
410
|
+
* dependentSchemas pair */
|
|
411
|
+
size_t cnt = jv_member_count(val);
|
|
412
|
+
for (size_t p = 0; p < cnt; p++) {
|
|
413
|
+
jv *dv = jv_member_val(val, p);
|
|
414
|
+
if (jv_kind_of(dv) == JV_ARRAY) {
|
|
415
|
+
if (!sch_compile_dep_req(c, n, jv_member_key(val, p), dv))
|
|
416
|
+
return NULL;
|
|
417
|
+
} else if (jv_kind_of(dv) == JV_OBJECT ||
|
|
418
|
+
jv_kind_of(dv) == JV_BOOL) {
|
|
419
|
+
if (!sch_compile_dep_schema(c, n, jv_member_key(val, p),
|
|
420
|
+
dv))
|
|
421
|
+
return NULL;
|
|
422
|
+
} else {
|
|
423
|
+
sfail(c, "dependencies values must be arrays or schemas",
|
|
424
|
+
k);
|
|
425
|
+
return NULL;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
} else if (strcmp(k, "dependentRequired") == 0 &&
|
|
429
|
+
jv_kind_of(val) == JV_OBJECT) {
|
|
430
|
+
size_t cnt = jv_member_count(val);
|
|
431
|
+
for (size_t p = 0; p < cnt; p++) {
|
|
432
|
+
jv *list = jv_member_val(val, p);
|
|
433
|
+
if (jv_kind_of(list) != JV_ARRAY) {
|
|
434
|
+
sfail(c, "dependentRequired values must be arrays", k);
|
|
435
|
+
return NULL;
|
|
436
|
+
}
|
|
437
|
+
if (!sch_compile_dep_req(c, n, jv_member_key(val, p), list))
|
|
438
|
+
return NULL;
|
|
439
|
+
}
|
|
440
|
+
} else if (strcmp(k, "dependentSchemas") == 0 &&
|
|
441
|
+
jv_kind_of(val) == JV_OBJECT) {
|
|
442
|
+
size_t cnt = jv_member_count(val);
|
|
443
|
+
for (size_t p = 0; p < cnt; p++) {
|
|
444
|
+
if (!sch_compile_dep_schema(c, n, jv_member_key(val, p),
|
|
445
|
+
jv_member_val(val, p)))
|
|
446
|
+
return NULL;
|
|
447
|
+
}
|
|
448
|
+
} else if (strcmp(k, "contentEncoding") == 0 &&
|
|
449
|
+
jv_kind_of(val) == JV_STRING) {
|
|
450
|
+
n->content_encoding = jv_str(val);
|
|
451
|
+
} else if (strcmp(k, "contentMediaType") == 0 &&
|
|
452
|
+
jv_kind_of(val) == JV_STRING) {
|
|
453
|
+
n->content_media_type = jv_str(val);
|
|
454
|
+
} else if (strcmp(k, "items") == 0 && jv_kind_of(val) == JV_ARRAY) {
|
|
455
|
+
/* draft-07 tuple form: positional prefixes; the union
|
|
456
|
+
* reads them exactly like prefixItems, with
|
|
457
|
+
* additionalItems covering anything beyond */
|
|
458
|
+
size_t cnt = jv_item_count(val);
|
|
459
|
+
size_t base = n->n_prefix;
|
|
460
|
+
size_t total = base + cnt;
|
|
461
|
+
sch_node **np = teptris_arena_alloc(
|
|
462
|
+
c->a, (total ? total : 1) * sizeof(sch_node *));
|
|
463
|
+
if (base > 0) memcpy(np, n->prefix, base * sizeof(sch_node *));
|
|
464
|
+
for (size_t p = 0; p < cnt; p++) {
|
|
465
|
+
np[base + p] = sch_compile_node(c, jv_item(val, p));
|
|
466
|
+
if (np[base + p] == NULL && c->err[0] != '\0') return NULL;
|
|
467
|
+
}
|
|
468
|
+
n->prefix = np;
|
|
469
|
+
n->n_prefix = total;
|
|
470
|
+
} else if (strcmp(k, "items") == 0) {
|
|
471
|
+
n->items = sch_compile_node(c, val);
|
|
472
|
+
if (n->items == NULL && c->err[0] != '\0') return NULL;
|
|
473
|
+
n->items_set = true;
|
|
474
|
+
} else if (strcmp(k, "additionalItems") == 0) {
|
|
475
|
+
n->add_items = sch_compile_node(c, val);
|
|
476
|
+
if (n->add_items == NULL && c->err[0] != '\0') return NULL;
|
|
477
|
+
n->add_items_set = true;
|
|
478
|
+
} else if (strcmp(k, "prefixItems") == 0 &&
|
|
479
|
+
jv_kind_of(val) == JV_ARRAY) {
|
|
480
|
+
size_t cnt = jv_item_count(val);
|
|
481
|
+
size_t base = n->n_prefix;
|
|
482
|
+
size_t total = base + cnt;
|
|
483
|
+
sch_node **np = teptris_arena_alloc(
|
|
484
|
+
c->a, (total ? total : 1) * sizeof(sch_node *));
|
|
485
|
+
if (base > 0) memcpy(np, n->prefix, base * sizeof(sch_node *));
|
|
486
|
+
for (size_t p = 0; p < cnt; p++) {
|
|
487
|
+
np[base + p] = sch_compile_node(c, jv_item(val, p));
|
|
488
|
+
if (np[base + p] == NULL && c->err[0] != '\0') return NULL;
|
|
489
|
+
}
|
|
490
|
+
n->prefix = np;
|
|
491
|
+
n->n_prefix = total;
|
|
492
|
+
} else if (strcmp(k, "contains") == 0) {
|
|
493
|
+
n->contains = sch_compile_node(c, val);
|
|
494
|
+
if (n->contains == NULL && c->err[0] != '\0') return NULL;
|
|
495
|
+
n->has_contains = true;
|
|
496
|
+
} else if (strcmp(k, "minContains") == 0 && jv_kind_of(val) == JV_INT) {
|
|
497
|
+
n->min_contains = (long)jv_int(val);
|
|
498
|
+
n->has_min_contains = true;
|
|
499
|
+
} else if (strcmp(k, "maxContains") == 0 && jv_kind_of(val) == JV_INT) {
|
|
500
|
+
n->max_contains = (long)jv_int(val);
|
|
501
|
+
n->has_max_contains = true;
|
|
502
|
+
} else if (strcmp(k, "uniqueItems") == 0 && jv_kind_of(val) == JV_BOOL) {
|
|
503
|
+
n->unique_items = jv_bool(val);
|
|
504
|
+
n->has_unique = true;
|
|
505
|
+
} else if (strcmp(k, "minItems") == 0 && jv_kind_of(val) == JV_INT) {
|
|
506
|
+
n->min_items = (long)jv_int(val);
|
|
507
|
+
n->has_min_items = true;
|
|
508
|
+
} else if (strcmp(k, "maxItems") == 0 && jv_kind_of(val) == JV_INT) {
|
|
509
|
+
n->max_items = (long)jv_int(val);
|
|
510
|
+
n->has_max_items = true;
|
|
511
|
+
} else if (strcmp(k, "pattern") == 0 && jv_kind_of(val) == JV_STRING) {
|
|
512
|
+
char rerr[128] = {0};
|
|
513
|
+
n->pattern = sch_regex_compile(c->a, jv_str(val), jv_str_len(val),
|
|
514
|
+
rerr, sizeof rerr);
|
|
515
|
+
if (n->pattern == NULL) {
|
|
516
|
+
sfail(c, rerr[0] != '\0' ? rerr : "invalid pattern", k);
|
|
517
|
+
return NULL;
|
|
518
|
+
}
|
|
519
|
+
} else if (strcmp(k, "minLength") == 0 && jv_kind_of(val) == JV_INT) {
|
|
520
|
+
n->min_length = (long)jv_int(val);
|
|
521
|
+
n->has_min_length = true;
|
|
522
|
+
} else if (strcmp(k, "maxLength") == 0 && jv_kind_of(val) == JV_INT) {
|
|
523
|
+
n->max_length = (long)jv_int(val);
|
|
524
|
+
n->has_max_length = true;
|
|
525
|
+
} else if (strcmp(k, "format") == 0 && jv_kind_of(val) == JV_STRING) {
|
|
526
|
+
n->format = format_kind(jv_str(val));
|
|
527
|
+
} else if ((strcmp(k, "minimum") == 0 || strcmp(k, "maximum") == 0 ||
|
|
528
|
+
strcmp(k, "exclusiveMinimum") == 0 ||
|
|
529
|
+
strcmp(k, "exclusiveMaximum") == 0) &&
|
|
530
|
+
jv_kind_of(val) == JV_INT) {
|
|
531
|
+
if (strcmp(k, "minimum") == 0) {
|
|
532
|
+
n->fmin = (double)jv_int(val); n->imin = jv_int(val); n->has_min = true;
|
|
533
|
+
} else if (strcmp(k, "maximum") == 0) {
|
|
534
|
+
n->fmax = (double)jv_int(val); n->imax = jv_int(val); n->has_max = true;
|
|
535
|
+
} else if (strcmp(k, "exclusiveMinimum") == 0) {
|
|
536
|
+
n->fex_min = (double)jv_int(val); n->iex_min = jv_int(val); n->has_ex_min = true;
|
|
537
|
+
} else {
|
|
538
|
+
n->fex_max = (double)jv_int(val); n->iex_max = jv_int(val); n->has_ex_max = true;
|
|
539
|
+
}
|
|
540
|
+
} else if ((strcmp(k, "minimum") == 0 || strcmp(k, "maximum") == 0 ||
|
|
541
|
+
strcmp(k, "exclusiveMinimum") == 0 ||
|
|
542
|
+
strcmp(k, "exclusiveMaximum") == 0) &&
|
|
543
|
+
jv_kind_of(val) == JV_NUM) {
|
|
544
|
+
if (strcmp(k, "minimum") == 0) { n->fmin = jv_num(val); n->has_min = true; }
|
|
545
|
+
else if (strcmp(k, "maximum") == 0) { n->fmax = jv_num(val); n->has_max = true; }
|
|
546
|
+
else if (strcmp(k, "exclusiveMinimum") == 0) { n->fex_min = jv_num(val); n->has_ex_min = true; }
|
|
547
|
+
else { n->fex_max = jv_num(val); n->has_ex_max = true; }
|
|
548
|
+
} else if (strcmp(k, "multipleOf") == 0) {
|
|
549
|
+
double m = jv_num(val);
|
|
550
|
+
if (!(m > 0.0)) {
|
|
551
|
+
sfail(c, "multipleOf must be > 0", k);
|
|
552
|
+
return NULL;
|
|
553
|
+
}
|
|
554
|
+
n->has_mult = true;
|
|
555
|
+
n->fmult = m;
|
|
556
|
+
if (jv_kind_of(val) == JV_INT) {
|
|
557
|
+
n->mult_is_int = true;
|
|
558
|
+
n->imult = jv_int(val);
|
|
559
|
+
} else if (sch_decimal_of_double(m, &n->mult_mant,
|
|
560
|
+
&n->mult_exp)) {
|
|
561
|
+
n->mult_exact = true;
|
|
562
|
+
}
|
|
563
|
+
} else if (strcmp(k, "allOf") == 0 && jv_kind_of(val) == JV_ARRAY) {
|
|
564
|
+
size_t cnt = jv_item_count(val);
|
|
565
|
+
n->all_of = teptris_arena_alloc(c->a, (cnt ? cnt : 1) * sizeof(sch_node *));
|
|
566
|
+
for (size_t p = 0; p < cnt; p++) {
|
|
567
|
+
n->all_of[p] = sch_compile_node(c, jv_item(val, p));
|
|
568
|
+
if (n->all_of[p] == NULL && c->err[0] != '\0') return NULL;
|
|
569
|
+
}
|
|
570
|
+
n->n_all = cnt;
|
|
571
|
+
} else if (strcmp(k, "anyOf") == 0 && jv_kind_of(val) == JV_ARRAY) {
|
|
572
|
+
size_t cnt = jv_item_count(val);
|
|
573
|
+
n->any_of = teptris_arena_alloc(c->a, (cnt ? cnt : 1) * sizeof(sch_node *));
|
|
574
|
+
for (size_t p = 0; p < cnt; p++) {
|
|
575
|
+
n->any_of[p] = sch_compile_node(c, jv_item(val, p));
|
|
576
|
+
if (n->any_of[p] == NULL && c->err[0] != '\0') return NULL;
|
|
577
|
+
}
|
|
578
|
+
n->n_any = cnt;
|
|
579
|
+
} else if (strcmp(k, "oneOf") == 0 && jv_kind_of(val) == JV_ARRAY) {
|
|
580
|
+
size_t cnt = jv_item_count(val);
|
|
581
|
+
n->one_of = teptris_arena_alloc(c->a, (cnt ? cnt : 1) * sizeof(sch_node *));
|
|
582
|
+
for (size_t p = 0; p < cnt; p++) {
|
|
583
|
+
n->one_of[p] = sch_compile_node(c, jv_item(val, p));
|
|
584
|
+
if (n->one_of[p] == NULL && c->err[0] != '\0') return NULL;
|
|
585
|
+
}
|
|
586
|
+
n->n_one = cnt;
|
|
587
|
+
} else if (strcmp(k, "not") == 0) {
|
|
588
|
+
n->_not = sch_compile_node(c, val);
|
|
589
|
+
if (n->_not == NULL && c->err[0] != '\0') return NULL;
|
|
590
|
+
} else if (strcmp(k, "if") == 0) {
|
|
591
|
+
n->if_s = sch_compile_node(c, val);
|
|
592
|
+
if (n->if_s == NULL && c->err[0] != '\0') return NULL;
|
|
593
|
+
} else if (strcmp(k, "then") == 0) {
|
|
594
|
+
n->then_s = sch_compile_node(c, val);
|
|
595
|
+
if (n->then_s == NULL && c->err[0] != '\0') return NULL;
|
|
596
|
+
} else if (strcmp(k, "else") == 0) {
|
|
597
|
+
n->else_s = sch_compile_node(c, val);
|
|
598
|
+
if (n->else_s == NULL && c->err[0] != '\0') return NULL;
|
|
599
|
+
}
|
|
600
|
+
/* everything else: annotations, $schema/$id/$comment/$defs/
|
|
601
|
+
* definitions/$anchor (containers resolved via $ref), unknown
|
|
602
|
+
* keywords (extensions per the spec): all ignored */
|
|
603
|
+
}
|
|
604
|
+
memo_done(c, v);
|
|
605
|
+
return n;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/* ------------------------------------------------------------ public -- */
|
|
609
|
+
|
|
610
|
+
/* the shortest decimal text that round-trips through d, read back as
|
|
611
|
+
* mant * 10^exp (the same decimal view serde_json/fraction-style
|
|
612
|
+
* exact arithmetic effectively uses) */
|
|
613
|
+
bool sch_decimal_of_double(double d, int64_t *mant, int *exp) {
|
|
614
|
+
if (!(d == d) || d == INFINITY || d == -INFINITY) return false;
|
|
615
|
+
char buf[64];
|
|
616
|
+
for (int prec = 1; prec <= 17; prec++) {
|
|
617
|
+
snprintf(buf, sizeof buf, "%.*g", prec, d);
|
|
618
|
+
if (strtod(buf, NULL) == d) break;
|
|
619
|
+
}
|
|
620
|
+
/* [+-] D[.DDD] [e[+-]NN] */
|
|
621
|
+
const char *p = buf;
|
|
622
|
+
if (*p == '+' || *p == '-') p++;
|
|
623
|
+
int64_t m = 0;
|
|
624
|
+
int e = 0;
|
|
625
|
+
for (; *p >= '0' && *p <= '9'; p++) m = m * 10 + (*p - '0');
|
|
626
|
+
if (*p == '.') {
|
|
627
|
+
for (p++; *p >= '0' && *p <= '9'; p++) {
|
|
628
|
+
m = m * 10 + (*p - '0');
|
|
629
|
+
e--;
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
if (*p == 'e' || *p == 'E') {
|
|
633
|
+
e += (int)strtol(p + 1, NULL, 10);
|
|
634
|
+
}
|
|
635
|
+
*mant = m;
|
|
636
|
+
*exp = e;
|
|
637
|
+
return true;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
TEPTRIS_API teptris_status teptris_schema_compile(const char *json, size_t len,
|
|
641
|
+
struct teptris_schema **out,
|
|
642
|
+
char **diag_buf,
|
|
643
|
+
size_t *diag_len) {
|
|
644
|
+
if (json == NULL || out == NULL) {
|
|
645
|
+
return TEPTRIS_ERR_ARG;
|
|
646
|
+
}
|
|
647
|
+
if (diag_buf != NULL) *diag_buf = NULL;
|
|
648
|
+
if (diag_len != NULL) *diag_len = 0;
|
|
649
|
+
|
|
650
|
+
struct teptris_schema *sch = malloc(sizeof(*sch));
|
|
651
|
+
if (sch == NULL) return TEPTRIS_ERR_ALLOC;
|
|
652
|
+
memset(sch, 0, sizeof(*sch));
|
|
653
|
+
teptris_arena_init(&sch->arena);
|
|
654
|
+
|
|
655
|
+
char jerr[160] = {0};
|
|
656
|
+
jv *root = json_read(&sch->arena, json, len, jerr, sizeof jerr);
|
|
657
|
+
if (root == NULL) {
|
|
658
|
+
if (diag_buf != NULL) {
|
|
659
|
+
char diag[256];
|
|
660
|
+
snprintf(diag, sizeof diag, "{\"error\":%s}",
|
|
661
|
+
jerr[0] != '\0' ? jerr : "invalid schema JSON");
|
|
662
|
+
/* wrap the message as a JSON string */
|
|
663
|
+
char quoted[288];
|
|
664
|
+
size_t o = 0;
|
|
665
|
+
quoted[o++] = '"';
|
|
666
|
+
for (const char *p = jerr[0] != '\0' ? jerr : "invalid schema JSON";
|
|
667
|
+
*p != '\0' && o < sizeof quoted - 2; p++) {
|
|
668
|
+
if (*p == '"') quoted[o++] = '\\';
|
|
669
|
+
quoted[o++] = *p;
|
|
670
|
+
}
|
|
671
|
+
quoted[o++] = '"';
|
|
672
|
+
quoted[o] = '\0';
|
|
673
|
+
snprintf(diag, sizeof diag, "{\"error\":%s}", quoted);
|
|
674
|
+
size_t dlen = strlen(diag);
|
|
675
|
+
char *buf = malloc(dlen + 1);
|
|
676
|
+
if (buf != NULL) {
|
|
677
|
+
memcpy(buf, diag, dlen + 1);
|
|
678
|
+
*diag_buf = buf;
|
|
679
|
+
if (diag_len != NULL) *diag_len = dlen;
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
teptris_arena_destroy(&sch->arena);
|
|
683
|
+
free(sch);
|
|
684
|
+
return TEPTRIS_ERR_ARG;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
sctx c;
|
|
688
|
+
memset(&c, 0, sizeof(c));
|
|
689
|
+
c.a = &sch->arena;
|
|
690
|
+
c.root = root;
|
|
691
|
+
c.err = sch->err;
|
|
692
|
+
c.err_cap = sizeof sch->err;
|
|
693
|
+
sch->err[0] = '\0';
|
|
694
|
+
|
|
695
|
+
collect_anchors(&c, root);
|
|
696
|
+
sch_node *compiled = sch_compile_node(&c, root);
|
|
697
|
+
if (compiled == NULL) {
|
|
698
|
+
if (diag_buf != NULL) {
|
|
699
|
+
char quoted[288];
|
|
700
|
+
size_t o = 0;
|
|
701
|
+
quoted[o++] = '"';
|
|
702
|
+
const char *msg = sch->err[0] != '\0' ? sch->err : "invalid schema";
|
|
703
|
+
for (const char *p = msg; *p != '\0' && o < sizeof quoted - 2; p++) {
|
|
704
|
+
if (*p == '"') quoted[o++] = '\\';
|
|
705
|
+
quoted[o++] = *p;
|
|
706
|
+
}
|
|
707
|
+
quoted[o++] = '"';
|
|
708
|
+
quoted[o] = '\0';
|
|
709
|
+
char diag[320];
|
|
710
|
+
snprintf(diag, sizeof diag, "{\"error\":%s}", quoted);
|
|
711
|
+
size_t dlen = strlen(diag);
|
|
712
|
+
char *buf = malloc(dlen + 1);
|
|
713
|
+
if (buf != NULL) {
|
|
714
|
+
memcpy(buf, diag, dlen + 1);
|
|
715
|
+
*diag_buf = buf;
|
|
716
|
+
if (diag_len != NULL) *diag_len = dlen;
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
teptris_arena_destroy(&sch->arena);
|
|
720
|
+
free(sch);
|
|
721
|
+
return TEPTRIS_ERR_ARG;
|
|
722
|
+
}
|
|
723
|
+
sch->root = compiled;
|
|
724
|
+
*out = sch;
|
|
725
|
+
return TEPTRIS_OK;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
TEPTRIS_API void teptris_diag_free(char *buf) { free(buf); }
|
|
729
|
+
|
|
730
|
+
TEPTRIS_API void teptris_schema_free(struct teptris_schema *sch) {
|
|
731
|
+
if (sch == NULL) return;
|
|
732
|
+
teptris_arena_destroy(&sch->arena);
|
|
733
|
+
free(sch);
|
|
734
|
+
}
|