@maroonedog/luq 2.0.0 → 2.2.0

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 (165) hide show
  1. package/README.md +364 -57
  2. package/dist/builder/builder-surface.types.d.ts +1 -0
  3. package/dist/builder/create-builder.js +9 -0
  4. package/dist/builder/create-builder.mjs +9 -0
  5. package/dist/builder/field-builder.types.d.ts +13 -0
  6. package/dist/chain/bundle-paths.types.d.ts +26 -0
  7. package/dist/chain/bundle-paths.types.js +2 -0
  8. package/dist/chain/bundle-paths.types.mjs +1 -0
  9. package/dist/chain/chain-method.types.d.ts +8 -3
  10. package/dist/chain/create-chain-node.js +22 -1
  11. package/dist/chain/create-chain-node.mjs +22 -1
  12. package/dist/chain/resolve-args.types.d.ts +4 -2
  13. package/dist/compile/compile-array-node.d.ts +0 -7
  14. package/dist/compile/compile-array-node.js +4 -0
  15. package/dist/compile/compile-array-node.mjs +4 -0
  16. package/dist/compile/compile-field.js +10 -1
  17. package/dist/compile/compile-field.mjs +10 -1
  18. package/dist/compile/resolve-conditional-presence.js +1 -0
  19. package/dist/compile/resolve-conditional-presence.mjs +1 -0
  20. package/dist/compile/resolve-presence.js +4 -0
  21. package/dist/compile/resolve-presence.mjs +4 -0
  22. package/dist/compile/split-rules-by-kind.js +31 -7
  23. package/dist/compile/split-rules-by-kind.mjs +31 -7
  24. package/dist/compile/validation-plan.types.d.ts +33 -0
  25. package/dist/json-schema/build-from-schema.js +2 -3
  26. package/dist/json-schema/build-from-schema.mjs +2 -3
  27. package/dist/json-schema/collect-definitions.d.ts +30 -0
  28. package/dist/json-schema/collect-definitions.js +47 -3
  29. package/dist/json-schema/collect-definitions.mjs +46 -4
  30. package/dist/json-schema/collect-sub-schema-rules.d.ts +12 -1
  31. package/dist/json-schema/collect-sub-schema-rules.js +37 -7
  32. package/dist/json-schema/collect-sub-schema-rules.mjs +38 -8
  33. package/dist/json-schema/create-structural-context.d.ts +13 -3
  34. package/dist/json-schema/create-structural-context.js +72 -6
  35. package/dist/json-schema/create-structural-context.mjs +72 -7
  36. package/dist/json-schema/declare-additional-properties.d.ts +19 -0
  37. package/dist/json-schema/declare-additional-properties.js +42 -0
  38. package/dist/json-schema/declare-additional-properties.mjs +37 -0
  39. package/dist/json-schema/declare-object-keywords.d.ts +0 -2
  40. package/dist/json-schema/declare-object-keywords.js +6 -13
  41. package/dist/json-schema/declare-object-keywords.mjs +7 -13
  42. package/dist/json-schema/declare-presence.d.ts +9 -3
  43. package/dist/json-schema/declare-presence.js +12 -6
  44. package/dist/json-schema/declare-presence.mjs +12 -6
  45. package/dist/json-schema/extensions/json-schema/index.d.ts +1 -0
  46. package/dist/json-schema/extensions/json-schema/json-schema.d.ts +17 -2
  47. package/dist/json-schema/extensions/json-schema/json-schema.js +15 -7
  48. package/dist/json-schema/extensions/json-schema/json-schema.mjs +16 -8
  49. package/dist/json-schema/extensions/json-schema-full-feature/index.d.ts +1 -0
  50. package/dist/json-schema/extensions/json-schema-full-feature/json-schema-full-feature.d.ts +2 -1
  51. package/dist/json-schema/extensions/json-schema-full-feature/json-schema-full-feature.js +3 -1
  52. package/dist/json-schema/extensions/json-schema-full-feature/json-schema-full-feature.mjs +3 -1
  53. package/dist/json-schema/flatten-schema.js +1 -1
  54. package/dist/json-schema/flatten-schema.mjs +1 -1
  55. package/dist/json-schema/follow-json-pointer.d.ts +23 -0
  56. package/dist/json-schema/follow-json-pointer.js +87 -0
  57. package/dist/json-schema/follow-json-pointer.mjs +82 -0
  58. package/dist/json-schema/index.d.ts +7 -2
  59. package/dist/json-schema/index.js +10 -2
  60. package/dist/json-schema/index.mjs +5 -2
  61. package/dist/json-schema/keyword-map-object.d.ts +1 -1
  62. package/dist/json-schema/ref-resolution-error.d.ts +4 -0
  63. package/dist/json-schema/ref-resolution-error.js +20 -0
  64. package/dist/json-schema/ref-resolution-error.mjs +16 -0
  65. package/dist/json-schema/ref-scope.d.ts +18 -0
  66. package/dist/json-schema/ref-scope.js +37 -0
  67. package/dist/json-schema/ref-scope.mjs +33 -0
  68. package/dist/json-schema/resolve-ref.d.ts +14 -9
  69. package/dist/json-schema/resolve-ref.js +89 -59
  70. package/dist/json-schema/resolve-ref.mjs +86 -55
  71. package/dist/json-schema/schema-registry.d.ts +30 -0
  72. package/dist/json-schema/schema-registry.js +142 -0
  73. package/dist/json-schema/schema-registry.mjs +139 -0
  74. package/dist/json-schema/schema-to-declarations.js +2 -1
  75. package/dist/json-schema/schema-to-declarations.mjs +2 -1
  76. package/dist/json-schema/structural-expansion.types.d.ts +8 -1
  77. package/dist/json-schema/uri-reference.d.ts +29 -0
  78. package/dist/json-schema/uri-reference.js +118 -0
  79. package/dist/json-schema/uri-reference.mjs +111 -0
  80. package/dist/path/create-value-writer.js +27 -2
  81. package/dist/path/create-value-writer.mjs +27 -2
  82. package/dist/path/reserved-segment.d.ts +22 -4
  83. package/dist/path/reserved-segment.js +27 -7
  84. package/dist/path/reserved-segment.mjs +27 -7
  85. package/dist/plugin-kit/compiled-rule.d.ts +13 -0
  86. package/dist/plugin-kit/create-rule.d.ts +11 -0
  87. package/dist/plugin-kit/create-rule.js +1 -0
  88. package/dist/plugin-kit/create-rule.mjs +1 -0
  89. package/dist/plugin-kit/marker.types.d.ts +18 -0
  90. package/dist/plugin-kit/plugin-definition.d.ts +15 -0
  91. package/dist/plugin-kit/plugin-definition.js +1 -0
  92. package/dist/plugin-kit/plugin-definition.mjs +1 -0
  93. package/dist/plugins/index.generated.d.ts +1 -0
  94. package/dist/plugins/index.generated.js +4 -2
  95. package/dist/plugins/index.generated.mjs +1 -0
  96. package/dist/plugins/manifest.generated.js +1 -0
  97. package/dist/plugins/manifest.generated.mjs +1 -0
  98. package/dist/plugins/object-additional-properties/object-additional-properties-schema.d.ts +1 -1
  99. package/dist/plugins/object-additional-properties/object-additional-properties-schema.js +4 -2
  100. package/dist/plugins/object-additional-properties/object-additional-properties-schema.mjs +4 -2
  101. package/dist/plugins/object-additional-properties/object-additional-properties.d.ts +1 -1
  102. package/dist/plugins/object-additional-properties/object-additional-properties.js +4 -2
  103. package/dist/plugins/object-additional-properties/object-additional-properties.mjs +4 -2
  104. package/dist/plugins/object-additional-properties/select-additional-keys.d.ts +15 -0
  105. package/dist/plugins/object-additional-properties/select-additional-keys.js +51 -0
  106. package/dist/plugins/object-additional-properties/select-additional-keys.mjs +47 -0
  107. package/dist/plugins/stitch/stitch.d.ts +28 -8
  108. package/dist/plugins/stitch-with/index.d.ts +2 -0
  109. package/dist/plugins/stitch-with/index.js +5 -0
  110. package/dist/plugins/stitch-with/index.mjs +1 -0
  111. package/dist/plugins/stitch-with/stitch-with.d.ts +12 -0
  112. package/dist/plugins/stitch-with/stitch-with.js +94 -0
  113. package/dist/plugins/stitch-with/stitch-with.mjs +91 -0
  114. package/dist/plugins/stitchWith.d.ts +1 -0
  115. package/dist/plugins/stitchWith.js +2 -0
  116. package/dist/plugins/stitchWith.mjs +1 -0
  117. package/dist/plugins/string-exact-length/string-exact-length.js +2 -2
  118. package/dist/plugins/string-exact-length/string-exact-length.mjs +3 -3
  119. package/dist/plugins/string-max/string-max.js +2 -2
  120. package/dist/plugins/string-max/string-max.mjs +3 -3
  121. package/dist/plugins/string-min/string-min.js +21 -3
  122. package/dist/plugins/string-min/string-min.mjs +21 -3
  123. package/dist/presets/index.d.ts +1 -0
  124. package/dist/presets/index.js +9 -0
  125. package/dist/presets/index.mjs +1 -0
  126. package/dist/presets/presets.d.ts +165 -0
  127. package/dist/presets/presets.js +80 -0
  128. package/dist/presets/presets.mjs +77 -0
  129. package/dist/runtime/create-field-validator.js +24 -8
  130. package/dist/runtime/create-field-validator.mjs +24 -8
  131. package/dist/runtime/create-validator.js +38 -8
  132. package/dist/runtime/create-validator.mjs +38 -8
  133. package/dist/runtime/decide-presence.js +2 -1
  134. package/dist/runtime/decide-presence.mjs +2 -1
  135. package/dist/runtime/field-rule-context.d.ts +32 -0
  136. package/dist/runtime/field-rule-context.js +48 -0
  137. package/dist/runtime/field-rule-context.mjs +44 -0
  138. package/dist/runtime/index-stack.d.ts +29 -4
  139. package/dist/runtime/index-stack.js +76 -15
  140. package/dist/runtime/index-stack.mjs +76 -15
  141. package/dist/runtime/run-array-node.js +22 -10
  142. package/dist/runtime/run-array-node.mjs +22 -10
  143. package/dist/runtime/run-field.js +47 -17
  144. package/dist/runtime/run-field.mjs +47 -17
  145. package/dist/runtime/run-plan.js +5 -1
  146. package/dist/runtime/run-plan.mjs +5 -1
  147. package/dist/standard-schema/index.d.ts +6 -0
  148. package/dist/standard-schema/index.js +7 -0
  149. package/dist/standard-schema/index.mjs +2 -0
  150. package/dist/standard-schema/split-issue-path.d.ts +9 -0
  151. package/dist/standard-schema/split-issue-path.js +66 -0
  152. package/dist/standard-schema/split-issue-path.mjs +63 -0
  153. package/dist/standard-schema/standard-schema.types.d.ts +46 -0
  154. package/dist/standard-schema/standard-schema.types.js +13 -0
  155. package/dist/standard-schema/standard-schema.types.mjs +12 -0
  156. package/dist/standard-schema/to-standard-schema.d.ts +41 -0
  157. package/dist/standard-schema/to-standard-schema.js +60 -0
  158. package/dist/standard-schema/to-standard-schema.mjs +57 -0
  159. package/dist/standard-schema.d.ts +1 -0
  160. package/dist/standard-schema.js +2 -0
  161. package/dist/standard-schema.mjs +1 -0
  162. package/dist/types/index.d.ts +24 -0
  163. package/dist/types/index.js +18 -0
  164. package/dist/types/index.mjs +17 -0
  165. package/package.json +42 -19
