yeptris 0.6.64.0-riscv64-linux-gnu

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.
Files changed (164) hide show
  1. checksums.yaml +7 -0
  2. data/README.adoc +150 -0
  3. data/ext/build_windows_native.rb +60 -0
  4. data/ext/libyeptris/extconf.rb +57 -0
  5. data/ext/yeptris_native/cbor_ruby.c +198 -0
  6. data/ext/yeptris_native/extconf.rb +99 -0
  7. data/ext/yeptris_native/json_ruby.c +414 -0
  8. data/ext/yeptris_native/version.h +29 -0
  9. data/ext/yeptris_native/yeptris_native.c +507 -0
  10. data/lib/yeptris/3.3/native.so +0 -0
  11. data/lib/yeptris/cbor.rb +169 -0
  12. data/lib/yeptris/document.rb +319 -0
  13. data/lib/yeptris/ffi.rb +572 -0
  14. data/lib/yeptris/json/descriptor.rb +266 -0
  15. data/lib/yeptris/json.rb +502 -0
  16. data/lib/yeptris/materializer.rb +389 -0
  17. data/lib/yeptris/node.rb +482 -0
  18. data/lib/yeptris/psych/class_loader.rb +24 -0
  19. data/lib/yeptris/psych/coder_shim.rb +55 -0
  20. data/lib/yeptris/psych/drop_in.rb +63 -0
  21. data/lib/yeptris/psych/encodable.rb +14 -0
  22. data/lib/yeptris/psych/handler.rb +95 -0
  23. data/lib/yeptris/psych/parser.rb +114 -0
  24. data/lib/yeptris/psych/scalar_scanner.rb +149 -0
  25. data/lib/yeptris/psych/tree_builder.rb +119 -0
  26. data/lib/yeptris/psych/visitors.rb +670 -0
  27. data/lib/yeptris/psych.rb +796 -0
  28. data/lib/yeptris/schema.rb +253 -0
  29. data/lib/yeptris/validation.rb +320 -0
  30. data/lib/yeptris/valueml.rb +477 -0
  31. data/lib/yeptris/yaml/descriptor.rb +56 -0
  32. data/lib/yeptris/yaml.rb +668 -0
  33. data/lib/yeptris.rb +130 -0
  34. data/libyeptris.so +0 -0
  35. data/vendor/libyeptris/CMakeLists.txt +280 -0
  36. data/vendor/libyeptris/cmake/yeptris-config.cmake.in +5 -0
  37. data/vendor/libyeptris/cmake/yeptris.pc.in +11 -0
  38. data/vendor/libyeptris/src/CMakeLists.txt +183 -0
  39. data/vendor/libyeptris/src/include/yeptris/api.h +32 -0
  40. data/vendor/libyeptris/src/include/yeptris/cbor.h +174 -0
  41. data/vendor/libyeptris/src/include/yeptris/dom.h +196 -0
  42. data/vendor/libyeptris/src/include/yeptris/emit.h +128 -0
  43. data/vendor/libyeptris/src/include/yeptris/error.h +42 -0
  44. data/vendor/libyeptris/src/include/yeptris/events.h +171 -0
  45. data/vendor/libyeptris/src/include/yeptris/json.h +57 -0
  46. data/vendor/libyeptris/src/include/yeptris/json.hpp +258 -0
  47. data/vendor/libyeptris/src/include/yeptris/jsonc_compat.h +115 -0
  48. data/vendor/libyeptris/src/include/yeptris/marshal.h +75 -0
  49. data/vendor/libyeptris/src/include/yeptris/parse.h +121 -0
  50. data/vendor/libyeptris/src/include/yeptris/plan.h +100 -0
  51. data/vendor/libyeptris/src/include/yeptris/resolve.h +81 -0
  52. data/vendor/libyeptris/src/include/yeptris/schema.h +121 -0
  53. data/vendor/libyeptris/src/include/yeptris/tape.h +128 -0
  54. data/vendor/libyeptris/src/include/yeptris/types.h +38 -0
  55. data/vendor/libyeptris/src/include/yeptris/validate.h +401 -0
  56. data/vendor/libyeptris/src/include/yeptris/values.h +108 -0
  57. data/vendor/libyeptris/src/include/yeptris/version.h.in +29 -0
  58. data/vendor/libyeptris/src/include/yeptris/visit.h +77 -0
  59. data/vendor/libyeptris/src/include/yeptris/yajl_compat.h +135 -0
  60. data/vendor/libyeptris/src/include/yeptris.h +24 -0
  61. data/vendor/libyeptris/src/yeptris/build.c +393 -0
  62. data/vendor/libyeptris/src/yeptris/cbor/cbor.h +35 -0
  63. data/vendor/libyeptris/src/yeptris/cbor/decode.c +1054 -0
  64. data/vendor/libyeptris/src/yeptris/cbor/encode.c +836 -0
  65. data/vendor/libyeptris/src/yeptris/cbor/encode_priv.h +32 -0
  66. data/vendor/libyeptris/src/yeptris/cbor/from_marshal.c +982 -0
  67. data/vendor/libyeptris/src/yeptris/cbor/sink.h +36 -0
  68. data/vendor/libyeptris/src/yeptris/common/chartype.c +41 -0
  69. data/vendor/libyeptris/src/yeptris/common/chartype.h +77 -0
  70. data/vendor/libyeptris/src/yeptris/common/cpu.c +81 -0
  71. data/vendor/libyeptris/src/yeptris/common/cpu.h +40 -0
  72. data/vendor/libyeptris/src/yeptris/common/error.c +65 -0
  73. data/vendor/libyeptris/src/yeptris/common/error.h +81 -0
  74. data/vendor/libyeptris/src/yeptris/common/mutex.h +45 -0
  75. data/vendor/libyeptris/src/yeptris/common/nametab.c +275 -0
  76. data/vendor/libyeptris/src/yeptris/common/nametab.h +68 -0
  77. data/vendor/libyeptris/src/yeptris/common/port.h +74 -0
  78. data/vendor/libyeptris/src/yeptris/common/simd_text.c +49 -0
  79. data/vendor/libyeptris/src/yeptris/common/simd_text.h +342 -0
  80. data/vendor/libyeptris/src/yeptris/common/simd_text_avx2.c +672 -0
  81. data/vendor/libyeptris/src/yeptris/common/simd_text_neon.c +793 -0
  82. data/vendor/libyeptris/src/yeptris/common/simd_text_scalar.c +324 -0
  83. data/vendor/libyeptris/src/yeptris/common/string_view.c +34 -0
  84. data/vendor/libyeptris/src/yeptris/common/string_view.h +93 -0
  85. data/vendor/libyeptris/src/yeptris/doc.h +86 -0
  86. data/vendor/libyeptris/src/yeptris/dom/dom.c +1312 -0
  87. data/vendor/libyeptris/src/yeptris/dom/dom.h +332 -0
  88. data/vendor/libyeptris/src/yeptris/dom/hpool.c +107 -0
  89. data/vendor/libyeptris/src/yeptris/dom/mapindex.c +173 -0
  90. data/vendor/libyeptris/src/yeptris/dom/mapindex.h +61 -0
  91. data/vendor/libyeptris/src/yeptris/dom/mutate.c +431 -0
  92. data/vendor/libyeptris/src/yeptris/dom/ytape.c +2323 -0
  93. data/vendor/libyeptris/src/yeptris/dom/ytape.h +96 -0
  94. data/vendor/libyeptris/src/yeptris/emit/emit.c +670 -0
  95. data/vendor/libyeptris/src/yeptris/emit/float/api.h +43 -0
  96. data/vendor/libyeptris/src/yeptris/emit/float/dragon.c +405 -0
  97. data/vendor/libyeptris/src/yeptris/emit/float/floatint.h +231 -0
  98. data/vendor/libyeptris/src/yeptris/emit/float/fpconv.c +428 -0
  99. data/vendor/libyeptris/src/yeptris/emit/float/print.c +396 -0
  100. data/vendor/libyeptris/src/yeptris/emit/style.c +21 -0
  101. data/vendor/libyeptris/src/yeptris/emit/style.h +32 -0
  102. data/vendor/libyeptris/src/yeptris/emit/writer.c +1951 -0
  103. data/vendor/libyeptris/src/yeptris/emit/writer.h +111 -0
  104. data/vendor/libyeptris/src/yeptris/encoding/bom.c +31 -0
  105. data/vendor/libyeptris/src/yeptris/encoding/encoding.h +63 -0
  106. data/vendor/libyeptris/src/yeptris/encoding/transcode.c +157 -0
  107. data/vendor/libyeptris/src/yeptris/encoding/utf8_validate.c +178 -0
  108. data/vendor/libyeptris/src/yeptris/events/capture.c +166 -0
  109. data/vendor/libyeptris/src/yeptris/events/capture.h +39 -0
  110. data/vendor/libyeptris/src/yeptris/events/iterparse.c +298 -0
  111. data/vendor/libyeptris/src/yeptris/events/pull.c +117 -0
  112. data/vendor/libyeptris/src/yeptris/events/push.c +151 -0
  113. data/vendor/libyeptris/src/yeptris/events/recorder.c +144 -0
  114. data/vendor/libyeptris/src/yeptris/events/values.c +630 -0
  115. data/vendor/libyeptris/src/yeptris/events/values_priv.h +53 -0
  116. data/vendor/libyeptris/src/yeptris/events/yaml_compat.c +86 -0
  117. data/vendor/libyeptris/src/yeptris/events/yaml_compat.h +90 -0
  118. data/vendor/libyeptris/src/yeptris/jsonapi/jsonc_compat.c +687 -0
  119. data/vendor/libyeptris/src/yeptris/jsonapi/yajl_compat.c +728 -0
  120. data/vendor/libyeptris/src/yeptris/marshal/to_json.c +650 -0
  121. data/vendor/libyeptris/src/yeptris/marshal.c +988 -0
  122. data/vendor/libyeptris/src/yeptris/memory/allocator.c +20 -0
  123. data/vendor/libyeptris/src/yeptris/memory/allocator.h +43 -0
  124. data/vendor/libyeptris/src/yeptris/memory/arena.c +150 -0
  125. data/vendor/libyeptris/src/yeptris/memory/arena.h +52 -0
  126. data/vendor/libyeptris/src/yeptris/memory/pool.c +133 -0
  127. data/vendor/libyeptris/src/yeptris/memory/pool.h +36 -0
  128. data/vendor/libyeptris/src/yeptris/parse/engine.c +6528 -0
  129. data/vendor/libyeptris/src/yeptris/parse/engine.h +96 -0
  130. data/vendor/libyeptris/src/yeptris/parse/events.h +137 -0
  131. data/vendor/libyeptris/src/yeptris/parse/numbers.c +374 -0
  132. data/vendor/libyeptris/src/yeptris/parse/numbers.h +33 -0
  133. data/vendor/libyeptris/src/yeptris/parse/scalars.c +435 -0
  134. data/vendor/libyeptris/src/yeptris/parse/scalars.h +87 -0
  135. data/vendor/libyeptris/src/yeptris/parse/stream_iter.c +358 -0
  136. data/vendor/libyeptris/src/yeptris/parse.c +1074 -0
  137. data/vendor/libyeptris/src/yeptris/plan.c +824 -0
  138. data/vendor/libyeptris/src/yeptris/resolve/compat11.c +339 -0
  139. data/vendor/libyeptris/src/yeptris/resolve/core12.c +153 -0
  140. data/vendor/libyeptris/src/yeptris/resolve/full12.c +201 -0
  141. data/vendor/libyeptris/src/yeptris/resolve/json12.c +109 -0
  142. data/vendor/libyeptris/src/yeptris/resolve/resolver.h +103 -0
  143. data/vendor/libyeptris/src/yeptris/resolve/tags.c +250 -0
  144. data/vendor/libyeptris/src/yeptris/scan/json.c +839 -0
  145. data/vendor/libyeptris/src/yeptris/scan/json.h +116 -0
  146. data/vendor/libyeptris/src/yeptris/scan/scan.c +862 -0
  147. data/vendor/libyeptris/src/yeptris/scan/scan.h +177 -0
  148. data/vendor/libyeptris/src/yeptris/schema.c +309 -0
  149. data/vendor/libyeptris/src/yeptris/tape.c +2375 -0
  150. data/vendor/libyeptris/src/yeptris/tape_in.h +39 -0
  151. data/vendor/libyeptris/src/yeptris/validate/compile.c +306 -0
  152. data/vendor/libyeptris/src/yeptris/validate/internal.h +347 -0
  153. data/vendor/libyeptris/src/yeptris/validate/jsonschema.c +1922 -0
  154. data/vendor/libyeptris/src/yeptris/validate/output.c +246 -0
  155. data/vendor/libyeptris/src/yeptris/validate/regexp.c +1600 -0
  156. data/vendor/libyeptris/src/yeptris/validate/registry.c +118 -0
  157. data/vendor/libyeptris/src/yeptris/validate/resources.c +601 -0
  158. data/vendor/libyeptris/src/yeptris/validate/stream.c +1155 -0
  159. data/vendor/libyeptris/src/yeptris/validate/validate.c +1043 -0
  160. data/vendor/libyeptris/src/yeptris/version.c +7 -0
  161. data/vendor/libyeptris/src/yeptris/visit/dom_visit.c +115 -0
  162. data/vendor/libyeptris/src/yeptris/visit/json_visit.c +293 -0
  163. data/vendor/libyeptris/src/yeptris/visit/yaml_visit.c +127 -0
  164. metadata +221 -0
