yeptris 0.6.58.1-arm-linux-eabihf → 0.6.60.1-arm-linux-eabihf
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/yeptris_native/version.h +2 -2
- data/lib/yeptris/3.4/native.so +0 -0
- data/lib/yeptris/document.rb +15 -0
- data/lib/yeptris/ffi.rb +112 -0
- data/lib/yeptris/psych/visitors.rb +16 -6
- data/lib/yeptris/psych.rb +11 -5
- data/lib/yeptris/validation.rb +320 -0
- data/lib/yeptris/yaml.rb +128 -6
- data/lib/yeptris.rb +2 -1
- data/libyeptris.so +0 -0
- data/vendor/libyeptris/CMakeLists.txt +1 -1
- data/vendor/libyeptris/src/CMakeLists.txt +8 -0
- data/vendor/libyeptris/src/include/yeptris/emit.h +30 -2
- data/vendor/libyeptris/src/include/yeptris/parse.h +52 -0
- data/vendor/libyeptris/src/include/yeptris/resolve.h +8 -0
- data/vendor/libyeptris/src/include/yeptris/validate.h +259 -15
- data/vendor/libyeptris/src/include/yeptris/values.h +8 -2
- data/vendor/libyeptris/src/yeptris/cbor/cbor.h +10 -0
- data/vendor/libyeptris/src/yeptris/cbor/decode.c +5 -7
- data/vendor/libyeptris/src/yeptris/cbor/encode.c +9 -9
- data/vendor/libyeptris/src/yeptris/cbor/from_marshal.c +1 -1
- data/vendor/libyeptris/src/yeptris/common/error.h +2 -0
- data/vendor/libyeptris/src/yeptris/doc.h +20 -0
- data/vendor/libyeptris/src/yeptris/emit/emit.c +85 -13
- data/vendor/libyeptris/src/yeptris/emit/writer.c +158 -642
- data/vendor/libyeptris/src/yeptris/emit/writer.h +15 -1
- data/vendor/libyeptris/src/yeptris/events/push.c +28 -3
- data/vendor/libyeptris/src/yeptris/events/values.c +24 -0
- data/vendor/libyeptris/src/yeptris/marshal/to_json.c +650 -0
- data/vendor/libyeptris/src/yeptris/parse/engine.c +121 -40
- data/vendor/libyeptris/src/yeptris/parse/stream_iter.c +326 -0
- data/vendor/libyeptris/src/yeptris/parse.c +181 -46
- data/vendor/libyeptris/src/yeptris/resolve/compat11.c +1 -1
- data/vendor/libyeptris/src/yeptris/resolve/core12.c +11 -3
- data/vendor/libyeptris/src/yeptris/resolve/full12.c +201 -0
- data/vendor/libyeptris/src/yeptris/resolve/json12.c +12 -6
- data/vendor/libyeptris/src/yeptris/resolve/resolver.h +20 -0
- data/vendor/libyeptris/src/yeptris/validate/compile.c +306 -0
- data/vendor/libyeptris/src/yeptris/validate/internal.h +255 -10
- data/vendor/libyeptris/src/yeptris/validate/jsonschema.c +1025 -125
- data/vendor/libyeptris/src/yeptris/validate/output.c +246 -0
- data/vendor/libyeptris/src/yeptris/validate/regexp.c +791 -163
- data/vendor/libyeptris/src/yeptris/validate/registry.c +118 -0
- data/vendor/libyeptris/src/yeptris/validate/resources.c +601 -0
- data/vendor/libyeptris/src/yeptris/validate/stream.c +1155 -0
- data/vendor/libyeptris/src/yeptris/validate/validate.c +330 -226
- metadata +10 -1
|
@@ -1,26 +1,75 @@
|
|
|
1
1
|
/* jsonschema.c — the JSON Schema draft 2020-12 keyword subset
|
|
2
|
-
* (#882 slice 2).
|
|
2
|
+
* (#882 slice 2; #885 item 2 sweep; the 2020-12 sota sweep).
|
|
3
3
|
*
|
|
4
4
|
* The schema and the target are both parsed with the same engine; the
|
|
5
5
|
* walk applies the schema tree to the instance tree and emits
|
|
6
6
|
* diagnostics at instance JSON-pointer paths. Subset: type, enum,
|
|
7
|
-
* const, pattern, minimum/maximum/exclusive*,
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
7
|
+
* const, pattern, minimum/maximum/exclusive*, multipleOf (libm-free
|
|
8
|
+
* quotient integrality), minLength/maxLength (code points),
|
|
9
|
+
* minItems/maxItems, uniqueItems, contains (+ minContains/maxContains
|
|
10
|
+
* via suppressed probes), properties, required, patternProperties
|
|
11
|
+
* (the pattern kernel over KEY strings), propertyNames (the schema
|
|
12
|
+
* applies to the key string itself), minProperties/maxProperties,
|
|
13
|
+
* additionalProperties (bool or schema; excludes properties AND
|
|
14
|
+
* patternProperties matches), dependentRequired, dependentSchemas,
|
|
15
|
+
* if/then/else (the if-schema rides a suppressed probe), items
|
|
16
|
+
* (+ prefixItems; the draft-07 array form rides as prefixItems),
|
|
17
|
+
* allOf/anyOf/oneOf/not, unevaluatedProperties/unevaluatedItems (the
|
|
18
|
+
* annotation model, below), $ref, $dynamicRef (below), boolean
|
|
19
|
+
* schemas. Unknown keywords, and known keywords whose values cannot
|
|
20
|
+
* be read, are IGNORED (2020-12's posture) — an unreadable constraint
|
|
21
|
+
* never guesses.
|
|
22
|
+
*
|
|
23
|
+
* THE ANNOTATION MODEL (2020-12's unevaluated*): every application of
|
|
24
|
+
* a schema object to an instance location returns (a) its validity —
|
|
25
|
+
* one fail_mark comparison around the application, unsuppressed emits
|
|
26
|
+
* increment the mark — and (b) the properties/items it EVALUATED.
|
|
27
|
+
* Annotations collect upward only: a schema object's set is its own
|
|
28
|
+
* adjacent keywords' appends (properties, patternProperties,
|
|
29
|
+
* additionalProperties, contains, items, prefixItems,
|
|
30
|
+
* unevaluatedProperties/unevaluatedItems themselves) plus whatever its
|
|
31
|
+
* in-place applicators brought back (allOf branches always;
|
|
32
|
+
* anyOf/oneOf SUCCESSFUL branches only — a failed branch's annotations
|
|
33
|
+
* are released; the if-probe when it holds, plus the branch taken;
|
|
34
|
+
* $ref/$dynamicRef targets; dependentSchemas when triggered). `not`
|
|
35
|
+
* and the probes never annotate outward. Applications to CHILD
|
|
36
|
+
* locations (properties' values, items, propertyNames' keys, the
|
|
37
|
+
* unevaluated* subschemas) release their sets — an annotation never
|
|
38
|
+
* crosses an instance-location boundary. unevaluatedProperties /
|
|
39
|
+
* unevaluatedItems run LAST in their schema object, whatever the
|
|
40
|
+
* document order, and flag the properties/items absent from the
|
|
41
|
+
* collected set.
|
|
42
|
+
*
|
|
43
|
+
* REFERENCES (the sota sweep): resolution rides the compiled schema's
|
|
44
|
+
* identifier map (resources.c): $id base-URI composition (RFC 3986
|
|
45
|
+
* path merge, fragment-free canonical forms), $anchor and
|
|
46
|
+
* $dynamicAnchor plain-name fragments scoped to their nearest $id
|
|
47
|
+
* resource, JSON Pointer fragments with the percent-encoding layer
|
|
48
|
+
* (%25 -> '%', then ~0/~1), and $ref targets anywhere in the schema's
|
|
49
|
+
* own identifier space. No network: a target outside that space is
|
|
50
|
+
* unresolvable (DIAG_REF), never fetched. $dynamicRef rides the
|
|
51
|
+
* draft's runtime dynamic scope (core spec 8.2.3.2 + appendix C): a
|
|
52
|
+
* plain-name fragment that statically resolves to a $dynamicAnchor is
|
|
53
|
+
* replaced by the identically-named $dynamicAnchor of the OUTERMOST
|
|
54
|
+
* schema resource in the dynamic scope — the stack of resources
|
|
55
|
+
* entered during evaluation; probes (anyOf/oneOf/not/if trials) enter
|
|
56
|
+
* no scope, exactly as their annotations are discarded.
|
|
57
|
+
*
|
|
58
|
+
* Carried out of the subset (documented in
|
|
59
|
+
* test/conformance/jsonschema/MATRIX.md): remote targets (the
|
|
60
|
+
* metaschema and the suite's HTTP fixtures — no network corpus is
|
|
61
|
+
* vendored), contentMediaType/contentSchema (annotation-only in
|
|
62
|
+
* 2020-12).
|
|
15
63
|
*
|
|
16
64
|
* Instance types ride the core-schema resolution (the YAML->JSON data
|
|
17
65
|
* model): "y" is boolean, 1.0 is integer (2020-12: zero fractional
|
|
18
66
|
* part), floats and ints are both numbers.
|
|
19
67
|
*
|
|
20
68
|
* Guards: the walk depth is the DOM's own parse-bounded depth; a
|
|
21
|
-
* consecutive-$ref chain (schema-only cycles that consume
|
|
22
|
-
* structure) caps at 64 expansions and reports DIAG_REF.
|
|
23
|
-
* is bounded; allocation failure flags the context and
|
|
69
|
+
* consecutive-$ref/$dynamicRef chain (schema-only cycles that consume
|
|
70
|
+
* no instance structure) caps at 64 expansions and reports DIAG_REF.
|
|
71
|
+
* Every array is bounded; allocation failure flags the context and
|
|
72
|
+
* unwinds.
|
|
24
73
|
*/
|
|
25
74
|
|
|
26
75
|
#include <stdio.h>
|
|
@@ -36,11 +85,40 @@
|
|
|
36
85
|
#define JS_MAX_REF_CHAIN 64
|
|
37
86
|
#define JS_MAX_TOKENS 256
|
|
38
87
|
#define JS_MAX_UNIQUE 4096 /* uniqueItems' hash table cap; over: O(n^2) probe */
|
|
88
|
+
#define JS_MAX_EVALS 16384 /* the annotation arena's entry cap; over: OOM */
|
|
89
|
+
|
|
90
|
+
/* One application's result: validity plus the annotation window it
|
|
91
|
+
* owns — [pmark, plen) of the evaluated-property key-node arena and
|
|
92
|
+
* [imark, ilen) of the evaluated-item index arena. Absorbing a result
|
|
93
|
+
* into the caller's set is a NO-OP (the window simply stays); dropping
|
|
94
|
+
* it is two assignments (the release). Zero-copy in both directions. */
|
|
95
|
+
typedef struct {
|
|
96
|
+
int valid;
|
|
97
|
+
size_t pmark, plen;
|
|
98
|
+
size_t imark, ilen;
|
|
99
|
+
} js_eval;
|
|
39
100
|
|
|
40
101
|
typedef struct {
|
|
41
102
|
yep_vctx v; /* instance-side context (path, report, guards) */
|
|
42
103
|
const yep_dom* sd; /* the schema document's tree */
|
|
43
104
|
uint32_t sroot; /* the schema's document root */
|
|
105
|
+
/* the compiled schema's retained pattern programs (NULL: the
|
|
106
|
+
* one-shot face — every search compiles fresh, identical verdicts) */
|
|
107
|
+
const yep_re_cache* cache;
|
|
108
|
+
/* the compiled schema's identifier map (NULL: only bare "#/..."
|
|
109
|
+
* pointers against the document root resolve) */
|
|
110
|
+
const js_resources* res;
|
|
111
|
+
/* the dynamic scope: resource indices, outermost first — pushed
|
|
112
|
+
* as evaluation enters each schema object's resource (dedup'd
|
|
113
|
+
* against the top), popped on exit; probes push nothing */
|
|
114
|
+
uint32_t scope[YEP_JS_MAX_SCOPE];
|
|
115
|
+
int scope_n;
|
|
116
|
+
/* the annotation arenas: evaluated property KEY node ids and item
|
|
117
|
+
* indices, appended during the walk, released per application */
|
|
118
|
+
uint32_t* ep;
|
|
119
|
+
size_t ep_len, ep_cap;
|
|
120
|
+
uint32_t* ei;
|
|
121
|
+
size_t ei_len, ei_cap;
|
|
44
122
|
} js_ctx;
|
|
45
123
|
|
|
46
124
|
/* ---- schema-side facts -------------------------------------------------- */
|
|
@@ -220,45 +298,192 @@ static int inst_seq_count(const yep_dom* d, uint32_t id, uint32_t* count) {
|
|
|
220
298
|
return 1;
|
|
221
299
|
}
|
|
222
300
|
|
|
301
|
+
/* Finds an instance pair by key bytes (scalar keys only): the VALUE
|
|
302
|
+
* node id, UINT32_MAX absent. *key_out receives the KEY node id when
|
|
303
|
+
* non-NULL (patternProperties/propertyNames walk the key itself). */
|
|
304
|
+
static uint32_t inst_find_key(const yep_dom* d, uint32_t map_id, const char* name, size_t nl,
|
|
305
|
+
uint32_t* key_out) {
|
|
306
|
+
const yep_dnode* n = yep_dom_node(d, map_id);
|
|
307
|
+
if (n == NULL || n->kind != YEP_DOM_MAPPING) {
|
|
308
|
+
return UINT32_MAX;
|
|
309
|
+
}
|
|
310
|
+
uint32_t k = n->first_child;
|
|
311
|
+
while (k != UINT32_MAX) {
|
|
312
|
+
uint32_t v = yep_dom_node(d, k) ? yep_dom_node(d, k)->next_sibling : UINT32_MAX;
|
|
313
|
+
const yep_dnode* kn = yep_dom_node(d, k);
|
|
314
|
+
if (kn != NULL && kn->kind == YEP_DOM_SCALAR) {
|
|
315
|
+
yep_view kv = yep_dom_view(d, kn->value);
|
|
316
|
+
if (kv.len == nl && (nl == 0 || memcmp(kv.p, name, nl) == 0)) {
|
|
317
|
+
if (key_out != NULL) {
|
|
318
|
+
*key_out = k;
|
|
319
|
+
}
|
|
320
|
+
return v;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
k = v != UINT32_MAX && yep_dom_node(d, v) ? yep_dom_node(d, v)->next_sibling : UINT32_MAX;
|
|
324
|
+
}
|
|
325
|
+
return UINT32_MAX;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/* ---- the annotation arenas ---------------------------------------------- */
|
|
329
|
+
|
|
330
|
+
/* Appends an evaluated property key node; -1 over cap (the walk's OOM
|
|
331
|
+
* flag fires — bounded memory is the law, never a silent guess). */
|
|
332
|
+
static int ep_append(js_ctx* j, uint32_t key_node) {
|
|
333
|
+
if (j->ep_len == j->ep_cap) {
|
|
334
|
+
const yep_allocator* sys = yep_system_allocator();
|
|
335
|
+
size_t ncap = j->ep_cap ? j->ep_cap * 2 : 64;
|
|
336
|
+
uint32_t* np = yep_alloc(sys, ncap * sizeof(uint32_t));
|
|
337
|
+
if (np == NULL) {
|
|
338
|
+
j->v.oom = 1;
|
|
339
|
+
return -1;
|
|
340
|
+
}
|
|
341
|
+
if (j->ep != NULL) {
|
|
342
|
+
memcpy(np, j->ep, j->ep_len * sizeof(uint32_t));
|
|
343
|
+
yep_free(sys, j->ep);
|
|
344
|
+
}
|
|
345
|
+
j->ep = np;
|
|
346
|
+
j->ep_cap = ncap;
|
|
347
|
+
}
|
|
348
|
+
if (j->ep_len >= JS_MAX_EVALS) {
|
|
349
|
+
j->v.oom = 1;
|
|
350
|
+
return -1;
|
|
351
|
+
}
|
|
352
|
+
j->ep[j->ep_len++] = key_node;
|
|
353
|
+
return 0;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
static int ei_append(js_ctx* j, uint32_t index) {
|
|
357
|
+
if (j->ei_len == j->ei_cap) {
|
|
358
|
+
const yep_allocator* sys = yep_system_allocator();
|
|
359
|
+
size_t ncap = j->ei_cap ? j->ei_cap * 2 : 64;
|
|
360
|
+
uint32_t* np = yep_alloc(sys, ncap * sizeof(uint32_t));
|
|
361
|
+
if (np == NULL) {
|
|
362
|
+
j->v.oom = 1;
|
|
363
|
+
return -1;
|
|
364
|
+
}
|
|
365
|
+
if (j->ei != NULL) {
|
|
366
|
+
memcpy(np, j->ei, j->ei_len * sizeof(uint32_t));
|
|
367
|
+
yep_free(sys, j->ei);
|
|
368
|
+
}
|
|
369
|
+
j->ei = np;
|
|
370
|
+
j->ei_cap = ncap;
|
|
371
|
+
}
|
|
372
|
+
if (j->ei_len >= JS_MAX_EVALS) {
|
|
373
|
+
j->v.oom = 1;
|
|
374
|
+
return -1;
|
|
375
|
+
}
|
|
376
|
+
j->ei[j->ei_len++] = index;
|
|
377
|
+
return 0;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/* 1 when key_node's name is in the window [pmark, ep_len). */
|
|
381
|
+
static int eval_has_prop(const js_ctx* j, size_t pmark, uint32_t key_node) {
|
|
382
|
+
const yep_dnode* kn = yep_dom_node(j->v.dom, key_node);
|
|
383
|
+
if (kn == NULL) {
|
|
384
|
+
return 0;
|
|
385
|
+
}
|
|
386
|
+
yep_view kv = yep_dom_view(j->v.dom, kn->value);
|
|
387
|
+
for (size_t i = pmark; i < j->ep_len; i++) {
|
|
388
|
+
const yep_dnode* en = yep_dom_node(j->v.dom, j->ep[i]);
|
|
389
|
+
if (en == NULL) {
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
yep_view ev = yep_dom_view(j->v.dom, en->value);
|
|
393
|
+
if (ev.len == kv.len && (kv.len == 0 || memcmp(ev.p, kv.p, kv.len) == 0)) {
|
|
394
|
+
return 1;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return 0;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
static int eval_has_item(const js_ctx* j, size_t imark, uint32_t index) {
|
|
401
|
+
for (size_t i = imark; i < j->ei_len; i++) {
|
|
402
|
+
if (j->ei[i] == index) {
|
|
403
|
+
return 1;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
return 0;
|
|
407
|
+
}
|
|
408
|
+
|
|
223
409
|
/* ---- the walk ----------------------------------------------------------- */
|
|
224
410
|
|
|
225
|
-
static
|
|
411
|
+
static js_eval js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth);
|
|
412
|
+
static void js_apply_body(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth, size_t pmark,
|
|
413
|
+
size_t imark);
|
|
414
|
+
|
|
415
|
+
/* the object-keyword walkers (defined after js_apply) */
|
|
416
|
+
static void check_pattern_properties(js_ctx* j, uint32_t pp_id, uint32_t inst_id, int depth);
|
|
417
|
+
static void check_property_names(js_ctx* j, uint32_t pn_id, uint32_t inst_id, int depth);
|
|
418
|
+
static void check_dependent_required(js_ctx* j, uint32_t dr_id, uint32_t inst_id, int depth);
|
|
419
|
+
static void check_dependent_schemas(js_ctx* j, uint32_t ds_id, uint32_t inst_id, int depth);
|
|
420
|
+
static void check_unevaluated_properties(js_ctx* j, uint32_t upv, int upf, uint32_t inst_id,
|
|
421
|
+
int depth, size_t pmark);
|
|
422
|
+
static void check_unevaluated_items(js_ctx* j, uint32_t uv, int uf, uint32_t inst_id, int depth,
|
|
423
|
+
size_t imark);
|
|
424
|
+
|
|
425
|
+
/* Applies a schema to a CHILD instance location: the child's own
|
|
426
|
+
* annotations stay at the child (released here) — an annotation never
|
|
427
|
+
* crosses an instance-location boundary. */
|
|
428
|
+
static void js_apply_child(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth) {
|
|
429
|
+
size_t pm = j->ep_len, im = j->ei_len;
|
|
430
|
+
js_apply(j, schema_id, inst_id, depth);
|
|
431
|
+
j->ep_len = pm;
|
|
432
|
+
j->ei_len = im;
|
|
433
|
+
}
|
|
226
434
|
|
|
227
435
|
/* A suppressed probe: does `schema_id` hold at `inst_id`? Diagnostics
|
|
228
|
-
* during the trial are swallowed
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
436
|
+
* during the trial are swallowed (and the trial enters no dynamic
|
|
437
|
+
* scope — its anchors are as discarded as its annotations). The
|
|
438
|
+
* probe's validity is its OWN trial_fail delta — a suppressed emit
|
|
439
|
+
* never touches the enclosing application's fail_mark. The CALLER
|
|
440
|
+
* decides the window's fate: absorbed when the probe's annotations
|
|
441
|
+
* feed the parent (a held if / a matching anyOf branch), released
|
|
442
|
+
* otherwise. */
|
|
443
|
+
static js_eval js_probe(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth) {
|
|
444
|
+
int tf = j->v.trial_fail;
|
|
232
445
|
j->v.suppress++;
|
|
233
|
-
js_apply(j, schema_id, inst_id, depth);
|
|
446
|
+
js_eval r = js_apply(j, schema_id, inst_id, depth);
|
|
234
447
|
j->v.suppress--;
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
448
|
+
/* the extent's own delta only: nested probes restored their share,
|
|
449
|
+
* so what remains is what THIS trial swallowed directly */
|
|
450
|
+
int my = j->v.trial_fail - tf;
|
|
451
|
+
j->v.trial_fail = tf;
|
|
452
|
+
if (my != 0) {
|
|
453
|
+
r.valid = 0;
|
|
454
|
+
}
|
|
455
|
+
return r;
|
|
238
456
|
}
|
|
239
457
|
|
|
240
|
-
/*
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
return i == plen;
|
|
250
|
-
}
|
|
458
|
+
/* ---- reference resolution ------------------------------------------------ */
|
|
459
|
+
|
|
460
|
+
/* JSON Pointer walk from `start` with the URI percent layer decoded
|
|
461
|
+
* before the ~0/~1 escapes (%25 -> '%', then the RFC 6901 unescape).
|
|
462
|
+
* `p` spans the raw fragment text ("/$defs/foo"); the loop consumes a
|
|
463
|
+
* '/' before each token. */
|
|
464
|
+
static int pointer_walk(js_ctx* j, uint32_t start, const char* p, size_t plen, uint32_t* out) {
|
|
465
|
+
uint32_t cur = start;
|
|
466
|
+
size_t i = 0;
|
|
251
467
|
int tokens = 0;
|
|
252
468
|
while (i < plen) {
|
|
253
469
|
if (p[i] != '/') {
|
|
254
470
|
return 0;
|
|
255
471
|
}
|
|
256
472
|
i++;
|
|
257
|
-
char token[
|
|
473
|
+
char token[256];
|
|
258
474
|
size_t tl = 0;
|
|
259
475
|
while (i < plen && p[i] != '/') {
|
|
260
476
|
char b = p[i];
|
|
261
|
-
if (b == '
|
|
477
|
+
if (b == '%' && i + 2 < plen) {
|
|
478
|
+
int hi = (p[i + 1] >= '0' && p[i + 1] <= '9') ? p[i + 1] - '0'
|
|
479
|
+
: ((p[i + 1] | 0x20) - 'a' + 10);
|
|
480
|
+
int lo = (p[i + 2] >= '0' && p[i + 2] <= '9') ? p[i + 2] - '0'
|
|
481
|
+
: ((p[i + 2] | 0x20) - 'a' + 10);
|
|
482
|
+
if ((unsigned)hi < 16 && (unsigned)lo < 16) {
|
|
483
|
+
b = (char)((hi << 4) | lo);
|
|
484
|
+
i += 2;
|
|
485
|
+
}
|
|
486
|
+
} else if (b == '~' && i + 1 < plen && (p[i + 1] == '0' || p[i + 1] == '1')) {
|
|
262
487
|
b = p[i + 1] == '0' ? '~' : '/';
|
|
263
488
|
i++;
|
|
264
489
|
} else if (b == '~') {
|
|
@@ -326,6 +551,125 @@ static int resolve_ref(js_ctx* j, const char* p, uint32_t plen, uint32_t* out) {
|
|
|
326
551
|
return cur != UINT32_MAX;
|
|
327
552
|
}
|
|
328
553
|
|
|
554
|
+
/* The plain-name anchor lookup: canonical base URI + '#' + name. When
|
|
555
|
+
* `dynamic` and the static anchor is a $dynamicAnchor, the draft's
|
|
556
|
+
* runtime rule applies (core spec 8.2.3.2): the OUTERMOST schema
|
|
557
|
+
* resource in the dynamic scope defining an identically-named
|
|
558
|
+
* $dynamicAnchor wins; with nothing in scope, the static target
|
|
559
|
+
* stands (the behavior is "$ref-like" — the bookending requirement is
|
|
560
|
+
* the schema author's). */
|
|
561
|
+
static int anchor_lookup(js_ctx* j, const char* base, const char* name, size_t nl, int dynamic,
|
|
562
|
+
uint32_t* out) {
|
|
563
|
+
if (j->res == NULL) {
|
|
564
|
+
return 0;
|
|
565
|
+
}
|
|
566
|
+
char key[YEP_JS_URI_MAX];
|
|
567
|
+
size_t bn = strlen(base);
|
|
568
|
+
if (bn + 1 + nl + 1 > sizeof(key)) {
|
|
569
|
+
return 0;
|
|
570
|
+
}
|
|
571
|
+
memcpy(key, base, bn);
|
|
572
|
+
key[bn] = '#';
|
|
573
|
+
memcpy(key + bn + 1, name, nl);
|
|
574
|
+
key[bn + 1 + nl] = '\0';
|
|
575
|
+
uint32_t node = 0;
|
|
576
|
+
uint8_t dyn = 0;
|
|
577
|
+
if (!js_anchor_find(j->res, key, bn + 1 + nl, &node, &dyn)) {
|
|
578
|
+
return 0;
|
|
579
|
+
}
|
|
580
|
+
if (dynamic && dyn == 1) {
|
|
581
|
+
uint32_t found = UINT32_MAX;
|
|
582
|
+
for (int i = 0; i < j->scope_n && found == UINT32_MAX; i++) {
|
|
583
|
+
uint32_t anode = 0;
|
|
584
|
+
if (js_anchor_dynamic_in_res(j->res, j->scope[i], name, nl, &anode)) {
|
|
585
|
+
found = anode;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
if (found != UINT32_MAX) {
|
|
589
|
+
*out = found;
|
|
590
|
+
return 1;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
*out = node;
|
|
594
|
+
return 1;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/* Resolves the URI-reference at `site` (the schema object carrying
|
|
598
|
+
* $ref/$dynamicRef) against the site's compiled base. `dynamic`
|
|
599
|
+
* enables the $dynamicRef runtime scope. 1 + *out, 0 unresolvable. */
|
|
600
|
+
static int resolve_ref_ext(js_ctx* j, uint32_t site, const char* rp, uint32_t rl, int dynamic,
|
|
601
|
+
uint32_t* out) {
|
|
602
|
+
const char* base = "";
|
|
603
|
+
uint32_t base_root = j->sroot;
|
|
604
|
+
if (j->res != NULL) {
|
|
605
|
+
js_refsite_find(j->res, site, &base, &base_root);
|
|
606
|
+
}
|
|
607
|
+
if (rl == 0) {
|
|
608
|
+
*out = base_root; /* "" : the base resource root */
|
|
609
|
+
return 1;
|
|
610
|
+
}
|
|
611
|
+
if (rp[0] == '#') {
|
|
612
|
+
const char* frag = rp + 1;
|
|
613
|
+
size_t fl = rl - 1;
|
|
614
|
+
if (fl == 0) {
|
|
615
|
+
*out = base_root; /* "#" : the base resource root */
|
|
616
|
+
return 1;
|
|
617
|
+
}
|
|
618
|
+
if (frag[0] == '/') {
|
|
619
|
+
return pointer_walk(j, base_root, frag, fl, out);
|
|
620
|
+
}
|
|
621
|
+
return anchor_lookup(j, base, frag, fl, dynamic, out);
|
|
622
|
+
}
|
|
623
|
+
/* a URI-reference with its own (possibly empty) fragment */
|
|
624
|
+
const char* hash = memchr(rp, '#', rl);
|
|
625
|
+
size_t ul = hash != NULL ? (size_t)(hash - rp) : rl;
|
|
626
|
+
char canon[YEP_JS_URI_MAX];
|
|
627
|
+
if (js_uri_resolve(base, rp, ul, canon, sizeof(canon)) != 0) {
|
|
628
|
+
return 0;
|
|
629
|
+
}
|
|
630
|
+
if (hash == NULL) {
|
|
631
|
+
/* no fragment: the resource root itself */
|
|
632
|
+
uint32_t ri = 0;
|
|
633
|
+
if (j->res != NULL && js_res_find(j->res, canon, &ri)) {
|
|
634
|
+
*out = j->res->res[ri].root;
|
|
635
|
+
return 1;
|
|
636
|
+
}
|
|
637
|
+
if (strcmp(canon, base) == 0) {
|
|
638
|
+
*out = base_root;
|
|
639
|
+
return 1;
|
|
640
|
+
}
|
|
641
|
+
return 0; /* outside the schema's identifier space: never fetched */
|
|
642
|
+
}
|
|
643
|
+
const char* frag = hash + 1;
|
|
644
|
+
size_t fl = (size_t)(rp + rl - frag);
|
|
645
|
+
if (frag[0] == '/') {
|
|
646
|
+
uint32_t start = UINT32_MAX;
|
|
647
|
+
uint32_t ri = 0;
|
|
648
|
+
if (j->res != NULL && js_res_find(j->res, canon, &ri)) {
|
|
649
|
+
start = j->res->res[ri].root;
|
|
650
|
+
} else if (strcmp(canon, base) == 0) {
|
|
651
|
+
start = base_root;
|
|
652
|
+
}
|
|
653
|
+
if (start == UINT32_MAX) {
|
|
654
|
+
return 0;
|
|
655
|
+
}
|
|
656
|
+
return pointer_walk(j, start, frag, fl, out);
|
|
657
|
+
}
|
|
658
|
+
if (fl == 0) {
|
|
659
|
+
uint32_t ri = 0;
|
|
660
|
+
if (j->res != NULL && js_res_find(j->res, canon, &ri)) {
|
|
661
|
+
*out = j->res->res[ri].root;
|
|
662
|
+
return 1;
|
|
663
|
+
}
|
|
664
|
+
if (strcmp(canon, base) == 0) {
|
|
665
|
+
*out = base_root;
|
|
666
|
+
return 1;
|
|
667
|
+
}
|
|
668
|
+
return 0;
|
|
669
|
+
}
|
|
670
|
+
return anchor_lookup(j, canon, frag, fl, dynamic, out);
|
|
671
|
+
}
|
|
672
|
+
|
|
329
673
|
/* uniqueItems: canonical hash buckets, canonical equality inside a
|
|
330
674
|
* bucket; allocation failure skips the check (oom flags the walk). */
|
|
331
675
|
static void check_unique(js_ctx* j, uint32_t seq_id, uint32_t inst_off) {
|
|
@@ -390,17 +734,18 @@ static void check_unique(js_ctx* j, uint32_t seq_id, uint32_t inst_off) {
|
|
|
390
734
|
/* The instance's string length in code points (2020-12 minLength/
|
|
391
735
|
* maxLength). 0 when the instance is not a string. */
|
|
392
736
|
static int inst_str_len(const yep_dom* d, uint32_t id, uint32_t* out) {
|
|
393
|
-
const
|
|
737
|
+
const yep_dom* dd = d;
|
|
738
|
+
const yep_dnode* n = yep_dom_node(dd, id);
|
|
394
739
|
if (n == NULL) {
|
|
395
740
|
return 0;
|
|
396
741
|
}
|
|
397
742
|
if (n->kind == YEP_DOM_ALIAS) {
|
|
398
|
-
return n->target != UINT32_MAX && inst_str_len(
|
|
743
|
+
return n->target != UINT32_MAX && inst_str_len(dd, n->target, out);
|
|
399
744
|
}
|
|
400
745
|
if (n->kind != YEP_DOM_SCALAR || yep_validate_scalar_tag(n) != YEPTRIS_TAG_STR) {
|
|
401
746
|
return 0;
|
|
402
747
|
}
|
|
403
|
-
yep_view v = yep_dom_view(
|
|
748
|
+
yep_view v = yep_dom_view(dd, n->value);
|
|
404
749
|
uint32_t chars = 0;
|
|
405
750
|
for (uint32_t i = 0; i < v.len; i++) {
|
|
406
751
|
chars += ((((unsigned char)v.p[i]) & 0xC0) != 0x80);
|
|
@@ -444,7 +789,57 @@ static int schema_word(const js_ctx* j, uint32_t id, const char** p, uint32_t* l
|
|
|
444
789
|
return 1;
|
|
445
790
|
}
|
|
446
791
|
|
|
447
|
-
|
|
792
|
+
/* Copies a bounded name for diagnostics/detail text: the copied
|
|
793
|
+
* length (the caller pushes paths with it — keys may hold NULs). */
|
|
794
|
+
static size_t name_copy(char* buf, size_t cap, const char* p, size_t len) {
|
|
795
|
+
size_t cl = len < cap - 1 ? len : cap - 1;
|
|
796
|
+
if (cl > 0 && p != NULL) {
|
|
797
|
+
memcpy(buf, p, cl);
|
|
798
|
+
}
|
|
799
|
+
buf[cl] = '\0';
|
|
800
|
+
return cl;
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
/* The dynamic scope: entering a schema object whose resource differs
|
|
804
|
+
* from the current top pushes that resource (outermost first); probes
|
|
805
|
+
* (suppress > 0) enter no scope — their anchors are as discarded as
|
|
806
|
+
* their annotations. 1 pushed, 0 not. */
|
|
807
|
+
static int js_scope_push(js_ctx* j, uint32_t schema_id) {
|
|
808
|
+
if (j->res == NULL || j->v.suppress > 0 || schema_id >= j->res->rindex_n) {
|
|
809
|
+
return 0;
|
|
810
|
+
}
|
|
811
|
+
uint32_t ri = j->res->rindex[schema_id];
|
|
812
|
+
if (ri == UINT32_MAX || j->scope_n >= YEP_JS_MAX_SCOPE) {
|
|
813
|
+
return 0;
|
|
814
|
+
}
|
|
815
|
+
if (j->scope_n > 0 && j->scope[j->scope_n - 1] == ri) {
|
|
816
|
+
return 0;
|
|
817
|
+
}
|
|
818
|
+
j->scope[j->scope_n++] = ri;
|
|
819
|
+
return 1;
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
/* One application: brackets the body with the validity mark and the
|
|
823
|
+
* annotation-window marks, pushes the dynamic scope, and hands the
|
|
824
|
+
* whole window to the caller (absorb = keep, drop = release). */
|
|
825
|
+
static js_eval js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth) {
|
|
826
|
+
int pushed = js_scope_push(j, schema_id);
|
|
827
|
+
int fm = j->v.fail_mark;
|
|
828
|
+
js_eval r;
|
|
829
|
+
r.pmark = j->ep_len;
|
|
830
|
+
r.imark = j->ei_len;
|
|
831
|
+
js_apply_body(j, schema_id, inst_id, depth, r.pmark, r.imark);
|
|
832
|
+
r.valid = (fm == j->v.fail_mark && !j->v.oom);
|
|
833
|
+
r.plen = j->ep_len;
|
|
834
|
+
r.ilen = j->ei_len;
|
|
835
|
+
if (pushed) {
|
|
836
|
+
j->scope_n--;
|
|
837
|
+
}
|
|
838
|
+
return r;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
static void js_apply_body(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth, size_t pmark,
|
|
842
|
+
size_t imark) {
|
|
448
843
|
if (j->v.oom) {
|
|
449
844
|
return;
|
|
450
845
|
}
|
|
@@ -474,32 +869,46 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
|
|
|
474
869
|
return; /* true (or anything unreadable) constrains nothing */
|
|
475
870
|
}
|
|
476
871
|
|
|
477
|
-
/* $ref
|
|
478
|
-
* schema-only cycles that consume no instance
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
872
|
+
/* $ref / $dynamicRef compose with their siblings (2020-12); the
|
|
873
|
+
* chain guard kills schema-only cycles that consume no instance
|
|
874
|
+
* structure. Same-location application: the target's annotations
|
|
875
|
+
* are absorbed into this object's set. */
|
|
876
|
+
{
|
|
877
|
+
int is_dyn = 0;
|
|
878
|
+
uint32_t ref_val = UINT32_MAX;
|
|
879
|
+
if (!kw_get(j, schema_id, "$ref", &ref_val) || ref_val == UINT32_MAX) {
|
|
880
|
+
is_dyn = 1;
|
|
881
|
+
if (!kw_get(j, schema_id, "$dynamicRef", &ref_val) || ref_val == UINT32_MAX) {
|
|
882
|
+
is_dyn = 0;
|
|
883
|
+
ref_val = UINT32_MAX;
|
|
489
884
|
}
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
885
|
+
}
|
|
886
|
+
if (ref_val != UINT32_MAX) {
|
|
887
|
+
const char* rp = NULL;
|
|
888
|
+
uint32_t rl = 0;
|
|
889
|
+
if (schema_word(j, ref_val, &rp, &rl)) {
|
|
890
|
+
if (++j->v.ref_depth > JS_MAX_REF_CHAIN) {
|
|
891
|
+
yep_validate_emit(&j->v, YEPTRIS_DIAG_REF,
|
|
892
|
+
yep_validate_node_off(j->v.dom, inst_id),
|
|
893
|
+
"$ref chain exceeds %d (a cycle?)", JS_MAX_REF_CHAIN);
|
|
894
|
+
j->v.ref_depth = 0;
|
|
895
|
+
return;
|
|
896
|
+
}
|
|
897
|
+
uint32_t target = UINT32_MAX;
|
|
898
|
+
if (!resolve_ref_ext(j, schema_id, rp, rl, is_dyn, &target)) {
|
|
899
|
+
yep_validate_emit(
|
|
900
|
+
&j->v, YEPTRIS_DIAG_REF, yep_validate_node_off(j->v.dom, inst_id),
|
|
901
|
+
"$ref \"%.*s\" does not resolve", (int)(rl > 64 ? 64 : rl), rp);
|
|
902
|
+
j->v.ref_depth = 0;
|
|
903
|
+
return;
|
|
904
|
+
}
|
|
905
|
+
js_apply(j, target, inst_id, depth); /* absorbed */
|
|
494
906
|
j->v.ref_depth = 0;
|
|
495
|
-
return;
|
|
496
907
|
}
|
|
497
|
-
|
|
908
|
+
/* an unreadable $ref value: ignored like any unknown keyword */
|
|
909
|
+
} else {
|
|
498
910
|
j->v.ref_depth = 0;
|
|
499
911
|
}
|
|
500
|
-
/* an unreadable $ref value: ignored like any unknown keyword */
|
|
501
|
-
} else {
|
|
502
|
-
j->v.ref_depth = 0;
|
|
503
912
|
}
|
|
504
913
|
|
|
505
914
|
/* type: a single word or a list of words */
|
|
@@ -621,6 +1030,28 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
|
|
|
621
1030
|
"%g is not less than %g", iv, b);
|
|
622
1031
|
}
|
|
623
1032
|
}
|
|
1033
|
+
if (kw_get(j, schema_id, "multipleOf", &mv) && mv != UINT32_MAX) {
|
|
1034
|
+
double m;
|
|
1035
|
+
if (schema_number(j, mv, &m)) {
|
|
1036
|
+
/* integrality of iv/m, libm-free: NaN fails (q==q),
|
|
1037
|
+
* +-inf fails (q-q is NaN); every finite |q| >= 2^53
|
|
1038
|
+
* is already an integer at double precision, and
|
|
1039
|
+
* below that the int64 cast round-trips iff q is
|
|
1040
|
+
* integral. A 0 divisor (never a legal schema) lands
|
|
1041
|
+
* in inf/NaN and fails every instance. */
|
|
1042
|
+
double q = iv / m;
|
|
1043
|
+
int ok = (q == q) && (q - q == 0);
|
|
1044
|
+
if (ok) {
|
|
1045
|
+
double aq = q < 0 ? -q : q;
|
|
1046
|
+
ok = aq >= 9007199254740992.0 /* 2^53 */ || q == (double)(int64_t)q;
|
|
1047
|
+
}
|
|
1048
|
+
if (!ok) {
|
|
1049
|
+
yep_validate_emit(&j->v, YEPTRIS_DIAG_MULTIPLE_OF,
|
|
1050
|
+
yep_validate_node_off(j->v.dom, inst_id),
|
|
1051
|
+
"%g is not a multiple of %g", iv, m);
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
624
1055
|
}
|
|
625
1056
|
}
|
|
626
1057
|
|
|
@@ -653,7 +1084,7 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
|
|
|
653
1084
|
if (schema_word(j, pv, &pat, &pl)) {
|
|
654
1085
|
const yep_dnode* n = yep_dom_node(j->v.dom, inst_id);
|
|
655
1086
|
yep_view v = yep_dom_view(j->v.dom, n->value);
|
|
656
|
-
int rc =
|
|
1087
|
+
int rc = yep_re_cache_search(j->cache, pat, pl, v.p != NULL ? v.p : "", v.len);
|
|
657
1088
|
if (rc == 0) {
|
|
658
1089
|
yep_validate_emit(&j->v, YEPTRIS_DIAG_PATTERN,
|
|
659
1090
|
yep_validate_node_off(j->v.dom, inst_id),
|
|
@@ -691,6 +1122,65 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
|
|
|
691
1122
|
if (kw_schema(j, schema_id, "uniqueItems", &uv, &uf) && uf == 0) {
|
|
692
1123
|
check_unique(j, inst_id, yep_validate_node_off(j->v.dom, inst_id));
|
|
693
1124
|
}
|
|
1125
|
+
/* contains: count the items the schema holds on (suppressed
|
|
1126
|
+
* probes), then the minContains/maxContains qualifiers
|
|
1127
|
+
* (defaults: at least one; no maximum). min/maxContains
|
|
1128
|
+
* without contains are annotations — ignored. The matching
|
|
1129
|
+
* indices are this object's ANNOTATION — kept when contains
|
|
1130
|
+
* holds, released when it fails. */
|
|
1131
|
+
uint32_t ctv;
|
|
1132
|
+
if (kw_get(j, schema_id, "contains", &ctv) && ctv != UINT32_MAX) {
|
|
1133
|
+
int ctf = 0;
|
|
1134
|
+
if (snode(j, ctv) != NULL && schema_truth(j, ctv, &ctf)) {
|
|
1135
|
+
uint32_t matches = 0;
|
|
1136
|
+
uint32_t ich = yep_dom_node(j->v.dom, inst_id)
|
|
1137
|
+
? yep_dom_node(j->v.dom, inst_id)->first_child
|
|
1138
|
+
: UINT32_MAX;
|
|
1139
|
+
for (uint32_t i = 0; i < cnt && ich != UINT32_MAX; i++) {
|
|
1140
|
+
uint32_t ni = yep_dom_node(j->v.dom, ich)
|
|
1141
|
+
? yep_dom_node(j->v.dom, ich)->next_sibling
|
|
1142
|
+
: UINT32_MAX;
|
|
1143
|
+
js_eval pr = js_probe(j, ctv, ich, depth);
|
|
1144
|
+
j->ep_len = pr.pmark; /* the item probe annotates nothing outward */
|
|
1145
|
+
j->ei_len = pr.imark;
|
|
1146
|
+
if (pr.valid) {
|
|
1147
|
+
matches++;
|
|
1148
|
+
ei_append(j, i);
|
|
1149
|
+
}
|
|
1150
|
+
ich = ni;
|
|
1151
|
+
}
|
|
1152
|
+
double minc = 1.0;
|
|
1153
|
+
double maxc = -1.0; /* unset */
|
|
1154
|
+
uint32_t mc;
|
|
1155
|
+
int have_minc = 0;
|
|
1156
|
+
if (kw_get(j, schema_id, "minContains", &mc) && mc != UINT32_MAX) {
|
|
1157
|
+
if (schema_number(j, mc, &minc)) {
|
|
1158
|
+
have_minc = 1;
|
|
1159
|
+
} else {
|
|
1160
|
+
minc = 1.0; /* an unreadable qualifier: default */
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
if (kw_get(j, schema_id, "maxContains", &mc) && mc != UINT32_MAX) {
|
|
1164
|
+
if (!schema_number(j, mc, &maxc)) {
|
|
1165
|
+
maxc = -1.0;
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
if ((double)matches < minc) {
|
|
1169
|
+
j->ei_len = imark; /* a failed contains annotates nothing */
|
|
1170
|
+
yep_validate_emit(
|
|
1171
|
+
&j->v, have_minc ? YEPTRIS_DIAG_MIN_CONTAINS : YEPTRIS_DIAG_CONTAINS,
|
|
1172
|
+
yep_validate_node_off(j->v.dom, inst_id),
|
|
1173
|
+
"%u of %u items match the contains schema (need %g)", matches, cnt,
|
|
1174
|
+
minc);
|
|
1175
|
+
} else if (maxc >= 0.0 && (double)matches > maxc) {
|
|
1176
|
+
j->ei_len = imark;
|
|
1177
|
+
yep_validate_emit(&j->v, YEPTRIS_DIAG_MAX_CONTAINS,
|
|
1178
|
+
yep_validate_node_off(j->v.dom, inst_id),
|
|
1179
|
+
"%u of %u items match the contains schema (max %g)",
|
|
1180
|
+
matches, cnt, maxc);
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
694
1184
|
/* prefixItems: per-index schemas; the draft-07 array form
|
|
695
1185
|
* of items rides the same semantics */
|
|
696
1186
|
uint32_t prefix = 0;
|
|
@@ -708,9 +1198,10 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
|
|
|
708
1198
|
: UINT32_MAX;
|
|
709
1199
|
size_t mark = j->v.path_len;
|
|
710
1200
|
if (yep_validate_path_push_index(&j->v, i) == 0) {
|
|
711
|
-
|
|
1201
|
+
js_apply_child(j, ch, ich, depth);
|
|
712
1202
|
yep_validate_path_pop(&j->v, mark);
|
|
713
1203
|
}
|
|
1204
|
+
ei_append(j, i);
|
|
714
1205
|
prefix++;
|
|
715
1206
|
i++;
|
|
716
1207
|
ch = ns;
|
|
@@ -735,9 +1226,10 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
|
|
|
735
1226
|
: UINT32_MAX;
|
|
736
1227
|
size_t mark = j->v.path_len;
|
|
737
1228
|
if (yep_validate_path_push_index(&j->v, i) == 0) {
|
|
738
|
-
|
|
1229
|
+
js_apply_child(j, ch, ich, depth);
|
|
739
1230
|
yep_validate_path_pop(&j->v, mark);
|
|
740
1231
|
}
|
|
1232
|
+
ei_append(j, i);
|
|
741
1233
|
i++;
|
|
742
1234
|
ch = ns;
|
|
743
1235
|
ich = ni;
|
|
@@ -756,9 +1248,10 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
|
|
|
756
1248
|
: UINT32_MAX;
|
|
757
1249
|
size_t mark = j->v.path_len;
|
|
758
1250
|
if (yep_validate_path_push_index(&j->v, i) == 0) {
|
|
759
|
-
|
|
1251
|
+
js_apply_child(j, pv, ich, depth);
|
|
760
1252
|
yep_validate_path_pop(&j->v, mark);
|
|
761
1253
|
}
|
|
1254
|
+
ei_append(j, i);
|
|
762
1255
|
ich = ni;
|
|
763
1256
|
}
|
|
764
1257
|
}
|
|
@@ -766,7 +1259,10 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
|
|
|
766
1259
|
}
|
|
767
1260
|
}
|
|
768
1261
|
|
|
769
|
-
/* allOf / anyOf / oneOf / not
|
|
1262
|
+
/* allOf / anyOf / oneOf / not: same-location applicators — a
|
|
1263
|
+
* branch's annotations join this object's set when the branch
|
|
1264
|
+
* holds (allOf unconditionally: its verdict is this object's), a
|
|
1265
|
+
* failed probe's window is released, `not` never annotates. */
|
|
770
1266
|
{
|
|
771
1267
|
uint32_t cv;
|
|
772
1268
|
if (kw_get(j, schema_id, "allOf", &cv) && cv != UINT32_MAX) {
|
|
@@ -775,7 +1271,7 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
|
|
|
775
1271
|
uint32_t ch = cn->first_child;
|
|
776
1272
|
while (ch != UINT32_MAX) {
|
|
777
1273
|
uint32_t nc = snode(j, ch) ? snode(j, ch)->next_sibling : UINT32_MAX;
|
|
778
|
-
js_apply(j, ch, inst_id, depth);
|
|
1274
|
+
js_apply(j, ch, inst_id, depth); /* absorbed */
|
|
779
1275
|
ch = nc;
|
|
780
1276
|
}
|
|
781
1277
|
}
|
|
@@ -788,8 +1284,12 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
|
|
|
788
1284
|
uint32_t ch = cn->first_child;
|
|
789
1285
|
while (ch != UINT32_MAX) {
|
|
790
1286
|
branches++;
|
|
791
|
-
|
|
792
|
-
|
|
1287
|
+
js_eval br = js_probe(j, ch, inst_id, depth);
|
|
1288
|
+
if (br.valid) {
|
|
1289
|
+
any = 1; /* absorbed: the held branch annotates */
|
|
1290
|
+
} else {
|
|
1291
|
+
j->ep_len = br.pmark;
|
|
1292
|
+
j->ei_len = br.imark;
|
|
793
1293
|
}
|
|
794
1294
|
ch = snode(j, ch) ? snode(j, ch)->next_sibling : UINT32_MAX;
|
|
795
1295
|
}
|
|
@@ -808,12 +1308,18 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
|
|
|
808
1308
|
uint32_t ch = cn->first_child;
|
|
809
1309
|
while (ch != UINT32_MAX) {
|
|
810
1310
|
branches++;
|
|
811
|
-
|
|
812
|
-
|
|
1311
|
+
js_eval br = js_probe(j, ch, inst_id, depth);
|
|
1312
|
+
if (br.valid) {
|
|
1313
|
+
matches++; /* absorbed */
|
|
1314
|
+
} else {
|
|
1315
|
+
j->ep_len = br.pmark;
|
|
1316
|
+
j->ei_len = br.imark;
|
|
813
1317
|
}
|
|
814
1318
|
ch = snode(j, ch) ? snode(j, ch)->next_sibling : UINT32_MAX;
|
|
815
1319
|
}
|
|
816
1320
|
if (matches != 1) {
|
|
1321
|
+
j->ep_len = pmark; /* no single branch: no annotations */
|
|
1322
|
+
j->ei_len = imark;
|
|
817
1323
|
yep_validate_emit(&j->v, YEPTRIS_DIAG_ONE_OF,
|
|
818
1324
|
yep_validate_node_off(j->v.dom, inst_id),
|
|
819
1325
|
"%u of the %u oneOf branches matched (exactly one "
|
|
@@ -823,21 +1329,68 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
|
|
|
823
1329
|
}
|
|
824
1330
|
}
|
|
825
1331
|
if (kw_get(j, schema_id, "not", &cv) && cv != UINT32_MAX) {
|
|
826
|
-
|
|
1332
|
+
js_eval nr = js_probe(j, cv, inst_id, depth);
|
|
1333
|
+
j->ep_len = nr.pmark; /* the not-schema never annotates outward */
|
|
1334
|
+
j->ei_len = nr.imark;
|
|
1335
|
+
if (nr.valid) {
|
|
827
1336
|
yep_validate_emit(&j->v, YEPTRIS_DIAG_NOT, yep_validate_node_off(j->v.dom, inst_id),
|
|
828
1337
|
"the not-schema matched");
|
|
829
1338
|
}
|
|
830
1339
|
}
|
|
831
1340
|
}
|
|
832
1341
|
|
|
1342
|
+
/* if/then/else: the if-schema rides a suppressed probe; exactly one
|
|
1343
|
+
* of then/else applies. A HOLDING if annotates (the probe's window
|
|
1344
|
+
* is absorbed); a failed one is released. then/else are
|
|
1345
|
+
* same-location applications — absorbed. An unreadable if (or
|
|
1346
|
+
* then/else) is ignored. */
|
|
1347
|
+
{
|
|
1348
|
+
uint32_t ifv;
|
|
1349
|
+
if (kw_get(j, schema_id, "if", &ifv) && ifv != UINT32_MAX) {
|
|
1350
|
+
int iff = 0;
|
|
1351
|
+
if (schema_truth(j, ifv, &iff)) {
|
|
1352
|
+
uint32_t tv = UINT32_MAX, ev = UINT32_MAX;
|
|
1353
|
+
int tf = 0, ef = 0;
|
|
1354
|
+
int have_t = kw_schema(j, schema_id, "then", &tv, &tf);
|
|
1355
|
+
int have_e = kw_schema(j, schema_id, "else", &ev, &ef);
|
|
1356
|
+
js_eval ir = js_probe(j, ifv, inst_id, depth);
|
|
1357
|
+
if (ir.valid) { /* absorbed: the if's annotations count */
|
|
1358
|
+
if (have_t) {
|
|
1359
|
+
js_apply(j, tv, inst_id, depth);
|
|
1360
|
+
}
|
|
1361
|
+
} else {
|
|
1362
|
+
j->ep_len = ir.pmark;
|
|
1363
|
+
j->ei_len = ir.imark;
|
|
1364
|
+
if (have_e) {
|
|
1365
|
+
js_apply(j, ev, inst_id, depth);
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
|
|
833
1372
|
/* the object keywords (ignored on non-objects) */
|
|
834
1373
|
{
|
|
835
1374
|
const yep_dnode* n = yep_dom_node(j->v.dom, inst_id);
|
|
836
1375
|
if (n != NULL && n->kind == YEP_DOM_MAPPING) {
|
|
1376
|
+
const uint32_t pairs = n->count / 2;
|
|
837
1377
|
/* properties: known keys */
|
|
838
1378
|
uint32_t props = UINT32_MAX;
|
|
839
1379
|
int have_props = kw_get(j, schema_id, "properties", &props) && props != UINT32_MAX &&
|
|
840
1380
|
snode(j, props) != NULL && snode(j, props)->kind == YEP_DOM_MAPPING;
|
|
1381
|
+
/* patternProperties: pattern -> schema over the KEY strings
|
|
1382
|
+
* (readable once; additionalProperties must exclude its
|
|
1383
|
+
* matches too) */
|
|
1384
|
+
uint32_t pp_id = UINT32_MAX;
|
|
1385
|
+
{
|
|
1386
|
+
uint32_t ppid;
|
|
1387
|
+
if (kw_get(j, schema_id, "patternProperties", &ppid) && ppid != UINT32_MAX) {
|
|
1388
|
+
const yep_dnode* pnm = snode(j, ppid);
|
|
1389
|
+
if (pnm != NULL && pnm->kind == YEP_DOM_MAPPING) {
|
|
1390
|
+
pp_id = ppid;
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
841
1394
|
uint32_t req;
|
|
842
1395
|
if (kw_get(j, schema_id, "required", &req) && req != UINT32_MAX) {
|
|
843
1396
|
const yep_dnode* rn = snode(j, req);
|
|
@@ -847,48 +1400,30 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
|
|
|
847
1400
|
const char* name = NULL;
|
|
848
1401
|
uint32_t nl = 0;
|
|
849
1402
|
uint32_t nc = snode(j, ch) ? snode(j, ch)->next_sibling : UINT32_MAX;
|
|
850
|
-
if (schema_word(j, ch, &name, &nl)
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
? yep_dom_node(j->v.dom, v)->next_sibling
|
|
867
|
-
: UINT32_MAX;
|
|
868
|
-
}
|
|
869
|
-
if (hit == UINT32_MAX) {
|
|
870
|
-
char namebuf[64];
|
|
871
|
-
size_t cl = nl < sizeof(namebuf) - 1 ? nl : sizeof(namebuf) - 1;
|
|
872
|
-
memcpy(namebuf, name, cl);
|
|
873
|
-
namebuf[cl] = '\0';
|
|
874
|
-
/* the diagnostic points at the missing
|
|
875
|
-
* leaf: object path + /key (the ajv
|
|
876
|
-
* instancePath convention) */
|
|
877
|
-
size_t mark = j->v.path_len;
|
|
878
|
-
int pushed = yep_validate_path_push(&j->v, namebuf, cl);
|
|
879
|
-
yep_validate_emit(&j->v, YEPTRIS_DIAG_REQUIRED,
|
|
880
|
-
yep_validate_node_off(j->v.dom, inst_id),
|
|
881
|
-
"missing required property \"%s\"", namebuf);
|
|
882
|
-
if (pushed == 0) {
|
|
883
|
-
yep_validate_path_pop(&j->v, mark);
|
|
884
|
-
}
|
|
1403
|
+
if (schema_word(j, ch, &name, &nl) &&
|
|
1404
|
+
inst_find_key(j->v.dom, inst_id, name, nl, NULL) == UINT32_MAX) {
|
|
1405
|
+
char namebuf[64];
|
|
1406
|
+
size_t cl = nl < sizeof(namebuf) - 1 ? nl : sizeof(namebuf) - 1;
|
|
1407
|
+
memcpy(namebuf, name, cl);
|
|
1408
|
+
namebuf[cl] = '\0';
|
|
1409
|
+
/* the diagnostic points at the missing
|
|
1410
|
+
* leaf: object path + /key (the ajv
|
|
1411
|
+
* instancePath convention) */
|
|
1412
|
+
size_t mark = j->v.path_len;
|
|
1413
|
+
int pushed = yep_validate_path_push(&j->v, namebuf, cl);
|
|
1414
|
+
yep_validate_emit(&j->v, YEPTRIS_DIAG_REQUIRED,
|
|
1415
|
+
yep_validate_node_off(j->v.dom, inst_id),
|
|
1416
|
+
"missing required property \"%s\"", namebuf);
|
|
1417
|
+
if (pushed == 0) {
|
|
1418
|
+
yep_validate_path_pop(&j->v, mark);
|
|
885
1419
|
}
|
|
886
1420
|
}
|
|
887
1421
|
ch = nc;
|
|
888
1422
|
}
|
|
889
1423
|
}
|
|
890
1424
|
}
|
|
891
|
-
/* properties walk (values through their schemas)
|
|
1425
|
+
/* properties walk (values through their schemas): the
|
|
1426
|
+
* PRESENT keys are this object's annotation */
|
|
892
1427
|
if (have_props) {
|
|
893
1428
|
const yep_dnode* pn = snode(j, props);
|
|
894
1429
|
uint32_t k = pn->first_child;
|
|
@@ -911,9 +1446,10 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
|
|
|
911
1446
|
size_t mark = j->v.path_len;
|
|
912
1447
|
if (yep_validate_path_push(&j->v, kv.p != NULL ? kv.p : "",
|
|
913
1448
|
kv.len) == 0) {
|
|
914
|
-
|
|
1449
|
+
js_apply_child(j, v, cv, depth);
|
|
915
1450
|
yep_validate_path_pop(&j->v, mark);
|
|
916
1451
|
}
|
|
1452
|
+
ep_append(j, ck);
|
|
917
1453
|
break;
|
|
918
1454
|
}
|
|
919
1455
|
}
|
|
@@ -925,7 +1461,60 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
|
|
|
925
1461
|
k = v != UINT32_MAX && snode(j, v) ? snode(j, v)->next_sibling : UINT32_MAX;
|
|
926
1462
|
}
|
|
927
1463
|
}
|
|
928
|
-
/*
|
|
1464
|
+
/* patternProperties: every pattern that matches a key
|
|
1465
|
+
* applies its schema to that key's value */
|
|
1466
|
+
if (pp_id != UINT32_MAX) {
|
|
1467
|
+
check_pattern_properties(j, pp_id, inst_id, depth);
|
|
1468
|
+
}
|
|
1469
|
+
/* propertyNames: the schema applies to each KEY string
|
|
1470
|
+
* (a different instance location: annotates nothing here) */
|
|
1471
|
+
{
|
|
1472
|
+
uint32_t pnv;
|
|
1473
|
+
if (kw_get(j, schema_id, "propertyNames", &pnv) && pnv != UINT32_MAX) {
|
|
1474
|
+
int pnf = 0;
|
|
1475
|
+
if (snode(j, pnv) != NULL && schema_truth(j, pnv, &pnf)) {
|
|
1476
|
+
check_property_names(j, pnv, inst_id, depth);
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
}
|
|
1480
|
+
/* min/maxProperties */
|
|
1481
|
+
{
|
|
1482
|
+
uint32_t mv;
|
|
1483
|
+
if (kw_get(j, schema_id, "minProperties", &mv) && mv != UINT32_MAX) {
|
|
1484
|
+
double b;
|
|
1485
|
+
if (schema_number(j, mv, &b) && (double)pairs < b) {
|
|
1486
|
+
yep_validate_emit(&j->v, YEPTRIS_DIAG_MIN_PROPERTIES,
|
|
1487
|
+
yep_validate_node_off(j->v.dom, inst_id),
|
|
1488
|
+
"%u properties is fewer than %g", pairs, b);
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
if (kw_get(j, schema_id, "maxProperties", &mv) && mv != UINT32_MAX) {
|
|
1492
|
+
double b;
|
|
1493
|
+
if (schema_number(j, mv, &b) && (double)pairs > b) {
|
|
1494
|
+
yep_validate_emit(&j->v, YEPTRIS_DIAG_MAX_PROPERTIES,
|
|
1495
|
+
yep_validate_node_off(j->v.dom, inst_id),
|
|
1496
|
+
"%u properties is more than %g", pairs, b);
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
}
|
|
1500
|
+
/* dependentRequired: name -> the names that must ride along */
|
|
1501
|
+
{
|
|
1502
|
+
uint32_t drv;
|
|
1503
|
+
if (kw_get(j, schema_id, "dependentRequired", &drv) && drv != UINT32_MAX) {
|
|
1504
|
+
check_dependent_required(j, drv, inst_id, depth);
|
|
1505
|
+
}
|
|
1506
|
+
}
|
|
1507
|
+
/* dependentSchemas: when the named key is present, its
|
|
1508
|
+
* schema applies to the whole object (same location: the
|
|
1509
|
+
* inner annotations join this object's set) */
|
|
1510
|
+
{
|
|
1511
|
+
uint32_t dsv;
|
|
1512
|
+
if (kw_get(j, schema_id, "dependentSchemas", &dsv) && dsv != UINT32_MAX) {
|
|
1513
|
+
check_dependent_schemas(j, dsv, inst_id, depth);
|
|
1514
|
+
}
|
|
1515
|
+
}
|
|
1516
|
+
/* additionalProperties: the keys properties does not name —
|
|
1517
|
+
* the keys it APPLIED to are this object's annotation */
|
|
929
1518
|
uint32_t ap;
|
|
930
1519
|
int af = 0;
|
|
931
1520
|
if (kw_schema(j, schema_id, "additionalProperties", &ap, &af)) {
|
|
@@ -942,15 +1531,38 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
|
|
|
942
1531
|
uint32_t pk = snode(j, props)->first_child;
|
|
943
1532
|
while (pk != UINT32_MAX && !known) {
|
|
944
1533
|
const yep_dnode* pkn = snode(j, pk);
|
|
945
|
-
uint32_t
|
|
1534
|
+
uint32_t pv2 =
|
|
946
1535
|
snode(j, pk) ? snode(j, pk)->next_sibling : UINT32_MAX;
|
|
947
1536
|
if (pkn != NULL && pkn->kind == YEP_DOM_SCALAR) {
|
|
948
1537
|
yep_view pw = yep_dom_view(j->sd, pkn->value);
|
|
949
1538
|
known = pw.len == kv.len &&
|
|
950
1539
|
(kv.len == 0 || memcmp(pw.p, kv.p, kv.len) == 0);
|
|
951
1540
|
}
|
|
952
|
-
pk =
|
|
953
|
-
|
|
1541
|
+
pk = pv2 != UINT32_MAX && snode(j, pv2)
|
|
1542
|
+
? snode(j, pv2)->next_sibling
|
|
1543
|
+
: UINT32_MAX;
|
|
1544
|
+
}
|
|
1545
|
+
}
|
|
1546
|
+
/* patternProperties matches take the key out of
|
|
1547
|
+
* additionalProperties' scope too (2020-12) */
|
|
1548
|
+
if (!known && pp_id != UINT32_MAX) {
|
|
1549
|
+
const yep_dnode* pnm = snode(j, pp_id);
|
|
1550
|
+
uint32_t pk = pnm->first_child;
|
|
1551
|
+
while (pk != UINT32_MAX && !known) {
|
|
1552
|
+
const yep_dnode* pkn = snode(j, pk);
|
|
1553
|
+
uint32_t pv2 =
|
|
1554
|
+
snode(j, pk) ? snode(j, pk)->next_sibling : UINT32_MAX;
|
|
1555
|
+
const char* pat = NULL;
|
|
1556
|
+
uint32_t pl = 0;
|
|
1557
|
+
if (pkn != NULL && pkn->kind == YEP_DOM_SCALAR &&
|
|
1558
|
+
schema_word(j, pk, &pat, &pl) &&
|
|
1559
|
+
yep_re_cache_search(j->cache, pat, pl, kv.p != NULL ? kv.p : "",
|
|
1560
|
+
kv.len) == 1) {
|
|
1561
|
+
known = 1;
|
|
1562
|
+
}
|
|
1563
|
+
pk = pv2 != UINT32_MAX && snode(j, pv2)
|
|
1564
|
+
? snode(j, pv2)->next_sibling
|
|
1565
|
+
: UINT32_MAX;
|
|
954
1566
|
}
|
|
955
1567
|
}
|
|
956
1568
|
if (!known) {
|
|
@@ -967,9 +1579,10 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
|
|
|
967
1579
|
} else {
|
|
968
1580
|
size_t mark = j->v.path_len;
|
|
969
1581
|
if (yep_validate_path_push(&j->v, namebuf, cl) == 0) {
|
|
970
|
-
|
|
1582
|
+
js_apply_child(j, ap, v, depth);
|
|
971
1583
|
yep_validate_path_pop(&j->v, mark);
|
|
972
1584
|
}
|
|
1585
|
+
ep_append(j, k);
|
|
973
1586
|
}
|
|
974
1587
|
}
|
|
975
1588
|
}
|
|
@@ -980,6 +1593,249 @@ static void js_apply(js_ctx* j, uint32_t schema_id, uint32_t inst_id, int depth)
|
|
|
980
1593
|
}
|
|
981
1594
|
}
|
|
982
1595
|
}
|
|
1596
|
+
|
|
1597
|
+
/* the unevaluated* keywords ride LAST in their schema object,
|
|
1598
|
+
* whatever the document order: their verdict reads every sibling
|
|
1599
|
+
* keyword's and every in-place applicator's annotations for THIS
|
|
1600
|
+
* location — [pmark, ep_len) / [imark, ei_len) */
|
|
1601
|
+
{
|
|
1602
|
+
uint32_t uv;
|
|
1603
|
+
int uf = 0;
|
|
1604
|
+
if (kw_schema(j, schema_id, "unevaluatedProperties", &uv, &uf)) {
|
|
1605
|
+
check_unevaluated_properties(j, uv, uf, inst_id, depth, pmark);
|
|
1606
|
+
}
|
|
1607
|
+
if (kw_schema(j, schema_id, "unevaluatedItems", &uv, &uf)) {
|
|
1608
|
+
check_unevaluated_items(j, uv, uf, inst_id, depth, imark);
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
|
|
1613
|
+
/* ---- the object-keyword walkers ------------------------------------------ */
|
|
1614
|
+
|
|
1615
|
+
/* patternProperties: pattern -> schema over the KEY strings; every
|
|
1616
|
+
* matching pattern applies its schema to the key's value (a false
|
|
1617
|
+
* schema rides js_apply's FALSE_SCHEMA diagnostic). The matched keys
|
|
1618
|
+
* are this object's annotation. */
|
|
1619
|
+
static void check_pattern_properties(js_ctx* j, uint32_t pp_id, uint32_t inst_id, int depth) {
|
|
1620
|
+
const yep_dom* d = j->v.dom;
|
|
1621
|
+
const yep_dnode* pnm = snode(j, pp_id);
|
|
1622
|
+
const yep_dnode* inst = yep_dom_node(d, inst_id);
|
|
1623
|
+
if (pnm == NULL || inst == NULL) {
|
|
1624
|
+
return;
|
|
1625
|
+
}
|
|
1626
|
+
uint32_t k = inst->first_child;
|
|
1627
|
+
while (k != UINT32_MAX && !j->v.oom) {
|
|
1628
|
+
uint32_t v = yep_dom_node(d, k) ? yep_dom_node(d, k)->next_sibling : UINT32_MAX;
|
|
1629
|
+
const yep_dnode* kn = yep_dom_node(d, k);
|
|
1630
|
+
uint32_t nk =
|
|
1631
|
+
v != UINT32_MAX && yep_dom_node(d, v) ? yep_dom_node(d, v)->next_sibling : UINT32_MAX;
|
|
1632
|
+
if (kn != NULL && kn->kind == YEP_DOM_SCALAR && v != UINT32_MAX) {
|
|
1633
|
+
yep_view kv = yep_dom_view(d, kn->value);
|
|
1634
|
+
uint32_t pk = pnm->first_child;
|
|
1635
|
+
while (pk != UINT32_MAX && !j->v.oom) {
|
|
1636
|
+
const yep_dnode* pkn = snode(j, pk);
|
|
1637
|
+
uint32_t pv = snode(j, pk) ? snode(j, pk)->next_sibling : UINT32_MAX;
|
|
1638
|
+
uint32_t np =
|
|
1639
|
+
pv != UINT32_MAX && snode(j, pv) ? snode(j, pv)->next_sibling : UINT32_MAX;
|
|
1640
|
+
const char* pat = NULL;
|
|
1641
|
+
uint32_t pl = 0;
|
|
1642
|
+
if (pkn != NULL && pkn->kind == YEP_DOM_SCALAR && pv != UINT32_MAX &&
|
|
1643
|
+
schema_word(j, pk, &pat, &pl) &&
|
|
1644
|
+
yep_re_cache_search(j->cache, pat, pl, kv.p != NULL ? kv.p : "", kv.len) == 1) {
|
|
1645
|
+
char namebuf[64];
|
|
1646
|
+
size_t cl = name_copy(namebuf, sizeof(namebuf), kv.p, kv.len);
|
|
1647
|
+
size_t mark = j->v.path_len;
|
|
1648
|
+
if (yep_validate_path_push(&j->v, namebuf, cl) == 0) {
|
|
1649
|
+
js_apply_child(j, pv, v, depth);
|
|
1650
|
+
yep_validate_path_pop(&j->v, mark);
|
|
1651
|
+
}
|
|
1652
|
+
ep_append(j, k);
|
|
1653
|
+
}
|
|
1654
|
+
pk = np;
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
k = nk;
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
|
|
1661
|
+
/* propertyNames: the schema applies to each KEY (the key string IS the
|
|
1662
|
+
* instance; a false propertyNames rides FALSE_SCHEMA at the key). */
|
|
1663
|
+
static void check_property_names(js_ctx* j, uint32_t pn_id, uint32_t inst_id, int depth) {
|
|
1664
|
+
const yep_dom* d = j->v.dom;
|
|
1665
|
+
const yep_dnode* inst = yep_dom_node(d, inst_id);
|
|
1666
|
+
if (inst == NULL) {
|
|
1667
|
+
return;
|
|
1668
|
+
}
|
|
1669
|
+
uint32_t k = inst->first_child;
|
|
1670
|
+
while (k != UINT32_MAX && !j->v.oom) {
|
|
1671
|
+
uint32_t v = yep_dom_node(d, k) ? yep_dom_node(d, k)->next_sibling : UINT32_MAX;
|
|
1672
|
+
const yep_dnode* kn = yep_dom_node(d, k);
|
|
1673
|
+
uint32_t nk =
|
|
1674
|
+
v != UINT32_MAX && yep_dom_node(d, v) ? yep_dom_node(d, v)->next_sibling : UINT32_MAX;
|
|
1675
|
+
if (kn != NULL && kn->kind == YEP_DOM_SCALAR) {
|
|
1676
|
+
yep_view kv = yep_dom_view(d, kn->value);
|
|
1677
|
+
char namebuf[64];
|
|
1678
|
+
size_t cl = name_copy(namebuf, sizeof(namebuf), kv.p, kv.len);
|
|
1679
|
+
size_t mark = j->v.path_len;
|
|
1680
|
+
if (yep_validate_path_push(&j->v, namebuf, cl) == 0) {
|
|
1681
|
+
js_apply_child(j, pn_id, k, depth);
|
|
1682
|
+
yep_validate_path_pop(&j->v, mark);
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
k = nk;
|
|
1686
|
+
}
|
|
1687
|
+
}
|
|
1688
|
+
|
|
1689
|
+
/* dependentRequired: name -> the names required alongside it. A
|
|
1690
|
+
* present trigger with an absent dependent is DIAG_DEPENDENT_REQUIRED
|
|
1691
|
+
* at the object (the missing leaf's path, the required convention). */
|
|
1692
|
+
static void check_dependent_required(js_ctx* j, uint32_t dr_id, uint32_t inst_id, int depth) {
|
|
1693
|
+
(void)depth;
|
|
1694
|
+
const yep_dom* d = j->v.dom;
|
|
1695
|
+
const yep_dnode* dm = snode(j, dr_id);
|
|
1696
|
+
if (dm == NULL || dm->kind != YEP_DOM_MAPPING) {
|
|
1697
|
+
return;
|
|
1698
|
+
}
|
|
1699
|
+
uint32_t sk = dm->first_child;
|
|
1700
|
+
while (sk != UINT32_MAX && !j->v.oom) {
|
|
1701
|
+
uint32_t sv = snode(j, sk) ? snode(j, sk)->next_sibling : UINT32_MAX;
|
|
1702
|
+
uint32_t ns = sv != UINT32_MAX && snode(j, sv) ? snode(j, sv)->next_sibling : UINT32_MAX;
|
|
1703
|
+
const yep_dnode* skn = snode(j, sk);
|
|
1704
|
+
if (skn != NULL && skn->kind == YEP_DOM_SCALAR && sv != UINT32_MAX) {
|
|
1705
|
+
yep_view trig = yep_dom_view(j->sd, skn->value);
|
|
1706
|
+
const yep_dnode* ln = snode(j, sv);
|
|
1707
|
+
if (trig.p != NULL && ln != NULL && ln->kind == YEP_DOM_SEQUENCE &&
|
|
1708
|
+
inst_find_key(d, inst_id, trig.p, trig.len, NULL) != UINT32_MAX) {
|
|
1709
|
+
char trigbuf[64];
|
|
1710
|
+
name_copy(trigbuf, sizeof(trigbuf), trig.p, trig.len);
|
|
1711
|
+
uint32_t rk = ln->first_child;
|
|
1712
|
+
while (rk != UINT32_MAX && !j->v.oom) {
|
|
1713
|
+
uint32_t rn = snode(j, rk) ? snode(j, rk)->next_sibling : UINT32_MAX;
|
|
1714
|
+
const char* name = NULL;
|
|
1715
|
+
uint32_t nl = 0;
|
|
1716
|
+
if (schema_word(j, rk, &name, &nl) &&
|
|
1717
|
+
inst_find_key(d, inst_id, name, nl, NULL) == UINT32_MAX) {
|
|
1718
|
+
/* the detail text is printf-composed: the view
|
|
1719
|
+
* is NOT NUL-terminated — %s reads past the
|
|
1720
|
+
* DOM allocation (ASan-caught); copy first */
|
|
1721
|
+
char namebuf[64];
|
|
1722
|
+
size_t cl = name_copy(namebuf, sizeof(namebuf), name, nl);
|
|
1723
|
+
size_t mark = j->v.path_len;
|
|
1724
|
+
int pushed = yep_validate_path_push(&j->v, namebuf, cl);
|
|
1725
|
+
yep_validate_emit(&j->v, YEPTRIS_DIAG_DEPENDENT_REQUIRED,
|
|
1726
|
+
yep_validate_node_off(j->v.dom, inst_id),
|
|
1727
|
+
"\"%s\" requires \"%s\"", trigbuf, namebuf);
|
|
1728
|
+
if (pushed == 0) {
|
|
1729
|
+
yep_validate_path_pop(&j->v, mark);
|
|
1730
|
+
}
|
|
1731
|
+
}
|
|
1732
|
+
rk = rn;
|
|
1733
|
+
}
|
|
1734
|
+
}
|
|
1735
|
+
}
|
|
1736
|
+
sk = ns;
|
|
1737
|
+
}
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1740
|
+
/* dependentSchemas: when the named key is present, its schema applies
|
|
1741
|
+
* to the whole object (same location: its annotations join this
|
|
1742
|
+
* object's set). */
|
|
1743
|
+
static void check_dependent_schemas(js_ctx* j, uint32_t ds_id, uint32_t inst_id, int depth) {
|
|
1744
|
+
const yep_dom* d = j->v.dom;
|
|
1745
|
+
const yep_dnode* dm = snode(j, ds_id);
|
|
1746
|
+
if (dm == NULL || dm->kind != YEP_DOM_MAPPING) {
|
|
1747
|
+
return;
|
|
1748
|
+
}
|
|
1749
|
+
uint32_t sk = dm->first_child;
|
|
1750
|
+
while (sk != UINT32_MAX && !j->v.oom) {
|
|
1751
|
+
uint32_t sv = snode(j, sk) ? snode(j, sk)->next_sibling : UINT32_MAX;
|
|
1752
|
+
uint32_t ns = sv != UINT32_MAX && snode(j, sv) ? snode(j, sv)->next_sibling : UINT32_MAX;
|
|
1753
|
+
const yep_dnode* skn = snode(j, sk);
|
|
1754
|
+
if (skn != NULL && skn->kind == YEP_DOM_SCALAR && sv != UINT32_MAX) {
|
|
1755
|
+
yep_view trig = yep_dom_view(j->sd, skn->value);
|
|
1756
|
+
if (trig.p != NULL && inst_find_key(d, inst_id, trig.p, trig.len, NULL) != UINT32_MAX) {
|
|
1757
|
+
js_apply(j, sv, inst_id, depth); /* absorbed */
|
|
1758
|
+
}
|
|
1759
|
+
}
|
|
1760
|
+
sk = ns;
|
|
1761
|
+
}
|
|
1762
|
+
}
|
|
1763
|
+
|
|
1764
|
+
/* unevaluatedProperties: the keys no sibling keyword and no in-place
|
|
1765
|
+
* applicator evaluated — [pmark, ep_len). A false schema flags each
|
|
1766
|
+
* one; a readable schema is APPLIED to the value (the applied keys
|
|
1767
|
+
* then annotate, so a nested unevaluatedProperties:true marks every
|
|
1768
|
+
* property evaluated for the parent). */
|
|
1769
|
+
static void check_unevaluated_properties(js_ctx* j, uint32_t upv, int upf, uint32_t inst_id,
|
|
1770
|
+
int depth, size_t pmark) {
|
|
1771
|
+
const yep_dom* d = j->v.dom;
|
|
1772
|
+
const yep_dnode* n = yep_dom_node(d, inst_id);
|
|
1773
|
+
if (n == NULL || n->kind != YEP_DOM_MAPPING) {
|
|
1774
|
+
return;
|
|
1775
|
+
}
|
|
1776
|
+
uint32_t k = n->first_child;
|
|
1777
|
+
while (k != UINT32_MAX && !j->v.oom) {
|
|
1778
|
+
uint32_t v = yep_dom_node(d, k) ? yep_dom_node(d, k)->next_sibling : UINT32_MAX;
|
|
1779
|
+
uint32_t nk =
|
|
1780
|
+
v != UINT32_MAX && yep_dom_node(d, v) ? yep_dom_node(d, v)->next_sibling : UINT32_MAX;
|
|
1781
|
+
const yep_dnode* kn = yep_dom_node(d, k);
|
|
1782
|
+
if (kn != NULL && kn->kind == YEP_DOM_SCALAR && !eval_has_prop(j, pmark, k)) {
|
|
1783
|
+
yep_view kv = yep_dom_view(d, kn->value);
|
|
1784
|
+
char namebuf[64];
|
|
1785
|
+
size_t cl = name_copy(namebuf, sizeof(namebuf), kv.p, kv.len);
|
|
1786
|
+
if (upf) {
|
|
1787
|
+
size_t mark = j->v.path_len;
|
|
1788
|
+
int pushed = yep_validate_path_push(&j->v, namebuf, cl);
|
|
1789
|
+
yep_validate_emit(&j->v, YEPTRIS_DIAG_UNEVALUATED_PROPERTY,
|
|
1790
|
+
yep_validate_node_off(d, k), "property \"%s\" is not evaluated",
|
|
1791
|
+
namebuf);
|
|
1792
|
+
if (pushed == 0) {
|
|
1793
|
+
yep_validate_path_pop(&j->v, mark);
|
|
1794
|
+
}
|
|
1795
|
+
} else {
|
|
1796
|
+
size_t mark = j->v.path_len;
|
|
1797
|
+
if (yep_validate_path_push(&j->v, namebuf, cl) == 0) {
|
|
1798
|
+
js_apply_child(j, upv, v, depth);
|
|
1799
|
+
yep_validate_path_pop(&j->v, mark);
|
|
1800
|
+
}
|
|
1801
|
+
ep_append(j, k); /* the applied property now counts as evaluated */
|
|
1802
|
+
}
|
|
1803
|
+
}
|
|
1804
|
+
k = nk;
|
|
1805
|
+
}
|
|
1806
|
+
}
|
|
1807
|
+
|
|
1808
|
+
/* unevaluatedItems: the mirror over array indices ([imark, ei_len)). */
|
|
1809
|
+
static void check_unevaluated_items(js_ctx* j, uint32_t uv, int uf, uint32_t inst_id, int depth,
|
|
1810
|
+
size_t imark) {
|
|
1811
|
+
const yep_dom* d = j->v.dom;
|
|
1812
|
+
const yep_dnode* n = yep_dom_node(d, inst_id);
|
|
1813
|
+
if (n == NULL || n->kind != YEP_DOM_SEQUENCE) {
|
|
1814
|
+
return;
|
|
1815
|
+
}
|
|
1816
|
+
uint32_t ich = n->first_child;
|
|
1817
|
+
for (uint32_t i = 0; ich != UINT32_MAX && !j->v.oom; i++) {
|
|
1818
|
+
uint32_t ni = yep_dom_node(d, ich) ? yep_dom_node(d, ich)->next_sibling : UINT32_MAX;
|
|
1819
|
+
if (!eval_has_item(j, imark, i)) {
|
|
1820
|
+
if (uf) {
|
|
1821
|
+
size_t mark = j->v.path_len;
|
|
1822
|
+
int pushed = yep_validate_path_push_index(&j->v, i);
|
|
1823
|
+
yep_validate_emit(&j->v, YEPTRIS_DIAG_UNEVALUATED_ITEM,
|
|
1824
|
+
yep_validate_node_off(d, ich), "item %u is not evaluated", i);
|
|
1825
|
+
if (pushed == 0) {
|
|
1826
|
+
yep_validate_path_pop(&j->v, mark);
|
|
1827
|
+
}
|
|
1828
|
+
} else {
|
|
1829
|
+
size_t mark = j->v.path_len;
|
|
1830
|
+
if (yep_validate_path_push_index(&j->v, i) == 0) {
|
|
1831
|
+
js_apply_child(j, uv, ich, depth);
|
|
1832
|
+
yep_validate_path_pop(&j->v, mark);
|
|
1833
|
+
}
|
|
1834
|
+
ei_append(j, i);
|
|
1835
|
+
}
|
|
1836
|
+
}
|
|
1837
|
+
ich = ni;
|
|
1838
|
+
}
|
|
983
1839
|
}
|
|
984
1840
|
|
|
985
1841
|
/* ---- the entry ----------------------------------------------------------- */
|
|
@@ -991,32 +1847,76 @@ static int js_validate_doc(js_ctx* j, uint32_t inst_root) {
|
|
|
991
1847
|
return j->v.oom ? -1 : 0;
|
|
992
1848
|
}
|
|
993
1849
|
|
|
994
|
-
/* The public
|
|
995
|
-
*
|
|
996
|
-
*
|
|
1850
|
+
/* The public entries live in validate.c/compile.c (yeptris_json_schema_
|
|
1851
|
+
* validate compiles+validates+frees through the compiled handle; the
|
|
1852
|
+
* compiled face drives this seam per instance stream): they parse the
|
|
1853
|
+
* sides, build the report and hand over the schema DOM, the retained
|
|
1854
|
+
* pattern cache and the identifier map (the declarations are
|
|
1855
|
+
* internal.h's — the shared SSOT). */
|
|
997
1856
|
int yep_jsonschema_run(const yep_dom* schema_dom, uint32_t schema_root, const yep_dom* instance_dom,
|
|
998
|
-
const uint32_t* doc_roots, uint32_t doc_count,
|
|
999
|
-
|
|
1857
|
+
const uint32_t* doc_roots, uint32_t doc_count, yeptris_validate_report* rep,
|
|
1858
|
+
const yep_re_cache* cache, const js_resources* res) {
|
|
1000
1859
|
js_ctx j;
|
|
1001
1860
|
memset(&j, 0, sizeof(j));
|
|
1002
1861
|
j.sd = schema_dom;
|
|
1003
1862
|
j.sroot = schema_root;
|
|
1863
|
+
j.cache = cache;
|
|
1864
|
+
j.res = res;
|
|
1004
1865
|
j.v.dom = instance_dom;
|
|
1005
1866
|
j.v.rep = rep;
|
|
1006
1867
|
j.v.input = instance_dom->input_base;
|
|
1007
1868
|
j.v.input_len = instance_dom->input_len;
|
|
1869
|
+
int rc = 0;
|
|
1008
1870
|
for (uint32_t i = 0; i < doc_count; i++) {
|
|
1009
1871
|
j.v.path_len = 0;
|
|
1872
|
+
/* the stream face's document attribution: a multi-document
|
|
1873
|
+
* stream addresses its documents as array elements */
|
|
1874
|
+
size_t mark = yep_validate_doc_prefix(&j.v, doc_count > 1, i);
|
|
1010
1875
|
js_validate_doc(&j, doc_roots[i]);
|
|
1876
|
+
yep_validate_path_pop(&j.v, mark);
|
|
1011
1877
|
if (j.v.oom) {
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
return -1;
|
|
1878
|
+
rc = -1;
|
|
1879
|
+
break;
|
|
1015
1880
|
}
|
|
1016
1881
|
}
|
|
1017
|
-
/* the walk's scratch (the lazy line index, the path buffer
|
|
1018
|
-
* here: the report carries only finished
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1882
|
+
/* the walk's scratch (the lazy line index, the path buffer, the
|
|
1883
|
+
* annotation arenas) dies here: the report carries only finished
|
|
1884
|
+
* diagnostics */
|
|
1885
|
+
const yep_allocator* sys = yep_system_allocator();
|
|
1886
|
+
yep_free(sys, j.v.line_off);
|
|
1887
|
+
yep_free(sys, j.v.path);
|
|
1888
|
+
yep_free(sys, j.ep);
|
|
1889
|
+
yep_free(sys, j.ei);
|
|
1890
|
+
return rc;
|
|
1891
|
+
}
|
|
1892
|
+
|
|
1893
|
+
/* The tag-driven hook's seam (internal.h, #906 slice 4): one bound
|
|
1894
|
+
* schema applied to ONE instance subtree through the CALLER'S walk
|
|
1895
|
+
* context. The context is copied in (its path prefix is the tag
|
|
1896
|
+
* site's — the subtree's instance pointers extend it), the schema
|
|
1897
|
+
* root is applied at depth 0 (the same walk, the same annotation
|
|
1898
|
+
* model), and the context is copied back: the report, the guards and
|
|
1899
|
+
* the scratch pointers stay the caller's, so the cap law and the
|
|
1900
|
+
* line/col resolution hold across the whole report. */
|
|
1901
|
+
int yep_jsonschema_apply_subtree(const yep_dom* schema_dom, uint32_t schema_root, yep_vctx* inst,
|
|
1902
|
+
uint32_t inst_root, const yep_re_cache* cache,
|
|
1903
|
+
const js_resources* res) {
|
|
1904
|
+
if (schema_dom == NULL || inst == NULL) {
|
|
1905
|
+
return 0;
|
|
1906
|
+
}
|
|
1907
|
+
js_ctx j;
|
|
1908
|
+
memset(&j, 0, sizeof(j));
|
|
1909
|
+
j.sd = schema_dom;
|
|
1910
|
+
j.sroot = schema_root;
|
|
1911
|
+
j.cache = cache;
|
|
1912
|
+
j.res = res;
|
|
1913
|
+
j.v = *inst; /* the caller's prefix, report, line index, guards */
|
|
1914
|
+
js_apply(&j, j.sroot, inst_root, 0);
|
|
1915
|
+
/* the annotation arenas are THIS application's scratch (the full
|
|
1916
|
+
* driver's tail law) — they never join the caller's context */
|
|
1917
|
+
const yep_allocator* sys = yep_system_allocator();
|
|
1918
|
+
yep_free(sys, j.ep);
|
|
1919
|
+
yep_free(sys, j.ei);
|
|
1920
|
+
*inst = j.v; /* scratch may have grown: the caller still frees it */
|
|
1921
|
+
return j.v.oom ? -1 : 0;
|
|
1022
1922
|
}
|