@@ -5,14 +5,17 @@
5
5
  // the only json-schema surface anything outside src/json-schema/ may reach.
6
6
  // ===========================================================================
7
7
  export { NotASchemaError, buildFieldEntries, buildFromSchema, fromJsonSchema, } from "./build-from-schema.mjs";
8
- export { DEFINITION_CONTAINERS, collectDefinitionNames, isDefinitionContainer, readRefPointer, resolveSchemaNode, toSchemaObject, } from "./collect-definitions.mjs";
8
+ export { DEFINITION_CONTAINERS, collectDefinitionNames, isDefinitionContainer, readRefPointer, resolveSchemaNode, resolveSchemaNodeInScope, toSchemaObject, } from "./collect-definitions.mjs";
9
9
  export { flattenSchema, joinDeclaredPath } from "./flatten-schema.mjs";
10
10
  export { STRUCTURAL_EXPANSIONS, expandSchemaRules, isStructuralKeyword, readChildSchemas, } from "./schema-to-declarations.mjs";
11
11
  export { collectSubSchemaRules, toSchemaBranch, } from "./collect-sub-schema-rules.mjs";
12
12
  export { createStructuralContext } from "./create-structural-context.mjs";
13
13
  export { declareRequiredProperties } from "./declare-required-properties.mjs";
