@bastani/atomic-schema 0.0.0-stage → 0.9.88

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 (178) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +22 -0
  3. package/NOTICE.md +13 -0
  4. package/README.md +63 -2
  5. package/THIRD_PARTY_NOTICES/typebox-LICENSE +23 -0
  6. package/dist/annotations.d.ts +44 -0
  7. package/dist/annotations.d.ts.map +1 -0
  8. package/dist/annotations.js +193 -0
  9. package/dist/annotations.js.map +1 -0
  10. package/dist/builders.d.ts +143 -0
  11. package/dist/builders.d.ts.map +1 -0
  12. package/dist/builders.js +126 -0
  13. package/dist/builders.js.map +1 -0
  14. package/dist/checks.d.ts +114 -0
  15. package/dist/checks.d.ts.map +1 -0
  16. package/dist/checks.js +578 -0
  17. package/dist/checks.js.map +1 -0
  18. package/dist/coerce.d.ts +53 -0
  19. package/dist/coerce.d.ts.map +1 -0
  20. package/dist/coerce.js +624 -0
  21. package/dist/coerce.js.map +1 -0
  22. package/dist/compiler.d.ts +53 -0
  23. package/dist/compiler.d.ts.map +1 -0
  24. package/dist/compiler.js +551 -0
  25. package/dist/compiler.js.map +1 -0
  26. package/dist/explain.d.ts +29 -0
  27. package/dist/explain.d.ts.map +1 -0
  28. package/dist/explain.js +392 -0
  29. package/dist/explain.js.map +1 -0
  30. package/dist/formats/index.d.ts +7 -0
  31. package/dist/formats/index.d.ts.map +1 -0
  32. package/dist/formats/index.js +51 -0
  33. package/dist/formats/index.js.map +1 -0
  34. package/dist/formats/typebox/date.d.ts +7 -0
  35. package/dist/formats/typebox/date.d.ts.map +1 -0
  36. package/dist/formats/typebox/date.js +47 -0
  37. package/dist/formats/typebox/date.js.map +1 -0
  38. package/dist/formats/typebox/date_time.d.ts +7 -0
  39. package/dist/formats/typebox/date_time.d.ts.map +1 -0
  40. package/dist/formats/typebox/date_time.js +39 -0
  41. package/dist/formats/typebox/date_time.js.map +1 -0
  42. package/dist/formats/typebox/duration.d.ts +6 -0
  43. package/dist/formats/typebox/duration.d.ts.map +1 -0
  44. package/dist/formats/typebox/duration.js +36 -0
  45. package/dist/formats/typebox/duration.js.map +1 -0
  46. package/dist/formats/typebox/email.d.ts +6 -0
  47. package/dist/formats/typebox/email.d.ts.map +1 -0
  48. package/dist/formats/typebox/email.js +36 -0
  49. package/dist/formats/typebox/email.js.map +1 -0
  50. package/dist/formats/typebox/hostname.d.ts +8 -0
  51. package/dist/formats/typebox/hostname.d.ts.map +1 -0
  52. package/dist/formats/typebox/hostname.js +38 -0
  53. package/dist/formats/typebox/hostname.js.map +1 -0
  54. package/dist/formats/typebox/idn_email.d.ts +6 -0
  55. package/dist/formats/typebox/idn_email.d.ts.map +1 -0
  56. package/dist/formats/typebox/idn_email.js +36 -0
  57. package/dist/formats/typebox/idn_email.js.map +1 -0
  58. package/dist/formats/typebox/idn_hostname.d.ts +9 -0
  59. package/dist/formats/typebox/idn_hostname.d.ts.map +1 -0
  60. package/dist/formats/typebox/idn_hostname.js +39 -0
  61. package/dist/formats/typebox/idn_hostname.js.map +1 -0
  62. package/dist/formats/typebox/idna/format/bidi.d.ts +4 -0
  63. package/dist/formats/typebox/idna/format/bidi.d.ts.map +1 -0
  64. package/dist/formats/typebox/idna/format/bidi.js +132 -0
  65. package/dist/formats/typebox/idna/format/bidi.js.map +1 -0
  66. package/dist/formats/typebox/idna/format/puny.d.ts +4 -0
  67. package/dist/formats/typebox/idna/format/puny.d.ts.map +1 -0
  68. package/dist/formats/typebox/idna/format/puny.js +172 -0
  69. package/dist/formats/typebox/idna/format/puny.js.map +1 -0
  70. package/dist/formats/typebox/idna/hostname.d.ts +2 -0
  71. package/dist/formats/typebox/idna/hostname.d.ts.map +1 -0
  72. package/dist/formats/typebox/idna/hostname.js +43 -0
  73. package/dist/formats/typebox/idna/hostname.js.map +1 -0
  74. package/dist/formats/typebox/idna/idn-hostname.d.ts +2 -0
  75. package/dist/formats/typebox/idna/idn-hostname.d.ts.map +1 -0
  76. package/dist/formats/typebox/idna/idn-hostname.js +71 -0
  77. package/dist/formats/typebox/idna/idn-hostname.js.map +1 -0
  78. package/dist/formats/typebox/idna/index.d.ts +3 -0
  79. package/dist/formats/typebox/idna/index.d.ts.map +1 -0
  80. package/dist/formats/typebox/idna/index.js +30 -0
  81. package/dist/formats/typebox/idna/index.js.map +1 -0
  82. package/dist/formats/typebox/idna/label/ascii.d.ts +2 -0
  83. package/dist/formats/typebox/idna/label/ascii.d.ts.map +1 -0
  84. package/dist/formats/typebox/idna/label/ascii.js +37 -0
  85. package/dist/formats/typebox/idna/label/ascii.js.map +1 -0
  86. package/dist/formats/typebox/idna/label/puny.d.ts +2 -0
  87. package/dist/formats/typebox/idna/label/puny.d.ts.map +1 -0
  88. package/dist/formats/typebox/idna/label/puny.js +56 -0
  89. package/dist/formats/typebox/idna/label/puny.js.map +1 -0
  90. package/dist/formats/typebox/idna/label/unicode.d.ts +2 -0
  91. package/dist/formats/typebox/idna/label/unicode.d.ts.map +1 -0
  92. package/dist/formats/typebox/idna/label/unicode.js +134 -0
  93. package/dist/formats/typebox/idna/label/unicode.js.map +1 -0
  94. package/dist/formats/typebox/idna/pattern/pattern.d.ts +31 -0
  95. package/dist/formats/typebox/idna/pattern/pattern.d.ts.map +1 -0
  96. package/dist/formats/typebox/idna/pattern/pattern.js +98 -0
  97. package/dist/formats/typebox/idna/pattern/pattern.js.map +1 -0
  98. package/dist/formats/typebox/ipv4.d.ts +6 -0
  99. package/dist/formats/typebox/ipv4.d.ts.map +1 -0
  100. package/dist/formats/typebox/ipv4.js +36 -0
  101. package/dist/formats/typebox/ipv4.js.map +1 -0
  102. package/dist/formats/typebox/ipv6.d.ts +6 -0
  103. package/dist/formats/typebox/ipv6.d.ts.map +1 -0
  104. package/dist/formats/typebox/ipv6.js +36 -0
  105. package/dist/formats/typebox/ipv6.js.map +1 -0
  106. package/dist/formats/typebox/iri.d.ts +6 -0
  107. package/dist/formats/typebox/iri.d.ts.map +1 -0
  108. package/dist/formats/typebox/iri.js +63 -0
  109. package/dist/formats/typebox/iri.js.map +1 -0
  110. package/dist/formats/typebox/iri_reference.d.ts +6 -0
  111. package/dist/formats/typebox/iri_reference.d.ts.map +1 -0
  112. package/dist/formats/typebox/iri_reference.js +40 -0
  113. package/dist/formats/typebox/iri_reference.js.map +1 -0
  114. package/dist/formats/typebox/json_pointer.d.ts +7 -0
  115. package/dist/formats/typebox/json_pointer.d.ts.map +1 -0
  116. package/dist/formats/typebox/json_pointer.js +37 -0
  117. package/dist/formats/typebox/json_pointer.js.map +1 -0
  118. package/dist/formats/typebox/json_pointer_uri_fragment.d.ts +7 -0
  119. package/dist/formats/typebox/json_pointer_uri_fragment.d.ts.map +1 -0
  120. package/dist/formats/typebox/json_pointer_uri_fragment.js +37 -0
  121. package/dist/formats/typebox/json_pointer_uri_fragment.js.map +1 -0
  122. package/dist/formats/typebox/regex.d.ts +6 -0
  123. package/dist/formats/typebox/regex.d.ts.map +1 -0
  124. package/dist/formats/typebox/regex.js +41 -0
  125. package/dist/formats/typebox/regex.js.map +1 -0
  126. package/dist/formats/typebox/relative_json_pointer.d.ts +7 -0
  127. package/dist/formats/typebox/relative_json_pointer.d.ts.map +1 -0
  128. package/dist/formats/typebox/relative_json_pointer.js +37 -0
  129. package/dist/formats/typebox/relative_json_pointer.js.map +1 -0
  130. package/dist/formats/typebox/time.d.ts +6 -0
  131. package/dist/formats/typebox/time.d.ts.map +1 -0
  132. package/dist/formats/typebox/time.js +60 -0
  133. package/dist/formats/typebox/time.js.map +1 -0
  134. package/dist/formats/typebox/uri.d.ts +6 -0
  135. package/dist/formats/typebox/uri.d.ts.map +1 -0
  136. package/dist/formats/typebox/uri.js +36 -0
  137. package/dist/formats/typebox/uri.js.map +1 -0
  138. package/dist/formats/typebox/uri_reference.d.ts +6 -0
  139. package/dist/formats/typebox/uri_reference.d.ts.map +1 -0
  140. package/dist/formats/typebox/uri_reference.js +36 -0
  141. package/dist/formats/typebox/uri_reference.js.map +1 -0
  142. package/dist/formats/typebox/uri_template.d.ts +6 -0
  143. package/dist/formats/typebox/uri_template.d.ts.map +1 -0
  144. package/dist/formats/typebox/uri_template.js +37 -0
  145. package/dist/formats/typebox/uri_template.js.map +1 -0
  146. package/dist/formats/typebox/url.d.ts +7 -0
  147. package/dist/formats/typebox/url.d.ts.map +1 -0
  148. package/dist/formats/typebox/url.js +36 -0
  149. package/dist/formats/typebox/url.js.map +1 -0
  150. package/dist/formats/typebox/uuid.d.ts +7 -0
  151. package/dist/formats/typebox/uuid.d.ts.map +1 -0
  152. package/dist/formats/typebox/uuid.js +37 -0
  153. package/dist/formats/typebox/uuid.js.map +1 -0
  154. package/dist/index.d.ts +5 -0
  155. package/dist/index.d.ts.map +1 -0
  156. package/dist/index.js +6 -0
  157. package/dist/index.js.map +1 -0
  158. package/dist/resolver.d.ts +58 -0
  159. package/dist/resolver.d.ts.map +1 -0
  160. package/dist/resolver.js +209 -0
  161. package/dist/resolver.js.map +1 -0
  162. package/dist/shared.d.ts +9 -0
  163. package/dist/shared.d.ts.map +1 -0
  164. package/dist/shared.js +33 -0
  165. package/dist/shared.js.map +1 -0
  166. package/dist/typebox-static.d.ts +56 -0
  167. package/dist/typebox-static.d.ts.map +1 -0
  168. package/dist/typebox-static.js +7 -0
  169. package/dist/typebox-static.js.map +1 -0
  170. package/dist/types.d.ts +48 -0
  171. package/dist/types.d.ts.map +1 -0
  172. package/dist/types.js +3 -0
  173. package/dist/types.js.map +1 -0
  174. package/dist/validator.d.ts +48 -0
  175. package/dist/validator.d.ts.map +1 -0
  176. package/dist/validator.js +81 -0
  177. package/dist/validator.js.map +1 -0
  178. package/package.json +64 -4