@@ -0,0 +1,1922 @@
1
+ /* jsonschema.c — the JSON Schema draft 2020-12 keyword subset
2
+ * (#882 slice 2; #885 item 2 sweep; the 2020-12 sota sweep).
3
+ *
4
+ * The schema and the target are both parsed with the same engine; the
5
+ * walk applies the schema tree to the instance tree and emits
6
+ * diagnostics at instance JSON-pointer paths. Subset: type, enum,
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).
63
+ *
64
+ * Instance types ride the core-schema resolution (the YAML->JSON data
65
+ * model): "y" is boolean, 1.0 is integer (2020-12: zero fractional
66
+ * part), floats and ints are both numbers.
67
+ *
68
+ * Guards: the walk depth is the DOM's own parse-bounded depth; a
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.
73
+ */
74
+
75
+ #include <stdio.h>
76
+ #include <string.h>
77
+
78
+ #include <yeptris/validate.h>
79
+
80
+ #include "../memory/allocator.h"
81
+ #include "../parse/numbers.h"
82
+ #include "../resolve/resolver.h"
83
+ #include "internal.h"
84
+
85
+ #define JS_MAX_REF_CHAIN 64
86
+ #define JS_MAX_TOKENS 256
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;
100
+
101
+ typedef struct {
102
+ yep_vctx v; /* instance-side context (path, report, guards) */
103
+ const yep_dom* sd; /* the schema document's tree */
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;
122
+ } js_ctx;
123
+
124
+ /* ---- schema-side facts -------------------------------------------------- */
125
+
126
+ static const yep_dnode* snode(const js_ctx* j, uint32_t id) {
127
+ return yep_dom_node(j->sd, id);
128
+ }
129
+
130
+ /* First pair of the mapping whose key bytes equal `name`; 1 + *val_out,
131
+ * 0 absent. Only scalar keys match (collection keys never will). */
132
+ static int kw_get(const js_ctx* j, uint32_t map_id, const char* name, uint32_t* val_out) {
133
+ const yep_dnode* m = snode(j, map_id);
134
+ if (m == NULL || m->kind != YEP_DOM_MAPPING) {
135
+ return 0;
136
+ }
137
+ size_t nl = strlen(name);
138
+ uint32_t k = m->first_child;
139
+ while (k != UINT32_MAX) {
140
+ uint32_t v = snode(j, k) ? snode(j, k)->next_sibling : UINT32_MAX;
141
+ const yep_dnode* kn = snode(j, k);
142
+ if (kn != NULL && kn->kind == YEP_DOM_SCALAR) {
143
+ yep_view kv = yep_dom_view(j->sd, kn->value);
144
+ if (kv.len == nl && (nl == 0 || memcmp(kv.p, name, nl) == 0)) {
145
+ if (val_out != NULL) {
146
+ *val_out = v;
147
+ }
148
+ return 1;
149
+ }
150
+ }
151
+ k = v != UINT32_MAX && snode(j, v) ? snode(j, v)->next_sibling : UINT32_MAX;
152
+ }
153
+ return 0;
154
+ }
155
+
156
+ /* A schema-valued keyword: a mapping, or the boolean schemas. */
157
+ static int schema_truth(const js_ctx* j, uint32_t id, int* is_false) {
158
+ const yep_dnode* n = snode(j, id);
159
+ if (n == NULL) {
160
+ *is_false = 0;
161
+ return 0;
162
+ }
163
+ if (n->kind == YEP_DOM_MAPPING) {
164
+ *is_false = 0;
165
+ return 1;
166
+ }
167
+ if (n->kind == YEP_DOM_SCALAR && yep_validate_scalar_tag(n) == YEPTRIS_TAG_BOOL) {
168
+ yep_view v = yep_dom_view(j->sd, n->value);
169
+ *is_false = (v.len == 5 && memcmp(v.p, "false", 5) == 0);
170
+ return 1; /* true/false both carry meaning */
171
+ }
172
+ *is_false = 0;
173
+ return 0; /* an unreadable schema: ignored */
174
+ }
175
+
176
+ static int kw_schema(const js_ctx* j, uint32_t map_id, const char* name, uint32_t* schema_out,
177
+ int* is_false_out) {
178
+ uint32_t val;
179
+ if (!kw_get(j, map_id, name, &val) || val == UINT32_MAX) {
180
+ return 0;
181
+ }
182
+ int is_false = 0;
183
+ if (!schema_truth(j, val, &is_false)) {
184
+ return 0;
185
+ }
186
+ *schema_out = val;
187
+ *is_false_out = is_false;
188
+ return 1;
189
+ }
190
+
191
+ /* ---- instance-side facts ------------------------------------------------ */
192
+
193
+ /* Resolves an alias to the real node id (one hop; the DOM's targets
194
+ * are real nodes). */
195
+ static uint32_t inst_real(const yep_dom* d, uint32_t id) {
196
+ const yep_dnode* n = yep_dom_node(d, id);
197
+ if (n != NULL && n->kind == YEP_DOM_ALIAS && n->target != UINT32_MAX) {
198
+ return n->target;
199
+ }
200
+ return id;
201
+ }
202
+
203
+ /* The instance's JSON type word (7 bytes max); *integer receives 1 when
204
+ * a float also satisfies 2020-12's zero-fraction rule. */
205
+ static void inst_type(const yep_dom* d, uint32_t id, const char** word, int* integer) {
206
+ *integer = 0;
207
+ const yep_dnode* n = yep_dom_node(d, id);
208
+ if (n == NULL) {
209
+ *word = "string";
210
+ return;
211
+ }
212
+ if (n->kind == YEP_DOM_ALIAS) {
213
+ if (n->target != UINT32_MAX) {
214
+ inst_type(d, n->target, word, integer);
215
+ } else {
216
+ *word = "string";
217
+ }
218
+ return;
219
+ }
220
+ if (n->kind == YEP_DOM_SEQUENCE) {
221
+ *word = "array";
222
+ return;
223
+ }
224
+ if (n->kind == YEP_DOM_MAPPING) {
225
+ *word = "object";
226
+ return;
227
+ }
228
+ switch (yep_validate_scalar_tag(n)) {
229
+ case YEPTRIS_TAG_NULL:
230
+ *word = "null";
231
+ break;
232
+ case YEPTRIS_TAG_BOOL:
233
+ *word = "boolean";
234
+ break;
235
+ case YEPTRIS_TAG_INT:
236
+ *word = "integer";
237
+ *integer = 1;
238
+ break;
239
+ case YEPTRIS_TAG_FLOAT: {
240
+ yep_view v = yep_dom_view(d, n->value);
241
+ double dv;
242
+ /* 2020-12: "integer" means a zero fractional part. The range
243
+ * guard must read the PARSED value (it once sat before the
244
+ * parse, saw dv==0, folded constant-true, and left the cast
245
+ * UB on out-of-range/NaN doubles). No libm: in int64 range a
246
+ * cast round-trips iff the fractional part is zero; outside
247
+ * it every finite double is already integral; NaN is not. */
248
+ if (yep_num_f64(v.p, v.len, &dv) == 0) {
249
+ const int in_i64 = dv >= -9223372036854775808.0 && dv < 9223372036854775808.0;
250
+ if (in_i64 ? dv == (double)(int64_t)dv : dv == dv) {
251
+ *word = "integer";
252
+ *integer = 1;
253
+ } else {
254
+ *word = "number";
255
+ }
256
+ } else {
257
+ *word = "number";
258
+ }
259
+ break;
260
+ }
261
+ default:
262
+ *word = "string";
263
+ break;
264
+ }
265
+ }
266
+
267
+ /* The instance's numeric value (int or float); 0 when not numeric. */
268
+ static int inst_number(const yep_dom* d, uint32_t id, double* out) {
269
+ const yep_dnode* n = yep_dom_node(d, id);
270
+ if (n == NULL) {
271
+ return 0;
272
+ }
273
+ if (n->kind == YEP_DOM_ALIAS) {
274
+ return n->target != UINT32_MAX && inst_number(d, n->target, out);
275
+ }
276
+ if (n->kind != YEP_DOM_SCALAR) {
277
+ return 0;
278
+ }
279
+ uint8_t tag = yep_validate_scalar_tag(n);
280
+ if (tag != YEPTRIS_TAG_INT && tag != YEPTRIS_TAG_FLOAT) {
281
+ return 0;
282
+ }
283
+ yep_view v = yep_dom_view(d, n->value);
284
+ int64_t iv = 0;
285
+ if (yep_num_i64(v.p, v.len, &iv) == 0) {
286
+ *out = (double)iv;
287
+ return 1;
288
+ }
289
+ return yep_num_f64(v.p, v.len, out) == 0;
290
+ }
291
+
292
+ static int inst_seq_count(const yep_dom* d, uint32_t id, uint32_t* count) {
293
+ const yep_dnode* n = yep_dom_node(d, id);
294
+ if (n == NULL || n->kind != YEP_DOM_SEQUENCE) {
295
+ return 0;
296
+ }
297
+ *count = n->count;
298
+ return 1;
299
+ }
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
+
409
+ /* ---- the walk ----------------------------------------------------------- */
410
+
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
+ }
434
+
435
+ /* A suppressed probe: does `schema_id` hold at `inst_id`? Diagnostics
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;
445
+ j->v.suppress++;
446
+ js_eval r = js_apply(j, schema_id, inst_id, depth);
447
+ j->v.suppress--;
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;
456
+ }
457
+
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;
467
+ int tokens = 0;
468
+ while (i < plen) {
469
+ if (p[i] != '/') {
470
+ return 0;
471
+ }
472
+ i++;
473
+ char token[256];
474
+ size_t tl = 0;
475
+ while (i < plen && p[i] != '/') {
476
+ char b = p[i];
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')) {
487
+ b = p[i + 1] == '0' ? '~' : '/';
488
+ i++;
489
+ } else if (b == '~') {
490
+ return 0; /* a dangling ~ escape */
491
+ }
492
+ if (tl + 1 >= sizeof(token)) {
493
+ return 0;
494
+ }
495
+ token[tl++] = b;
496
+ i++;
497
+ }
498
+ if (++tokens > JS_MAX_TOKENS) {
499
+ return 0;
500
+ }
501
+ const yep_dnode* n = snode(j, cur);
502
+ if (n == NULL) {
503
+ return 0;
504
+ }
505
+ if (n->kind == YEP_DOM_SEQUENCE) {
506
+ /* a decimal array index (JSON pointer: no leading zeros,
507
+ * but be lenient — the pointer either resolves or not) */
508
+ uint32_t idx = 0;
509
+ if (tl == 0) {
510
+ return 0;
511
+ }
512
+ for (size_t t = 0; t < tl; t++) {
513
+ if (token[t] < '0' || token[t] > '9' || idx > 999999) {
514
+ return 0;
515
+ }
516
+ idx = idx * 10 + (uint32_t)(token[t] - '0');
517
+ }
518
+ uint32_t count = 0;
519
+ if (!inst_seq_count(j->sd, cur, &count) || idx >= count) {
520
+ return 0;
521
+ }
522
+ uint32_t ch = n->first_child;
523
+ while (ch != UINT32_MAX && idx > 0) {
524
+ ch = snode(j, ch) ? snode(j, ch)->next_sibling : UINT32_MAX;
525
+ idx--;
526
+ }
527
+ cur = ch;
528
+ } else if (n->kind == YEP_DOM_MAPPING) {
529
+ uint32_t k = n->first_child;
530
+ int found = 0;
531
+ while (k != UINT32_MAX && !found) {
532
+ uint32_t v = snode(j, k) ? snode(j, k)->next_sibling : UINT32_MAX;
533
+ const yep_dnode* kn = snode(j, k);
534
+ if (kn != NULL && kn->kind == YEP_DOM_SCALAR) {
535
+ yep_view kv = yep_dom_view(j->sd, kn->value);
536
+ if (kv.len == tl && (tl == 0 || memcmp(kv.p, token, tl) == 0)) {
537
+ cur = v;
538
+ found = 1;
539
+ }
540
+ }
541
+ k = v != UINT32_MAX && snode(j, v) ? snode(j, v)->next_sibling : UINT32_MAX;
542
+ }
543
+ if (!found) {
544
+ return 0;
545
+ }
546
+ } else {
547
+ return 0; /* a pointer into a scalar */
548
+ }
549
+ }
550
+ *out = cur;
551
+ return cur != UINT32_MAX;
552
+ }
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
+
673
+ /* uniqueItems: canonical hash buckets, canonical equality inside a
674
+ * bucket; allocation failure skips the check (oom flags the walk). */
675
+ static void check_unique(js_ctx* j, uint32_t seq_id, uint32_t inst_off) {
676
+ const yep_dom* d = j->v.dom;
677
+ const yep_dnode* s = yep_dom_node(d, seq_id);
678
+ uint32_t n = s->count;
679
+ if (n < 2) {
680
+ return;
681
+ }
682
+ const yep_allocator* sys = yep_system_allocator();
683
+ uint32_t cap = 8;
684
+ while (cap < n * 2 && cap < JS_MAX_UNIQUE) {
685
+ cap *= 2;
686
+ }
687
+ /* cap never reaches n when n exceeds JS_MAX_UNIQUE: elements beyond
688
+ * a FULL table are unchecked below (a 100%%-full table cannot probe
689
+ * — the linear scan would spin forever). Bounded memory, bounded
690
+ * time; the check degrades to the first `cap` distinct entries. */
691
+ struct {
692
+ uint32_t hash;
693
+ uint32_t node; /* +1 encoded */
694
+ }* tbl = yep_alloc(sys, (size_t)cap * sizeof(*tbl));
695
+ if (tbl == NULL) {
696
+ j->v.oom = 1;
697
+ return;
698
+ }
699
+ memset(tbl, 0, (size_t)cap * sizeof(*tbl));
700
+ uint32_t ch = s->first_child;
701
+ int idx = 0;
702
+ int reported = 0;
703
+ uint32_t inserted = 0;
704
+ while (ch != UINT32_MAX && !reported) {
705
+ if (inserted == cap) {
706
+ break; /* the table is full: stop checking (see the cap note) */
707
+ }
708
+ uint64_t h64 = yep_validate_node_hash(d, ch, 0);
709
+ uint32_t h = (uint32_t)(h64 ^ (h64 >> 32));
710
+ uint32_t i = h & (cap - 1);
711
+ int dup = 0;
712
+ while (tbl[i].node != 0) {
713
+ if (tbl[i].hash == h && yep_validate_node_equal(d, tbl[i].node - 1, d, ch, 0)) {
714
+ dup = 1;
715
+ break;
716
+ }
717
+ i = (i + 1) & (cap - 1);
718
+ }
719
+ if (dup) {
720
+ reported = 1;
721
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_UNIQUE_ITEMS, inst_off,
722
+ "elements %u and %u are equal", (uint32_t)idx, (uint32_t)(idx - 1));
723
+ } else {
724
+ tbl[i].hash = h;
725
+ tbl[i].node = ch + 1;
726
+ inserted++;
727
+ }
728
+ ch = yep_dom_node(d, ch) ? yep_dom_node(d, ch)->next_sibling : UINT32_MAX;
729
+ idx++;
730
+ }
731
+ yep_free(sys, tbl);
732
+ }
733
+
734
+ /* The instance's string length in code points (2020-12 minLength/
735
+ * maxLength). 0 when the instance is not a string. */
736
+ static int inst_str_len(const yep_dom* d, uint32_t id, uint32_t* out) {
737
+ const yep_dom* dd = d;
738
+ const yep_dnode* n = yep_dom_node(dd, id);
739
+ if (n == NULL) {
740
+ return 0;
741
+ }
742
+ if (n->kind == YEP_DOM_ALIAS) {
743
+ return n->target != UINT32_MAX && inst_str_len(dd, n->target, out);
744
+ }
745
+ if (n->kind != YEP_DOM_SCALAR || yep_validate_scalar_tag(n) != YEPTRIS_TAG_STR) {
746
+ return 0;
747
+ }
748
+ yep_view v = yep_dom_view(dd, n->value);
749
+ uint32_t chars = 0;
750
+ for (uint32_t i = 0; i < v.len; i++) {
751
+ chars += ((((unsigned char)v.p[i]) & 0xC0) != 0x80);
752
+ }
753
+ *out = chars;
754
+ return 1;
755
+ }
756
+
757
+ /* Reads a keyword's numeric value (int or float scalar); 0 unreadable. */
758
+ static int schema_number(const js_ctx* j, uint32_t id, double* out) {
759
+ const yep_dnode* n = snode(j, id);
760
+ if (n == NULL || n->kind != YEP_DOM_SCALAR) {
761
+ return 0;
762
+ }
763
+ uint8_t tag = yep_validate_scalar_tag(n);
764
+ if (tag != YEPTRIS_TAG_INT && tag != YEPTRIS_TAG_FLOAT) {
765
+ return 0;
766
+ }
767
+ yep_view v = yep_dom_view(j->sd, n->value);
768
+ int64_t iv = 0;
769
+ if (yep_num_i64(v.p, v.len, &iv) == 0) {
770
+ *out = (double)iv;
771
+ return 1;
772
+ }
773
+ return yep_num_f64(v.p, v.len, out) == 0;
774
+ }
775
+
776
+ /* Applies one keyword value that must be a string (the type names,
777
+ * pattern sources); 0 when unreadable. */
778
+ static int schema_word(const js_ctx* j, uint32_t id, const char** p, uint32_t* len) {
779
+ const yep_dnode* n = snode(j, id);
780
+ if (n == NULL || n->kind != YEP_DOM_SCALAR || yep_validate_scalar_tag(n) != YEPTRIS_TAG_STR) {
781
+ return 0;
782
+ }
783
+ yep_view v = yep_dom_view(j->sd, n->value);
784
+ if (v.p == NULL) {
785
+ return 0;
786
+ }
787
+ *p = v.p;
788
+ *len = v.len;
789
+ return 1;
790
+ }
791
+
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) {
843
+ if (j->v.oom) {
844
+ return;
845
+ }
846
+ if (++depth > YEP_DOM_MAX_DEPTH) {
847
+ if (depth == YEP_DOM_MAX_DEPTH + 1) {
848
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_DEPTH, UINT32_MAX,
849
+ "schema application exceeded the nesting limit");
850
+ }
851
+ return;
852
+ }
853
+ inst_id = inst_real(j->v.dom, inst_id);
854
+ const yep_dnode* s = snode(j, schema_id);
855
+ if (s == NULL) {
856
+ return;
857
+ }
858
+
859
+ /* the boolean schemas */
860
+ if (s->kind != YEP_DOM_MAPPING) {
861
+ if (s->kind == YEP_DOM_SCALAR && yep_validate_scalar_tag(s) == YEPTRIS_TAG_BOOL) {
862
+ yep_view v = yep_dom_view(j->sd, s->value);
863
+ if (v.len == 5 && memcmp(v.p, "false", 5) == 0) {
864
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_FALSE_SCHEMA,
865
+ yep_validate_node_off(j->v.dom, inst_id),
866
+ "the schema is false here");
867
+ }
868
+ }
869
+ return; /* true (or anything unreadable) constrains nothing */
870
+ }
871
+
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;
884
+ }
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 */
906
+ j->v.ref_depth = 0;
907
+ }
908
+ /* an unreadable $ref value: ignored like any unknown keyword */
909
+ } else {
910
+ j->v.ref_depth = 0;
911
+ }
912
+ }
913
+
914
+ /* type: a single word or a list of words */
915
+ {
916
+ uint32_t tv;
917
+ if (kw_get(j, schema_id, "type", &tv) && tv != UINT32_MAX) {
918
+ const yep_dnode* tn = snode(j, tv);
919
+ const char* word = NULL;
920
+ uint32_t wl = 0;
921
+ const char* it = NULL;
922
+ int iis_int = 0;
923
+ inst_type(j->v.dom, inst_id, &it, &iis_int);
924
+ int matched = 0;
925
+ int have = 0;
926
+ if (tn != NULL && tn->kind == YEP_DOM_SCALAR && schema_word(j, tv, &word, &wl)) {
927
+ have = 1;
928
+ matched = (strlen(it) == wl && memcmp(it, word, wl) == 0);
929
+ /* "number" also accepts integers */
930
+ if (!matched && wl == 6 && memcmp(word, "number", 6) == 0 && iis_int) {
931
+ matched = 1;
932
+ }
933
+ } else if (tn != NULL && tn->kind == YEP_DOM_SEQUENCE) {
934
+ have = 1;
935
+ uint32_t ch = tn->first_child;
936
+ while (ch != UINT32_MAX && !matched) {
937
+ const char* w2 = NULL;
938
+ uint32_t l2 = 0;
939
+ if (schema_word(j, ch, &w2, &l2)) {
940
+ matched = (strlen(it) == l2 && memcmp(it, w2, l2) == 0);
941
+ if (!matched && l2 == 6 && memcmp(w2, "number", 6) == 0 && iis_int) {
942
+ matched = 1;
943
+ }
944
+ }
945
+ ch = snode(j, ch) ? snode(j, ch)->next_sibling : UINT32_MAX;
946
+ }
947
+ }
948
+ if (have && !matched) {
949
+ char want[24];
950
+ size_t cl = wl < sizeof(want) - 1 ? wl : sizeof(want) - 1;
951
+ if (cl > 0 && word != NULL) {
952
+ memcpy(want, word, cl);
953
+ }
954
+ want[cl] = '\0';
955
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_TYPE,
956
+ yep_validate_node_off(j->v.dom, inst_id), "expected %s, got %s",
957
+ want, it);
958
+ }
959
+ }
960
+ }
961
+
962
+ /* const / enum */
963
+ {
964
+ uint32_t cv;
965
+ if (kw_get(j, schema_id, "const", &cv) && cv != UINT32_MAX && snode(j, cv) != NULL) {
966
+ if (!yep_validate_node_equal(j->sd, cv, j->v.dom, inst_id, 0)) {
967
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_CONST,
968
+ yep_validate_node_off(j->v.dom, inst_id),
969
+ "value is not the constant");
970
+ }
971
+ }
972
+ uint32_t ev;
973
+ if (kw_get(j, schema_id, "enum", &ev) && ev != UINT32_MAX) {
974
+ const yep_dnode* en = snode(j, ev);
975
+ if (en != NULL && en->kind == YEP_DOM_SEQUENCE) {
976
+ int found = 0;
977
+ uint32_t members = 0;
978
+ uint32_t ch = en->first_child;
979
+ while (ch != UINT32_MAX) {
980
+ members++;
981
+ if (!found && yep_validate_node_equal(j->sd, ch, j->v.dom, inst_id, 0)) {
982
+ found = 1;
983
+ }
984
+ ch = snode(j, ch) ? snode(j, ch)->next_sibling : UINT32_MAX;
985
+ }
986
+ if (!found) {
987
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_ENUM,
988
+ yep_validate_node_off(j->v.dom, inst_id),
989
+ "value is not one of %u enum members", members);
990
+ }
991
+ }
992
+ }
993
+ }
994
+
995
+ /* the numeric bounds (ignored on non-numeric instances) */
996
+ {
997
+ double iv = 0;
998
+ int is_num = inst_number(j->v.dom, inst_id, &iv);
999
+ if (is_num) {
1000
+ uint32_t mv;
1001
+ if (kw_get(j, schema_id, "minimum", &mv) && mv != UINT32_MAX) {
1002
+ double b;
1003
+ if (schema_number(j, mv, &b) && iv < b) {
1004
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_MINIMUM,
1005
+ yep_validate_node_off(j->v.dom, inst_id),
1006
+ "%g is less than the minimum %g", iv, b);
1007
+ }
1008
+ }
1009
+ if (kw_get(j, schema_id, "maximum", &mv) && mv != UINT32_MAX) {
1010
+ double b;
1011
+ if (schema_number(j, mv, &b) && iv > b) {
1012
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_MAXIMUM,
1013
+ yep_validate_node_off(j->v.dom, inst_id),
1014
+ "%g is greater than the maximum %g", iv, b);
1015
+ }
1016
+ }
1017
+ if (kw_get(j, schema_id, "exclusiveMinimum", &mv) && mv != UINT32_MAX) {
1018
+ double b;
1019
+ if (schema_number(j, mv, &b) && iv <= b) {
1020
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_EXCLUSIVE_MINIMUM,
1021
+ yep_validate_node_off(j->v.dom, inst_id),
1022
+ "%g is not greater than %g", iv, b);
1023
+ }
1024
+ }
1025
+ if (kw_get(j, schema_id, "exclusiveMaximum", &mv) && mv != UINT32_MAX) {
1026
+ double b;
1027
+ if (schema_number(j, mv, &b) && iv >= b) {
1028
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_EXCLUSIVE_MAXIMUM,
1029
+ yep_validate_node_off(j->v.dom, inst_id),
1030
+ "%g is not less than %g", iv, b);
1031
+ }
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
+ }
1055
+ }
1056
+ }
1057
+
1058
+ /* string length + pattern (ignored on non-strings) */
1059
+ {
1060
+ uint32_t sl = 0;
1061
+ int is_str = inst_str_len(j->v.dom, inst_id, &sl);
1062
+ if (is_str) {
1063
+ uint32_t mv;
1064
+ if (kw_get(j, schema_id, "minLength", &mv) && mv != UINT32_MAX) {
1065
+ double b;
1066
+ if (schema_number(j, mv, &b) && (double)sl < b) {
1067
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_MIN_LENGTH,
1068
+ yep_validate_node_off(j->v.dom, inst_id),
1069
+ "length %u is shorter than %g", sl, b);
1070
+ }
1071
+ }
1072
+ if (kw_get(j, schema_id, "maxLength", &mv) && mv != UINT32_MAX) {
1073
+ double b;
1074
+ if (schema_number(j, mv, &b) && (double)sl > b) {
1075
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_MAX_LENGTH,
1076
+ yep_validate_node_off(j->v.dom, inst_id),
1077
+ "length %u is longer than %g", sl, b);
1078
+ }
1079
+ }
1080
+ uint32_t pv;
1081
+ if (kw_get(j, schema_id, "pattern", &pv) && pv != UINT32_MAX) {
1082
+ const char* pat = NULL;
1083
+ uint32_t pl = 0;
1084
+ if (schema_word(j, pv, &pat, &pl)) {
1085
+ const yep_dnode* n = yep_dom_node(j->v.dom, inst_id);
1086
+ yep_view v = yep_dom_view(j->v.dom, n->value);
1087
+ int rc = yep_re_cache_search(j->cache, pat, pl, v.p != NULL ? v.p : "", v.len);
1088
+ if (rc == 0) {
1089
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_PATTERN,
1090
+ yep_validate_node_off(j->v.dom, inst_id),
1091
+ "does not match the pattern");
1092
+ }
1093
+ /* rc < 0: the pattern is outside the subset — ignored */
1094
+ }
1095
+ }
1096
+ }
1097
+ }
1098
+
1099
+ /* array bounds + uniqueness + items (ignored on non-arrays) */
1100
+ {
1101
+ uint32_t cnt = 0;
1102
+ if (inst_seq_count(j->v.dom, inst_id, &cnt)) {
1103
+ uint32_t mv;
1104
+ if (kw_get(j, schema_id, "minItems", &mv) && mv != UINT32_MAX) {
1105
+ double b;
1106
+ if (schema_number(j, mv, &b) && (double)cnt < b) {
1107
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_MIN_ITEMS,
1108
+ yep_validate_node_off(j->v.dom, inst_id),
1109
+ "%u items is fewer than %g", cnt, b);
1110
+ }
1111
+ }
1112
+ if (kw_get(j, schema_id, "maxItems", &mv) && mv != UINT32_MAX) {
1113
+ double b;
1114
+ if (schema_number(j, mv, &b) && (double)cnt > b) {
1115
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_MAX_ITEMS,
1116
+ yep_validate_node_off(j->v.dom, inst_id),
1117
+ "%u items is more than %g", cnt, b);
1118
+ }
1119
+ }
1120
+ uint32_t uv;
1121
+ int uf = 0;
1122
+ if (kw_schema(j, schema_id, "uniqueItems", &uv, &uf) && uf == 0) {
1123
+ check_unique(j, inst_id, yep_validate_node_off(j->v.dom, inst_id));
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
+ }
1184
+ /* prefixItems: per-index schemas; the draft-07 array form
1185
+ * of items rides the same semantics */
1186
+ uint32_t prefix = 0;
1187
+ uint32_t pv;
1188
+ if (kw_get(j, schema_id, "prefixItems", &pv) && pv != UINT32_MAX) {
1189
+ const yep_dnode* pn = snode(j, pv);
1190
+ if (pn != NULL && pn->kind == YEP_DOM_SEQUENCE) {
1191
+ uint32_t i = 0;
1192
+ uint32_t ch = pn->first_child;
1193
+ uint32_t ich = yep_dom_node(j->v.dom, inst_id)->first_child;
1194
+ while (ch != UINT32_MAX && ich != UINT32_MAX) {
1195
+ uint32_t ns = snode(j, ch) ? snode(j, ch)->next_sibling : UINT32_MAX;
1196
+ uint32_t ni = yep_dom_node(j->v.dom, ich)
1197
+ ? yep_dom_node(j->v.dom, ich)->next_sibling
1198
+ : UINT32_MAX;
1199
+ size_t mark = j->v.path_len;
1200
+ if (yep_validate_path_push_index(&j->v, i) == 0) {
1201
+ js_apply_child(j, ch, ich, depth);
1202
+ yep_validate_path_pop(&j->v, mark);
1203
+ }
1204
+ ei_append(j, i);
1205
+ prefix++;
1206
+ i++;
1207
+ ch = ns;
1208
+ ich = ni;
1209
+ }
1210
+ }
1211
+ }
1212
+ /* items: a single schema over the remaining elements; the
1213
+ * draft-07 array form counts as the prefix */
1214
+ if (kw_get(j, schema_id, "items", &pv) && pv != UINT32_MAX) {
1215
+ const yep_dnode* pn = snode(j, pv);
1216
+ int is_false = 0;
1217
+ if (pn != NULL && pn->kind == YEP_DOM_SEQUENCE) {
1218
+ /* the draft-07 tuple shim: per-index, from scratch */
1219
+ uint32_t i = 0;
1220
+ uint32_t ch = pn->first_child;
1221
+ uint32_t ich = yep_dom_node(j->v.dom, inst_id)->first_child;
1222
+ while (ch != UINT32_MAX && ich != UINT32_MAX) {
1223
+ uint32_t ns = snode(j, ch) ? snode(j, ch)->next_sibling : UINT32_MAX;
1224
+ uint32_t ni = yep_dom_node(j->v.dom, ich)
1225
+ ? yep_dom_node(j->v.dom, ich)->next_sibling
1226
+ : UINT32_MAX;
1227
+ size_t mark = j->v.path_len;
1228
+ if (yep_validate_path_push_index(&j->v, i) == 0) {
1229
+ js_apply_child(j, ch, ich, depth);
1230
+ yep_validate_path_pop(&j->v, mark);
1231
+ }
1232
+ ei_append(j, i);
1233
+ i++;
1234
+ ch = ns;
1235
+ ich = ni;
1236
+ }
1237
+ } else if (schema_truth(j, pv, &is_false)) {
1238
+ /* items applies from prefixItems' count on */
1239
+ uint32_t ich = yep_dom_node(j->v.dom, inst_id)->first_child;
1240
+ for (uint32_t skip = 0; skip < prefix && ich != UINT32_MAX; skip++) {
1241
+ ich = yep_dom_node(j->v.dom, ich)
1242
+ ? yep_dom_node(j->v.dom, ich)->next_sibling
1243
+ : UINT32_MAX;
1244
+ }
1245
+ for (uint32_t i = prefix; i < cnt && ich != UINT32_MAX; i++) {
1246
+ uint32_t ni = yep_dom_node(j->v.dom, ich)
1247
+ ? yep_dom_node(j->v.dom, ich)->next_sibling
1248
+ : UINT32_MAX;
1249
+ size_t mark = j->v.path_len;
1250
+ if (yep_validate_path_push_index(&j->v, i) == 0) {
1251
+ js_apply_child(j, pv, ich, depth);
1252
+ yep_validate_path_pop(&j->v, mark);
1253
+ }
1254
+ ei_append(j, i);
1255
+ ich = ni;
1256
+ }
1257
+ }
1258
+ }
1259
+ }
1260
+ }
1261
+
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. */
1266
+ {
1267
+ uint32_t cv;
1268
+ if (kw_get(j, schema_id, "allOf", &cv) && cv != UINT32_MAX) {
1269
+ const yep_dnode* cn = snode(j, cv);
1270
+ if (cn != NULL && cn->kind == YEP_DOM_SEQUENCE) {
1271
+ uint32_t ch = cn->first_child;
1272
+ while (ch != UINT32_MAX) {
1273
+ uint32_t nc = snode(j, ch) ? snode(j, ch)->next_sibling : UINT32_MAX;
1274
+ js_apply(j, ch, inst_id, depth); /* absorbed */
1275
+ ch = nc;
1276
+ }
1277
+ }
1278
+ }
1279
+ if (kw_get(j, schema_id, "anyOf", &cv) && cv != UINT32_MAX) {
1280
+ const yep_dnode* cn = snode(j, cv);
1281
+ if (cn != NULL && cn->kind == YEP_DOM_SEQUENCE) {
1282
+ int any = 0;
1283
+ uint32_t branches = 0;
1284
+ uint32_t ch = cn->first_child;
1285
+ while (ch != UINT32_MAX) {
1286
+ branches++;
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;
1293
+ }
1294
+ ch = snode(j, ch) ? snode(j, ch)->next_sibling : UINT32_MAX;
1295
+ }
1296
+ if (!any) {
1297
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_ANY_OF,
1298
+ yep_validate_node_off(j->v.dom, inst_id),
1299
+ "none of the %u anyOf branches matched", branches);
1300
+ }
1301
+ }
1302
+ }
1303
+ if (kw_get(j, schema_id, "oneOf", &cv) && cv != UINT32_MAX) {
1304
+ const yep_dnode* cn = snode(j, cv);
1305
+ if (cn != NULL && cn->kind == YEP_DOM_SEQUENCE) {
1306
+ uint32_t matches = 0;
1307
+ uint32_t branches = 0;
1308
+ uint32_t ch = cn->first_child;
1309
+ while (ch != UINT32_MAX) {
1310
+ branches++;
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;
1317
+ }
1318
+ ch = snode(j, ch) ? snode(j, ch)->next_sibling : UINT32_MAX;
1319
+ }
1320
+ if (matches != 1) {
1321
+ j->ep_len = pmark; /* no single branch: no annotations */
1322
+ j->ei_len = imark;
1323
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_ONE_OF,
1324
+ yep_validate_node_off(j->v.dom, inst_id),
1325
+ "%u of the %u oneOf branches matched (exactly one "
1326
+ "required)",
1327
+ matches, branches);
1328
+ }
1329
+ }
1330
+ }
1331
+ if (kw_get(j, schema_id, "not", &cv) && cv != UINT32_MAX) {
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) {
1336
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_NOT, yep_validate_node_off(j->v.dom, inst_id),
1337
+ "the not-schema matched");
1338
+ }
1339
+ }
1340
+ }
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
+
1372
+ /* the object keywords (ignored on non-objects) */
1373
+ {
1374
+ const yep_dnode* n = yep_dom_node(j->v.dom, inst_id);
1375
+ if (n != NULL && n->kind == YEP_DOM_MAPPING) {
1376
+ const uint32_t pairs = n->count / 2;
1377
+ /* properties: known keys */
1378
+ uint32_t props = UINT32_MAX;
1379
+ int have_props = kw_get(j, schema_id, "properties", &props) && props != UINT32_MAX &&
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
+ }
1394
+ uint32_t req;
1395
+ if (kw_get(j, schema_id, "required", &req) && req != UINT32_MAX) {
1396
+ const yep_dnode* rn = snode(j, req);
1397
+ if (rn != NULL && rn->kind == YEP_DOM_SEQUENCE) {
1398
+ uint32_t ch = rn->first_child;
1399
+ while (ch != UINT32_MAX) {
1400
+ const char* name = NULL;
1401
+ uint32_t nl = 0;
1402
+ uint32_t nc = snode(j, ch) ? snode(j, ch)->next_sibling : UINT32_MAX;
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);
1419
+ }
1420
+ }
1421
+ ch = nc;
1422
+ }
1423
+ }
1424
+ }
1425
+ /* properties walk (values through their schemas): the
1426
+ * PRESENT keys are this object's annotation */
1427
+ if (have_props) {
1428
+ const yep_dnode* pn = snode(j, props);
1429
+ uint32_t k = pn->first_child;
1430
+ while (k != UINT32_MAX) {
1431
+ uint32_t v = snode(j, k) ? snode(j, k)->next_sibling : UINT32_MAX;
1432
+ const yep_dnode* kn = snode(j, k);
1433
+ if (kn != NULL && kn->kind == YEP_DOM_SCALAR) {
1434
+ yep_view kv = yep_dom_view(j->sd, kn->value);
1435
+ /* find the child pair by key bytes */
1436
+ uint32_t ck = n->first_child;
1437
+ while (ck != UINT32_MAX) {
1438
+ uint32_t cv = yep_dom_node(j->v.dom, ck)
1439
+ ? yep_dom_node(j->v.dom, ck)->next_sibling
1440
+ : UINT32_MAX;
1441
+ const yep_dnode* ckn = yep_dom_node(j->v.dom, ck);
1442
+ if (ckn != NULL && ckn->kind == YEP_DOM_SCALAR) {
1443
+ yep_view ckv = yep_dom_view(j->v.dom, ckn->value);
1444
+ if (ckv.len == kv.len &&
1445
+ (kv.len == 0 || memcmp(ckv.p, kv.p, kv.len) == 0)) {
1446
+ size_t mark = j->v.path_len;
1447
+ if (yep_validate_path_push(&j->v, kv.p != NULL ? kv.p : "",
1448
+ kv.len) == 0) {
1449
+ js_apply_child(j, v, cv, depth);
1450
+ yep_validate_path_pop(&j->v, mark);
1451
+ }
1452
+ ep_append(j, ck);
1453
+ break;
1454
+ }
1455
+ }
1456
+ ck = cv != UINT32_MAX && yep_dom_node(j->v.dom, cv)
1457
+ ? yep_dom_node(j->v.dom, cv)->next_sibling
1458
+ : UINT32_MAX;
1459
+ }
1460
+ }
1461
+ k = v != UINT32_MAX && snode(j, v) ? snode(j, v)->next_sibling : UINT32_MAX;
1462
+ }
1463
+ }
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 */
1518
+ uint32_t ap;
1519
+ int af = 0;
1520
+ if (kw_schema(j, schema_id, "additionalProperties", &ap, &af)) {
1521
+ /* k steps pair-to-pair: every visited node IS a key */
1522
+ uint32_t k = n->first_child;
1523
+ while (k != UINT32_MAX) {
1524
+ uint32_t v = yep_dom_node(j->v.dom, k) ? yep_dom_node(j->v.dom, k)->next_sibling
1525
+ : UINT32_MAX;
1526
+ const yep_dnode* kn = yep_dom_node(j->v.dom, k);
1527
+ if (kn != NULL && kn->kind == YEP_DOM_SCALAR) {
1528
+ yep_view kv = yep_dom_view(j->v.dom, kn->value);
1529
+ int known = 0;
1530
+ if (have_props) {
1531
+ uint32_t pk = snode(j, props)->first_child;
1532
+ while (pk != UINT32_MAX && !known) {
1533
+ const yep_dnode* pkn = snode(j, pk);
1534
+ uint32_t pv2 =
1535
+ snode(j, pk) ? snode(j, pk)->next_sibling : UINT32_MAX;
1536
+ if (pkn != NULL && pkn->kind == YEP_DOM_SCALAR) {
1537
+ yep_view pw = yep_dom_view(j->sd, pkn->value);
1538
+ known = pw.len == kv.len &&
1539
+ (kv.len == 0 || memcmp(pw.p, kv.p, kv.len) == 0);
1540
+ }
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;
1566
+ }
1567
+ }
1568
+ if (!known) {
1569
+ char namebuf[64];
1570
+ size_t cl = kv.len < sizeof(namebuf) - 1 ? kv.len : sizeof(namebuf) - 1;
1571
+ if (cl > 0 && kv.p != NULL) {
1572
+ memcpy(namebuf, kv.p, cl);
1573
+ }
1574
+ namebuf[cl] = '\0';
1575
+ if (af) {
1576
+ yep_validate_emit(&j->v, YEPTRIS_DIAG_ADDITIONAL_PROPERTY,
1577
+ yep_validate_node_off(j->v.dom, k),
1578
+ "additional property \"%s\"", namebuf);
1579
+ } else {
1580
+ size_t mark = j->v.path_len;
1581
+ if (yep_validate_path_push(&j->v, namebuf, cl) == 0) {
1582
+ js_apply_child(j, ap, v, depth);
1583
+ yep_validate_path_pop(&j->v, mark);
1584
+ }
1585
+ ep_append(j, k);
1586
+ }
1587
+ }
1588
+ }
1589
+ k = v != UINT32_MAX && yep_dom_node(j->v.dom, v)
1590
+ ? yep_dom_node(j->v.dom, v)->next_sibling
1591
+ : UINT32_MAX;
1592
+ }
1593
+ }
1594
+ }
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
+ }
1839
+ }
1840
+
1841
+ /* ---- the entry ----------------------------------------------------------- */
1842
+
1843
+ /* Validates one instance document root against the schema root.
1844
+ * Returns 0 ok, -1 OOM. */
1845
+ static int js_validate_doc(js_ctx* j, uint32_t inst_root) {
1846
+ js_apply(j, j->sroot, inst_root, 0);
1847
+ return j->v.oom ? -1 : 0;
1848
+ }
1849
+
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). */
1856
+ int yep_jsonschema_run(const yep_dom* schema_dom, uint32_t schema_root, const yep_dom* instance_dom,
1857
+ const uint32_t* doc_roots, uint32_t doc_count, yeptris_validate_report* rep,
1858
+ const yep_re_cache* cache, const js_resources* res) {
1859
+ js_ctx j;
1860
+ memset(&j, 0, sizeof(j));
1861
+ j.sd = schema_dom;
1862
+ j.sroot = schema_root;
1863
+ j.cache = cache;
1864
+ j.res = res;
1865
+ j.v.dom = instance_dom;
1866
+ j.v.rep = rep;
1867
+ j.v.input = instance_dom->input_base;
1868
+ j.v.input_len = instance_dom->input_len;
1869
+ int rc = 0;
1870
+ for (uint32_t i = 0; i < doc_count; i++) {
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);
1875
+ js_validate_doc(&j, doc_roots[i]);
1876
+ yep_validate_path_pop(&j.v, mark);
1877
+ if (j.v.oom) {
1878
+ rc = -1;
1879
+ break;
1880
+ }
1881
+ }
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;
1922
+ }