teptris 0.3.20.0 → 0.3.22.0
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/ext/teptris_ext/engine/README.md +1 -1
- data/ext/teptris_ext/engine/src/include/teptris/version.h +2 -2
- data/ext/teptris_ext/engine/src/teptris/schema/formats.c +690 -0
- data/ext/teptris_ext/engine/src/teptris/schema/json_read.c +5 -2
- data/ext/teptris_ext/engine/src/teptris/schema/schema.c +309 -67
- data/ext/teptris_ext/engine/src/teptris/schema/schema_internal.h +62 -1
- data/ext/teptris_ext/engine/src/teptris/schema/validate.c +550 -40
- data/lib/teptris/version.rb +1 -1
- metadata +3 -2
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/* Schema compilation: JSON Schema text -> a compiled sch_node graph.
|
|
2
2
|
* See schema.h for the policy contract. Unsupported keywords that
|
|
3
3
|
* would silently change results are rejected here, at compile. */
|
|
4
|
+
#include <math.h>
|
|
4
5
|
#include <stdio.h>
|
|
5
6
|
#include <stdlib.h>
|
|
6
7
|
#include <string.h>
|
|
@@ -94,12 +95,18 @@ static jv *resolve_local(sctx *c, const char *ref) {
|
|
|
94
95
|
}
|
|
95
96
|
return cur;
|
|
96
97
|
}
|
|
97
|
-
/* plain-name fragment: an $anchor
|
|
98
|
+
/* plain-name fragment: an $anchor or a $dynamicAnchor (a plain
|
|
99
|
+
* $ref to either behaves the same way) */
|
|
98
100
|
for (size_t i = 0; i < c->n_anchors; i++) {
|
|
99
101
|
if (strcmp(c->anchors[i].name, ref + 1) == 0) {
|
|
100
102
|
return c->anchors[i].target;
|
|
101
103
|
}
|
|
102
104
|
}
|
|
105
|
+
for (size_t i = 0; i < c->n_dyn_anchors; i++) {
|
|
106
|
+
if (strcmp(c->dyn_anchors[i].name, ref + 1) == 0) {
|
|
107
|
+
return c->dyn_anchors[i].target;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
103
110
|
return NULL;
|
|
104
111
|
}
|
|
105
112
|
|
|
@@ -131,6 +138,26 @@ static void collect_anchors(sctx *c, jv *v) {
|
|
|
131
138
|
c->anchors[c->n_anchors].target = v;
|
|
132
139
|
c->n_anchors++;
|
|
133
140
|
}
|
|
141
|
+
if (strcmp(k, "$dynamicAnchor") == 0 && jv_kind_of(val) == JV_STRING) {
|
|
142
|
+
if (c->n_dyn_anchors == c->cap_dyn_anchors) {
|
|
143
|
+
c->cap_dyn_anchors =
|
|
144
|
+
c->cap_dyn_anchors == 0 ? 8 : c->cap_dyn_anchors * 2;
|
|
145
|
+
void *na = teptris_arena_alloc(
|
|
146
|
+
c->a, c->cap_dyn_anchors * sizeof(*c->dyn_anchors));
|
|
147
|
+
if (c->n_dyn_anchors > 0) {
|
|
148
|
+
memcpy(na, c->dyn_anchors,
|
|
149
|
+
c->n_dyn_anchors * sizeof(*c->dyn_anchors));
|
|
150
|
+
}
|
|
151
|
+
c->dyn_anchors = na;
|
|
152
|
+
}
|
|
153
|
+
c->dyn_anchors[c->n_dyn_anchors].name = jv_str(val);
|
|
154
|
+
c->dyn_anchors[c->n_dyn_anchors].target = v;
|
|
155
|
+
c->n_dyn_anchors++;
|
|
156
|
+
}
|
|
157
|
+
if (strcmp(k, "$recursiveAnchor") == 0 && jv_kind_of(val) == JV_BOOL &&
|
|
158
|
+
jv_bool(val) && c->rec_anchor_target == NULL) {
|
|
159
|
+
c->rec_anchor_target = v;
|
|
160
|
+
}
|
|
134
161
|
collect_anchors(c, val);
|
|
135
162
|
}
|
|
136
163
|
} else if (jv_kind_of(v) == JV_ARRAY) {
|
|
@@ -156,6 +183,33 @@ static int format_kind(const char *name) {
|
|
|
156
183
|
if (strcmp(name, "date-time") == 0) return FMT_DATETIME;
|
|
157
184
|
if (strcmp(name, "date") == 0) return FMT_DATE;
|
|
158
185
|
if (strcmp(name, "time") == 0) return FMT_TIME;
|
|
186
|
+
if (strcmp(name, "date-time-local") == 0 ||
|
|
187
|
+
strcmp(name, "partial-date-time") == 0) {
|
|
188
|
+
return FMT_DATETIME_LOCAL;
|
|
189
|
+
}
|
|
190
|
+
if (strcmp(name, "time-local") == 0 ||
|
|
191
|
+
strcmp(name, "partial-time") == 0) {
|
|
192
|
+
return FMT_TIME_LOCAL;
|
|
193
|
+
}
|
|
194
|
+
if (strcmp(name, "email") == 0) return FMT_EMAIL;
|
|
195
|
+
if (strcmp(name, "idn-email") == 0) return FMT_IDN_EMAIL;
|
|
196
|
+
if (strcmp(name, "hostname") == 0) return FMT_HOSTNAME;
|
|
197
|
+
if (strcmp(name, "idn-hostname") == 0) return FMT_IDN_HOSTNAME;
|
|
198
|
+
if (strcmp(name, "ipv4") == 0) return FMT_IPV4;
|
|
199
|
+
if (strcmp(name, "ipv6") == 0) return FMT_IPV6;
|
|
200
|
+
if (strcmp(name, "uri") == 0) return FMT_URI;
|
|
201
|
+
if (strcmp(name, "iri") == 0) return FMT_IRI;
|
|
202
|
+
if (strcmp(name, "uri-reference") == 0) return FMT_URI_REFERENCE;
|
|
203
|
+
if (strcmp(name, "iri-reference") == 0) return FMT_IRI_REFERENCE;
|
|
204
|
+
if (strcmp(name, "json-pointer") == 0) return FMT_JSON_POINTER;
|
|
205
|
+
if (strcmp(name, "relative-json-pointer") == 0)
|
|
206
|
+
return FMT_RELATIVE_JSON_POINTER;
|
|
207
|
+
if (strcmp(name, "regex") == 0) return FMT_REGEX;
|
|
208
|
+
if (strcmp(name, "uri-template") == 0) return FMT_URI_TEMPLATE;
|
|
209
|
+
if (strcmp(name, "uuid") == 0) return FMT_UUID;
|
|
210
|
+
if (strcmp(name, "duration") == 0) return FMT_DURATION;
|
|
211
|
+
if (strcmp(name, "semver") == 0) return FMT_SEMVER;
|
|
212
|
+
if (strcmp(name, "semver-requirement") == 0) return FMT_SEMVER_REQUIREMENT;
|
|
159
213
|
return FMT_NONE; /* other formats: annotations (host's choice) */
|
|
160
214
|
}
|
|
161
215
|
|
|
@@ -199,6 +253,80 @@ static bool compile_type(sctx *c, sch_node *n, jv *tv, const char *where) {
|
|
|
199
253
|
return false;
|
|
200
254
|
}
|
|
201
255
|
|
|
256
|
+
/* the suite writes count constraints as decimals ("maxLength": 3.0):
|
|
257
|
+
* a JSON number is valid there, so accept float tokens whose value is
|
|
258
|
+
* integral (a non-integral count cannot be satisfied; ignore it like
|
|
259
|
+
* an annotation would lie - reject the schema instead) */
|
|
260
|
+
static bool count_value(sctx *c, jv *val, long *out) {
|
|
261
|
+
if (jv_kind_of(val) == JV_INT) {
|
|
262
|
+
*out = (long)jv_int(val);
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
265
|
+
if (jv_kind_of(val) == JV_NUM) {
|
|
266
|
+
double d = jv_num(val);
|
|
267
|
+
if (d != d || d != (double)(long)d) {
|
|
268
|
+
sfail(c, "count constraint must be an integer", NULL);
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
*out = (long)d;
|
|
272
|
+
return true;
|
|
273
|
+
}
|
|
274
|
+
sfail(c, "count constraint must be an integer", NULL);
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
static bool grow_dep_list(sctx *c, sch_kschema **list, size_t *n, size_t *cap) {
|
|
279
|
+
if (*n == *cap) {
|
|
280
|
+
*cap = *cap == 0 ? 4 : *cap * 2;
|
|
281
|
+
sch_kschema *nl =
|
|
282
|
+
teptris_arena_alloc(c->a, *cap * sizeof(**list));
|
|
283
|
+
if (*n > 0) memcpy(nl, *list, *n * sizeof(**list));
|
|
284
|
+
*list = nl;
|
|
285
|
+
}
|
|
286
|
+
return true;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/* one "key": [names...] pair (dependentRequired, or dependencies'
|
|
290
|
+
* array form): the fake node reuses the required storage shape */
|
|
291
|
+
static bool sch_compile_dep_req(sctx *c, sch_node *n, const char *key,
|
|
292
|
+
jv *list) {
|
|
293
|
+
grow_dep_list(c, &n->dep_req, &n->n_dep_req, &c->cap_dep_req);
|
|
294
|
+
sch_node *fake = sch_node_new(c);
|
|
295
|
+
size_t dn = jv_item_count(list);
|
|
296
|
+
fake->required = teptris_arena_alloc(c->a, (dn ? dn : 1) * sizeof(char *));
|
|
297
|
+
for (size_t d = 0; d < dn; d++) {
|
|
298
|
+
jv *e = jv_item(list, d);
|
|
299
|
+
if (jv_kind_of(e) != JV_STRING) {
|
|
300
|
+
sfail(c, "dependency entries must be strings", key);
|
|
301
|
+
return false;
|
|
302
|
+
}
|
|
303
|
+
size_t slen = jv_str_len(e);
|
|
304
|
+
char *rb = teptris_arena_alloc(c->a, slen + 1);
|
|
305
|
+
memcpy(rb, jv_str(e), slen);
|
|
306
|
+
rb[slen] = '\0';
|
|
307
|
+
fake->required[d] = rb;
|
|
308
|
+
}
|
|
309
|
+
fake->n_req = dn;
|
|
310
|
+
n->dep_req[n->n_dep_req].key = key;
|
|
311
|
+
n->dep_req[n->n_dep_req].schema = fake;
|
|
312
|
+
n->n_dep_req++;
|
|
313
|
+
return true;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/* one "key": {schema} pair (dependentSchemas, or dependencies'
|
|
317
|
+
* schema form; a boolean is a schema) */
|
|
318
|
+
static bool sch_compile_dep_schema(sctx *c, sch_node *n, const char *key,
|
|
319
|
+
jv *dv) {
|
|
320
|
+
grow_dep_list(c, &n->dep_schemas, &n->n_dep_schemas,
|
|
321
|
+
&c->cap_dep_schemas);
|
|
322
|
+
sch_node *s = sch_compile_node(c, dv);
|
|
323
|
+
if (s == NULL && c->err[0] != '\0') return false;
|
|
324
|
+
n->dep_schemas[n->n_dep_schemas].key = key;
|
|
325
|
+
n->dep_schemas[n->n_dep_schemas].schema = s;
|
|
326
|
+
n->n_dep_schemas++;
|
|
327
|
+
return true;
|
|
328
|
+
}
|
|
329
|
+
|
|
202
330
|
sch_node *sch_compile_node(sctx *c, const jv *v) {
|
|
203
331
|
bool found, busy;
|
|
204
332
|
sch_node *memo_hit = memo_get(c, v, &found, &busy);
|
|
@@ -221,24 +349,10 @@ sch_node *sch_compile_node(sctx *c, const jv *v) {
|
|
|
221
349
|
sch_node *n = sch_node_new(c);
|
|
222
350
|
memo_put(c, v, n);
|
|
223
351
|
|
|
224
|
-
const char *unsupported[] = {
|
|
225
|
-
"unevaluatedProperties", "unevaluatedItems", "$dynamicRef",
|
|
226
|
-
"$dynamicAnchor", "$recursiveRef", "$recursiveAnchor", NULL
|
|
227
|
-
};
|
|
228
|
-
|
|
229
352
|
for (size_t i = 0; i < jv_member_count(v); i++) {
|
|
230
353
|
const char *k = jv_member_key(v, i);
|
|
231
354
|
jv *val = jv_member_val(v, i);
|
|
232
355
|
|
|
233
|
-
/* unsupported KNOWN keywords: reject (ignoring them would
|
|
234
|
-
* silently accept documents that should fail) */
|
|
235
|
-
for (int u = 0; unsupported[u] != NULL; u++) {
|
|
236
|
-
if (strcmp(k, unsupported[u]) == 0) {
|
|
237
|
-
sfail(c, "unsupported keyword", k);
|
|
238
|
-
return NULL;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
356
|
if (strcmp(k, "type") == 0) {
|
|
243
357
|
if (!compile_type(c, n, val, k)) return NULL;
|
|
244
358
|
} else if (strcmp(k, "$ref") == 0) {
|
|
@@ -302,6 +416,75 @@ sch_node *sch_compile_node(sctx *c, const jv *v) {
|
|
|
302
416
|
n->additional = sch_compile_node(c, val);
|
|
303
417
|
if (n->additional == NULL && c->err[0] != '\0') return NULL;
|
|
304
418
|
n->additional_set = true;
|
|
419
|
+
} else if (strcmp(k, "unevaluatedProperties") == 0) {
|
|
420
|
+
n->uneval_props = sch_compile_node(c, val);
|
|
421
|
+
if (n->uneval_props == NULL && c->err[0] != '\0') return NULL;
|
|
422
|
+
} else if (strcmp(k, "unevaluatedItems") == 0) {
|
|
423
|
+
n->uneval_items = sch_compile_node(c, val);
|
|
424
|
+
if (n->uneval_items == NULL && c->err[0] != '\0') return NULL;
|
|
425
|
+
} else if (strcmp(k, "$dynamicAnchor") == 0 &&
|
|
426
|
+
jv_kind_of(val) == JV_STRING) {
|
|
427
|
+
n->dynamic_anchor = jv_str(val);
|
|
428
|
+
} else if (strcmp(k, "$recursiveAnchor") == 0 &&
|
|
429
|
+
jv_kind_of(val) == JV_BOOL && jv_bool(val)) {
|
|
430
|
+
n->recursive_anchor = "$";
|
|
431
|
+
} else if (strcmp(k, "$dynamicRef") == 0 ||
|
|
432
|
+
strcmp(k, "$recursiveRef") == 0) {
|
|
433
|
+
if (jv_kind_of(val) != JV_STRING) {
|
|
434
|
+
sfail(c, "$dynamicRef/$recursiveRef must be a string", k);
|
|
435
|
+
return NULL;
|
|
436
|
+
}
|
|
437
|
+
const char *r = jv_str(val);
|
|
438
|
+
bool recursive = strcmp(k, "$recursiveRef") == 0;
|
|
439
|
+
/* $recursiveRef is always the bare "#"; a pointer-form
|
|
440
|
+
* $dynamicRef ("#/pointer") does not participate in
|
|
441
|
+
* dynamic scope resolution - the spec and the suite both
|
|
442
|
+
* read it exactly like a plain $ref - so only the
|
|
443
|
+
* plain-name fragment form reaches the scope machinery */
|
|
444
|
+
if (r[0] != '#' || (recursive && r[1] != '\0') ||
|
|
445
|
+
(!recursive && r[1] == '\0')) {
|
|
446
|
+
sfail(c,
|
|
447
|
+
"dynamic $refs are plain-name fragments "
|
|
448
|
+
"(\"#anchor\") or pointers (\"#/a/b\")",
|
|
449
|
+
r);
|
|
450
|
+
return NULL;
|
|
451
|
+
}
|
|
452
|
+
if (!recursive && strchr(r + 1, '/') != NULL) {
|
|
453
|
+
n->ref = sch_resolve_pointer(c, r);
|
|
454
|
+
if (n->ref == NULL) {
|
|
455
|
+
sfail(c, "unresolvable $dynamicRef", r);
|
|
456
|
+
return NULL;
|
|
457
|
+
}
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
/* the static fallback: the doc-wide anchor collection;
|
|
461
|
+
* the innermost scope wins at validation time */
|
|
462
|
+
jv *target = NULL;
|
|
463
|
+
if (recursive) {
|
|
464
|
+
target = c->rec_anchor_target != NULL
|
|
465
|
+
? c->rec_anchor_target
|
|
466
|
+
: c->root;
|
|
467
|
+
} else {
|
|
468
|
+
for (size_t d = 0; d < c->n_dyn_anchors && target == NULL; d++) {
|
|
469
|
+
if (strcmp(c->dyn_anchors[d].name, r + 1) == 0) {
|
|
470
|
+
target = c->dyn_anchors[d].target;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
if (target == NULL) {
|
|
474
|
+
sfail(c, "unresolvable $dynamicRef (no matching "
|
|
475
|
+
"$dynamicAnchor)",
|
|
476
|
+
r);
|
|
477
|
+
return NULL;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
n->dynamic_ref = jv_str(val);
|
|
481
|
+
n->dynamic_ref_target = sch_compile_node(c, target);
|
|
482
|
+
if (n->dynamic_ref_target == NULL && c->err[0] != '\0') {
|
|
483
|
+
return NULL;
|
|
484
|
+
}
|
|
485
|
+
} else if (strcmp(k, "contentSchema") == 0) {
|
|
486
|
+
n->content_schema = sch_compile_node(c, val);
|
|
487
|
+
if (n->content_schema == NULL && c->err[0] != '\0') return NULL;
|
|
305
488
|
} else if (strcmp(k, "required") == 0 &&
|
|
306
489
|
jv_kind_of(val) == JV_ARRAY) {
|
|
307
490
|
size_t cnt = jv_item_count(val);
|
|
@@ -323,90 +506,116 @@ sch_node *sch_compile_node(sctx *c, const jv *v) {
|
|
|
323
506
|
} else if (strcmp(k, "propertyNames") == 0) {
|
|
324
507
|
n->prop_names = sch_compile_node(c, val);
|
|
325
508
|
if (n->prop_names == NULL && c->err[0] != '\0') return NULL;
|
|
326
|
-
} else if (strcmp(k, "minProperties") == 0
|
|
327
|
-
|
|
328
|
-
n->min_props = (long)jv_int(val);
|
|
509
|
+
} else if (strcmp(k, "minProperties") == 0) {
|
|
510
|
+
if (!count_value(c, val, &n->min_props)) return NULL;
|
|
329
511
|
n->has_min_props = true;
|
|
330
|
-
} else if (strcmp(k, "maxProperties") == 0
|
|
331
|
-
|
|
332
|
-
n->max_props = (long)jv_int(val);
|
|
512
|
+
} else if (strcmp(k, "maxProperties") == 0) {
|
|
513
|
+
if (!count_value(c, val, &n->max_props)) return NULL;
|
|
333
514
|
n->has_max_props = true;
|
|
515
|
+
} else if (strcmp(k, "dependencies") == 0 &&
|
|
516
|
+
jv_kind_of(val) == JV_OBJECT) {
|
|
517
|
+
/* draft-07 dependencies: an array value is a
|
|
518
|
+
* dependentRequired pair, a schema value a
|
|
519
|
+
* dependentSchemas pair */
|
|
520
|
+
size_t cnt = jv_member_count(val);
|
|
521
|
+
for (size_t p = 0; p < cnt; p++) {
|
|
522
|
+
jv *dv = jv_member_val(val, p);
|
|
523
|
+
if (jv_kind_of(dv) == JV_ARRAY) {
|
|
524
|
+
if (!sch_compile_dep_req(c, n, jv_member_key(val, p), dv))
|
|
525
|
+
return NULL;
|
|
526
|
+
} else if (jv_kind_of(dv) == JV_OBJECT ||
|
|
527
|
+
jv_kind_of(dv) == JV_BOOL) {
|
|
528
|
+
if (!sch_compile_dep_schema(c, n, jv_member_key(val, p),
|
|
529
|
+
dv))
|
|
530
|
+
return NULL;
|
|
531
|
+
} else {
|
|
532
|
+
sfail(c, "dependencies values must be arrays or schemas",
|
|
533
|
+
k);
|
|
534
|
+
return NULL;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
334
537
|
} else if (strcmp(k, "dependentRequired") == 0 &&
|
|
335
538
|
jv_kind_of(val) == JV_OBJECT) {
|
|
336
539
|
size_t cnt = jv_member_count(val);
|
|
337
|
-
n->dep_req =
|
|
338
|
-
teptris_arena_alloc(c->a, (cnt ? cnt : 1) * sizeof(sch_kschema));
|
|
339
540
|
for (size_t p = 0; p < cnt; p++) {
|
|
340
541
|
jv *list = jv_member_val(val, p);
|
|
341
542
|
if (jv_kind_of(list) != JV_ARRAY) {
|
|
342
543
|
sfail(c, "dependentRequired values must be arrays", k);
|
|
343
544
|
return NULL;
|
|
344
545
|
}
|
|
345
|
-
n
|
|
346
|
-
|
|
347
|
-
* required list carries the dependency keys */
|
|
348
|
-
sch_node *fake = sch_node_new(c);
|
|
349
|
-
size_t dn = jv_item_count(list);
|
|
350
|
-
fake->required =
|
|
351
|
-
teptris_arena_alloc(c->a, (dn ? dn : 1) * sizeof(char *));
|
|
352
|
-
for (size_t d = 0; d < dn; d++) {
|
|
353
|
-
jv *e = jv_item(list, d);
|
|
354
|
-
if (jv_kind_of(e) != JV_STRING) {
|
|
355
|
-
sfail(c, "dependentRequired entries must be strings", k);
|
|
356
|
-
return NULL;
|
|
357
|
-
}
|
|
358
|
-
size_t slen = jv_str_len(e);
|
|
359
|
-
char *rb = teptris_arena_alloc(c->a, slen + 1);
|
|
360
|
-
memcpy(rb, jv_str(e), slen);
|
|
361
|
-
rb[slen] = '\0';
|
|
362
|
-
fake->required[d] = rb;
|
|
363
|
-
}
|
|
364
|
-
fake->n_req = dn;
|
|
365
|
-
n->dep_req[p].schema = fake;
|
|
546
|
+
if (!sch_compile_dep_req(c, n, jv_member_key(val, p), list))
|
|
547
|
+
return NULL;
|
|
366
548
|
}
|
|
367
|
-
n->n_dep_req = cnt;
|
|
368
549
|
} else if (strcmp(k, "dependentSchemas") == 0 &&
|
|
369
550
|
jv_kind_of(val) == JV_OBJECT) {
|
|
370
551
|
size_t cnt = jv_member_count(val);
|
|
371
|
-
n->dep_schemas = teptris_arena_alloc(
|
|
372
|
-
c->a, (cnt ? cnt : 1) * sizeof(sch_kschema));
|
|
373
552
|
for (size_t p = 0; p < cnt; p++) {
|
|
374
|
-
n
|
|
375
|
-
|
|
376
|
-
|
|
553
|
+
if (!sch_compile_dep_schema(c, n, jv_member_key(val, p),
|
|
554
|
+
jv_member_val(val, p)))
|
|
555
|
+
return NULL;
|
|
556
|
+
}
|
|
557
|
+
} else if (strcmp(k, "contentEncoding") == 0 &&
|
|
558
|
+
jv_kind_of(val) == JV_STRING) {
|
|
559
|
+
n->content_encoding = jv_str(val);
|
|
560
|
+
} else if (strcmp(k, "contentMediaType") == 0 &&
|
|
561
|
+
jv_kind_of(val) == JV_STRING) {
|
|
562
|
+
n->content_media_type = jv_str(val);
|
|
563
|
+
} else if (strcmp(k, "items") == 0 && jv_kind_of(val) == JV_ARRAY) {
|
|
564
|
+
/* draft-07 tuple form: positional prefixes; the union
|
|
565
|
+
* reads them exactly like prefixItems, with
|
|
566
|
+
* additionalItems covering anything beyond */
|
|
567
|
+
size_t cnt = jv_item_count(val);
|
|
568
|
+
size_t base = n->n_prefix;
|
|
569
|
+
size_t total = base + cnt;
|
|
570
|
+
sch_node **np = teptris_arena_alloc(
|
|
571
|
+
c->a, (total ? total : 1) * sizeof(sch_node *));
|
|
572
|
+
if (base > 0) memcpy(np, n->prefix, base * sizeof(sch_node *));
|
|
573
|
+
for (size_t p = 0; p < cnt; p++) {
|
|
574
|
+
np[base + p] = sch_compile_node(c, jv_item(val, p));
|
|
575
|
+
if (np[base + p] == NULL && c->err[0] != '\0') return NULL;
|
|
377
576
|
}
|
|
378
|
-
n->
|
|
577
|
+
n->prefix = np;
|
|
578
|
+
n->n_prefix = total;
|
|
379
579
|
} else if (strcmp(k, "items") == 0) {
|
|
380
580
|
n->items = sch_compile_node(c, val);
|
|
381
581
|
if (n->items == NULL && c->err[0] != '\0') return NULL;
|
|
382
582
|
n->items_set = true;
|
|
583
|
+
} else if (strcmp(k, "additionalItems") == 0) {
|
|
584
|
+
n->add_items = sch_compile_node(c, val);
|
|
585
|
+
if (n->add_items == NULL && c->err[0] != '\0') return NULL;
|
|
586
|
+
n->add_items_set = true;
|
|
383
587
|
} else if (strcmp(k, "prefixItems") == 0 &&
|
|
384
588
|
jv_kind_of(val) == JV_ARRAY) {
|
|
385
589
|
size_t cnt = jv_item_count(val);
|
|
386
|
-
|
|
590
|
+
size_t base = n->n_prefix;
|
|
591
|
+
size_t total = base + cnt;
|
|
592
|
+
sch_node **np = teptris_arena_alloc(
|
|
593
|
+
c->a, (total ? total : 1) * sizeof(sch_node *));
|
|
594
|
+
if (base > 0) memcpy(np, n->prefix, base * sizeof(sch_node *));
|
|
387
595
|
for (size_t p = 0; p < cnt; p++) {
|
|
388
|
-
|
|
389
|
-
if (
|
|
596
|
+
np[base + p] = sch_compile_node(c, jv_item(val, p));
|
|
597
|
+
if (np[base + p] == NULL && c->err[0] != '\0') return NULL;
|
|
390
598
|
}
|
|
391
|
-
n->
|
|
599
|
+
n->prefix = np;
|
|
600
|
+
n->n_prefix = total;
|
|
392
601
|
} else if (strcmp(k, "contains") == 0) {
|
|
393
602
|
n->contains = sch_compile_node(c, val);
|
|
394
603
|
if (n->contains == NULL && c->err[0] != '\0') return NULL;
|
|
395
604
|
n->has_contains = true;
|
|
396
|
-
} else if (strcmp(k, "minContains") == 0
|
|
397
|
-
n->min_contains
|
|
605
|
+
} else if (strcmp(k, "minContains") == 0) {
|
|
606
|
+
if (!count_value(c, val, &n->min_contains)) return NULL;
|
|
398
607
|
n->has_min_contains = true;
|
|
399
|
-
} else if (strcmp(k, "maxContains") == 0
|
|
400
|
-
n->max_contains
|
|
608
|
+
} else if (strcmp(k, "maxContains") == 0) {
|
|
609
|
+
if (!count_value(c, val, &n->max_contains)) return NULL;
|
|
401
610
|
n->has_max_contains = true;
|
|
402
611
|
} else if (strcmp(k, "uniqueItems") == 0 && jv_kind_of(val) == JV_BOOL) {
|
|
403
612
|
n->unique_items = jv_bool(val);
|
|
404
613
|
n->has_unique = true;
|
|
405
|
-
} else if (strcmp(k, "minItems") == 0
|
|
406
|
-
n->min_items
|
|
614
|
+
} else if (strcmp(k, "minItems") == 0) {
|
|
615
|
+
if (!count_value(c, val, &n->min_items)) return NULL;
|
|
407
616
|
n->has_min_items = true;
|
|
408
|
-
} else if (strcmp(k, "maxItems") == 0
|
|
409
|
-
n->max_items
|
|
617
|
+
} else if (strcmp(k, "maxItems") == 0) {
|
|
618
|
+
if (!count_value(c, val, &n->max_items)) return NULL;
|
|
410
619
|
n->has_max_items = true;
|
|
411
620
|
} else if (strcmp(k, "pattern") == 0 && jv_kind_of(val) == JV_STRING) {
|
|
412
621
|
char rerr[128] = {0};
|
|
@@ -416,11 +625,11 @@ sch_node *sch_compile_node(sctx *c, const jv *v) {
|
|
|
416
625
|
sfail(c, rerr[0] != '\0' ? rerr : "invalid pattern", k);
|
|
417
626
|
return NULL;
|
|
418
627
|
}
|
|
419
|
-
} else if (strcmp(k, "minLength") == 0
|
|
420
|
-
n->min_length
|
|
628
|
+
} else if (strcmp(k, "minLength") == 0) {
|
|
629
|
+
if (!count_value(c, val, &n->min_length)) return NULL;
|
|
421
630
|
n->has_min_length = true;
|
|
422
|
-
} else if (strcmp(k, "maxLength") == 0
|
|
423
|
-
n->max_length
|
|
631
|
+
} else if (strcmp(k, "maxLength") == 0) {
|
|
632
|
+
if (!count_value(c, val, &n->max_length)) return NULL;
|
|
424
633
|
n->has_max_length = true;
|
|
425
634
|
} else if (strcmp(k, "format") == 0 && jv_kind_of(val) == JV_STRING) {
|
|
426
635
|
n->format = format_kind(jv_str(val));
|
|
@@ -456,6 +665,9 @@ sch_node *sch_compile_node(sctx *c, const jv *v) {
|
|
|
456
665
|
if (jv_kind_of(val) == JV_INT) {
|
|
457
666
|
n->mult_is_int = true;
|
|
458
667
|
n->imult = jv_int(val);
|
|
668
|
+
} else if (sch_decimal_of_double(m, &n->mult_mant,
|
|
669
|
+
&n->mult_exp)) {
|
|
670
|
+
n->mult_exact = true;
|
|
459
671
|
}
|
|
460
672
|
} else if (strcmp(k, "allOf") == 0 && jv_kind_of(val) == JV_ARRAY) {
|
|
461
673
|
size_t cnt = jv_item_count(val);
|
|
@@ -504,6 +716,36 @@ sch_node *sch_compile_node(sctx *c, const jv *v) {
|
|
|
504
716
|
|
|
505
717
|
/* ------------------------------------------------------------ public -- */
|
|
506
718
|
|
|
719
|
+
/* the shortest decimal text that round-trips through d, read back as
|
|
720
|
+
* mant * 10^exp (the same decimal view serde_json/fraction-style
|
|
721
|
+
* exact arithmetic effectively uses) */
|
|
722
|
+
bool sch_decimal_of_double(double d, int64_t *mant, int *exp) {
|
|
723
|
+
if (!(d == d) || d == INFINITY || d == -INFINITY) return false;
|
|
724
|
+
char buf[64];
|
|
725
|
+
for (int prec = 1; prec <= 17; prec++) {
|
|
726
|
+
snprintf(buf, sizeof buf, "%.*g", prec, d);
|
|
727
|
+
if (strtod(buf, NULL) == d) break;
|
|
728
|
+
}
|
|
729
|
+
/* [+-] D[.DDD] [e[+-]NN] */
|
|
730
|
+
const char *p = buf;
|
|
731
|
+
if (*p == '+' || *p == '-') p++;
|
|
732
|
+
int64_t m = 0;
|
|
733
|
+
int e = 0;
|
|
734
|
+
for (; *p >= '0' && *p <= '9'; p++) m = m * 10 + (*p - '0');
|
|
735
|
+
if (*p == '.') {
|
|
736
|
+
for (p++; *p >= '0' && *p <= '9'; p++) {
|
|
737
|
+
m = m * 10 + (*p - '0');
|
|
738
|
+
e--;
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
if (*p == 'e' || *p == 'E') {
|
|
742
|
+
e += (int)strtol(p + 1, NULL, 10);
|
|
743
|
+
}
|
|
744
|
+
*mant = m;
|
|
745
|
+
*exp = e;
|
|
746
|
+
return true;
|
|
747
|
+
}
|
|
748
|
+
|
|
507
749
|
TEPTRIS_API teptris_status teptris_schema_compile(const char *json, size_t len,
|
|
508
750
|
struct teptris_schema **out,
|
|
509
751
|
char **diag_buf,
|
|
@@ -27,7 +27,31 @@ enum {
|
|
|
27
27
|
FMT_NONE = 0,
|
|
28
28
|
FMT_DATETIME,
|
|
29
29
|
FMT_DATE,
|
|
30
|
-
FMT_TIME
|
|
30
|
+
FMT_TIME,
|
|
31
|
+
FMT_DATETIME_LOCAL,
|
|
32
|
+
FMT_TIME_LOCAL,
|
|
33
|
+
/* the taplo parity set: every format the jsonschema crate (the
|
|
34
|
+
* engine behind taplo) asserts on; names outside this set are
|
|
35
|
+
* annotations on both sides */
|
|
36
|
+
FMT_EMAIL,
|
|
37
|
+
FMT_IDN_EMAIL,
|
|
38
|
+
FMT_HOSTNAME,
|
|
39
|
+
FMT_IDN_HOSTNAME,
|
|
40
|
+
FMT_IPV4,
|
|
41
|
+
FMT_IPV6,
|
|
42
|
+
FMT_URI,
|
|
43
|
+
FMT_IRI,
|
|
44
|
+
FMT_URI_REFERENCE,
|
|
45
|
+
FMT_IRI_REFERENCE,
|
|
46
|
+
FMT_JSON_POINTER,
|
|
47
|
+
FMT_RELATIVE_JSON_POINTER,
|
|
48
|
+
FMT_REGEX,
|
|
49
|
+
FMT_URI_TEMPLATE,
|
|
50
|
+
FMT_UUID,
|
|
51
|
+
FMT_DURATION,
|
|
52
|
+
/* taplo's own custom formats (registered by its schema cache) */
|
|
53
|
+
FMT_SEMVER,
|
|
54
|
+
FMT_SEMVER_REQUIREMENT
|
|
31
55
|
};
|
|
32
56
|
|
|
33
57
|
typedef struct sch_node sch_node;
|
|
@@ -80,6 +104,16 @@ struct sch_node {
|
|
|
80
104
|
size_t n_prefix;
|
|
81
105
|
sch_node *items;
|
|
82
106
|
bool items_set;
|
|
107
|
+
sch_node *add_items; /* additionalItems: applies beyond prefix */
|
|
108
|
+
bool add_items_set;
|
|
109
|
+
sch_node *uneval_props; /* unevaluatedProperties: frame-local */
|
|
110
|
+
sch_node *uneval_items; /* unevaluatedItems: frame-local */
|
|
111
|
+
sch_node *content_schema; /* contentSchema: asserts decoded content */
|
|
112
|
+
const char *dynamic_anchor; /* $dynamicAnchor name carried here */
|
|
113
|
+
const char *recursive_anchor; /* "$" when $recursiveAnchor is true */
|
|
114
|
+
const char *dynamic_ref; /* "#name" fragment for
|
|
115
|
+
* $dynamicRef / $recursiveRef */
|
|
116
|
+
sch_node *dynamic_ref_target; /* compile-resolved static fallback */
|
|
83
117
|
sch_node *contains;
|
|
84
118
|
long min_contains, max_contains;
|
|
85
119
|
bool has_contains, has_min_contains, has_max_contains;
|
|
@@ -93,6 +127,8 @@ struct sch_node {
|
|
|
93
127
|
long min_length, max_length;
|
|
94
128
|
bool has_min_length, has_max_length;
|
|
95
129
|
int format;
|
|
130
|
+
const char *content_encoding; /* enforced: "base64" */
|
|
131
|
+
const char *content_media_type; /* enforced: "application/json" */
|
|
96
132
|
|
|
97
133
|
/* numeric keywords: the int form is exact when the constraint was
|
|
98
134
|
* an exact integer token */
|
|
@@ -103,6 +139,11 @@ struct sch_node {
|
|
|
103
139
|
double fmult;
|
|
104
140
|
int64_t imult;
|
|
105
141
|
bool mult_is_int;
|
|
142
|
+
/* exact-decimal view of a non-integer multipleOf: the
|
|
143
|
+
* shortest-roundtrip mantissa and decimal exponent */
|
|
144
|
+
bool mult_exact;
|
|
145
|
+
int64_t mult_mant;
|
|
146
|
+
int mult_exp;
|
|
106
147
|
|
|
107
148
|
/* value constraints */
|
|
108
149
|
jv **enum_vals;
|
|
@@ -127,12 +168,22 @@ typedef struct {
|
|
|
127
168
|
bool in_progress;
|
|
128
169
|
} *memo;
|
|
129
170
|
size_t n_memo, cap_memo;
|
|
171
|
+
/* dependency-list capacities (grow-in-place across keywords) */
|
|
172
|
+
size_t cap_dep_req, cap_dep_schemas;
|
|
130
173
|
/* anchors: "$anchor": name -> subtree */
|
|
131
174
|
struct {
|
|
132
175
|
const char *name;
|
|
133
176
|
jv *target;
|
|
134
177
|
} *anchors;
|
|
135
178
|
size_t n_anchors, cap_anchors;
|
|
179
|
+
/* $dynamicAnchor / $recursiveAnchor:true targets (the static
|
|
180
|
+
* fallback for $dynamicRef / $recursiveRef) */
|
|
181
|
+
struct {
|
|
182
|
+
const char *name;
|
|
183
|
+
jv *target;
|
|
184
|
+
} *dyn_anchors;
|
|
185
|
+
size_t n_dyn_anchors, cap_dyn_anchors;
|
|
186
|
+
jv *rec_anchor_target;
|
|
136
187
|
char *err;
|
|
137
188
|
size_t err_cap;
|
|
138
189
|
} sctx;
|
|
@@ -170,10 +221,20 @@ struct sch_svalue {
|
|
|
170
221
|
size_t n_members;
|
|
171
222
|
sch_svalue **items;
|
|
172
223
|
size_t n_items;
|
|
224
|
+
bool has_offset; /* offset datetimes only (kind rules for formats) */
|
|
173
225
|
};
|
|
174
226
|
|
|
175
227
|
/* schema.c */
|
|
176
228
|
sch_node *sch_compile_node(sctx *c, const jv *v);
|
|
177
229
|
sch_node *sch_resolve_pointer(sctx *c, const char *pointer);
|
|
230
|
+
/* the shortest-roundtrip decimal view of a double: d = mant * 10^exp
|
|
231
|
+
* with |mant| < 10^18; false for non-finite d */
|
|
232
|
+
bool sch_decimal_of_double(double d, int64_t *mant, int *exp);
|
|
233
|
+
|
|
234
|
+
/* formats.c */
|
|
235
|
+
/* the string-side format battery (the taplo parity set); datetimes
|
|
236
|
+
* reach it through their canonical text like any other string */
|
|
237
|
+
bool sch_format_string_check(int format, const char *s, size_t len,
|
|
238
|
+
teptris_arena *scratch);
|
|
178
239
|
|
|
179
240
|
#endif
|