@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,7 @@
1
+ /**
2
+ * Returns true if the value is a uuid
3
+ * @specification https://www.rfc-editor.org/info/rfc4122/
4
+ * @specification https://www.rfc-editor.org/info/rfc9562/
5
+ */
6
+ export declare function IsUuid(value: string): boolean;
7
+ //# sourceMappingURL=uuid.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uuid.d.ts","sourceRoot":"","sources":["../../../src/formats/typebox/uuid.ts"],"names":[],"mappings":"AA8BA;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAE7C"}
@@ -0,0 +1,37 @@
1
+ /*--------------------------------------------------------------------------
2
+
3
+ TypeBox
4
+
5
+ The MIT License (MIT)
6
+
7
+ Copyright (c) 2017-2026 Haydn Paterson
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ of this software and associated documentation files (the "Software"), to deal
11
+ in the Software without restriction, including without limitation the rights
12
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ copies of the Software, and to permit persons to whom the Software is
14
+ furnished to do so, subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in
17
+ all copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
+ THE SOFTWARE.
26
+
27
+ ---------------------------------------------------------------------------*/
28
+ const Uuid = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i;
29
+ /**
30
+ * Returns true if the value is a uuid
31
+ * @specification https://www.rfc-editor.org/info/rfc4122/
32
+ * @specification https://www.rfc-editor.org/info/rfc9562/
33
+ */
34
+ export function IsUuid(value) {
35
+ return Uuid.test(value);
36
+ }
37
+ //# sourceMappingURL=uuid.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uuid.js","sourceRoot":"","sources":["../../../src/formats/typebox/uuid.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;6EA0B6E;AAE7E,MAAM,IAAI,GAAG,gDAAgD,CAAC;AAE9D;;;;GAIG;AACH,MAAM,UAAU,MAAM,CAAC,KAAa;IACnC,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AACzB,CAAC","sourcesContent":["/*--------------------------------------------------------------------------\n\nTypeBox\n\nThe MIT License (MIT)\n\nCopyright (c) 2017-2026 Haydn Paterson\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n\n---------------------------------------------------------------------------*/\n\nconst Uuid = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i;\n\n/**\n * Returns true if the value is a uuid\n * @specification https://www.rfc-editor.org/info/rfc4122/\n * @specification https://www.rfc-editor.org/info/rfc9562/\n */\nexport function IsUuid(value: string): boolean {\n\treturn Uuid.test(value);\n}\n"]}
@@ -0,0 +1,5 @@
1
+ export { type Annotations, arr, type BigintBounds, bigint, bool, bytes, int, type LengthBounds, lit, map, type NumberBounds, nul, nullable, num, Optional, obj, oneOf, opt, type Properties, recursive, type Schema, type Static, type StaticObject, str, tuple, union, unknown, unsafe, } from "./builders.ts";
2
+ export { deepEqual as equal } from "./checks.ts";
3
+ export type { Check, CompileOptions, FormatCheck, JsonSchema, ParseResult, ValidationError } from "./types.ts";
4
+ export { CoercingValidator, compile, Validator } from "./validator.ts";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EACN,KAAK,WAAW,EAChB,GAAG,EACH,KAAK,YAAY,EACjB,MAAM,EACN,IAAI,EACJ,KAAK,EACL,GAAG,EACH,KAAK,YAAY,EACjB,GAAG,EACH,GAAG,EACH,KAAK,YAAY,EACjB,GAAG,EACH,QAAQ,EACR,GAAG,EACH,QAAQ,EACR,GAAG,EACH,KAAK,EACL,GAAG,EACH,KAAK,UAAU,EACf,SAAS,EACT,KAAK,MAAM,EACX,KAAK,MAAM,EACX,KAAK,YAAY,EACjB,GAAG,EACH,KAAK,EACL,KAAK,EACL,OAAO,EACP,MAAM,GACN,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,SAAS,IAAI,KAAK,EAAE,MAAM,aAAa,CAAC;AACjD,YAAY,EAAE,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,UAAU,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC/G,OAAO,EAAE,iBAAiB,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ // JSON Schema validation (drafts 2020-12 and 7) without eval: compile() builds a schema into plain closures once.
2
+ // The builders return plain JSON Schema objects typed with the values they accept.
3
+ export { arr, bigint, bool, bytes, int, lit, map, nul, nullable, num, Optional, obj, oneOf, opt, recursive, str, tuple, union, unknown, unsafe, } from "./builders.js";
4
+ export { deepEqual as equal } from "./checks.js";
5
+ export { CoercingValidator, compile, Validator } from "./validator.js";
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,kHAAkH;AAClH,mFAAmF;AAEnF,OAAO,EAEN,GAAG,EAEH,MAAM,EACN,IAAI,EACJ,KAAK,EACL,GAAG,EAEH,GAAG,EACH,GAAG,EAEH,GAAG,EACH,QAAQ,EACR,GAAG,EACH,QAAQ,EACR,GAAG,EACH,KAAK,EACL,GAAG,EAEH,SAAS,EAIT,GAAG,EACH,KAAK,EACL,KAAK,EACL,OAAO,EACP,MAAM,GACN,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,SAAS,IAAI,KAAK,EAAE,MAAM,aAAa,CAAC;AAEjD,OAAO,EAAE,iBAAiB,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC","sourcesContent":["// JSON Schema validation (drafts 2020-12 and 7) without eval: compile() builds a schema into plain closures once.\n// The builders return plain JSON Schema objects typed with the values they accept.\n\nexport {\n\ttype Annotations,\n\tarr,\n\ttype BigintBounds,\n\tbigint,\n\tbool,\n\tbytes,\n\tint,\n\ttype LengthBounds,\n\tlit,\n\tmap,\n\ttype NumberBounds,\n\tnul,\n\tnullable,\n\tnum,\n\tOptional,\n\tobj,\n\toneOf,\n\topt,\n\ttype Properties,\n\trecursive,\n\ttype Schema,\n\ttype Static,\n\ttype StaticObject,\n\tstr,\n\ttuple,\n\tunion,\n\tunknown,\n\tunsafe,\n} from \"./builders.ts\";\nexport { deepEqual as equal } from \"./checks.ts\";\nexport type { Check, CompileOptions, FormatCheck, JsonSchema, ParseResult, ValidationError } from \"./types.ts\";\nexport { CoercingValidator, compile, Validator } from \"./validator.ts\";\n"]}
@@ -0,0 +1,58 @@
1
+ import type { CompileOptions, JsonSchema, SchemaObject } from "./types.ts";
2
+ /** A schema and the base URI its references resolve against. */
3
+ export interface Resolved {
4
+ readonly node: unknown;
5
+ readonly base: string;
6
+ }
7
+ /** The base URI of a root schema without `$id`; relative references resolve against it. */
8
+ export declare const DEFAULT_BASE = "https://json-schema.invalid/root.json";
9
+ /**
10
+ * Resolves references in the root schema and the registered documents. The schema is indexed on the first
11
+ * reference or the first `$id` a walker meets, so a schema with neither is never walked for identifiers.
12
+ */
13
+ export declare class Resolver {
14
+ readonly draft: "2020-12" | "7";
15
+ /** The base URI the root schema is walked from; walkers apply the root's own `$id` through `baseFor`. */
16
+ readonly rootBase = "https://json-schema.invalid/root.json";
17
+ /** Schema object to the base URI it was indexed under. */
18
+ readonly baseOf: Map<object, string>;
19
+ /** URI (with a `#name` fragment) of every `$dynamicAnchor`, to its schema. */
20
+ readonly dynamicAnchors: Map<string, SchemaObject>;
21
+ private readonly root;
22
+ private readonly options;
23
+ private readonly registry;
24
+ private readonly rejectUnresolved;
25
+ /** The references that named nothing and resolved to `false` (with `unresolvedRefs: "reject"`). */
26
+ private readonly unresolved;
27
+ private readonly resources;
28
+ private readonly anchors;
29
+ private indexed;
30
+ constructor(root: JsonSchema, options: CompileOptions);
31
+ /**
32
+ * The base URI of `node`, whose parent has `base`: its `$id` changes it, unless draft 7 makes the `$id` inert. Once
33
+ * the schema is indexed, the indexed base (a resolved reference target already has its own).
34
+ */
35
+ baseFor(node: SchemaObject, base: string): string;
36
+ /** Indexes the root schema and the registered documents' URIs (once). */
37
+ ensureIndexed(): void;
38
+ /**
39
+ * The schema a reference names, with the base URI it compiles under. Throws if it names nothing, or, with
40
+ * `unresolvedRefs: "reject"`, resolves to `false`, the schema no value passes.
41
+ */
42
+ resolve(ref: string, base: string): Resolved;
43
+ /** Whether `ref`, from `base`, named nothing and resolved to `false`. */
44
+ isUnresolved(ref: string, base: string): boolean;
45
+ private find;
46
+ /** Whether `node` is the root of the schema resource at `base`. */
47
+ isResource(node: object, base: string): boolean;
48
+ private index;
49
+ private load;
50
+ private pointer;
51
+ }
52
+ /** Whether `value` is a schema object (not `true`/`false`, an array or a primitive). */
53
+ export declare function isSchemaObject(value: unknown): value is SchemaObject;
54
+ /** `ref` resolved against `base`. URNs and other bases a URL parser rejects take fragment-only references. */
55
+ export declare function resolveUri(ref: string, base: string): string;
56
+ /** `uri` without its fragment. */
57
+ export declare function documentOf(uri: string): string;
58
+ //# sourceMappingURL=resolver.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolver.d.ts","sourceRoot":"","sources":["../src/resolver.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE3E,gEAAgE;AAChE,MAAM,WAAW,QAAQ;IACxB,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACtB;AAED,2FAA2F;AAC3F,eAAO,MAAM,YAAY,0CAA0C,CAAC;AAEpE;;;GAGG;AACH,qBAAa,QAAQ;IACpB,QAAQ,CAAC,KAAK,EAAE,SAAS,GAAG,GAAG,CAAC;IAChC,yGAAyG;IACzG,QAAQ,CAAC,QAAQ,2CAAgB;IACjC,0DAA0D;IAC1D,QAAQ,CAAC,MAAM,sBAA6B;IAC5C,8EAA8E;IAC9E,QAAQ,CAAC,cAAc,4BAAmC;IAC1D,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAa;IAClC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAiC;IAC1D,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAU;IAC3C,mGAAmG;IACnG,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAqB;IAChD,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA8B;IACxD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAmC;IAC3D,OAAO,CAAC,OAAO,CAAS;IAExB,YAAY,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,cAAc,EAKpD;IAED;;;OAGG;IACH,OAAO,CAAC,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAahD;IAED,yEAAyE;IACzE,aAAa,IAAI,IAAI,CAQpB;IAED;;;OAGG;IACH,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,QAAQ,CAS3C;IAED,yEAAyE;IACzE,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAE/C;IAED,OAAO,CAAC,IAAI;IAaZ,mEAAmE;IACnE,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAG9C;IAGD,OAAO,CAAC,KAAK;IAkCb,OAAO,CAAC,IAAI;IAQZ,OAAO,CAAC,OAAO;CAcf;AAqBD,wFAAwF;AACxF,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,YAAY,CAEpE;AAED,8GAA8G;AAC9G,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAM5D;AAED,kCAAkC;AAClC,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAG9C"}
@@ -0,0 +1,209 @@
1
+ // Reference resolution of one compile: schema resources, anchors and JSON Pointers over the root schema and the
2
+ // registered documents. A Resolver lives only while a schema compiles (or while its errors are explained).
3
+ /** The base URI of a root schema without `$id`; relative references resolve against it. */
4
+ export const DEFAULT_BASE = "https://json-schema.invalid/root.json";
5
+ /**
6
+ * Resolves references in the root schema and the registered documents. The schema is indexed on the first
7
+ * reference or the first `$id` a walker meets, so a schema with neither is never walked for identifiers.
8
+ */
9
+ export class Resolver {
10
+ draft;
11
+ /** The base URI the root schema is walked from; walkers apply the root's own `$id` through `baseFor`. */
12
+ rootBase = DEFAULT_BASE;
13
+ /** Schema object to the base URI it was indexed under. */
14
+ baseOf = new Map();
15
+ /** URI (with a `#name` fragment) of every `$dynamicAnchor`, to its schema. */
16
+ dynamicAnchors = new Map();
17
+ root;
18
+ options;
19
+ registry = new Map();
20
+ rejectUnresolved;
21
+ /** The references that named nothing and resolved to `false` (with `unresolvedRefs: "reject"`). */
22
+ unresolved = new Set();
23
+ resources = new Map();
24
+ anchors = new Map();
25
+ indexed = false;
26
+ constructor(root, options) {
27
+ this.draft = options.draft === "7" ? "7" : "2020-12";
28
+ this.root = root;
29
+ this.options = options;
30
+ this.rejectUnresolved = options.unresolvedRefs === "reject";
31
+ }
32
+ /**
33
+ * The base URI of `node`, whose parent has `base`: its `$id` changes it, unless draft 7 makes the `$id` inert. Once
34
+ * the schema is indexed, the indexed base (a resolved reference target already has its own).
35
+ */
36
+ baseFor(node, base) {
37
+ let indexed = this.baseOf.get(node);
38
+ if (indexed !== undefined)
39
+ return indexed;
40
+ const id = node.$id;
41
+ if (typeof id !== "string")
42
+ return base;
43
+ // Index before applying an `$id`: a caller's `base` may already include it, and the index applies it once.
44
+ if (!this.indexed) {
45
+ this.ensureIndexed();
46
+ indexed = this.baseOf.get(node);
47
+ if (indexed !== undefined)
48
+ return indexed;
49
+ }
50
+ if (this.draft === "7" && (typeof node.$ref === "string" || id.startsWith("#")))
51
+ return base;
52
+ return documentOf(resolveUri(id, base));
53
+ }
54
+ /** Indexes the root schema and the registered documents' URIs (once). */
55
+ ensureIndexed() {
56
+ if (this.indexed)
57
+ return;
58
+ this.indexed = true;
59
+ for (const [uri, schema] of Object.entries(this.options.schemas ?? {})) {
60
+ this.registry.set(documentOf(resolveUri(uri, DEFAULT_BASE)), schema);
61
+ }
62
+ this.resources.set(DEFAULT_BASE, this.root);
63
+ this.index(this.root, DEFAULT_BASE);
64
+ }
65
+ /**
66
+ * The schema a reference names, with the base URI it compiles under. Throws if it names nothing, or, with
67
+ * `unresolvedRefs: "reject"`, resolves to `false`, the schema no value passes.
68
+ */
69
+ resolve(ref, base) {
70
+ this.ensureIndexed();
71
+ if (!this.rejectUnresolved)
72
+ return this.find(ref, base);
73
+ try {
74
+ return this.find(ref, base);
75
+ }
76
+ catch {
77
+ this.unresolved.add(resolveUri(ref, base));
78
+ return { node: false, base };
79
+ }
80
+ }
81
+ /** Whether `ref`, from `base`, named nothing and resolved to `false`. */
82
+ isUnresolved(ref, base) {
83
+ return this.unresolved.has(resolveUri(ref, base));
84
+ }
85
+ find(ref, base) {
86
+ const uri = resolveUri(ref, base);
87
+ const doc = documentOf(uri);
88
+ const fragment = uri.length > doc.length ? decodeURIComponent(uri.slice(doc.length + 1)) : "";
89
+ const root = this.resources.has(doc) ? this.resources.get(doc) : this.load(doc);
90
+ if (root === undefined)
91
+ throw new Error(`Unresolved $ref ${ref} (no schema registered for ${doc})`);
92
+ if (fragment === "")
93
+ return { node: root, base: doc };
94
+ if (fragment.startsWith("/"))
95
+ return this.pointer(root, doc, fragment);
96
+ const target = this.anchors.get(`${doc}#${fragment}`);
97
+ if (target === undefined)
98
+ throw new Error(`Unresolved $ref ${ref} (no anchor ${fragment} in ${doc})`);
99
+ return { node: target, base: this.baseOf.get(target) ?? doc };
100
+ }
101
+ /** Whether `node` is the root of the schema resource at `base`. */
102
+ isResource(node, base) {
103
+ this.ensureIndexed();
104
+ return this.resources.get(base) === node;
105
+ }
106
+ // Walks only keywords that hold subschemas: an `$id` inside `enum`, `const` or an unknown keyword names nothing.
107
+ index(node, base) {
108
+ if (!isSchemaObject(node) || this.baseOf.has(node))
109
+ return;
110
+ let b = base;
111
+ // In draft 7 a `$ref` makes every sibling keyword, `$id` included, inert.
112
+ const refOnly = this.draft === "7" && typeof node.$ref === "string";
113
+ if (!refOnly && typeof node.$id === "string") {
114
+ if (this.draft === "7" && node.$id.startsWith("#"))
115
+ this.anchors.set(documentOf(b) + node.$id, node);
116
+ else {
117
+ const uri = resolveUri(node.$id, b);
118
+ b = documentOf(uri);
119
+ this.resources.set(b, node);
120
+ if (uri.length > b.length + 1)
121
+ this.anchors.set(uri, node);
122
+ }
123
+ }
124
+ this.baseOf.set(node, b);
125
+ if (refOnly)
126
+ return;
127
+ if (this.draft !== "7") {
128
+ if (typeof node.$anchor === "string")
129
+ this.anchors.set(`${b}#${node.$anchor}`, node);
130
+ if (typeof node.$dynamicAnchor === "string") {
131
+ this.anchors.set(`${b}#${node.$dynamicAnchor}`, node);
132
+ this.dynamicAnchors.set(`${b}#${node.$dynamicAnchor}`, node);
133
+ }
134
+ }
135
+ for (const key of SCHEMA_KEYWORDS)
136
+ this.index(node[key], b);
137
+ for (const key of MAP_KEYWORDS) {
138
+ const map = node[key];
139
+ if (isSchemaObject(map))
140
+ for (const k of Object.keys(map))
141
+ this.index(map[k], b);
142
+ }
143
+ for (const key of ARRAY_KEYWORDS) {
144
+ const list = node[key];
145
+ if (Array.isArray(list))
146
+ for (const s of list)
147
+ this.index(s, b);
148
+ }
149
+ }
150
+ load(doc) {
151
+ const schema = this.registry.get(doc);
152
+ if (schema === undefined)
153
+ return undefined;
154
+ this.resources.set(doc, schema);
155
+ this.index(schema, doc);
156
+ return schema;
157
+ }
158
+ pointer(root, base, fragment) {
159
+ let node = root;
160
+ let b = base;
161
+ for (const raw of fragment.split("/").slice(1)) {
162
+ const key = raw.replace(/~1/g, "/").replace(/~0/g, "~");
163
+ node =
164
+ typeof node === "object" && node !== null && Object.hasOwn(node, key)
165
+ ? node[key]
166
+ : undefined;
167
+ if (node === undefined)
168
+ throw new Error(`Unresolved $ref #${fragment} in ${base}`);
169
+ if (typeof node === "object" && node !== null)
170
+ b = this.baseOf.get(node) ?? b;
171
+ }
172
+ return { node, base: b };
173
+ }
174
+ }
175
+ const SCHEMA_KEYWORDS = [
176
+ "additionalProperties",
177
+ "propertyNames",
178
+ "contains",
179
+ "not",
180
+ "if",
181
+ "then",
182
+ "else",
183
+ "unevaluatedProperties",
184
+ "unevaluatedItems",
185
+ "additionalItems",
186
+ "contentSchema",
187
+ "items",
188
+ ];
189
+ const MAP_KEYWORDS = ["properties", "patternProperties", "$defs", "definitions", "dependentSchemas", "dependencies"];
190
+ const ARRAY_KEYWORDS = ["allOf", "anyOf", "oneOf", "prefixItems", "items"];
191
+ /** Whether `value` is a schema object (not `true`/`false`, an array or a primitive). */
192
+ export function isSchemaObject(value) {
193
+ return typeof value === "object" && value !== null && !Array.isArray(value);
194
+ }
195
+ /** `ref` resolved against `base`. URNs and other bases a URL parser rejects take fragment-only references. */
196
+ export function resolveUri(ref, base) {
197
+ try {
198
+ return new URL(ref, base).href;
199
+ }
200
+ catch {
201
+ return ref.startsWith("#") ? documentOf(base) + ref : ref;
202
+ }
203
+ }
204
+ /** `uri` without its fragment. */
205
+ export function documentOf(uri) {
206
+ const hash = uri.indexOf("#");
207
+ return hash < 0 ? uri : uri.slice(0, hash);
208
+ }
209
+ //# sourceMappingURL=resolver.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolver.js","sourceRoot":"","sources":["../src/resolver.ts"],"names":[],"mappings":"AAAA,gHAAgH;AAChH,2GAA2G;AAU3G,2FAA2F;AAC3F,MAAM,CAAC,MAAM,YAAY,GAAG,uCAAuC,CAAC;AAEpE;;;GAGG;AACH,MAAM,OAAO,QAAQ;IACX,KAAK,CAAkB;IAChC,yGAAyG;IAChG,QAAQ,GAAG,YAAY,CAAC;IACjC,0DAA0D;IACjD,MAAM,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC5C,8EAA8E;IACrE,cAAc,GAAG,IAAI,GAAG,EAAwB,CAAC;IACzC,IAAI,CAAa;IACjB,OAAO,CAAiB;IACxB,QAAQ,GAAG,IAAI,GAAG,EAAsB,CAAC;IACzC,gBAAgB,CAAU;IAC3C,mGAAmG;IAClF,UAAU,GAAG,IAAI,GAAG,EAAU,CAAC;IAC/B,SAAS,GAAG,IAAI,GAAG,EAAmB,CAAC;IACvC,OAAO,GAAG,IAAI,GAAG,EAAwB,CAAC;IACnD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jB,eAAe;IACf,OAAO;CACP,CAAC;AAEF,MAAM,YAAY,GAAG,CAAC,YAAY,EAAE,mBAAmB,EAAE,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,cAAc,CAAC,CAAC;AAErH,MAAM,cAAc,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,OAAO,CAAC,CAAC;AAE3E,wFAAwF;AACxF,MAAM,UAAU,cAAc,CAAC,KAAc;IAC5C,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC7E,CAAC;AAED,8GAA8G;AAC9G,MAAM,UAAU,UAAU,CAAC,GAAW,EAAE,IAAY;IACnD,IAAI,CAAC;QACJ,OAAO,IAAI,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC;IAChC,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC;IAC3D,CAAC;AACF,CAAC;AAED,kCAAkC;AAClC,MAAM,UAAU,UAAU,CAAC,GAAW;IACrC,MAAM,IAAI,GAAG,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAC9B,OAAO,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;AAC5C,CAAC","sourcesContent":["// Reference resolution of one compile: schema resources, anchors and JSON Pointers over the root schema and the\n// registered documents. A Resolver lives only while a schema compiles (or while its errors are explained).\n\nimport type { CompileOptions, JsonSchema, SchemaObject } from \"./types.ts\";\n\n/** A schema and the base URI its references resolve against. */\nexport interface Resolved {\n\treadonly node: unknown;\n\treadonly base: string;\n}\n\n/** The base URI of a root schema without `$id`; relative references resolve against it. */\nexport const DEFAULT_BASE = \"https://json-schema.invalid/root.json\";\n\n/**\n * Resolves references in the root schema and the registered documents. The schema is indexed on the first\n * reference or the first `$id` a walker meets, so a schema with neither is never walked for identifiers.\n */\nexport class Resolver {\n\treadonly draft: \"2020-12\" | \"7\";\n\t/** The base URI the root schema is walked from; walkers apply the root's own `$id` through `baseFor`. */\n\treadonly rootBase = DEFAULT_BASE;\n\t/** Schema object to the base URI it was indexed under. */\n\treadonly baseOf = new Map<object, string>();\n\t/** URI (with a `#name` fragment) of every `$dynamicAnchor`, to its schema. */\n\treadonly dynamicAnchors = new Map<string, SchemaObject>();\n\tprivate readonly root: JsonSchema;\n\tprivate readonly options: CompileOptions;\n\tprivate readonly registry = new Map<string, JsonSchema>();\n\tprivate readonly rejectUnresolved: boolean;\n\t/** The references that named nothing and resolved to `false` (with `unresolvedRefs: \"reject\"`). */\n\tprivate readonly unresolved = new Set<string>();\n\tprivate readonly resources = new Map<string, unknown>();\n\tprivate readonly anchors = new Map<string, SchemaObject>();\n\tprivate indexed = false;\n\n\tconstructor(root: JsonSchema, options: CompileOptions) {\n\t\tthis.draft = options.draft === \"7\" ? \"7\" : \"2020-12\";\n\t\tthis.root = root;\n\t\tthis.options = options;\n\t\tthis.rejectUnresolved = options.unresolvedRefs === \"reject\";\n\t}\n\n\t/**\n\t * The base URI of `node`, whose parent has `base`: its `$id` changes it, unless draft 7 makes the `$id` inert. Once\n\t * the schema is indexed, the indexed base (a resolved reference target already has its own).\n\t */\n\tbaseFor(node: SchemaObject, base: string): string {\n\t\tlet indexed = this.baseOf.get(node);\n\t\tif (indexed !== undefined) return indexed;\n\t\tconst id = node.$id;\n\t\tif (typeof id !== \"string\") return base;\n\t\t// Index before applying an `$id`: a caller's `base` may already include it, and the index applies it once.\n\t\tif (!this.indexed) {\n\t\t\tthis.ensureIndexed();\n\t\t\tindexed = this.baseOf.get(node);\n\t\t\tif (indexed !== undefined) return indexed;\n\t\t}\n\t\tif (this.draft === \"7\" && (typeof node.$ref === \"string\" || id.startsWith(\"#\"))) return base;\n\t\treturn documentOf(resolveUri(id, base));\n\t}\n\n\t/** Indexes the root schema and the registered documents' URIs (once). */\n\tensureIndexed(): void {\n\t\tif (this.indexed) return;\n\t\tthis.indexed = true;\n\t\tfor (const [uri, schema] of Object.entries(this.options.schemas ?? {})) {\n\t\t\tthis.registry.set(documentOf(resolveUri(uri, DEFAULT_BASE)), schema);\n\t\t}\n\t\tthis.resources.set(DEFAULT_BASE, this.root);\n\t\tthis.index(this.root, DEFAULT_BASE);\n\t}\n\n\t/**\n\t * The schema a reference names, with the base URI it compiles under. Throws if it names nothing, or, with\n\t * `unresolvedRefs: \"reject\"`, resolves to `false`, the schema no value passes.\n\t */\n\tresolve(ref: string, base: string): Resolved {\n\t\tthis.ensureIndexed();\n\t\tif (!this.rejectUnresolved) return this.find(ref, base);\n\t\ttry {\n\t\t\treturn this.find(ref, base);\n\t\t} catch {\n\t\t\tthis.unresolved.add(resolveUri(ref, base));\n\t\t\treturn { node: false, base };\n\t\t}\n\t}\n\n\t/** Whether `ref`, from `base`, named nothing and resolved to `false`. */\n\tisUnresolved(ref: string, base: string): boolean {\n\t\treturn this.unresolved.has(resolveUri(ref, base));\n\t}\n\n\tprivate find(ref: string, base: string): Resolved {\n\t\tconst uri = resolveUri(ref, base);\n\t\tconst doc = documentOf(uri);\n\t\tconst fragment = uri.length > doc.length ? decodeURIComponent(uri.slice(doc.length + 1)) : \"\";\n\t\tconst root = this.resources.has(doc) ? this.resources.get(doc) : this.load(doc);\n\t\tif (root === undefined) throw new Error(`Unresolved $ref ${ref} (no schema registered for ${doc})`);\n\t\tif (fragment === \"\") return { node: root, base: doc };\n\t\tif (fragment.startsWith(\"/\")) return this.pointer(root, doc, fragment);\n\t\tconst target = this.anchors.get(`${doc}#${fragment}`);\n\t\tif (target === undefined) throw new Error(`Unresolved $ref ${ref} (no anchor ${fragment} in ${doc})`);\n\t\treturn { node: target, base: this.baseOf.get(target) ?? doc };\n\t}\n\n\t/** Whether `node` is the root of the schema resource at `base`. */\n\tisResource(node: object, base: string): boolean {\n\t\tthis.ensureIndexed();\n\t\treturn this.resources.get(base) === node;\n\t}\n\n\t// Walks only keywords that hold subschemas: an `$id` inside `enum`, `const` or an unknown keyword names nothing.\n\tprivate index(node: unknown, base: string): void {\n\t\tif (!isSchemaObject(node) || this.baseOf.has(node)) return;\n\t\tlet b = base;\n\t\t// In draft 7 a `$ref` makes every sibling keyword, `$id` included, inert.\n\t\tconst refOnly = this.draft === \"7\" && typeof node.$ref === \"string\";\n\t\tif (!refOnly && typeof node.$id === \"string\") {\n\t\t\tif (this.draft === \"7\" && node.$id.startsWith(\"#\")) this.anchors.set(documentOf(b) + node.$id, node);\n\t\t\telse {\n\t\t\t\tconst uri = resolveUri(node.$id, b);\n\t\t\t\tb = documentOf(uri);\n\t\t\t\tthis.resources.set(b, node);\n\t\t\t\tif (uri.length > b.length + 1) this.anchors.set(uri, node);\n\t\t\t}\n\t\t}\n\t\tthis.baseOf.set(node, b);\n\t\tif (refOnly) return;\n\t\tif (this.draft !== \"7\") {\n\t\t\tif (typeof node.$anchor === \"string\") this.anchors.set(`${b}#${node.$anchor}`, node);\n\t\t\tif (typeof node.$dynamicAnchor === \"string\") {\n\t\t\t\tthis.anchors.set(`${b}#${node.$dynamicAnchor}`, node);\n\t\t\t\tthis.dynamicAnchors.set(`${b}#${node.$dynamicAnchor}`, node);\n\t\t\t}\n\t\t}\n\t\tfor (const key of SCHEMA_KEYWORDS) this.index(node[key], b);\n\t\tfor (const key of MAP_KEYWORDS) {\n\t\t\tconst map = node[key];\n\t\t\tif (isSchemaObject(map)) for (const k of Object.keys(map)) this.index(map[k], b);\n\t\t}\n\t\tfor (const key of ARRAY_KEYWORDS) {\n\t\t\tconst list = node[key];\n\t\t\tif (Array.isArray(list)) for (const s of list) this.index(s, b);\n\t\t}\n\t}\n\n\tprivate load(doc: string): JsonSchema | undefined {\n\t\tconst schema = this.registry.get(doc);\n\t\tif (schema === undefined) return undefined;\n\t\tthis.resources.set(doc, schema);\n\t\tthis.index(schema, doc);\n\t\treturn schema;\n\t}\n\n\tprivate pointer(root: unknown, base: string, fragment: string): Resolved {\n\t\tlet node = root;\n\t\tlet b = base;\n\t\tfor (const raw of fragment.split(\"/\").slice(1)) {\n\t\t\tconst key = raw.replace(/~1/g, \"/\").replace(/~0/g, \"~\");\n\t\t\tnode =\n\t\t\t\ttypeof node === \"object\" && node !== null && Object.hasOwn(node, key)\n\t\t\t\t\t? (node as SchemaObject)[key]\n\t\t\t\t\t: undefined;\n\t\t\tif (node === undefined) throw new Error(`Unresolved $ref #${fragment} in ${base}`);\n\t\t\tif (typeof node === \"object\" && node !== null) b = this.baseOf.get(node) ?? b;\n\t\t}\n\t\treturn { node, base: b };\n\t}\n}\n\nconst SCHEMA_KEYWORDS = [\n\t\"additionalProperties\",\n\t\"propertyNames\",\n\t\"contains\",\n\t\"not\",\n\t\"if\",\n\t\"then\",\n\t\"else\",\n\t\"unevaluatedProperties\",\n\t\"unevaluatedItems\",\n\t\"additionalItems\",\n\t\"contentSchema\",\n\t\"items\",\n];\n\nconst MAP_KEYWORDS = [\"properties\", \"patternProperties\", \"$defs\", \"definitions\", \"dependentSchemas\", \"dependencies\"];\n\nconst ARRAY_KEYWORDS = [\"allOf\", \"anyOf\", \"oneOf\", \"prefixItems\", \"items\"];\n\n/** Whether `value` is a schema object (not `true`/`false`, an array or a primitive). */\nexport function isSchemaObject(value: unknown): value is SchemaObject {\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/** `ref` resolved against `base`. URNs and other bases a URL parser rejects take fragment-only references. */\nexport function resolveUri(ref: string, base: string): string {\n\ttry {\n\t\treturn new URL(ref, base).href;\n\t} catch {\n\t\treturn ref.startsWith(\"#\") ? documentOf(base) + ref : ref;\n\t}\n}\n\n/** `uri` without its fragment. */\nexport function documentOf(uri: string): string {\n\tconst hash = uri.indexOf(\"#\");\n\treturn hash < 0 ? uri : uri.slice(0, hash);\n}\n"]}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * The regular expression of a `pattern` source, shared by source. Patterns are ECMAScript regular expressions with
3
+ * the `u` flag; a source that is invalid with it (some older schemas escape characters `u` rejects) compiles
4
+ * without it.
5
+ */
6
+ export declare function sharedRegex(source: string): RegExp;
7
+ /** The number of cached regular expressions (for tests). */
8
+ export declare function sharedRegexCount(): number;
9
+ //# sourceMappingURL=shared.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAIA;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAYlD;AAQD,4DAA4D;AAC5D,wBAAgB,gBAAgB,IAAI,MAAM,CAEzC"}
package/dist/shared.js ADDED
@@ -0,0 +1,33 @@
1
+ // Regular expressions shared across all compiled schemas while one still uses them: tool schemas repeat a few
2
+ // patterns many times, and a RegExp is large once it has run. The cache holds its values weakly and drops an entry
3
+ // once its RegExp is collected, so it keeps nothing alive.
4
+ /**
5
+ * The regular expression of a `pattern` source, shared by source. Patterns are ECMAScript regular expressions with
6
+ * the `u` flag; a source that is invalid with it (some older schemas escape characters `u` rejects) compiles
7
+ * without it.
8
+ */
9
+ export function sharedRegex(source) {
10
+ const hit = regexes.get(source)?.deref();
11
+ if (hit !== undefined)
12
+ return hit;
13
+ let re;
14
+ try {
15
+ re = new RegExp(source, "u");
16
+ }
17
+ catch {
18
+ re = new RegExp(source);
19
+ }
20
+ regexes.set(source, new WeakRef(re));
21
+ regexCleanup.register(re, source);
22
+ return re;
23
+ }
24
+ const regexes = new Map();
25
+ const regexCleanup = new FinalizationRegistry((source) => {
26
+ if (regexes.get(source)?.deref() === undefined)
27
+ regexes.delete(source);
28
+ });
29
+ /** The number of cached regular expressions (for tests). */
30
+ export function sharedRegexCount() {
31
+ return regexes.size;
32
+ }
33
+ //# sourceMappingURL=shared.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared.js","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,8GAA8G;AAC9G,mHAAmH;AACnH,2DAA2D;AAE3D;;;;GAIG;AACH,MAAM,UAAU,WAAW,CAAC,MAAc;IACzC,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC;IACzC,IAAI,GAAG,KAAK,SAAS;QAAE,OAAO,GAAG,CAAC;IAClC,IAAI,EAAU,CAAC;IACf,IAAI,CAAC;QACJ,EAAE,GAAG,IAAI,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC9B,CAAC;IAAC,MAAM,CAAC;QACR,EAAE,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,CAAC;IACzB,CAAC;IACD,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC;IACrC,YAAY,CAAC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;IAClC,OAAO,EAAE,CAAC;AACX,CAAC;AAED,MAAM,OAAO,GAAG,IAAI,GAAG,EAA2B,CAAC;AAEnD,MAAM,YAAY,GAAG,IAAI,oBAAoB,CAAS,CAAC,MAAM,EAAE,EAAE;IAChE,IAAI,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,KAAK,SAAS;QAAE,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;AACxE,CAAC,CAAC,CAAC;AAEH,4DAA4D;AAC5D,MAAM,UAAU,gBAAgB;IAC/B,OAAO,OAAO,CAAC,IAAI,CAAC;AACrB,CAAC","sourcesContent":["// Regular expressions shared across all compiled schemas while one still uses them: tool schemas repeat a few\n// patterns many times, and a RegExp is large once it has run. The cache holds its values weakly and drops an entry\n// once its RegExp is collected, so it keeps nothing alive.\n\n/**\n * The regular expression of a `pattern` source, shared by source. Patterns are ECMAScript regular expressions with\n * the `u` flag; a source that is invalid with it (some older schemas escape characters `u` rejects) compiles\n * without it.\n */\nexport function sharedRegex(source: string): RegExp {\n\tconst hit = regexes.get(source)?.deref();\n\tif (hit !== undefined) return hit;\n\tlet re: RegExp;\n\ttry {\n\t\tre = new RegExp(source, \"u\");\n\t} catch {\n\t\tre = new RegExp(source);\n\t}\n\tregexes.set(source, new WeakRef(re));\n\tregexCleanup.register(re, source);\n\treturn re;\n}\n\nconst regexes = new Map<string, WeakRef<RegExp>>();\n\nconst regexCleanup = new FinalizationRegistry<string>((source) => {\n\tif (regexes.get(source)?.deref() === undefined) regexes.delete(source);\n});\n\n/** The number of cached regular expressions (for tests). */\nexport function sharedRegexCount(): number {\n\treturn regexes.size;\n}\n"]}
@@ -0,0 +1,56 @@
1
+ import type { Static } from "./builders.ts";
2
+ /** The value type of a TypeBox schema type by its `'~kind'`, or its `Type.Unsafe` type; `unknown` for other kinds. */
3
+ export type TypeBoxStatic<S> = S extends {
4
+ readonly "~kind": infer K;
5
+ } ? KindStatic<S, K> : S extends {
6
+ readonly "~unsafe": infer T;
7
+ } ? T : unknown;
8
+ interface Primitives {
9
+ String: string;
10
+ Number: number;
11
+ Integer: number;
12
+ Boolean: boolean;
13
+ Null: null;
14
+ BigInt: bigint;
15
+ Undefined: undefined;
16
+ Unknown: unknown;
17
+ Any: any;
18
+ }
19
+ type KindStatic<S, K> = K extends keyof Primitives ? Primitives[K] : K extends "Literal" ? S extends {
20
+ readonly const: infer V;
21
+ } ? V : unknown : K extends "Array" ? S extends {
22
+ readonly items: infer I;
23
+ } ? Static<I>[] : unknown : K extends "Object" ? S extends {
24
+ readonly properties: infer P;
25
+ } ? ObjectStatic<P> : unknown : K extends "Union" ? S extends {
26
+ readonly anyOf: readonly (infer B)[];
27
+ } ? Static<B> : unknown : K extends "Record" ? S extends {
28
+ readonly patternProperties: infer P;
29
+ } ? Record<RecordKey<keyof P>, Static<P[keyof P]>> : unknown : K extends "Enum" ? S extends {
30
+ readonly enum: readonly (infer V)[];
31
+ } ? V : unknown : K extends "Tuple" ? S extends {
32
+ readonly items: infer I extends readonly unknown[];
33
+ } ? {
34
+ -readonly [N in keyof I]: Static<I[N]>;
35
+ } : unknown : unknown;
36
+ type Optional = {
37
+ readonly "~optional": true;
38
+ };
39
+ type Readonly = {
40
+ readonly "~readonly": true;
41
+ };
42
+ type ObjectStatic<P> = keyof P extends never ? object : Simplify<{
43
+ -readonly [K in keyof P as P[K] extends Optional | Readonly ? never : K]: Static<P[K]>;
44
+ } & {
45
+ -readonly [K in keyof P as P[K] extends Optional ? (P[K] extends Readonly ? never : K) : never]?: Static<P[K]>;
46
+ } & {
47
+ readonly [K in keyof P as P[K] extends Readonly ? (P[K] extends Optional ? never : K) : never]: Static<P[K]>;
48
+ } & {
49
+ readonly [K in keyof P as P[K] extends Optional ? (P[K] extends Readonly ? K : never) : never]?: Static<P[K]>;
50
+ }>;
51
+ type Simplify<T> = {
52
+ [K in keyof T]: T[K];
53
+ } & {};
54
+ type RecordKey<Pattern> = Pattern extends "^-?(?:0|[1-9][0-9]*)$" | "^-?(?:0|[1-9][0-9]*)(?:\\.[0-9]+)?$" ? number : string;
55
+ export {};
56
+ //# sourceMappingURL=typebox-static.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typebox-static.d.ts","sourceRoot":"","sources":["../src/typebox-static.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAE5C,sHAAsH;AACtH,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,CAAC,SAAS;IAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,CAAA;CAAE,GACnE,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,GAChB,CAAC,SAAS;IAAE,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC,CAAA;CAAE,GACxC,CAAC,GACD,OAAO,CAAC;AAEZ,UAAU,UAAU;IACnB,MAAM,EAAE,MAAM,CAA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AAC,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,QAAQ,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,QAAQ,GAAG,CAAC,GAAG,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,EAAE,MAAM,CACtG,CAAC,CAAC,CAAC,CAAC,CACJ;CACD,CACD,CAAC;AAEJ,KAAK,QAAQ,CAAC,CAAC,IAAI;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,GAAG,EAAE,CAAC;AAGjD,KAAK,SAAS,CAAC,OAAO,IAAI,OAAO,SAAS,uBAAuB,GAAG,qCAAqC,GACtG,MAAM,GACN,MAAM,CAAC"}
@@ -0,0 +1,7 @@
1
+ // The static types of TypeBox-built schemas, read from the type shapes TypeBox 1.x declares (`'~kind'`, `'~optional'`,
2
+ // `'~readonly'`, `'~unsafe'`) without importing TypeBox, so `Static` also types tool arguments of TypeBox schemas.
3
+ // Only the common kinds are mapped; every other kind (Intersect, Cyclic, Ref, ...) is `unknown`. Tuples are plain
4
+ // tuples: an element marked `Type.Optional`, `Type.Rest` or `Type.Readonly` is typed by its schema alone (`[string]`
5
+ // where TypeBox gives `[string?]`), unlike TypeBox; tuples of tool and API schemas are fixed pairs, typed exactly.
6
+ export {};
7
+ //# sourceMappingURL=typebox-static.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typebox-static.js","sourceRoot":"","sources":["../src/typebox-static.ts"],"names":[],"mappings":"AAAA,uHAAuH;AACvH,mHAAmH;AACnH,kHAAkH;AAClH,qHAAqH;AACrH,mHAAmH","sourcesContent":["// The static types of TypeBox-built schemas, read from the type shapes TypeBox 1.x declares (`'~kind'`, `'~optional'`,\n// `'~readonly'`, `'~unsafe'`) without importing TypeBox, so `Static` also types tool arguments of TypeBox schemas.\n// Only the common kinds are mapped; every other kind (Intersect, Cyclic, Ref, ...) is `unknown`. Tuples are plain\n// tuples: an element marked `Type.Optional`, `Type.Rest` or `Type.Readonly` is typed by its schema alone (`[string]`\n// where TypeBox gives `[string?]`), unlike TypeBox; tuples of tool and API schemas are fixed pairs, typed exactly.\n\nimport type { Static } from \"./builders.ts\";\n\n/** The value type of a TypeBox schema type by its `'~kind'`, or its `Type.Unsafe` type; `unknown` for other kinds. */\nexport type TypeBoxStatic<S> = S extends { readonly \"~kind\": infer K }\n\t? KindStatic<S, K>\n\t: S extends { readonly \"~unsafe\": infer T }\n\t\t? T\n\t\t: unknown;\n\ninterface Primitives {\n\tString: string;\n\tNumber: number;\n\tInteger: number;\n\tBoolean: boolean;\n\tNull: null;\n\tBigInt: bigint;\n\tUndefined: undefined;\n\tUnknown: unknown;\n\tAny: any;\n}\n\ntype KindStatic<S, K> = K extends keyof Primitives\n\t? Primitives[K]\n\t: K extends \"Literal\"\n\t\t? S extends { readonly const: infer V }\n\t\t\t? V\n\t\t\t: unknown\n\t\t: K extends \"Array\"\n\t\t\t? S extends { readonly items: infer I }\n\t\t\t\t? Static<I>[]\n\t\t\t\t: unknown\n\t\t\t: K extends \"Object\"\n\t\t\t\t? S extends { readonly properties: infer P }\n\t\t\t\t\t? ObjectStatic<P>\n\t\t\t\t\t: unknown\n\t\t\t\t: K extends \"Union\"\n\t\t\t\t\t? S extends { readonly anyOf: readonly (infer B)[] }\n\t\t\t\t\t\t? Static<B>\n\t\t\t\t\t\t: unknown\n\t\t\t\t\t: K extends \"Record\"\n\t\t\t\t\t\t? S extends { readonly patternProperties: infer P }\n\t\t\t\t\t\t\t? Record<RecordKey<keyof P>, Static<P[keyof P]>>\n\t\t\t\t\t\t\t: unknown\n\t\t\t\t\t\t: K extends \"Enum\"\n\t\t\t\t\t\t\t? S extends { readonly enum: readonly (infer V)[] }\n\t\t\t\t\t\t\t\t? V\n\t\t\t\t\t\t\t\t: unknown\n\t\t\t\t\t\t\t: K extends \"Tuple\"\n\t\t\t\t\t\t\t\t? S extends { readonly items: infer I extends readonly unknown[] }\n\t\t\t\t\t\t\t\t\t? { -readonly [N in keyof I]: Static<I[N]> }\n\t\t\t\t\t\t\t\t\t: unknown\n\t\t\t\t\t\t\t\t: unknown;\n\ntype Optional = { readonly \"~optional\": true };\n\ntype Readonly = { readonly \"~readonly\": true };\n\n// As TypeBox: no properties is `object`; `'~optional'` properties may be omitted, `'~readonly'` ones are readonly.\ntype ObjectStatic<P> = keyof P extends never\n\t? object\n\t: Simplify<\n\t\t\t{ -readonly [K in keyof P as P[K] extends Optional | Readonly ? never : K]: Static<P[K]> } & {\n\t\t\t\t-readonly [K in keyof P as P[K] extends Optional ? (P[K] extends Readonly ? never : K) : never]?: Static<\n\t\t\t\t\tP[K]\n\t\t\t\t>;\n\t\t\t} & {\n\t\t\t\treadonly [K in keyof P as P[K] extends Readonly ? (P[K] extends Optional ? never : K) : never]: Static<\n\t\t\t\t\tP[K]\n\t\t\t\t>;\n\t\t\t} & {\n\t\t\t\treadonly [K in keyof P as P[K] extends Optional ? (P[K] extends Readonly ? K : never) : never]?: Static<\n\t\t\t\t\tP[K]\n\t\t\t\t>;\n\t\t\t}\n\t\t>;\n\ntype Simplify<T> = { [K in keyof T]: T[K] } & {};\n\n// TypeBox's key patterns for integer and number keys; every other pattern types its keys as strings.\ntype RecordKey<Pattern> = Pattern extends \"^-?(?:0|[1-9][0-9]*)$\" | \"^-?(?:0|[1-9][0-9]*)(?:\\\\.[0-9]+)?$\"\n\t? number\n\t: string;\n"]}
@@ -0,0 +1,48 @@
1
+ /**
2
+ * A JSON Schema: `true`, `false` or a schema object. Schemas come from tool definitions, MCP servers and other
3
+ * untrusted sources, so compile() reads every keyword defensively instead of trusting a static shape.
4
+ */
5
+ export type JsonSchema = boolean | object;
6
+ /** A format check: whether `value` (a string) has the format. */
7
+ export type FormatCheck = (value: string) => boolean;
8
+ /** Options of compile(). */
9
+ export interface CompileOptions {
10
+ /** The draft whose semantics apply: "2020-12" (default) or "7". */
11
+ readonly draft?: "2020-12" | "7";
12
+ /**
13
+ * Format name to check. Without it `format` is an annotation, as draft 2020-12 specifies. The standard formats are
14
+ * in `@bastani/atomic-schema/formats`.
15
+ */
16
+ readonly formats?: Readonly<Record<string, FormatCheck>>;
17
+ /** Schema documents by URI, for `$ref`s to other documents. */
18
+ readonly schemas?: Readonly<Record<string, JsonSchema>>;
19
+ /** Add `coerce()` and `parse()` to the validator, for loosely typed input (see the README's Coercion section). */
20
+ readonly coerce?: boolean;
21
+ /**
22
+ * What a `$ref` that names nothing (a missing definition, a document not in `schemas`) does: `"throw"` (default)
23
+ * makes compile() throw; `"reject"` compiles it as `false`, so only values at that place fail, with the error
24
+ * "unresolved reference".
25
+ */
26
+ readonly unresolvedRefs?: "throw" | "reject";
27
+ }
28
+ /** The result of `parse()`: the coerced value when it is valid, else the errors of the coerced value. */
29
+ export type ParseResult<T> = {
30
+ readonly ok: true;
31
+ readonly value: T;
32
+ } | {
33
+ readonly ok: false;
34
+ readonly errors: ValidationError[];
35
+ };
36
+ /** One failure of a value: where it is (a JSON Pointer, "" for the value itself), the failing keyword and a message. */
37
+ export interface ValidationError {
38
+ readonly path: string;
39
+ readonly keyword: string;
40
+ readonly message: string;
41
+ }
42
+ /** A schema object as compile() reads it: any keyword, of any value, checked where it is used. */
43
+ export type SchemaObject = {
44
+ readonly [keyword: string]: unknown;
45
+ };
46
+ /** A compiled boolean check. */
47
+ export type Check = (value: unknown) => boolean;
48
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,MAAM,UAAU,GAAG,OAAO,GAAG,MAAM,CAAC;AAE1C,iEAAiE;AACjE,MAAM,MAAM,WAAW,GAAG,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC;AAErD,4BAA4B;AAC5B,MAAM,WAAW,cAAc;IAC9B,mEAAmE;IACnE,QAAQ,CAAC,KAAK,CAAC,EAAE,SAAS,GAAG,GAAG,CAAC;IACjC;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC;IACzD,+DAA+D;IAC/D,QAAQ,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC,CAAC;IACxD,kHAAkH;IAClH,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;IAC1B;;;;OAIG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,OAAO,GAAG,QAAQ,CAAC;CAC7C;AAED,yGAAyG;AACzG,MAAM,MAAM,WAAW,CAAC,CAAC,IACtB;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAA;CAAE,GACxC;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,eAAe,EAAE,CAAA;CAAE,CAAC;AAE9D,wHAAwH;AACxH,MAAM,WAAW,eAAe;IAC/B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CACzB;AAED,kGAAkG;AAClG,MAAM,MAAM,YAAY,GAAG;IAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,CAAC;AAEnE,gCAAgC;AAChC,MAAM,MAAM,KAAK,GAAG,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC"}
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ // Public types: the schema accepted by compile(), its options, and the errors a failed value produces.
2
+ export {};
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,uGAAuG","sourcesContent":["// Public types: the schema accepted by compile(), its options, and the errors a failed value produces.\n\n/**\n * A JSON Schema: `true`, `false` or a schema object. Schemas come from tool definitions, MCP servers and other\n * untrusted sources, so compile() reads every keyword defensively instead of trusting a static shape.\n */\nexport type JsonSchema = boolean | object;\n\n/** A format check: whether `value` (a string) has the format. */\nexport type FormatCheck = (value: string) => boolean;\n\n/** Options of compile(). */\nexport interface CompileOptions {\n\t/** The draft whose semantics apply: \"2020-12\" (default) or \"7\". */\n\treadonly draft?: \"2020-12\" | \"7\";\n\t/**\n\t * Format name to check. Without it `format` is an annotation, as draft 2020-12 specifies. The standard formats are\n\t * in `@bastani/atomic-schema/formats`.\n\t */\n\treadonly formats?: Readonly<Record<string, FormatCheck>>;\n\t/** Schema documents by URI, for `$ref`s to other documents. */\n\treadonly schemas?: Readonly<Record<string, JsonSchema>>;\n\t/** Add `coerce()` and `parse()` to the validator, for loosely typed input (see the README's Coercion section). */\n\treadonly coerce?: boolean;\n\t/**\n\t * What a `$ref` that names nothing (a missing definition, a document not in `schemas`) does: `\"throw\"` (default)\n\t * makes compile() throw; `\"reject\"` compiles it as `false`, so only values at that place fail, with the error\n\t * \"unresolved reference\".\n\t */\n\treadonly unresolvedRefs?: \"throw\" | \"reject\";\n}\n\n/** The result of `parse()`: the coerced value when it is valid, else the errors of the coerced value. */\nexport type ParseResult<T> =\n\t| { readonly ok: true; readonly value: T }\n\t| { readonly ok: false; readonly errors: ValidationError[] };\n\n/** One failure of a value: where it is (a JSON Pointer, \"\" for the value itself), the failing keyword and a message. */\nexport interface ValidationError {\n\treadonly path: string;\n\treadonly keyword: string;\n\treadonly message: string;\n}\n\n/** A schema object as compile() reads it: any keyword, of any value, checked where it is used. */\nexport type SchemaObject = { readonly [keyword: string]: unknown };\n\n/** A compiled boolean check. */\nexport type Check = (value: unknown) => boolean;\n"]}