14
14
  export { declarePresenceRules } from "./declare-presence.mjs";
15
- export { RefResolutionError, isResolvableRef, resolveRef } from "./resolve-ref.mjs";
15
+ export { RefResolutionError } from "./ref-resolution-error.mjs";
16
+ export { isResolvableRef, resolveRef, resolveRefInScope } from "./resolve-ref.mjs";
17
+ export { createDocumentScope, createLocalScope } from "./ref-scope.mjs";
18
+ export { createSchemaRegistry } from "./schema-registry.mjs";
16
19
  export { UnsupportedKeywordError } from "./unsupported-keyword-error.mjs";
17
20
  export { isDraft07Schema, isSchemaObject } from "./draft07.types.mjs";
18
21
  export { assertKeywordSupported, countKeywordHandlings, findKeywordHandling, isDraft07Keyword, listBoundPluginNames, listDraft07Keywords, } from "./keyword-map.mjs";
@@ -13,5 +13,5 @@ export declare const requiredBinding: import("./keyword-binding.types").KeywordB
13
13
  * to the declared sibling keys, so `additionalProperties: false` no longer
14
14
  * rejects every property the way 1.x's empty `allowedProperties` default did.
15
15
  */
16
- export declare const additionalPropertiesBinding: import("./keyword-binding.types").KeywordBinding<"object", "additionalProperties", "objectAdditionalProperties", readonly [allowed: boolean, allowedProperties?: readonly string[] | undefined], boolean>;
16
+ export declare const additionalPropertiesBinding: import("./keyword-binding.types").KeywordBinding<"object", "additionalProperties", "objectAdditionalProperties", readonly [allowed: boolean, allowedProperties?: readonly string[] | undefined, allowedPatterns?: readonly string[] | undefined], boolean>;
17
17
  export declare const objectKeywordMap: KeywordTable<Draft07ObjectKeyword>;
