yeptris 0.6.57.1-arm-linux-eabihf → 0.6.59.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.
@@ -0,0 +1,900 @@
1
+ /* validate.c — the schema-family validation walk (#882 slice 1).
2
+ *
3
+ * One engine pass builds the DOM (the eager route: input-borrowed
4
+ * spans, no tape), one walk checks it against the family:
5
+ *
6
+ * - both families: explicit-tag consistency (the tag asserts a type
7
+ * the content cannot carry) and mapping-key uniqueness (spec
8
+ * 3.2.1.3: keys compare by canonical form — 0o13 and 0xB are ONE
9
+ * key; strings byte-wise; collections structurally);
10
+ * - 12_JSON only: mapping keys must be string scalars; plain scalars
11
+ * must match a JSON production (spec 10.2.2's `*` -> Error row);
12
+ * explicit non-JSON tags rejected.
13
+ *
14
+ * 11_COMPAT is the parse-compat resolver, not a validation family —
15
+ * the entry rejects it (ARG).
16
+ *
17
+ * MECE: parse/ owns the grammar (this walk only reads finished
18
+ * nodes), resolve/ owns typing (the family resolvers), emit/ owns
19
+ * bytes; diagnostics shape lives in validate.h. Allocation: the
20
+ * report and the scratch (line index, path, dedup table) come from
21
+ * the system allocator and are freed before return — nothing borrows
22
+ * the caller's buffer beyond the call.
23
+ */
24
+
25
+ #include <stdio.h>
26
+ #include <stdlib.h>
27
+ #include <string.h>
28
+
29
+ #include <yeptris/dom.h>
30
+ #include <yeptris/validate.h>
31
+
32
+ #include "../common/error.h"
33
+ #include "../doc.h"
34
+ #include "../memory/allocator.h"
35
+ #include "../parse/numbers.h"
36
+ #include "../resolve/resolver.h"
37
+ #include "../tape_in.h"
38
+ #include "internal.h"
39
+
40
+ /* ---- line/col resolution ---------------------------------------------- */
41
+
42
+ /* Builds the line-start index once, on the first positioned diagnostic;
43
+ * valid documents never pay. Returns 0 ok, -1 OOM (positions unknown). */
44
+ static int line_index_build(yep_vctx* c) {
45
+ if (c->line_off != NULL) {
46
+ return 0;
47
+ }
48
+ const yep_allocator* sys = yep_system_allocator();
49
+ uint32_t cap = 256;
50
+ uint32_t n = 0;
51
+ uint32_t* off = yep_alloc(sys, cap * sizeof(uint32_t));
52
+ if (off == NULL) {
53
+ return -1;
54
+ }
55
+ off[n++] = 0;
56
+ const char* p = c->input;
57
+ for (size_t i = 0; i < c->input_len; i++) {
58
+ if (p[i] == '\n') {
59
+ if (n == cap) {
60
+ uint32_t ncap = cap * 2;
61
+ uint32_t* no = yep_alloc(sys, (size_t)ncap * sizeof(uint32_t));
62
+ if (no == NULL) {
63
+ yep_free(sys, off);
64
+ return -1;
65
+ }
66
+ memcpy(no, off, (size_t)n * sizeof(uint32_t));
67
+ yep_free(sys, off);
68
+ off = no;
69
+ cap = ncap;
70
+ }
71
+ off[n++] = (uint32_t)(i + 1);
72
+ }
73
+ }
74
+ c->line_off = off;
75
+ c->line_cnt = n;
76
+ c->line_cap = cap;
77
+ return 0;
78
+ }
79
+
80
+ /* 1-based line/col of a byte offset; columns are code points (the
81
+ * libyaml mark semantics — multibyte characters advance one). */
82
+ static void line_col_of(yep_vctx* c, uint32_t off, uint32_t* line, uint32_t* col) {
83
+ *line = 0;
84
+ *col = 0;
85
+ if (off == UINT32_MAX || off >= c->input_len) {
86
+ return;
87
+ }
88
+ if (line_index_build(c) != 0) {
89
+ return;
90
+ }
91
+ uint32_t lo = 0, hi = c->line_cnt; /* find the last line start <= off */
92
+ while (lo + 1 < hi) {
93
+ uint32_t mid = lo + (hi - lo) / 2;
94
+ if (c->line_off[mid] <= off) {
95
+ lo = mid;
96
+ } else {
97
+ hi = mid;
98
+ }
99
+ }
100
+ uint32_t start = c->line_off[lo];
101
+ uint32_t chars = 0;
102
+ for (uint32_t i = start; i <= off; i++) {
103
+ chars += ((((unsigned char)c->input[i]) & 0xC0) != 0x80);
104
+ }
105
+ *line = lo + 1;
106
+ *col = chars;
107
+ }
108
+
109
+ /* ---- path scratch ------------------------------------------------------ */
110
+
111
+ static int path_reserve(yep_vctx* c, size_t need) {
112
+ if (c->path_len + need <= c->path_cap) {
113
+ return 0;
114
+ }
115
+ const yep_allocator* sys = yep_system_allocator();
116
+ size_t ncap = c->path_cap ? c->path_cap * 2 : 256;
117
+ while (ncap < c->path_len + need) {
118
+ ncap *= 2;
119
+ }
120
+ char* np = yep_alloc(sys, ncap);
121
+ if (np == NULL) {
122
+ c->oom = 1;
123
+ return -1;
124
+ }
125
+ if (c->path != NULL) {
126
+ memcpy(np, c->path, c->path_len);
127
+ yep_free(sys, c->path);
128
+ }
129
+ c->path = np;
130
+ c->path_cap = ncap;
131
+ return 0;
132
+ }
133
+
134
+ int yep_validate_path_push(yep_vctx* c, const char* seg, size_t seg_len) {
135
+ /* JSON-pointer escapes: ~ -> ~0, / -> ~1 */
136
+ size_t need = 1 + seg_len + 2; /* worst case: every byte escapes */
137
+ for (size_t i = 0; i < seg_len; i++) {
138
+ if (seg[i] == '~' || seg[i] == '/') {
139
+ need++;
140
+ }
141
+ }
142
+ if (path_reserve(c, need) != 0) {
143
+ return -1;
144
+ }
145
+ c->path[c->path_len++] = '/';
146
+ for (size_t i = 0; i < seg_len; i++) {
147
+ char b = seg[i];
148
+ if (b == '~') {
149
+ c->path[c->path_len++] = '~';
150
+ c->path[c->path_len++] = '0';
151
+ } else if (b == '/') {
152
+ c->path[c->path_len++] = '~';
153
+ c->path[c->path_len++] = '1';
154
+ } else {
155
+ c->path[c->path_len++] = b;
156
+ }
157
+ }
158
+ return 0;
159
+ }
160
+
161
+ int yep_validate_path_push_index(yep_vctx* c, uint32_t idx) {
162
+ char buf[12];
163
+ int n = snprintf(buf, sizeof(buf), "%u", idx);
164
+ return yep_validate_path_push(c, buf, (size_t)n);
165
+ }
166
+
167
+ void yep_validate_path_pop(yep_vctx* c, size_t mark) {
168
+ c->path_len = mark;
169
+ }
170
+
171
+ /* ---- the report --------------------------------------------------------- */
172
+
173
+ void yep_validate_emit(yep_vctx* c, int code, uint32_t off, const char* detail_fmt, ...) {
174
+ if (c->suppress > 0) {
175
+ c->trial_hit = 1; /* a probe (anyOf/oneOf/not): swallow and mark */
176
+ return;
177
+ }
178
+ yeptris_validate_report* r = c->rep;
179
+ if (r->count >= YEP_VALIDATE_MAX_DIAGS) {
180
+ /* the cap: the LAST slot becomes the LIMIT sentinel */
181
+ r->truncated = 1;
182
+ yeptris_diagnostic* last = &r->diags[r->count - 1];
183
+ memset(last, 0, sizeof(*last));
184
+ last->code = YEPTRIS_DIAG_LIMIT;
185
+ snprintf(last->detail, sizeof(last->detail), "diagnostics truncated at %d",
186
+ YEP_VALIDATE_MAX_DIAGS);
187
+ return;
188
+ }
189
+ yeptris_diagnostic* d = &r->diags[r->count];
190
+ memset(d, 0, sizeof(*d));
191
+ d->code = (uint32_t)code;
192
+ if (off != UINT32_MAX && off < c->input_len) {
193
+ d->offset = off;
194
+ line_col_of(c, off, &d->line, &d->col);
195
+ }
196
+ size_t plen = c->path_len < YEPTRIS_DIAG_PATH_MAX - 1 ? c->path_len : YEPTRIS_DIAG_PATH_MAX - 1;
197
+ if (c->path != NULL) {
198
+ memcpy(d->path, c->path, plen);
199
+ }
200
+ va_list ap;
201
+ va_start(ap, detail_fmt);
202
+ vsnprintf(d->detail, sizeof(d->detail), detail_fmt, ap);
203
+ va_end(ap);
204
+ r->count++;
205
+ }
206
+
207
+ /* ---- node facts --------------------------------------------------------- */
208
+
209
+ uint32_t yep_validate_node_off(const yep_dom* d, uint32_t id) {
210
+ uint32_t guard = 0;
211
+ while (id != UINT32_MAX && guard++ < YEP_DOM_MAX_DEPTH) {
212
+ const yep_dnode* n = yep_dom_node(d, id);
213
+ if (n == NULL) {
214
+ return UINT32_MAX;
215
+ }
216
+ if (n->kind == YEP_DOM_SCALAR || n->kind == YEP_DOM_ALIAS) {
217
+ if (n->value.len > 0 && (n->value.off & YEP_SV_INPUT) == 0) {
218
+ return n->value.off & YEP_SV_OFF; /* borrowed from the input */
219
+ }
220
+ return UINT32_MAX; /* arena-owned bytes: no input position */
221
+ }
222
+ id = n->first_child; /* collections: position at their first leaf */
223
+ }
224
+ return UINT32_MAX;
225
+ }
226
+
227
+ /* ---- canonical key/value facts (shared with jsonschema.c) ---------------- */
228
+
229
+ /* The resolved tag id of a scalar node (explicit tag wins, then the
230
+ * plain verdict already on the node, then str for quoted/block). */
231
+ uint8_t yep_validate_scalar_tag(const yep_dnode* n) {
232
+ if (n->tag.len > 0) {
233
+ return n->tag_id;
234
+ }
235
+ /* implicit marks the resolver's verdict even at style ANY (the
236
+ * document-level empty scalar carries no token to style) */
237
+ if (n->style == YEP_STYLE_PLAIN || n->implicit) {
238
+ return n->tag_id;
239
+ }
240
+ return YEPTRIS_TAG_STR;
241
+ }
242
+
243
+ /* Canonical hash of a node (the key-uniqueness fingerprint): tags and
244
+ * canonical forms, numeric values for int/float. */
245
+ static uint64_t hash_mix(uint64_t h, uint64_t v) {
246
+ h ^= v + 0x9E3779B97F4A7C15ULL + (h << 6) + (h >> 2);
247
+ return h;
248
+ }
249
+
250
+ uint64_t yep_validate_node_hash(const yep_dom* d, uint32_t id, int depth) {
251
+ if (depth > 64) {
252
+ return 0xDA7A;
253
+ }
254
+ const yep_dnode* n = yep_dom_node(d, id);
255
+ if (n == NULL) {
256
+ return 0;
257
+ }
258
+ if (n->kind == YEP_DOM_ALIAS) {
259
+ return n->target != UINT32_MAX ? yep_validate_node_hash(d, n->target, depth + 1)
260
+ : hash_mix(0xA11A, n->value.len);
261
+ }
262
+ if (n->kind == YEP_DOM_SEQUENCE) {
263
+ uint64_t h = 0x5EED51ULL;
264
+ uint32_t ch = n->first_child;
265
+ while (ch != UINT32_MAX) {
266
+ h = hash_mix(h, yep_validate_node_hash(d, ch, depth + 1));
267
+ ch = yep_dom_node(d, ch) ? yep_dom_node(d, ch)->next_sibling : UINT32_MAX;
268
+ }
269
+ return h;
270
+ }
271
+ if (n->kind == YEP_DOM_MAPPING) {
272
+ /* mappings are UNORDERED (spec 3.2.1.3 canonical form): the
273
+ * fold is commutative (XOR of per-pair mixes) so key order
274
+ * cannot change the fingerprint */
275
+ uint64_t h = 0x5EED4A9ULL;
276
+ uint32_t k = n->first_child;
277
+ while (k != UINT32_MAX) {
278
+ uint32_t v = yep_dom_node(d, k) ? yep_dom_node(d, k)->next_sibling : UINT32_MAX;
279
+ uint64_t pair = hash_mix(0x51D1, yep_validate_node_hash(d, k, depth + 1));
280
+ pair = hash_mix(pair, yep_validate_node_hash(d, v, depth + 1));
281
+ h ^= pair;
282
+ k = v != UINT32_MAX && yep_dom_node(d, v) ? yep_dom_node(d, v)->next_sibling
283
+ : UINT32_MAX;
284
+ }
285
+ return h;
286
+ }
287
+ /* scalar: tag + canonical form */
288
+ uint8_t tag = yep_validate_scalar_tag(n);
289
+ yep_view v = yep_dom_view(d, n->value);
290
+ uint64_t h = 0x5CA1A + tag;
291
+ if (tag == YEPTRIS_TAG_INT || tag == YEPTRIS_TAG_FLOAT) {
292
+ /* canonical: the numeric VALUE, one bucket for both tags (1 and
293
+ * 1.0 are the same canonical number) — the double bits; exact
294
+ * for i64-representable magnitudes up to 2^53, and beyond that
295
+ * two distinct ints may collide (equality is the authority, a
296
+ * collision only costs a probe) */
297
+ double dv = 0;
298
+ int64_t iv = 0;
299
+ if (yep_num_i64(v.p, v.len, &iv) == 0) {
300
+ dv = (double)iv;
301
+ } else {
302
+ yep_num_f64(v.p, v.len, &dv);
303
+ }
304
+ uint64_t bits;
305
+ memcpy(&bits, &dv, sizeof(bits));
306
+ h = hash_mix(h, bits);
307
+ return h;
308
+ }
309
+ for (uint32_t i = 0; i < v.len; i++) {
310
+ h = hash_mix(h, (unsigned char)v.p[i]);
311
+ }
312
+ return h;
313
+ }
314
+
315
+ /* Canonical equality (spec 3.2.1.3): same tag and canonical form.
316
+ * Collections compare structurally; numbers within one tag compare
317
+ * numerically. The two sides may live in different DOMs (the JSON
318
+ * Schema face compares instances against a schema's enum/const).
319
+ * Depth-bounded like the hashes. */
320
+ int yep_validate_node_equal(const yep_dom* da, uint32_t a, const yep_dom* db, uint32_t b,
321
+ int depth) {
322
+ if (depth > 64) {
323
+ return 0;
324
+ }
325
+ const yep_dnode* na = yep_dom_node(da, a);
326
+ const yep_dnode* nb = yep_dom_node(db, b);
327
+ if (na == NULL || nb == NULL) {
328
+ return 0;
329
+ }
330
+ if (na->kind == YEP_DOM_ALIAS) {
331
+ return na->target != UINT32_MAX &&
332
+ yep_validate_node_equal(da, na->target, db, b, depth + 1);
333
+ }
334
+ if (nb->kind == YEP_DOM_ALIAS) {
335
+ return nb->target != UINT32_MAX &&
336
+ yep_validate_node_equal(da, a, db, nb->target, depth + 1);
337
+ }
338
+ if (na->kind != nb->kind) {
339
+ return 0;
340
+ }
341
+ if (na->kind == YEP_DOM_SEQUENCE) {
342
+ if (na->count != nb->count) {
343
+ return 0;
344
+ }
345
+ uint32_t ca = na->first_child, cb = nb->first_child;
346
+ while (ca != UINT32_MAX && cb != UINT32_MAX) {
347
+ if (!yep_validate_node_equal(da, ca, db, cb, depth + 1)) {
348
+ return 0;
349
+ }
350
+ const yep_dnode* da2 = yep_dom_node(da, ca);
351
+ const yep_dnode* db2 = yep_dom_node(db, cb);
352
+ ca = da2 ? da2->next_sibling : UINT32_MAX;
353
+ cb = db2 ? db2->next_sibling : UINT32_MAX;
354
+ }
355
+ return ca == UINT32_MAX && cb == UINT32_MAX;
356
+ }
357
+ if (na->kind == YEP_DOM_MAPPING) {
358
+ if (na->count != nb->count) {
359
+ return 0;
360
+ }
361
+ uint32_t ka = na->first_child;
362
+ while (ka != UINT32_MAX) {
363
+ uint32_t va = yep_dom_node(da, ka) ? yep_dom_node(da, ka)->next_sibling : UINT32_MAX;
364
+ /* find ka's key in b */
365
+ uint32_t kb = nb->first_child;
366
+ int found = 0;
367
+ while (kb != UINT32_MAX && !found) {
368
+ uint32_t vb =
369
+ yep_dom_node(db, kb) ? yep_dom_node(db, kb)->next_sibling : UINT32_MAX;
370
+ if (yep_validate_node_equal(da, ka, db, kb, depth + 1)) {
371
+ if (!yep_validate_node_equal(da, va, db, vb, depth + 1)) {
372
+ return 0;
373
+ }
374
+ found = 1;
375
+ }
376
+ kb = vb != UINT32_MAX && yep_dom_node(db, vb) ? yep_dom_node(db, vb)->next_sibling
377
+ : UINT32_MAX;
378
+ }
379
+ if (!found) {
380
+ return 0;
381
+ }
382
+ ka = va != UINT32_MAX && yep_dom_node(da, va) ? yep_dom_node(da, va)->next_sibling
383
+ : UINT32_MAX;
384
+ }
385
+ return 1;
386
+ }
387
+ /* scalars: numbers (int or float, either tag) compare by numeric
388
+ * value (2020-12: 0 and 0.0 are one constant); everything else
389
+ * requires the same tag */
390
+ uint8_t ta = yep_validate_scalar_tag(na), tb = yep_validate_scalar_tag(nb);
391
+ int numa = ta == YEPTRIS_TAG_INT || ta == YEPTRIS_TAG_FLOAT;
392
+ int numb = tb == YEPTRIS_TAG_INT || tb == YEPTRIS_TAG_FLOAT;
393
+ if (numa != numb) {
394
+ return 0;
395
+ }
396
+ yep_view va = yep_dom_view(da, na->value);
397
+ yep_view vb = yep_dom_view(db, nb->value);
398
+ if (numa) {
399
+ int64_t ia, ib;
400
+ if (yep_num_i64(va.p, va.len, &ia) == 0 && yep_num_i64(vb.p, vb.len, &ib) == 0) {
401
+ return ia == ib;
402
+ }
403
+ double fa, fb;
404
+ if (yep_num_f64(va.p, va.len, &fa) == 0 && yep_num_f64(vb.p, vb.len, &fb) == 0) {
405
+ return fa == fb;
406
+ }
407
+ return 0;
408
+ }
409
+ if (ta != tb) {
410
+ return 0;
411
+ }
412
+ return va.len == vb.len && (va.len == 0 || memcmp(va.p, vb.p, va.len) == 0);
413
+ }
414
+
415
+ /* ---- the family walk ---------------------------------------------------- */
416
+
417
+ typedef struct {
418
+ uint32_t hash;
419
+ uint32_t node; /* 0 = empty slot (node 0 is the DOCUMENT root, never
420
+ * a key inside itself) */
421
+ } key_slot;
422
+
423
+ /* Duplicate-key detection over one mapping: open addressing, expected
424
+ * O(pairs). An allocation failure silently skips (the walk's other
425
+ * checks still run; oom flags the context). */
426
+ static void check_duplicates(yep_vctx* c, uint32_t map_id) {
427
+ const yep_dom* d = c->dom;
428
+ const yep_dnode* m = yep_dom_node(d, map_id);
429
+ uint32_t pairs = m->count / 2;
430
+ if (pairs < 2) {
431
+ return;
432
+ }
433
+ const yep_allocator* sys = yep_system_allocator();
434
+ uint32_t cap = 8;
435
+ while (cap < pairs * 4) {
436
+ cap *= 2;
437
+ }
438
+ key_slot* tbl = yep_alloc(sys, (size_t)cap * sizeof(key_slot));
439
+ if (tbl == NULL) {
440
+ c->oom = 1;
441
+ return;
442
+ }
443
+ memset(tbl, 0, (size_t)cap * sizeof(key_slot));
444
+ uint32_t k = m->first_child;
445
+ int reported = 0;
446
+ while (k != UINT32_MAX) {
447
+ uint32_t v = yep_dom_node(d, k) ? yep_dom_node(d, k)->next_sibling : UINT32_MAX;
448
+ uint64_t h64 = yep_validate_node_hash(d, k, 0);
449
+ uint32_t h = (uint32_t)(h64 ^ (h64 >> 32));
450
+ uint32_t i = h & (cap - 1);
451
+ while (tbl[i].node != 0) {
452
+ if (tbl[i].hash == h && yep_validate_node_equal(d, tbl[i].node - 1, d, k, 0)) {
453
+ if (!reported) {
454
+ reported = 1;
455
+ uint32_t off = yep_validate_node_off(d, k);
456
+ yep_view kv = yep_dom_view(d, yep_dom_node(d, k)->value);
457
+ char key[40];
458
+ size_t kl = kv.len < sizeof(key) - 8 ? kv.len : sizeof(key) - 8;
459
+ if (kl > 0 && kv.p != NULL) {
460
+ memcpy(key, kv.p, kl);
461
+ }
462
+ key[kl] = '\0';
463
+ yep_validate_emit(c, YEPTRIS_DIAG_DUPLICATE_KEY, off,
464
+ "duplicate mapping key (\"%s\")", key);
465
+ }
466
+ break;
467
+ }
468
+ i = (i + 1) & (cap - 1);
469
+ }
470
+ if (tbl[i].node == 0) {
471
+ tbl[i].hash = h;
472
+ tbl[i].node = k + 1; /* +1: node 0 means empty */
473
+ }
474
+ k = v != UINT32_MAX && yep_dom_node(d, v) ? yep_dom_node(d, v)->next_sibling : UINT32_MAX;
475
+ }
476
+ yep_free(sys, tbl);
477
+ }
478
+
479
+ static const char* tag_word(uint8_t id) {
480
+ switch (id) {
481
+ case YEPTRIS_TAG_STR:
482
+ return "string";
483
+ case YEPTRIS_TAG_INT:
484
+ return "int";
485
+ case YEPTRIS_TAG_FLOAT:
486
+ return "float";
487
+ case YEPTRIS_TAG_BOOL:
488
+ return "bool";
489
+ case YEPTRIS_TAG_NULL:
490
+ return "null";
491
+ case YEPTRIS_TAG_TIMESTAMP:
492
+ return "timestamp";
493
+ case YEPTRIS_TAG_SEQ:
494
+ return "sequence";
495
+ case YEPTRIS_TAG_MAP:
496
+ return "mapping";
497
+ case YEPTRIS_TAG_BINARY:
498
+ return "binary";
499
+ case YEPTRIS_TAG_MERGE:
500
+ return "merge";
501
+ case YEPTRIS_TAG_VALUE:
502
+ return "value";
503
+ default:
504
+ return "custom";
505
+ }
506
+ }
507
+
508
+ /* Explicit-tag consistency (both families): the tag asserts a type the
509
+ * content cannot carry. seq/map check the node KIND; the five core
510
+ * scalar tags re-resolve the content through the family resolver. */
511
+ static void check_tag(yep_vctx* c, uint32_t id, const yep_resolver* fam, int json_family) {
512
+ const yep_dom* d = c->dom;
513
+ const yep_dnode* n = yep_dom_node(d, id);
514
+ if (n->tag.len == 0) {
515
+ return; /* no explicit tag */
516
+ }
517
+ uint8_t want = n->tag_id;
518
+ const char* w = tag_word(want);
519
+ if (want == YEPTRIS_TAG_SEQ || want == YEPTRIS_TAG_MAP || want == YEPTRIS_TAG_STR) {
520
+ int ok = want == YEPTRIS_TAG_STR
521
+ ? n->kind == YEP_DOM_SCALAR
522
+ : (want == YEPTRIS_TAG_SEQ && n->kind == YEP_DOM_SEQUENCE) ||
523
+ (want == YEPTRIS_TAG_MAP && n->kind == YEP_DOM_MAPPING);
524
+ if (!ok) {
525
+ yep_validate_emit(c, YEPTRIS_DIAG_TAG_MISMATCH, yep_validate_node_off(d, id),
526
+ "explicit %s tag on a %s", w,
527
+ n->kind == YEP_DOM_SCALAR
528
+ ? "scalar"
529
+ : (n->kind == YEP_DOM_SEQUENCE ? "sequence" : "mapping"));
530
+ }
531
+ return;
532
+ }
533
+ if (want >= YEPTRIS_TAG_INT && want <= YEPTRIS_TAG_NULL) {
534
+ if (n->kind != YEP_DOM_SCALAR) {
535
+ yep_validate_emit(c, YEPTRIS_DIAG_TAG_MISMATCH, yep_validate_node_off(d, id),
536
+ "explicit %s tag on a %s", w,
537
+ n->kind == YEP_DOM_SEQUENCE ? "sequence" : "mapping");
538
+ return;
539
+ }
540
+ yep_view v = yep_dom_view(d, n->value);
541
+ uint8_t got = fam->resolve(fam->ctx, v.p != NULL ? v.p : "", v.len);
542
+ if (got != want) {
543
+ if (json_family) {
544
+ yep_validate_emit(c, YEPTRIS_DIAG_TAG_MISMATCH, yep_validate_node_off(d, id),
545
+ "explicit %s tag; content is not a JSON %s", w, w);
546
+ } else {
547
+ yep_validate_emit(c, YEPTRIS_DIAG_TAG_MISMATCH, yep_validate_node_off(d, id),
548
+ "explicit %s tag on %s content", w, tag_word(got));
549
+ }
550
+ }
551
+ return;
552
+ }
553
+ /* timestamp/binary/merge/value/custom: consumer-defined under core;
554
+ * the JSON family rejects them wholesale (checked by the caller). */
555
+ }
556
+
557
+ /* json12: a mapping key must be a string scalar. */
558
+ static void check_json_key(yep_vctx* c, uint32_t key_id) {
559
+ const yep_dom* d = c->dom;
560
+ const yep_dnode* n = yep_dom_node(d, key_id);
561
+ if (n->kind == YEP_DOM_ALIAS) {
562
+ if (n->target != UINT32_MAX) {
563
+ check_json_key(c, n->target);
564
+ }
565
+ return;
566
+ }
567
+ if (n->kind != YEP_DOM_SCALAR) {
568
+ uint32_t off = yep_validate_node_off(d, key_id);
569
+ yep_validate_emit(c, YEPTRIS_DIAG_KEY_NOT_STRING, off, "mapping key is a %s",
570
+ n->kind == YEP_DOM_SEQUENCE ? "sequence" : "mapping");
571
+ return;
572
+ }
573
+ uint32_t off = yep_validate_node_off(d, key_id);
574
+ if (n->tag.len > 0) {
575
+ uint8_t want = n->tag_id;
576
+ if (want == YEPTRIS_TAG_STR) {
577
+ return;
578
+ }
579
+ if (want >= YEPTRIS_TAG_INT && want <= YEPTRIS_TAG_NULL) {
580
+ yep_validate_emit(c, YEPTRIS_DIAG_KEY_NOT_STRING, off,
581
+ "mapping key is %s (expected a "
582
+ "string)",
583
+ tag_word(want));
584
+ } else {
585
+ yep_validate_emit(c, YEPTRIS_DIAG_NON_JSON_TYPE, off,
586
+ "mapping key carries a non-JSON "
587
+ "tag");
588
+ }
589
+ return;
590
+ }
591
+ if (!(n->implicit || n->style == YEP_STYLE_PLAIN)) {
592
+ return; /* quoted/block keys are strings */
593
+ }
594
+ yep_view v = yep_dom_view(d, n->value);
595
+ int cls = yep_json12_plain(v.p != NULL ? v.p : "", v.len);
596
+ if (cls == 0) {
597
+ yep_validate_emit(c, YEPTRIS_DIAG_NON_JSON_TYPE, off,
598
+ "plain scalar matches no JSON production");
599
+ return;
600
+ }
601
+ if (cls != (int)YEPTRIS_TAG_STR) {
602
+ yep_validate_emit(c, YEPTRIS_DIAG_KEY_NOT_STRING, off,
603
+ "mapping key is %s (expected a "
604
+ "string)",
605
+ tag_word((uint8_t)cls));
606
+ }
607
+ }
608
+
609
+ /* json12: plain scalars must match a JSON production; explicit
610
+ * non-JSON tags rejected (the caller runs check_tag separately). */
611
+ static void check_json_scalar(yep_vctx* c, uint32_t id) {
612
+ const yep_dom* d = c->dom;
613
+ const yep_dnode* n = yep_dom_node(d, id);
614
+ if (n->tag.len > 0) {
615
+ uint8_t want = n->tag_id;
616
+ if (want == YEPTRIS_TAG_TIMESTAMP || want == YEPTRIS_TAG_BINARY ||
617
+ want == YEPTRIS_TAG_MERGE || want == YEPTRIS_TAG_VALUE || want == YEPTRIS_TAG_CUSTOM) {
618
+ yep_validate_emit(c, YEPTRIS_DIAG_NON_JSON_TYPE, yep_validate_node_off(d, id),
619
+ "explicit %s tag is not a JSON type", tag_word(want));
620
+ }
621
+ return;
622
+ }
623
+ if (!(n->implicit || n->style == YEP_STYLE_PLAIN)) {
624
+ return; /* quoted/block scalars are strings */
625
+ }
626
+ yep_view v = yep_dom_view(d, n->value);
627
+ if (yep_json12_plain(v.p != NULL ? v.p : "", v.len) == 0) {
628
+ yep_validate_emit(c, YEPTRIS_DIAG_NON_JSON_TYPE, yep_validate_node_off(d, id),
629
+ "plain scalar matches no JSON production");
630
+ }
631
+ }
632
+
633
+ static void walk_family(yep_vctx* c, uint32_t id, const yep_resolver* fam, int json_family);
634
+
635
+ static void walk_children(yep_vctx* c, uint32_t first, const yep_resolver* fam, int json_family) {
636
+ uint32_t ch = first;
637
+ int guard = 0;
638
+ while (ch != UINT32_MAX && !c->oom) {
639
+ if (++guard > (int)c->dom->ncount) {
640
+ break; /* defensive: the sibling list is parse-built */
641
+ }
642
+ const yep_dnode* n = yep_dom_node(c->dom, ch);
643
+ uint32_t next = n ? n->next_sibling : UINT32_MAX;
644
+ walk_family(c, ch, fam, json_family);
645
+ ch = next;
646
+ }
647
+ }
648
+
649
+ static void walk_family(yep_vctx* c, uint32_t id, const yep_resolver* fam, int json_family) {
650
+ if (c->oom) {
651
+ return;
652
+ }
653
+ if (++c->depth > YEP_DOM_MAX_DEPTH) {
654
+ if (c->depth == YEP_DOM_MAX_DEPTH + 1) {
655
+ yep_validate_emit(c, YEPTRIS_DIAG_DEPTH, UINT32_MAX,
656
+ "validation walk exceeded the nesting limit");
657
+ }
658
+ c->depth--;
659
+ return;
660
+ }
661
+ const yep_dnode* n = yep_dom_node(c->dom, id);
662
+ if (n == NULL) {
663
+ c->depth--;
664
+ return;
665
+ }
666
+ switch (n->kind) {
667
+ case YEP_DOM_SCALAR:
668
+ check_tag(c, id, fam, json_family);
669
+ if (json_family) {
670
+ check_json_scalar(c, id);
671
+ }
672
+ break;
673
+ case YEP_DOM_ALIAS:
674
+ break; /* the target is validated at its own place */
675
+ case YEP_DOM_SEQUENCE: {
676
+ check_tag(c, id, fam, json_family);
677
+ size_t mark = c->path_len;
678
+ uint32_t idx = 0;
679
+ uint32_t ch = n->first_child;
680
+ while (ch != UINT32_MAX && !c->oom) {
681
+ const yep_dnode* cn = yep_dom_node(c->dom, ch);
682
+ uint32_t next = cn ? cn->next_sibling : UINT32_MAX;
683
+ if (yep_validate_path_push_index(c, idx++) == 0) {
684
+ walk_family(c, ch, fam, json_family);
685
+ yep_validate_path_pop(c, mark);
686
+ }
687
+ ch = next;
688
+ }
689
+ break;
690
+ }
691
+ case YEP_DOM_MAPPING: {
692
+ check_tag(c, id, fam, json_family);
693
+ size_t mark = c->path_len;
694
+ check_duplicates(c, id);
695
+ uint32_t k = n->first_child;
696
+ while (k != UINT32_MAX && !c->oom) {
697
+ uint32_t v =
698
+ yep_dom_node(c->dom, k) ? yep_dom_node(c->dom, k)->next_sibling : UINT32_MAX;
699
+ if (json_family) {
700
+ check_json_key(c, k);
701
+ }
702
+ /* keys: tag-checked like any scalar; collection keys walk
703
+ * under the mapping's path (their own path is undefined) */
704
+ const yep_dnode* kn = yep_dom_node(c->dom, k);
705
+ if (kn != NULL) {
706
+ if (kn->kind == YEP_DOM_SCALAR) {
707
+ check_tag(c, k, fam, json_family);
708
+ } else if (kn->kind != YEP_DOM_ALIAS) {
709
+ walk_children(c, kn->first_child, fam, json_family);
710
+ }
711
+ }
712
+ if (v != UINT32_MAX) {
713
+ yep_view kv = yep_dom_view(c->dom, kn ? kn->value : (yep_sview){0, 0});
714
+ if (yep_validate_path_push(c, kv.p != NULL ? kv.p : "", kv.len) == 0) {
715
+ walk_family(c, v, fam, json_family);
716
+ yep_validate_path_pop(c, mark);
717
+ }
718
+ }
719
+ k = v != UINT32_MAX && yep_dom_node(c->dom, v) ? yep_dom_node(c->dom, v)->next_sibling
720
+ : UINT32_MAX;
721
+ }
722
+ break;
723
+ }
724
+ default:
725
+ break;
726
+ }
727
+ c->depth--;
728
+ }
729
+
730
+ /* ---- the public entries -------------------------------------------------- */
731
+
732
+ YEPTRIS_API size_t yeptris_validate_report_count(const yeptris_validate_report* report) {
733
+ return report ? report->count : 0;
734
+ }
735
+
736
+ YEPTRIS_API const yeptris_diagnostic*
737
+ yeptris_validate_report_at(const yeptris_validate_report* report, size_t index) {
738
+ if (report == NULL || index >= report->count) {
739
+ return NULL;
740
+ }
741
+ return &report->diags[index];
742
+ }
743
+
744
+ YEPTRIS_API void yeptris_validate_report_free(yeptris_validate_report* report) {
745
+ if (report == NULL) {
746
+ return;
747
+ }
748
+ const yep_allocator* sys = yep_system_allocator();
749
+ yep_free(sys, report->diags);
750
+ yep_free(sys, report);
751
+ }
752
+
753
+ /* The report constructor (internal.h's seam — one home for the shape). */
754
+ yeptris_validate_report* yep_validate_report_new(void) {
755
+ const yep_allocator* sys = yep_system_allocator();
756
+ yeptris_validate_report* r = yep_alloc(sys, sizeof(*r));
757
+ if (r == NULL) {
758
+ return NULL;
759
+ }
760
+ r->diags = yep_alloc(sys, YEP_VALIDATE_MAX_DIAGS * sizeof(yeptris_diagnostic));
761
+ if (r->diags == NULL) {
762
+ yep_free(sys, r);
763
+ return NULL;
764
+ }
765
+ r->count = 0;
766
+ r->truncated = 0;
767
+ return r;
768
+ }
769
+
770
+ /* The shared tail: walk every document of the DOM into a fresh report. */
771
+ static YeptrisStatus validate_dom(yep_dom* dom, const yep_resolver* fam, int json_family,
772
+ yeptris_validate_report** out) {
773
+ const yep_allocator* sys = yep_system_allocator();
774
+ yeptris_validate_report* r = yep_validate_report_new();
775
+ if (r == NULL) {
776
+ return YEPTRIS_ERROR_MEMORY;
777
+ }
778
+
779
+ yep_vctx c;
780
+ memset(&c, 0, sizeof(c));
781
+ c.dom = dom;
782
+ c.rep = r;
783
+ c.input = dom->input_base;
784
+ c.input_len = dom->input_len;
785
+
786
+ for (uint32_t i = 0; i < dom->dcount && !c.oom; i++) {
787
+ c.path_len = 0; /* paths are root-relative per document */
788
+ walk_family(&c, dom->docs[i], fam, json_family);
789
+ }
790
+
791
+ yep_free(sys, c.line_off);
792
+ yep_free(sys, c.path);
793
+
794
+ YeptrisStatus st = YEPTRIS_OK;
795
+ if (c.oom) {
796
+ yeptris_validate_report_free(r);
797
+ return YEPTRIS_ERROR_MEMORY;
798
+ }
799
+ if (r->count > 0) {
800
+ st = YEPTRIS_ERROR_SCHEMA;
801
+ }
802
+ *out = r;
803
+ return st;
804
+ }
805
+
806
+ static int schema_is_family(YeptrisSchema schema, const yep_resolver** fam, int* json_family) {
807
+ switch (schema) {
808
+ case YEPTRIS_SCHEMA_12_CORE:
809
+ *fam = yep_resolver_core12();
810
+ *json_family = 0;
811
+ return 1;
812
+ case YEPTRIS_SCHEMA_12_JSON:
813
+ *fam = yep_resolver_json12();
814
+ *json_family = 1;
815
+ return 1;
816
+ default:
817
+ return 0;
818
+ }
819
+ }
820
+
821
+ YEPTRIS_API YeptrisStatus yeptris_validate_document(YeptrisDocument doc, YeptrisSchema schema,
822
+ yeptris_validate_report** out) {
823
+ if (out == NULL) {
824
+ return YEPTRIS_ERROR_ARG;
825
+ }
826
+ *out = NULL;
827
+ const yep_resolver* fam;
828
+ int json_family;
829
+ if (!schema_is_family(schema, &fam, &json_family)) {
830
+ yep_error_set(yep_error_tls(), YEP_ERR_UNEXPECTED, 0, 0, 0,
831
+ "schema %d is not a validation family", (int)schema);
832
+ return YEPTRIS_ERROR_ARG;
833
+ }
834
+ yep_dom* dom = doc ? yep_doc_dom((yeptris_document*)doc) : NULL;
835
+ if (doc != NULL && dom == NULL) {
836
+ return YEPTRIS_ERROR_MEMORY; /* a lazy tree that failed to build */
837
+ }
838
+ if (dom == NULL) {
839
+ /* an empty stream (or NULL doc): valid, empty report */
840
+ yeptris_validate_report* r = yep_validate_report_new();
841
+ if (r == NULL) {
842
+ return YEPTRIS_ERROR_MEMORY;
843
+ }
844
+ *out = r;
845
+ return YEPTRIS_OK;
846
+ }
847
+ return validate_dom(dom, fam, json_family, out);
848
+ }
849
+
850
+ YEPTRIS_API YeptrisStatus yeptris_validate(const char* buf, size_t len, YeptrisSchema schema,
851
+ yeptris_validate_report** out) {
852
+ if (out == NULL || (buf == NULL && len != 0)) {
853
+ return YEPTRIS_ERROR_ARG;
854
+ }
855
+ *out = NULL;
856
+ const yep_resolver* fam;
857
+ int json_family;
858
+ if (!schema_is_family(schema, &fam, &json_family)) {
859
+ yep_error_set(yep_error_tls(), YEP_ERR_UNEXPECTED, 0, 0, 0,
860
+ "schema %d is not a validation family", (int)schema);
861
+ return YEPTRIS_ERROR_ARG;
862
+ }
863
+ YeptrisParseOptions opts;
864
+ memset(&opts, 0, sizeof(opts));
865
+ opts.schema = schema; /* the parse resolves implicit types per family */
866
+ YeptrisStatus st = YEPTRIS_OK;
867
+ YeptrisDocument doc = yeptris_parse_eager_ex(buf, len, &opts, &st);
868
+ if (doc == NULL) {
869
+ if (st == YEPTRIS_OK) {
870
+ /* an empty stream: valid, empty report */
871
+ return yeptris_validate_document(NULL, schema, out);
872
+ }
873
+ return st; /* the parse's own error */
874
+ }
875
+ st = yeptris_validate_document(doc, schema, out);
876
+ yeptris_document_free(doc);
877
+ return st;
878
+ }
879
+
880
+ YEPTRIS_API YeptrisStatus yeptris_json_schema_validate(const char* schema, size_t schema_len,
881
+ const char* buf, size_t len,
882
+ yeptris_validate_report** out) {
883
+ if (out == NULL || (buf == NULL && len != 0) || (schema == NULL && schema_len != 0)) {
884
+ return YEPTRIS_ERROR_ARG;
885
+ }
886
+ *out = NULL;
887
+ /* the compiled-schema economy, one call deep (#885 item 3): the
888
+ * schema parses ONCE here exactly as it always did, rides the SAME
889
+ * walk and report paths as the retained handle, and dies at return
890
+ * — the observable statuses and byte-for-byte verdicts are the
891
+ * suite port's 1298 cases, unchanged */
892
+ YeptrisStatus st = YEPTRIS_OK;
893
+ yeptris_schema* s = yep_schema_compile_st(schema, schema_len, &st);
894
+ if (s == NULL) {
895
+ return st; /* the schema's parse/arg failure, verbatim */
896
+ }
897
+ st = yeptris_schema_validate(s, buf, len, out);
898
+ yeptris_schema_free(s);
899
+ return st;
900
+ }