@@ -0,0 +1,551 @@
1
+ // The compiler: a schema to one check closure, built once. Keywords group by the type they constrain, so a value
2
+ // is classified once per schema, and the common shapes (closed objects, arrays of one item schema, tagged unions,
3
+ // leaf schemas) get one specialized closure each.
4
+ import { ANNOTATING_FALSE, ANNOTATING_TRUE, annotateAll, annotateAny, annotateArray, annotateDependent, annotateIf, annotateObject, ignoreAnnotations, indirectAnnotating, isolateAnnotations, scopedAnnotating, unevaluatedItems, unevaluatedProperties, withFreshAnnotations, } from "./annotations.js";
5
+ import { ALWAYS, and, anyCheck, bytesCheck, constCheck, containsCheck, declaredObject, dependentRequiredCheck, dependentSchemasCheck, enumCheck, GUARD_LOOSE, GUARD_NONE, GUARD_STRICT, guardArray, guardObject, INHERITED, IS_ARRAY, IS_OBJECT, ifCheck, indirect, itemsCheck, leafOf, lengthCheck, listCheck, maskedAnyCheck, NEVER, notCheck, numberCheck, oneCheck, openObject, propertyCountCheck, propertyNamesCheck, REQUIRED, scopedCheck, stringCheck, TYPE_CHECKS, taggedCheck, typeCheck, typeMask, uniqueCheck, } from "./checks.js";
6
+ import { documentOf, isSchemaObject, Resolver, resolveUri } from "./resolver.js";
7
+ import { sharedRegex } from "./shared.js";
8
+ /** Thrown when a compile without dynamic scope tracking meets a bookended `$dynamicRef`: compile again with it. */
9
+ export const RESTART = Symbol("restart");
10
+ /** One compile of a schema. Its caches and indexes are garbage once the root check is built. */
11
+ export class Compiler {
12
+ r;
13
+ draft;
14
+ formats;
15
+ /** Whether checks track the dynamic scope (the schema has a bookended `$dynamicRef`). */
16
+ dynamic;
17
+ schema;
18
+ scope;
19
+ cache = new Map();
20
+ annotatingCache = new Map();
21
+ constructor(schema, options, dynamic) {
22
+ this.schema = schema;
23
+ this.r = new Resolver(schema, options);
24
+ this.draft = this.r.draft;
25
+ this.formats = options.formats;
26
+ this.dynamic = dynamic;
27
+ this.scope = [];
28
+ }
29
+ /** The check of the root schema. */
30
+ root() {
31
+ return this.check(this.schema, this.r.rootBase);
32
+ }
33
+ /** The check of a schema (cached by schema object; a recursive reference goes through an indirection). */
34
+ check(node, base) {
35
+ if (node === true)
36
+ return ALWAYS;
37
+ if (node === false)
38
+ return NEVER;
39
+ if (!isSchemaObject(node))
40
+ throw new Error(`A schema must be an object or a boolean, got ${JSON.stringify(node)}`);
41
+ const cached = this.cache.get(node);
42
+ if (cached !== undefined)
43
+ return cached;
44
+ const bare = this.bareType(node);
45
+ if (bare !== undefined)
46
+ return bare;
47
+ const slot = { check: undefined };
48
+ this.cache.set(node, indirect(slot));
49
+ const check = this.build(node, this.r.baseFor(node, base));
50
+ slot.check = check;
51
+ this.cache.set(node, check);
52
+ return check;
53
+ }
54
+ build(node, base) {
55
+ if (this.draft === "7" && typeof node.$ref === "string")
56
+ return this.ref(node.$ref, base);
57
+ if (this.draft !== "7" && (node.unevaluatedProperties !== undefined || node.unevaluatedItems !== undefined)) {
58
+ return withFreshAnnotations(this.annotating(node, base));
59
+ }
60
+ const check = this.plain(node, base);
61
+ return this.dynamic && this.r.isResource(node, base) ? scopedCheck(this.scope, base, check) : check;
62
+ }
63
+ /**
64
+ * The module-level check of a schema that is only a `type` (with annotations), `ALWAYS` for one with no assertion
65
+ * at all; undefined for every other schema. These are the most common subschemas and cost nothing to share.
66
+ */
67
+ bareType(node) {
68
+ let type;
69
+ for (const k in node) {
70
+ if (k === "type")
71
+ type = node.type;
72
+ else if (!NEUTRAL_KEYWORDS.has(k))
73
+ return undefined;
74
+ }
75
+ if (type === undefined)
76
+ return ALWAYS;
77
+ return typeof type === "string" ? TYPE_CHECKS.get(type) : undefined;
78
+ }
79
+ /** Every keyword of `node`, not tracking annotations. */
80
+ plain(node, base) {
81
+ const parts = [];
82
+ const types = node.type === undefined ? undefined : Array.isArray(node.type) ? node.type : [node.type];
83
+ const single = types?.length === 1 ? types[0] : undefined;
84
+ let typeInGroup = false;
85
+ if (single === "bytes") {
86
+ parts.push(bytesCheck(number(node.minLength) ?? 0, number(node.maxLength) ?? Number.POSITIVE_INFINITY));
87
+ typeInGroup = true;
88
+ }
89
+ else {
90
+ const string = this.stringGroup(node, single === "string");
91
+ if (string !== undefined)
92
+ parts.push(string);
93
+ const numeric = single === "number" || single === "integer" || single === "bigint";
94
+ const mode = single === "number" ? 1 : single === "integer" ? 2 : single === "bigint" ? 3 : 0;
95
+ const number = this.numberGroup(node, mode);
96
+ if (number !== undefined)
97
+ parts.push(number);
98
+ const object = this.objectGroup(node, base, single === "object");
99
+ if (object !== undefined)
100
+ parts.push(object);
101
+ const array = this.arrayGroup(node, base, single === "array");
102
+ if (array !== undefined)
103
+ parts.push(array);
104
+ typeInGroup =
105
+ (single === "string" && string !== undefined) ||
106
+ (numeric && number !== undefined) ||
107
+ (single === "object" && object !== undefined) ||
108
+ (single === "array" && array !== undefined);
109
+ }
110
+ if (types !== undefined && !typeInGroup)
111
+ parts.unshift(typeCheck(types));
112
+ this.inPlace(node, base, parts);
113
+ return and(parts);
114
+ }
115
+ // The keywords that apply to the value itself, whatever its type.
116
+ inPlace(node, base, parts) {
117
+ if (Array.isArray(node.enum))
118
+ parts.push(enumCheck(node.enum));
119
+ if (Object.hasOwn(node, "const"))
120
+ parts.push(constCheck(node.const));
121
+ if (Array.isArray(node.allOf))
122
+ parts.push(and(node.allOf.map((s) => this.check(s, base))));
123
+ if (Array.isArray(node.anyOf))
124
+ parts.push(this.union(node.anyOf, base, false));
125
+ if (Array.isArray(node.oneOf))
126
+ parts.push(this.union(node.oneOf, base, true));
127
+ if (node.not !== undefined)
128
+ parts.push(notCheck(this.check(node.not, base)));
129
+ if (node.if !== undefined && (node.then !== undefined || node.else !== undefined)) {
130
+ const then = node.then === undefined ? ALWAYS : this.check(node.then, base);
131
+ const otherwise = node.else === undefined ? ALWAYS : this.check(node.else, base);
132
+ parts.push(ifCheck(this.check(node.if, base), then, otherwise));
133
+ }
134
+ if (typeof node.$ref === "string")
135
+ parts.push(this.ref(node.$ref, base));
136
+ if (this.draft !== "7" && typeof node.$dynamicRef === "string") {
137
+ parts.push(this.dynamicRef(node.$dynamicRef, base));
138
+ }
139
+ }
140
+ union(branches, base, exactlyOne) {
141
+ const checks = () => branches.map((s) => this.check(s, base));
142
+ return (constUnion(branches, exactlyOne) ??
143
+ this.tagged(branches, base) ??
144
+ (exactlyOne ? oneCheck(checks()) : anyOf(checks())));
145
+ }
146
+ ref(ref, base) {
147
+ const target = this.r.resolve(ref, base);
148
+ const check = this.check(target.node, target.base);
149
+ return this.dynamic && target.base !== base ? scopedCheck(this.scope, target.base, check) : check;
150
+ }
151
+ dynamicRef(ref, base) {
152
+ const resolved = this.bookended(ref, base);
153
+ if (resolved === undefined)
154
+ return this.ref(ref, base);
155
+ const { initial, name } = resolved;
156
+ const fallback = this.check(initial.node, initial.base);
157
+ const lookup = this.dynamicLookup(name, (node, b) => this.check(node, b));
158
+ const scope = this.scope;
159
+ return (v) => {
160
+ for (let i = 0; i < scope.length; i++) {
161
+ const check = lookup(scope[i]);
162
+ if (check !== null)
163
+ return check(v);
164
+ }
165
+ return fallback(v);
166
+ };
167
+ }
168
+ /** The initial target and anchor name of a `$dynamicRef` that resolves dynamically, or undefined for a plain ref. */
169
+ bookended(ref, base) {
170
+ const initial = this.r.resolve(ref, base);
171
+ const uri = resolveUri(ref, base);
172
+ const name = uri.slice(documentOf(uri).length + 1);
173
+ const anchored = isSchemaObject(initial.node) && initial.node.$dynamicAnchor === name;
174
+ if (name === "" || name.startsWith("/") || !anchored)
175
+ return undefined;
176
+ if (!this.dynamic)
177
+ throw RESTART;
178
+ return { initial, name };
179
+ }
180
+ // Resolved at run time against the dynamic scope, so a schema with a bookended $dynamicRef keeps its compiler.
181
+ dynamicLookup(name, compile) {
182
+ const found = new Map();
183
+ return (uri) => {
184
+ const key = `${uri}#${name}`;
185
+ let check = found.get(key);
186
+ if (check === undefined) {
187
+ const target = this.r.dynamicAnchors.get(key);
188
+ check = target === undefined ? null : compile(target, this.r.baseOf.get(target) ?? uri);
189
+ found.set(key, check);
190
+ }
191
+ return check;
192
+ };
193
+ }
194
+ /** anyOf/oneOf whose branches are all objects with a distinct required const on one key: the tag picks the branch. */
195
+ tagged(branches, base) {
196
+ if (branches.length < 2)
197
+ return undefined;
198
+ const resolved = [];
199
+ for (const branch of branches) {
200
+ if (!isSchemaObject(branch))
201
+ return undefined;
202
+ const ref = typeof branch.$ref === "string" && Object.keys(branch).length === 1 ? branch.$ref : undefined;
203
+ const node = ref === undefined ? branch : this.r.resolve(ref, this.r.baseFor(branch, base)).node;
204
+ if (!isSchemaObject(node))
205
+ return undefined;
206
+ resolved.push(node);
207
+ }
208
+ const first = resolved[0].properties;
209
+ if (!isSchemaObject(first))
210
+ return undefined;
211
+ for (const key of Object.keys(first)) {
212
+ const tags = resolved.map((s) => tagOf(s, key));
213
+ if (tags.some((t) => t === undefined) || new Set(tags).size !== tags.length)
214
+ continue;
215
+ return taggedCheck(key, tags, branches.map((s) => this.check(s, base)));
216
+ }
217
+ return undefined;
218
+ }
219
+ stringGroup(node, strict) {
220
+ const min = number(node.minLength) ?? 0;
221
+ const max = number(node.maxLength) ?? Number.POSITIVE_INFINITY;
222
+ const re = typeof node.pattern === "string" ? sharedRegex(node.pattern) : undefined;
223
+ const format = this.formats !== undefined && typeof node.format === "string" && Object.hasOwn(this.formats, node.format)
224
+ ? this.formats[node.format]
225
+ : undefined;
226
+ if (min === 0 && max === Number.POSITIVE_INFINITY && re === undefined && format === undefined)
227
+ return undefined;
228
+ return stringCheck(strict, min, max, re, format);
229
+ }
230
+ numberGroup(node, mode) {
231
+ const min = bound(node.minimum);
232
+ const max = bound(node.maximum);
233
+ const xmin = bound(node.exclusiveMinimum);
234
+ const xmax = bound(node.exclusiveMaximum);
235
+ const multipleOf = bound(node.multipleOf);
236
+ if ([min, max, xmin, xmax, multipleOf].every((x) => x === undefined))
237
+ return undefined;
238
+ const inf = Number.POSITIVE_INFINITY;
239
+ return numberCheck(mode, min ?? -inf, max ?? inf, xmin ?? -inf, xmax ?? inf, multipleOf);
240
+ }
241
+ objectGroup(node, base, strict) {
242
+ const onlyCore = OBJECT_EXTRAS.every((k) => node[k] === undefined);
243
+ // A schema with only the core object keywords gets one closure that also does the type guard.
244
+ const core = this.objectCore(node, base, onlyCore ? (strict ? GUARD_STRICT : GUARD_LOOSE) : GUARD_NONE);
245
+ if (core !== undefined && onlyCore)
246
+ return core;
247
+ const parts = core === undefined ? [] : [core];
248
+ if (node.propertyNames !== undefined)
249
+ parts.push(propertyNamesCheck(this.check(node.propertyNames, base)));
250
+ const minProperties = number(node.minProperties);
251
+ const maxProperties = number(node.maxProperties);
252
+ if (minProperties !== undefined || maxProperties !== undefined) {
253
+ parts.push(propertyCountCheck(minProperties ?? 0, maxProperties ?? Number.POSITIVE_INFINITY));
254
+ }
255
+ const dependentRequired = [];
256
+ const dependentSchemas = [];
257
+ if (this.draft !== "7") {
258
+ for (const [k, list] of entries(node.dependentRequired))
259
+ if (Array.isArray(list))
260
+ dependentRequired.push([k, list]);
261
+ for (const [k, s] of entries(node.dependentSchemas))
262
+ dependentSchemas.push([k, this.check(s, base)]);
263
+ }
264
+ for (const [k, d] of entries(node.dependencies)) {
265
+ if (Array.isArray(d))
266
+ dependentRequired.push([k, d]);
267
+ else
268
+ dependentSchemas.push([k, this.check(d, base)]);
269
+ }
270
+ if (dependentRequired.length > 0)
271
+ parts.push(dependentRequiredCheck(dependentRequired));
272
+ if (dependentSchemas.length > 0)
273
+ parts.push(dependentSchemasCheck(dependentSchemas));
274
+ if (parts.length === 0)
275
+ return strict ? IS_OBJECT : undefined;
276
+ return guardObject(and(parts), strict);
277
+ }
278
+ objectCore(node, base, guard) {
279
+ const properties = isSchemaObject(node.properties) ? node.properties : {};
280
+ const required = Array.isArray(node.required) ? node.required.filter((k) => typeof k === "string") : [];
281
+ const patterns = isSchemaObject(node.patternProperties) ? node.patternProperties : {};
282
+ const additional = node.additionalProperties;
283
+ const keys = Object.keys(properties);
284
+ const patternKeys = Object.keys(patterns);
285
+ if (keys.length + required.length + patternKeys.length === 0 &&
286
+ (additional === undefined || additional === true)) {
287
+ return undefined;
288
+ }
289
+ // One array per object: key, check (or a leaf's argument), flags (leaf code, required, key also names an
290
+ // Object.prototype member).
291
+ const entries = [];
292
+ for (const k of keys) {
293
+ const check = this.check(properties[k], base);
294
+ const leaf = leafOf(check);
295
+ const flags = (leaf?.[0] ?? 0) | (required.includes(k) ? REQUIRED : 0) | (k in Object.prototype ? INHERITED : 0);
296
+ entries.push(k, leaf === undefined ? check : leaf[1], flags);
297
+ }
298
+ const extraRequired = required.filter((k) => !Object.hasOwn(properties, k));
299
+ const pats = patternKeys.map((p) => [sharedRegex(p), this.check(patterns[p], base)]);
300
+ const extra = additional === undefined || additional === true ? undefined : this.check(additional, base);
301
+ if (pats.length === 0 && extraRequired.length === 0 && (extra === undefined || additional === false)) {
302
+ const requiredCount = keys.filter((k) => required.includes(k)).length;
303
+ return declaredObject(entries, requiredCount, additional === false, guard);
304
+ }
305
+ return openObject(entries, keys, extraRequired, pats, extra, guard);
306
+ }
307
+ arrayGroup(node, base, strict) {
308
+ let prefix = [];
309
+ let rest;
310
+ if (Array.isArray(node.items)) {
311
+ prefix = node.items;
312
+ rest = node.additionalItems;
313
+ }
314
+ else {
315
+ if (this.draft !== "7" && Array.isArray(node.prefixItems))
316
+ prefix = node.prefixItems;
317
+ rest = node.items;
318
+ }
319
+ const prefixChecks = prefix.map((s) => this.check(s, base));
320
+ const restCheck = rest === undefined || rest === true ? undefined : this.check(rest, base);
321
+ const minItems = number(node.minItems);
322
+ const maxItems = number(node.maxItems);
323
+ if (node.contains === undefined &&
324
+ node.uniqueItems !== true &&
325
+ (prefixChecks.length > 0 || restCheck !== undefined)) {
326
+ // The common array: one closure for the guard, the length bounds and the items.
327
+ const guard = strict ? GUARD_STRICT : GUARD_LOOSE;
328
+ return listCheck(guard, minItems ?? 0, maxItems ?? Number.POSITIVE_INFINITY, prefixChecks, restCheck);
329
+ }
330
+ const parts = [];
331
+ if (prefixChecks.length > 0 || restCheck !== undefined)
332
+ parts.push(itemsCheck(prefixChecks, restCheck));
333
+ if (minItems !== undefined || maxItems !== undefined) {
334
+ parts.push(lengthCheck(minItems ?? 0, maxItems ?? Number.POSITIVE_INFINITY));
335
+ }
336
+ if (node.contains !== undefined) {
337
+ const min = this.draft === "7" ? 1 : (number(node.minContains) ?? 1);
338
+ const max = this.draft === "7" ? Number.POSITIVE_INFINITY : (number(node.maxContains) ?? Number.POSITIVE_INFINITY);
339
+ parts.push(containsCheck(this.check(node.contains, base), min, max));
340
+ }
341
+ if (node.uniqueItems === true)
342
+ parts.push(uniqueCheck);
343
+ if (parts.length === 0)
344
+ return strict ? IS_ARRAY : undefined;
345
+ return guardArray(and(parts), strict);
346
+ }
347
+ /** The annotating check of a schema evaluated in place (only below `unevaluatedProperties`/`unevaluatedItems`). */
348
+ annotating(node, base) {
349
+ if (node === true)
350
+ return ANNOTATING_TRUE;
351
+ if (node === false)
352
+ return ANNOTATING_FALSE;
353
+ if (!isSchemaObject(node))
354
+ throw new Error(`A schema must be an object or a boolean, got ${JSON.stringify(node)}`);
355
+ const cached = this.annotatingCache.get(node);
356
+ if (cached !== undefined)
357
+ return cached;
358
+ const slot = { check: undefined };
359
+ this.annotatingCache.set(node, indirectAnnotating(slot));
360
+ const check = this.buildAnnotating(node, this.r.baseFor(node, base));
361
+ slot.check = check;
362
+ this.annotatingCache.set(node, check);
363
+ return check;
364
+ }
365
+ buildAnnotating(node, base) {
366
+ const rest = {};
367
+ let annotates = false;
368
+ for (const k of Object.keys(node)) {
369
+ if (ANNOTATING.has(k))
370
+ annotates = true;
371
+ else
372
+ rest[k] = node[k];
373
+ }
374
+ const plain = this.plain(rest, base);
375
+ const parts = [];
376
+ if (annotates)
377
+ this.annotatingParts(node, base, parts);
378
+ const all = parts.length === 0 ? ignoreAnnotations(plain) : annotateAll(plain, parts);
379
+ const own = node.unevaluatedProperties !== undefined || node.unevaluatedItems !== undefined
380
+ ? isolateAnnotations(all)
381
+ : all;
382
+ return this.dynamic && this.r.isResource(node, base) ? scopedAnnotating(this.scope, base, own) : own;
383
+ }
384
+ annotatingParts(node, base, parts) {
385
+ if (node.properties !== undefined ||
386
+ node.patternProperties !== undefined ||
387
+ node.additionalProperties !== undefined) {
388
+ const props = entries(node.properties).map(([k, s]) => [k, this.check(s, base)]);
389
+ const pats = entries(node.patternProperties).map(([p, s]) => [sharedRegex(p), this.check(s, base)]);
390
+ const extra = node.additionalProperties === undefined ? undefined : this.check(node.additionalProperties, base);
391
+ parts.push(annotateObject(props, pats, extra));
392
+ }
393
+ if (node.prefixItems !== undefined || node.items !== undefined || node.contains !== undefined) {
394
+ const prefix = Array.isArray(node.prefixItems)
395
+ ? node.prefixItems
396
+ : Array.isArray(node.items)
397
+ ? node.items
398
+ : [];
399
+ const rest = Array.isArray(node.items) ? node.additionalItems : node.items;
400
+ parts.push(annotateArray(prefix.map((s) => this.check(s, base)), rest === undefined ? undefined : this.check(rest, base), node.contains === undefined ? undefined : this.check(node.contains, base), number(node.minContains) ?? 1, number(node.maxContains) ?? Number.POSITIVE_INFINITY));
401
+ }
402
+ if (Array.isArray(node.allOf))
403
+ for (const s of node.allOf)
404
+ parts.push(this.annotating(s, base));
405
+ if (Array.isArray(node.anyOf))
406
+ parts.push(annotateAny(node.anyOf.map((s) => this.annotating(s, base)), false));
407
+ if (Array.isArray(node.oneOf))
408
+ parts.push(annotateAny(node.oneOf.map((s) => this.annotating(s, base)), true));
409
+ if (node.if !== undefined) {
410
+ const then = node.then === undefined ? undefined : this.annotating(node.then, base);
411
+ const otherwise = node.else === undefined ? undefined : this.annotating(node.else, base);
412
+ parts.push(annotateIf(this.annotating(node.if, base), then, otherwise));
413
+ }
414
+ if (typeof node.$ref === "string")
415
+ parts.push(this.annotatingRef(node.$ref, base));
416
+ if (typeof node.$dynamicRef === "string")
417
+ parts.push(this.annotatingDynamicRef(node.$dynamicRef, base));
418
+ for (const [k, s] of entries(node.dependentSchemas))
419
+ parts.push(annotateDependent(k, this.annotating(s, base)));
420
+ if (node.unevaluatedProperties !== undefined) {
421
+ parts.push(unevaluatedProperties(this.check(node.unevaluatedProperties, base)));
422
+ }
423
+ if (node.unevaluatedItems !== undefined)
424
+ parts.push(unevaluatedItems(this.check(node.unevaluatedItems, base)));
425
+ }
426
+ annotatingRef(ref, base) {
427
+ const target = this.r.resolve(ref, base);
428
+ const check = this.annotating(target.node, target.base);
429
+ return this.dynamic && target.base !== base ? scopedAnnotating(this.scope, target.base, check) : check;
430
+ }
431
+ annotatingDynamicRef(ref, base) {
432
+ const resolved = this.bookended(ref, base);
433
+ if (resolved === undefined)
434
+ return this.annotatingRef(ref, base);
435
+ const { initial, name } = resolved;
436
+ const fallback = this.annotating(initial.node, initial.base);
437
+ const lookup = this.dynamicLookup(name, (node, b) => this.annotating(node, b));
438
+ const scope = this.scope;
439
+ return (v, a) => {
440
+ for (let i = 0; i < scope.length; i++) {
441
+ const check = lookup(scope[i]);
442
+ if (check !== null)
443
+ return check(v, a);
444
+ }
445
+ return fallback(v, a);
446
+ };
447
+ }
448
+ }
449
+ /** Keywords that never affect a check. */
450
+ export const NEUTRAL_KEYWORDS = new Set([
451
+ "$schema",
452
+ "$comment",
453
+ "title",
454
+ "description",
455
+ "default",
456
+ "examples",
457
+ "deprecated",
458
+ "readOnly",
459
+ "writeOnly",
460
+ "$defs",
461
+ "definitions",
462
+ "contentEncoding",
463
+ "contentMediaType",
464
+ "minContains",
465
+ "maxContains",
466
+ "$vocabulary",
467
+ ]);
468
+ // The object keywords besides the core (properties, required, patternProperties, additionalProperties).
469
+ const OBJECT_EXTRAS = [
470
+ "propertyNames",
471
+ "minProperties",
472
+ "maxProperties",
473
+ "dependentRequired",
474
+ "dependentSchemas",
475
+ "dependencies",
476
+ ];
477
+ // The keywords an annotating check handles itself; every other keyword compiles into its plain part.
478
+ const ANNOTATING = new Set([
479
+ "properties",
480
+ "patternProperties",
481
+ "additionalProperties",
482
+ "prefixItems",
483
+ "items",
484
+ "additionalItems",
485
+ "contains",
486
+ "minContains",
487
+ "maxContains",
488
+ "allOf",
489
+ "anyOf",
490
+ "oneOf",
491
+ "if",
492
+ "then",
493
+ "else",
494
+ "$ref",
495
+ "$dynamicRef",
496
+ "dependentSchemas",
497
+ "unevaluatedProperties",
498
+ "unevaluatedItems",
499
+ ]);
500
+ /** anyOf/oneOf whose branches are all `{ const: primitive }`: an enum (distinct values make oneOf the same). */
501
+ function constUnion(branches, exactlyOne) {
502
+ const values = [];
503
+ for (const branch of branches) {
504
+ if (!isSchemaObject(branch) || !Object.hasOwn(branch, "const"))
505
+ return undefined;
506
+ for (const k of Object.keys(branch))
507
+ if (k !== "const" && !NEUTRAL_KEYWORDS.has(k))
508
+ return undefined;
509
+ if (typeof branch.const === "object" && branch.const !== null)
510
+ return undefined;
511
+ if (exactlyOne && values.includes(branch.const))
512
+ return undefined;
513
+ values.push(branch.const);
514
+ }
515
+ return enumCheck(values);
516
+ }
517
+ /** `anyOf`: its bare-type branches (`nullable` gives one) run as one type test. */
518
+ function anyOf(checks) {
519
+ const types = [];
520
+ const rest = [];
521
+ for (const check of checks) {
522
+ const type = BARE_TYPES.get(check);
523
+ if (type === undefined)
524
+ rest.push(check);
525
+ else
526
+ types.push(type);
527
+ }
528
+ return types.length === 0 ? anyCheck(rest) : maskedAnyCheck(typeMask(types), rest);
529
+ }
530
+ const BARE_TYPES = new Map([...TYPE_CHECKS].map(([type, check]) => [check, type]));
531
+ // The tag of a tagged-union branch: a primitive const on a required property of an object schema.
532
+ function tagOf(branch, key) {
533
+ const properties = branch.properties;
534
+ if (branch.type !== "object" || !Array.isArray(branch.required) || !branch.required.includes(key))
535
+ return undefined;
536
+ const tag = isSchemaObject(properties) ? properties[key] : undefined;
537
+ if (!isSchemaObject(tag) || !Object.hasOwn(tag, "const"))
538
+ return undefined;
539
+ return typeof tag.const === "object" && tag.const !== null ? undefined : tag.const;
540
+ }
541
+ /** A number keyword's value: a number, or a bigint (TypeBox's `Type.BigInt` bounds). */
542
+ function bound(value) {
543
+ return typeof value === "number" || typeof value === "bigint" ? value : undefined;
544
+ }
545
+ function number(value) {
546
+ return typeof value === "number" ? value : undefined;
547
+ }
548
+ function entries(value) {
549
+ return isSchemaObject(value) ? Object.entries(value) : [];
550
+ }
551
+ //# sourceMappingURL=compiler.js.map