@@ -0,0 +1,4 @@
1
+ export declare class RefResolutionError extends Error {
2
+ readonly ref: string;
3
+ constructor(ref: string, reason: string);
4
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ // ===========================================================================
3
+ // L8 src/json-schema/ref-resolution-error.ts
4
+ //
5
+ // `$ref` が解けなかったことを表す例外。呼び出し側が **identity で捕まえられる**
6
+ // ことが要件なので、独立したクラスであり、独立したモジュールである
7
+ // (resolve-ref.ts から import すると、その巨大な依存を一緒に引く)。
8
+ // ===========================================================================
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.RefResolutionError = void 0;
11
+ class RefResolutionError extends Error {
12
+ constructor(ref, reason) {
13
+ super(`Cannot resolve $ref "${ref}": ${reason}`);
14
+ this.name = "RefResolutionError";
15
+ this.ref = ref;
16
+ // Without this, `instanceof` fails when the package is compiled to ES5.
17
+ Object.setPrototypeOf(this, RefResolutionError.prototype);
18
+ }
19
+ }
20
+ exports.RefResolutionError = RefResolutionError;
@@ -0,0 +1,16 @@
1
+ // ===========================================================================
2
+ // L8 src/json-schema/ref-resolution-error.ts
3
+ //
4
+ // `$ref` が解けなかったことを表す例外。呼び出し側が **identity で捕まえられる**
5
+ // ことが要件なので、独立したクラスであり、独立したモジュールである
6
+ // (resolve-ref.ts から import すると、その巨大な依存を一緒に引く)。
7
+ // ===========================================================================
8
+ export class RefResolutionError extends Error {
9
+ constructor(ref, reason) {
10
+ super(`Cannot resolve $ref "${ref}": ${reason}`);
11
+ this.name = "RefResolutionError";
12
+ this.ref = ref;
13
+ // Without this, `instanceof` fails when the package is compiled to ES5.
14
+ Object.setPrototypeOf(this, RefResolutionError.prototype);
15
+ }
16
+ }
@@ -0,0 +1,18 @@
1
+ import type { Draft07Schema } from "./draft07.types";
2
+ import type { SchemaRegistry } from "./schema-registry";
3
+ export interface RefScope {
4
+ /** The resource a fragment-only reference (`#/definitions/x`) resolves in. */
5
+ readonly document: Draft07Schema;
6
+ /** The base URI in force here, "" for a document that declares no `$id`. */
7
+ readonly baseUri: string;
8
+ /** Everything a URI can name: `$id` bases, anchors, supplied documents. */
9
+ readonly registry: SchemaRegistry;
10
+ }
11
+ /**
12
+ * The scope of a document that declares no `$id` and reaches nothing else.
13
+ * Used by callers that hold a root and nothing more, and by every test that
14
+ * is about pointers rather than about identity.
15
+ */
16
+ export declare function createLocalScope(root: Draft07Schema): RefScope;
17
+ /** A scope over a root plus the documents the caller already loaded. */
18
+ export declare function createDocumentScope(root: Draft07Schema, externalDocuments: Readonly<Record<string, unknown>>): RefScope;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createLocalScope = createLocalScope;
4
+ exports.createDocumentScope = createDocumentScope;
5
+ const draft07_types_1 = require("./draft07.types");
6
+ const schema_registry_1 = require("./schema-registry");
7
+ const uri_reference_1 = require("./uri-reference");
8
+ /**
9
+ * A document's own `$id` is where its base URI starts. Beginning at "" and
10
+ * ignoring it is how `{"$id":"http://host/a","properties":{"x":{"$ref":"b.json"}}}`
11
+ * ends up looking for a document literally named "b.json".
12
+ */
13
+ function baseUriOf(root) {
14
+ if (!(0, draft07_types_1.isSchemaObject)(root) || typeof root.$id !== "string")
15
+ return "";
16
+ return (0, uri_reference_1.nextBaseUri)("", root.$id);
17
+ }
18
+ /**
19
+ * The scope of a document that declares no `$id` and reaches nothing else.
20
+ * Used by callers that hold a root and nothing more, and by every test that
21
+ * is about pointers rather than about identity.
22
+ */
23
+ function createLocalScope(root) {
24
+ return {
25
+ document: root,
26
+ baseUri: baseUriOf(root),
27
+ registry: (0, schema_registry_1.createSchemaRegistry)(root),
28
+ };
29
+ }
30
+ /** A scope over a root plus the documents the caller already loaded. */
31
+ function createDocumentScope(root, externalDocuments) {
32
+ return {
33
+ document: root,
34
+ baseUri: baseUriOf(root),
35
+ registry: (0, schema_registry_1.createSchemaRegistry)(root, externalDocuments),
36
+ };
37
+ }
@@ -0,0 +1,33 @@
1
+ import { isSchemaObject } from "./draft07.types.mjs";
2
+ import { createSchemaRegistry } from "./schema-registry.mjs";
3
+ import { nextBaseUri } from "./uri-reference.mjs";
4
+ /**
5
+ * A document's own `$id` is where its base URI starts. Beginning at "" and
6
+ * ignoring it is how `{"$id":"http://host/a","properties":{"x":{"$ref":"b.json"}}}`
7
+ * ends up looking for a document literally named "b.json".
8
+ */
9
+ function baseUriOf(root) {
10
+ if (!isSchemaObject(root) || typeof root.$id !== "string")
11
+ return "";
12
+ return nextBaseUri("", root.$id);
13
+ }
14
+ /**
15
+ * The scope of a document that declares no `$id` and reaches nothing else.
16
+ * Used by callers that hold a root and nothing more, and by every test that
17
+ * is about pointers rather than about identity.
18
+ */
19
+ export function createLocalScope(root) {
20
+ return {
21
+ document: root,
22
+ baseUri: baseUriOf(root),
23
+ registry: createSchemaRegistry(root),
24
+ };
25
+ }
26
+ /** A scope over a root plus the documents the caller already loaded. */
27
+ export function createDocumentScope(root, externalDocuments) {
28
+ return {
29
+ document: root,
30
+ baseUri: baseUriOf(root),
31
+ registry: createSchemaRegistry(root, externalDocuments),
32
+ };
33
+ }
@@ -1,16 +1,21 @@
1
1
  import type { Draft07Schema } from "./draft07.types";
2
- export declare class RefResolutionError extends Error {
3
- readonly ref: string;
4
- constructor(ref: string, reason: string);
5
- }
2
+ import type { RefScope } from "./ref-scope";
6
3
  /**
7
- * Resolves `ref` against `root`, then keeps following `$ref` until it reaches
8
- * a schema that is not one. `visited` holds the pointers already entered on
9
- * THIS chain, so `a -> b -> a` throws instead of looping forever.
4
+ * Resolves `ref` in `scope`, then keeps following `$ref` until it reaches a
5
+ * schema that is not one. `visited` holds the ABSOLUTE URIs already entered on
6
+ * THIS chain, so `a -> b -> a` throws instead of looping forever — absolute,
7
+ * because two different bases can spell the same target differently and a
8
+ * relative comparison would miss the cycle.
10
9
  */
10
+ export declare function resolveRefInScope(ref: string, scope: RefScope): {
11
+ schema: Draft07Schema;
12
+ scope: RefScope;
13
+ };
14
+ /** The local-only door, kept for callers that have nothing but a root. */
11
15
  export declare function resolveRef(ref: string, root: Draft07Schema): Draft07Schema;
12
16
  /**
13
- * True when `ref` resolves without leaving the document and without entering a
14
- * cycle. It answers the question without making the caller catch to find out.
17
+ * True when `ref` resolves without leaving what the caller supplied and
18
+ * without entering a cycle. It answers the question without making the caller
19
+ * catch to find out.
15
20
  */
16
21
  export declare function isResolvableRef(ref: string, root: Draft07Schema): boolean;
@@ -1,68 +1,93 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RefResolutionError = void 0;
3
+ exports.resolveRefInScope = resolveRefInScope;
4
4
  exports.resolveRef = resolveRef;
5
5
  exports.isResolvableRef = isResolvableRef;
6
+ const ref_resolution_error_1 = require("./ref-resolution-error");
6
7
  const draft07_types_1 = require("./draft07.types");
7
- const types_1 = require("../types");
8
- class RefResolutionError extends Error {
9
- constructor(ref, reason) {
10
- super(`Cannot resolve $ref "${ref}": ${reason}`);
11
- this.name = "RefResolutionError";
12
- this.ref = ref;
13
- // Without this, `instanceof` fails when the package is compiled to ES5.
14
- Object.setPrototypeOf(this, RefResolutionError.prototype);
15
- }
16
- }
17
- exports.RefResolutionError = RefResolutionError;
8
+ const collect_definitions_1 = require("./collect-definitions");
9
+ const follow_json_pointer_1 = require("./follow-json-pointer");
10
+ const ref_scope_1 = require("./ref-scope");
11
+ const uri_reference_1 = require("./uri-reference");
18
12
  /** No legitimate document chains this many `$ref`s. See resolveRef. */
19
13
  const MAX_REF_HOPS = 1000;
20
- /** RFC 6901: `~1` is "/" and `~0` is "~", decoded in that order. */
21
- function decodePointerToken(token) {
22
- return token.replace(/~1/g, "/").replace(/~0/g, "~");
14
+ function followPointer(ref, fragment, scope) {
15
+ const walked = (0, follow_json_pointer_1.walkPointer)(fragment, scope.document, scope, (at, node) => (0, draft07_types_1.isDraft07Schema)(node) && (0, draft07_types_1.isSchemaObject)(node)
16
+ ? (0, collect_definitions_1.advanceBase)(at, node)
17
+ : at, (token) => {
18
+ throw new ref_resolution_error_1.RefResolutionError(ref, `no schema at segment "${token}"`);
19
+ });
20
+ if (!(0, draft07_types_1.isDraft07Schema)(walked.node)) {
21
+ throw new ref_resolution_error_1.RefResolutionError(ref, "the target is not a schema");
22
+ }
23
+ return { schema: walked.node, scope: walked.scope };
23
24
  }
24
- function toPointerTokens(ref) {
25
- const pointer = ref.slice(1);
26
- if (pointer === "" || pointer === "/")
27
- return [];
28
- return pointer.split("/").slice(1).map(decodePointerToken);
25
+ /** True for `#name`: a plain-name fragment, which names rather than locates. */
26
+ function isPlainNameFragment(fragment) {
27
+ return fragment !== "" && !fragment.startsWith("/");
29
28
  }
30
29
  /**
31
- * `definitions` and `$defs` are the same container to a pointer: a Draft-07
32
- * document spells it one way, a 2019-09 document the other, and a schema that
33
- * mixes them (they exist) must still resolve. A real `definitions` member
34
- * always wins, so a property literally named "definitions" is unaffected.
30
+ * One hop. The returned scope's `document` is the RESOURCE the target was
31
+ * found in, never the target itself otherwise `a -> b` would resolve `b`
32
+ * against the node `a` names, and a two-link chain of local pointers stops
33
+ * finding anything after the first hop.
35
34
  */
36
- function stepInto(current, token) {
37
- if ((0, types_1.isArray)(current))
38
- return current[Number(token)];
39
- if (!(0, types_1.isPlainObject)(current))
40
- return undefined;
41
- if (token === "definitions" || token === "$defs") {
42
- return current["definitions"] ?? current["$defs"];
35
+ function stepRef(ref, scope) {
36
+ const absolute = (0, uri_reference_1.resolveUriReference)(scope.baseUri, ref);
37
+ const { resource, fragment } = (0, uri_reference_1.splitUri)(absolute);
38
+ if (isPlainNameFragment(fragment)) {
39
+ const named = scope.registry.findIdentified(absolute);
40
+ if (named === undefined) {
41
+ throw new ref_resolution_error_1.RefResolutionError(ref, `no schema is named "${absolute}"`);
42
+ }
43
+ return {
44
+ schema: named.schema,
45
+ scope: {
46
+ registry: scope.registry,
47
+ document: named.document,
48
+ baseUri: named.baseUri,
49
+ },
50
+ };
43
51
  }
44
- return current[token];
52
+ const target = resolveResource(ref, resource, scope);
53
+ // The RESOURCE's retrieval URI wins over any `$id` the document carries —
54
+ // "retrieved nested refs resolve relative to their URI not $id" in the
55
+ // suite is exactly that. Every `$id` INSIDE it still counts, and
56
+ // followPointer collects them as it walks.
57
+ const landed = {
58
+ registry: scope.registry,
59
+ document: target.document,
60
+ baseUri: target.baseUri,
61
+ };
62
+ return followPointer(ref, fragment, landed);
45
63
  }
46
- function followPointer(ref, root) {
47
- let current = root;
48
- for (const token of toPointerTokens(ref)) {
49
- current = stepInto(current, token);
50
- if (current === undefined) {
51
- throw new RefResolutionError(ref, `no schema at segment "${token}"`);
52
- }
64
+ /**
65
+ * The DOCUMENT a resource URI names. An empty resource is "the document this
66
+ * reference was written in", which is what makes `#/definitions/x` local.
67
+ */
68
+ function resolveResource(ref, resource, scope) {
69
+ if (resource === "" || resource === scope.baseUri) {
70
+ return { document: scope.document, baseUri: scope.baseUri };
53
71
  }
54
- if (!(0, draft07_types_1.isDraft07Schema)(current)) {
55
- throw new RefResolutionError(ref, "the target is not a schema");
72
+ const identified = scope.registry.findIdentified(resource);
73
+ if (identified === undefined || !(0, draft07_types_1.isSchemaObject)(identified.document)) {
74
+ // The one place an external reference is refused, and it is refused for
75
+ // ONE reason: nobody handed Luq that document. Luq does not go and get it.
76
+ throw new ref_resolution_error_1.RefResolutionError(ref, `"${resource}" was not supplied. Luq never fetches a schema; pass it ` +
77
+ "in externalDocuments.");
56
78
  }
57
- return current;
79
+ return { document: identified.document, baseUri: identified.baseUri };
58
80
  }
59
81
  /**
60
- * Resolves `ref` against `root`, then keeps following `$ref` until it reaches
61
- * a schema that is not one. `visited` holds the pointers already entered on
62
- * THIS chain, so `a -> b -> a` throws instead of looping forever.
82
+ * Resolves `ref` in `scope`, then keeps following `$ref` until it reaches a
83
+ * schema that is not one. `visited` holds the ABSOLUTE URIs already entered on
84
+ * THIS chain, so `a -> b -> a` throws instead of looping forever — absolute,
85
+ * because two different bases can spell the same target differently and a
86
+ * relative comparison would miss the cycle.
63
87
  */
64
- function resolveRef(ref, root) {
88
+ function resolveRefInScope(ref, scope) {
65
89
  const visited = new Set();
90
+ let current = scope;
66
91
  let currentRef = ref;
67
92
  for (let hop = 0;; hop += 1) {
68
93
  // The visited set is the real cycle detector and gives the good message.
@@ -71,24 +96,29 @@ function resolveRef(ref, root) {
71
96
  // still terminate. Measured: removing the visited check without this made
72
97
  // the resolver hang the whole test process instead of failing it.
73
98
  if (hop > MAX_REF_HOPS) {
74
- throw new RefResolutionError(ref, `more than ${MAX_REF_HOPS} $ref hops`);
99
+ throw new ref_resolution_error_1.RefResolutionError(ref, `more than ${MAX_REF_HOPS} $ref hops`);
75
100
  }
76
- if (!currentRef.startsWith("#")) {
77
- throw new RefResolutionError(currentRef, "external references are not supported");
101
+ const absolute = (0, uri_reference_1.resolveUriReference)(current.baseUri, currentRef);
102
+ if (visited.has(absolute)) {
103
+ throw new ref_resolution_error_1.RefResolutionError(ref, `circular reference: ${[...visited, absolute].join(" -> ")}`);
78
104
  }
79
- if (visited.has(currentRef)) {
80
- throw new RefResolutionError(ref, `circular reference: ${[...visited, currentRef].join(" -> ")}`);
105
+ visited.add(absolute);
106
+ const step = stepRef(currentRef, current);
107
+ current = step.scope;
108
+ if (!(0, draft07_types_1.isSchemaObject)(step.schema) || step.schema.$ref === undefined) {
109
+ return { schema: step.schema, scope: current };
81
110
  }
82
- visited.add(currentRef);
83
- const target = followPointer(currentRef, root);
84
- if (!(0, draft07_types_1.isSchemaObject)(target) || target.$ref === undefined)
85
- return target;
86
- currentRef = target.$ref;
111
+ currentRef = step.schema.$ref;
87
112
  }
88
113
  }
114
+ /** The local-only door, kept for callers that have nothing but a root. */
115
+ function resolveRef(ref, root) {
116
+ return resolveRefInScope(ref, (0, ref_scope_1.createLocalScope)(root)).schema;
117
+ }
89
118
  /**
90
- * True when `ref` resolves without leaving the document and without entering a
91
- * cycle. It answers the question without making the caller catch to find out.
119
+ * True when `ref` resolves without leaving what the caller supplied and
120
+ * without entering a cycle. It answers the question without making the caller
121
+ * catch to find out.
92
122
  */
93
123
  function isResolvableRef(ref, root) {
94
124
  try {
@@ -96,7 +126,7 @@ function isResolvableRef(ref, root) {
96
126
  return true;
97
127
  }
98
128
  catch (error) {
99
- if (error instanceof RefResolutionError)
129
+ if (error instanceof ref_resolution_error_1.RefResolutionError)
100
130
  return false;
101
131
  throw error;
102
132
  }
@@ -1,62 +1,88 @@
1
+ import { RefResolutionError } from "./ref-resolution-error.mjs";
1
2
  import { isDraft07Schema, isSchemaObject } from "./draft07.types.mjs";
2
- import { isArray, isPlainObject } from "../types/index.mjs";
3
- export class RefResolutionError extends Error {
4
- constructor(ref, reason) {
5
- super(`Cannot resolve $ref "${ref}": ${reason}`);
6
- this.name = "RefResolutionError";
7
- this.ref = ref;
8
- // Without this, `instanceof` fails when the package is compiled to ES5.
9
- Object.setPrototypeOf(this, RefResolutionError.prototype);
10
- }
11
- }
3
+ import { advanceBase } from "./collect-definitions.mjs";
4
+ import { walkPointer } from "./follow-json-pointer.mjs";
5
+ import { createLocalScope } from "./ref-scope.mjs";
6
+ import { resolveUriReference, splitUri } from "./uri-reference.mjs";
12
7
  /** No legitimate document chains this many `$ref`s. See resolveRef. */
13
8
  const MAX_REF_HOPS = 1000;
14
- /** RFC 6901: `~1` is "/" and `~0` is "~", decoded in that order. */
15
- function decodePointerToken(token) {
16
- return token.replace(/~1/g, "/").replace(/~0/g, "~");
9
+ function followPointer(ref, fragment, scope) {
10
+ const walked = walkPointer(fragment, scope.document, scope, (at, node) => isDraft07Schema(node) && isSchemaObject(node)
11
+ ? advanceBase(at, node)
12
+ : at, (token) => {
13
+ throw new RefResolutionError(ref, `no schema at segment "${token}"`);
14
+ });
15
+ if (!isDraft07Schema(walked.node)) {
16
+ throw new RefResolutionError(ref, "the target is not a schema");
17
+ }
18
+ return { schema: walked.node, scope: walked.scope };
17
19
  }
18
- function toPointerTokens(ref) {
19
- const pointer = ref.slice(1);
20
- if (pointer === "" || pointer === "/")
21
- return [];
22
- return pointer.split("/").slice(1).map(decodePointerToken);
20
+ /** True for `#name`: a plain-name fragment, which names rather than locates. */
21
+ function isPlainNameFragment(fragment) {
22
+ return fragment !== "" && !fragment.startsWith("/");
23
23
  }
24
24
  /**
25
- * `definitions` and `$defs` are the same container to a pointer: a Draft-07
26
- * document spells it one way, a 2019-09 document the other, and a schema that
27
- * mixes them (they exist) must still resolve. A real `definitions` member
28
- * always wins, so a property literally named "definitions" is unaffected.
25
+ * One hop. The returned scope's `document` is the RESOURCE the target was
26
+ * found in, never the target itself otherwise `a -> b` would resolve `b`
27
+ * against the node `a` names, and a two-link chain of local pointers stops
28
+ * finding anything after the first hop.
29
29
  */
30
- function stepInto(current, token) {
31
- if (isArray(current))
32
- return current[Number(token)];
33
- if (!isPlainObject(current))
34
- return undefined;
35
- if (token === "definitions" || token === "$defs") {
36
- return current["definitions"] ?? current["$defs"];
30
+ function stepRef(ref, scope) {
31
+ const absolute = resolveUriReference(scope.baseUri, ref);
32
+ const { resource, fragment } = splitUri(absolute);
33
+ if (isPlainNameFragment(fragment)) {
34
+ const named = scope.registry.findIdentified(absolute);
35
+ if (named === undefined) {
36
+ throw new RefResolutionError(ref, `no schema is named "${absolute}"`);
37
+ }
38
+ return {
39
+ schema: named.schema,
40
+ scope: {
41
+ registry: scope.registry,
42
+ document: named.document,
43
+ baseUri: named.baseUri,
44
+ },
45
+ };
37
46
  }
38
- return current[token];
47
+ const target = resolveResource(ref, resource, scope);
48
+ // The RESOURCE's retrieval URI wins over any `$id` the document carries —
49
+ // "retrieved nested refs resolve relative to their URI not $id" in the
50
+ // suite is exactly that. Every `$id` INSIDE it still counts, and
51
+ // followPointer collects them as it walks.
52
+ const landed = {
53
+ registry: scope.registry,
54
+ document: target.document,
55
+ baseUri: target.baseUri,
56
+ };
57
+ return followPointer(ref, fragment, landed);
39
58
  }
40
- function followPointer(ref, root) {
41
- let current = root;
42
- for (const token of toPointerTokens(ref)) {
43
- current = stepInto(current, token);
44
- if (current === undefined) {
45
- throw new RefResolutionError(ref, `no schema at segment "${token}"`);
46
- }
59
+ /**
60
+ * The DOCUMENT a resource URI names. An empty resource is "the document this
61
+ * reference was written in", which is what makes `#/definitions/x` local.
62
+ */
63
+ function resolveResource(ref, resource, scope) {
64
+ if (resource === "" || resource === scope.baseUri) {
65
+ return { document: scope.document, baseUri: scope.baseUri };
47
66
  }
48
- if (!isDraft07Schema(current)) {
49
- throw new RefResolutionError(ref, "the target is not a schema");
67
+ const identified = scope.registry.findIdentified(resource);
68
+ if (identified === undefined || !isSchemaObject(identified.document)) {
69
+ // The one place an external reference is refused, and it is refused for
70
+ // ONE reason: nobody handed Luq that document. Luq does not go and get it.
71
+ throw new RefResolutionError(ref, `"${resource}" was not supplied. Luq never fetches a schema; pass it ` +
72
+ "in externalDocuments.");
50
73
  }
51
- return current;
74
+ return { document: identified.document, baseUri: identified.baseUri };
52
75
  }
53
76
  /**
54
- * Resolves `ref` against `root`, then keeps following `$ref` until it reaches
55
- * a schema that is not one. `visited` holds the pointers already entered on
56
- * THIS chain, so `a -> b -> a` throws instead of looping forever.
77
+ * Resolves `ref` in `scope`, then keeps following `$ref` until it reaches a
78
+ * schema that is not one. `visited` holds the ABSOLUTE URIs already entered on
79
+ * THIS chain, so `a -> b -> a` throws instead of looping forever — absolute,
80
+ * because two different bases can spell the same target differently and a
81
+ * relative comparison would miss the cycle.
57
82
  */
58
- export function resolveRef(ref, root) {
83
+ export function resolveRefInScope(ref, scope) {
59
84
  const visited = new Set();
85
+ let current = scope;
60
86
  let currentRef = ref;
61
87
  for (let hop = 0;; hop += 1) {
62
88
  // The visited set is the real cycle detector and gives the good message.
@@ -67,22 +93,27 @@ export function resolveRef(ref, root) {
67
93
  if (hop > MAX_REF_HOPS) {
68
94
  throw new RefResolutionError(ref, `more than ${MAX_REF_HOPS} $ref hops`);
69
95
  }
70
- if (!currentRef.startsWith("#")) {
71
- throw new RefResolutionError(currentRef, "external references are not supported");
96
+ const absolute = resolveUriReference(current.baseUri, currentRef);
97
+ if (visited.has(absolute)) {
98
+ throw new RefResolutionError(ref, `circular reference: ${[...visited, absolute].join(" -> ")}`);
72
99
  }
73
- if (visited.has(currentRef)) {
74
- throw new RefResolutionError(ref, `circular reference: ${[...visited, currentRef].join(" -> ")}`);
100
+ visited.add(absolute);
101
+ const step = stepRef(currentRef, current);
102
+ current = step.scope;
103
+ if (!isSchemaObject(step.schema) || step.schema.$ref === undefined) {
104
+ return { schema: step.schema, scope: current };
75
105
  }
76
- visited.add(currentRef);
77
- const target = followPointer(currentRef, root);
78
- if (!isSchemaObject(target) || target.$ref === undefined)
79
- return target;
80
- currentRef = target.$ref;
106
+ currentRef = step.schema.$ref;
81
107
  }
82
108
  }
109
+ /** The local-only door, kept for callers that have nothing but a root. */
110
+ export function resolveRef(ref, root) {
111
+ return resolveRefInScope(ref, createLocalScope(root)).schema;
112
+ }
83
113
  /**
84
- * True when `ref` resolves without leaving the document and without entering a
85
- * cycle. It answers the question without making the caller catch to find out.
114
+ * True when `ref` resolves without leaving what the caller supplied and
115
+ * without entering a cycle. It answers the question without making the caller
116
+ * catch to find out.
86
117
  */
87
118
  export function isResolvableRef(ref, root) {
88
119
  try {
@@ -0,0 +1,30 @@
1
+ import type { Draft07Schema } from "./draft07.types";
2
+ /**
3
+ * A node the index names, with the base URI in force inside it and the
4
+ * DOCUMENT that base belongs to.
5
+ *
6
+ * The document is not always the node. `$id: "#name"` names a sub-schema
7
+ * without starting a new resource, so a `#/definitions/x` written next to it
8
+ * still has to resolve against the enclosing document — resolving it against
9
+ * the anchored sub-schema instead is how a chain of refs silently stops
10
+ * finding anything after the first hop.
11
+ */
12
+ export interface RegisteredTarget {
13
+ readonly schema: Draft07Schema;
14
+ readonly baseUri: string;
15
+ readonly document: Draft07Schema;
16
+ }
17
+ export interface SchemaRegistry {
18
+ /** The node this absolute URI NAMES — a document `$id`, or an anchor. */
19
+ findIdentified(uri: string): RegisteredTarget | undefined;
20
+ }
21
+ /**
22
+ * Every `$id` in `root` and in each supplied external document.
23
+ *
24
+ * The external map's KEY is a URI in its own right: the suite (and real
25
+ * callers) fetch `http://host/a.json` into a document that does not repeat its
26
+ * own `$id`, so the key is what a `$ref` will name. A document that DOES carry
27
+ * `$id` registers that too, and the `$id` wins where they disagree — it is the
28
+ * document's own statement about its identity.
29
+ */
30
+ export declare function createSchemaRegistry(root: Draft07Schema, externalDocuments?: Readonly<Record<string, unknown>>): SchemaRegistry;