@depup/typebox 1.3.22-depup.0 → 1.3.30-depup.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 (136) hide show
  1. package/README.md +2 -2
  2. package/build/compile/validator.d.mts +1 -1
  3. package/build/compile/validator.mjs +1 -1
  4. package/build/format/iri.mjs +5 -1
  5. package/build/schema/build.mjs +2 -2
  6. package/build/schema/check.mjs +1 -1
  7. package/build/schema/compile.d.mts +3 -2
  8. package/build/schema/compile.mjs +8 -8
  9. package/build/schema/engine/_context.d.mts +6 -7
  10. package/build/schema/engine/_context.mjs +23 -15
  11. package/build/schema/engine/_functions.d.mts +2 -2
  12. package/build/schema/engine/_functions.mjs +1 -1
  13. package/build/schema/engine/_reducer.d.mts +3 -3
  14. package/build/schema/engine/_refine.d.mts +5 -5
  15. package/build/schema/engine/_refine.mjs +4 -7
  16. package/build/schema/engine/_stack.d.mts +34 -16
  17. package/build/schema/engine/_stack.mjs +148 -104
  18. package/build/schema/engine/additionalItems.d.mts +4 -4
  19. package/build/schema/engine/additionalProperties.d.mts +8 -8
  20. package/build/schema/engine/additionalProperties.mjs +11 -17
  21. package/build/schema/engine/allOf.d.mts +4 -4
  22. package/build/schema/engine/allOf.mjs +3 -3
  23. package/build/schema/engine/anyOf.d.mts +4 -4
  24. package/build/schema/engine/anyOf.mjs +5 -10
  25. package/build/schema/engine/boolean.d.mts +4 -4
  26. package/build/schema/engine/boolean.mjs +2 -6
  27. package/build/schema/engine/const.d.mts +4 -4
  28. package/build/schema/engine/const.mjs +2 -6
  29. package/build/schema/engine/contains.d.mts +4 -4
  30. package/build/schema/engine/contains.mjs +2 -6
  31. package/build/schema/engine/dependencies.d.mts +4 -4
  32. package/build/schema/engine/dependencies.mjs +3 -6
  33. package/build/schema/engine/dependentRequired.d.mts +4 -4
  34. package/build/schema/engine/dependentRequired.mjs +2 -6
  35. package/build/schema/engine/dependentSchemas.d.mts +4 -4
  36. package/build/schema/engine/dynamicRef.d.mts +4 -4
  37. package/build/schema/engine/dynamicRef.mjs +11 -7
  38. package/build/schema/engine/enum.d.mts +4 -4
  39. package/build/schema/engine/enum.mjs +2 -6
  40. package/build/schema/engine/exclusiveMaximum.d.mts +4 -4
  41. package/build/schema/engine/exclusiveMaximum.mjs +2 -6
  42. package/build/schema/engine/exclusiveMinimum.d.mts +4 -4
  43. package/build/schema/engine/exclusiveMinimum.mjs +2 -6
  44. package/build/schema/engine/format.d.mts +4 -4
  45. package/build/schema/engine/format.mjs +2 -6
  46. package/build/schema/engine/if.d.mts +4 -4
  47. package/build/schema/engine/if.mjs +7 -15
  48. package/build/schema/engine/items.d.mts +4 -4
  49. package/build/schema/engine/items.mjs +1 -1
  50. package/build/schema/engine/maxContains.d.mts +4 -4
  51. package/build/schema/engine/maxContains.mjs +2 -6
  52. package/build/schema/engine/maxItems.d.mts +4 -4
  53. package/build/schema/engine/maxItems.mjs +2 -6
  54. package/build/schema/engine/maxLength.d.mts +4 -4
  55. package/build/schema/engine/maxLength.mjs +2 -6
  56. package/build/schema/engine/maxProperties.d.mts +4 -4
  57. package/build/schema/engine/maxProperties.mjs +2 -6
  58. package/build/schema/engine/maximum.d.mts +4 -4
  59. package/build/schema/engine/maximum.mjs +2 -6
  60. package/build/schema/engine/minContains.d.mts +4 -4
  61. package/build/schema/engine/minContains.mjs +2 -6
  62. package/build/schema/engine/minItems.d.mts +5 -5
  63. package/build/schema/engine/minItems.mjs +2 -6
  64. package/build/schema/engine/minLength.d.mts +4 -4
  65. package/build/schema/engine/minLength.mjs +2 -6
  66. package/build/schema/engine/minProperties.d.mts +4 -4
  67. package/build/schema/engine/minProperties.mjs +2 -6
  68. package/build/schema/engine/minimum.d.mts +4 -4
  69. package/build/schema/engine/minimum.mjs +2 -6
  70. package/build/schema/engine/multipleOf.d.mts +4 -4
  71. package/build/schema/engine/multipleOf.mjs +2 -6
  72. package/build/schema/engine/not.d.mts +4 -4
  73. package/build/schema/engine/not.mjs +4 -8
  74. package/build/schema/engine/oneOf.d.mts +4 -4
  75. package/build/schema/engine/oneOf.mjs +6 -11
  76. package/build/schema/engine/pattern.d.mts +4 -4
  77. package/build/schema/engine/pattern.mjs +3 -7
  78. package/build/schema/engine/patternProperties.d.mts +4 -4
  79. package/build/schema/engine/patternProperties.mjs +1 -1
  80. package/build/schema/engine/prefixItems.d.mts +4 -4
  81. package/build/schema/engine/prefixItems.mjs +1 -1
  82. package/build/schema/engine/properties.d.mts +4 -4
  83. package/build/schema/engine/properties.mjs +1 -1
  84. package/build/schema/engine/propertyNames.d.mts +4 -4
  85. package/build/schema/engine/propertyNames.mjs +4 -10
  86. package/build/schema/engine/recursiveRef.d.mts +4 -4
  87. package/build/schema/engine/recursiveRef.mjs +11 -7
  88. package/build/schema/engine/ref.d.mts +4 -4
  89. package/build/schema/engine/ref.mjs +19 -15
  90. package/build/schema/engine/required.d.mts +4 -4
  91. package/build/schema/engine/required.mjs +2 -6
  92. package/build/schema/engine/schema.d.mts +7 -7
  93. package/build/schema/engine/schema.mjs +137 -133
  94. package/build/schema/engine/type.d.mts +4 -4
  95. package/build/schema/engine/type.mjs +2 -6
  96. package/build/schema/engine/unevaluatedItems.d.mts +4 -4
  97. package/build/schema/engine/unevaluatedItems.mjs +5 -9
  98. package/build/schema/engine/unevaluatedProperties.d.mts +4 -4
  99. package/build/schema/engine/unevaluatedProperties.mjs +5 -9
  100. package/build/schema/engine/uniqueItems.d.mts +4 -4
  101. package/build/schema/engine/uniqueItems.mjs +2 -6
  102. package/build/schema/errors.d.mts +2 -2
  103. package/build/schema/errors.mjs +8 -14
  104. package/build/schema/intern/index.d.mts +1 -0
  105. package/build/schema/intern/index.mjs +1 -0
  106. package/build/schema/intern/intern.d.mts +11 -0
  107. package/build/schema/intern/intern.mjs +293 -0
  108. package/build/schema/meta/draft_2019_09.d.mts +2 -0
  109. package/build/schema/meta/draft_2019_09.mjs +288 -0
  110. package/build/schema/meta/draft_2020_12.d.mts +2 -0
  111. package/build/schema/meta/draft_2020_12.mjs +300 -0
  112. package/build/schema/meta/draft_3.d.mts +2 -0
  113. package/build/schema/meta/draft_3.mjs +143 -0
  114. package/build/schema/meta/draft_4.d.mts +2 -0
  115. package/build/schema/meta/draft_4.mjs +149 -0
  116. package/build/schema/meta/draft_6.d.mts +2 -0
  117. package/build/schema/meta/draft_6.mjs +155 -0
  118. package/build/schema/meta/draft_7.d.mts +2 -0
  119. package/build/schema/meta/draft_7.mjs +172 -0
  120. package/build/schema/meta/index.d.mts +2 -0
  121. package/build/schema/meta/index.mjs +2 -0
  122. package/build/schema/meta/meta.d.mts +9 -0
  123. package/build/schema/meta/meta.mjs +15 -0
  124. package/build/schema/meta/schema.d.mts +6 -0
  125. package/build/schema/meta/schema.mjs +1 -0
  126. package/build/schema/parse.d.mts +1 -0
  127. package/build/schema/parse.mjs +10 -5
  128. package/build/schema/resolve/resolve.d.mts +8 -3
  129. package/build/schema/resolve/resolve.mjs +268 -139
  130. package/build/schema/schema.d.mts +2 -0
  131. package/build/schema/schema.mjs +2 -0
  132. package/build/value/errors/errors.d.mts +2 -12
  133. package/build/value/errors/errors.mjs +2 -7
  134. package/changes.json +1 -1
  135. package/package.json +3 -3
  136. package/readme.md +59 -89
package/README.md CHANGED
@@ -13,8 +13,8 @@ npm install @depup/typebox
13
13
 
14
14
  | Field | Value |
15
15
  |-------|-------|
16
- | Original | [typebox](https://www.npmjs.com/package/typebox) @ 1.3.22 |
17
- | Processed | 2026-08-30 |
16
+ | Original | [typebox](https://www.npmjs.com/package/typebox) @ 1.3.30 |
17
+ | Processed | 2026-09-13 |
18
18
  | Smoke test | passed |
19
19
  | Deps updated | 0 |
20
20
 
@@ -18,7 +18,7 @@ export declare class Validator<Context extends TProperties = TProperties, Type e
18
18
  Check(value: unknown): value is Encode;
19
19
  /** Validates a value and returns it. Will throw if invalid. */
20
20
  Parse(value: unknown): Encode;
21
- /** Inspects a value and returns a detailed list of validation errors. */
21
+ /** Returns an array of validation errors for the given value. */
22
22
  Errors(value: unknown): TLocalizedValidationError[];
23
23
  /** Cleans a value using the Validator type. */
24
24
  Clean(value: unknown): unknown;
@@ -53,7 +53,7 @@ export class Validator {
53
53
  return Parser(this.Context(), this.Type(), value);
54
54
  throw new ParseError(value, this.Errors(value));
55
55
  }
56
- /** Inspects a value and returns a detailed list of validation errors. */
56
+ /** Returns an array of validation errors for the given value. */
57
57
  Errors(value) {
58
58
  if (this.IsAccelerated() && this.Check(value))
59
59
  return [];
@@ -2,6 +2,7 @@
2
2
  const IpvFutureMatchMaxLength = 2048;
3
3
  const IpvFutureMatch = /\[[vV][0-9a-fA-F]+\.[^\]]+\]/; // Guarded By IpvFutureMatchMaxLength
4
4
  const InvalidIriChars = /[\x00-\x20<>\^`{|}\\]/;
5
+ const InvalidPercentEncoding = /%(?![0-9a-fA-F]{2})/;
5
6
  // ------------------------------------------------------------------
6
7
  // NarrowIpvFuture
7
8
  //
@@ -26,6 +27,9 @@ export function IsIri(value) {
26
27
  // 1. Reject strings containing unencoded whitespace or illegal control characters.
27
28
  if (InvalidIriChars.test(value))
28
29
  return false;
29
- // 2. Delegate to the native URL parser, patching the IPvFuture edge case beforehand.
30
+ // 2. Reject malformed percent-encoding triplets.
31
+ if (InvalidPercentEncoding.test(value))
32
+ return false;
33
+ // 3. Delegate to the native URL parser, patching the IPvFuture edge case beforehand.
30
34
  return URL.canParse(NarrowIpvFuture(value));
31
35
  }
@@ -27,7 +27,7 @@ function CreateEvaluatedCheck(build, code) {
27
27
  // CreateDynamicCheck
28
28
  // ------------------------------------------------------------------
29
29
  function CreateDynamicCheck(build) {
30
- const stack = new Engine.Stack(build.Context(), build.Schema());
30
+ const stack = Engine.Stack(build.Context(), build.Schema());
31
31
  const context = new Engine.CheckContext();
32
32
  return (value) => Engine.CheckSchema(stack, context, build.Schema(), value);
33
33
  }
@@ -109,7 +109,7 @@ export function Build(...args) {
109
109
  });
110
110
  Engine.ResetExternal();
111
111
  Engine.ResetFunctions();
112
- const stack = new Engine.Stack(context, schema);
112
+ const stack = Engine.Stack(context, schema);
113
113
  const build = new Engine.BuildContext(Engine.HasUnevaluated(context, schema));
114
114
  const call = Engine.CreateFunction(stack, build, schema, 'value');
115
115
  const functions = Engine.GetFunctions();
@@ -8,7 +8,7 @@ export function Check(...args) {
8
8
  3: (context, schema, value) => [context, schema, value],
9
9
  2: (schema, value) => [{}, schema, value]
10
10
  });
11
- const stack = new Engine.Stack(context, schema);
11
+ const stack = Engine.Stack(context, schema);
12
12
  const checkContext = new Engine.CheckContext();
13
13
  return Engine.CheckSchema(stack, checkContext, schema, value);
14
14
  }
@@ -2,8 +2,9 @@ import { type TLocalizedValidationError } from '../error/index.mjs';
2
2
  import { type Static } from '../type/types/static.mjs';
3
3
  import * as Schema from './types/index.mjs';
4
4
  export declare class Validator<const Schema extends Schema.XSchema = Schema.XSchema, Value extends unknown = Static<Schema>> {
5
- private readonly buildResult;
6
5
  private readonly evaluateResult;
6
+ private readonly context;
7
+ private readonly schema;
7
8
  constructor(context: Record<string, Schema.XSchema>, schema: Schema);
8
9
  /** Returns true if this Validator is using JIT acceleration. */
9
10
  IsAccelerated(): boolean;
@@ -13,7 +14,7 @@ export declare class Validator<const Schema extends Schema.XSchema = Schema.XSch
13
14
  Check(value: unknown): value is Value;
14
15
  /** Validates a value and returns it. Will throw if invalid. */
15
16
  Parse(value: unknown): Value;
16
- /** Inspects a value and returns a detailed list of validation errors. */
17
+ /** Returns an array of validation errors for the given value. */
17
18
  Errors(value: unknown): [result: boolean, errors: TLocalizedValidationError[]];
18
19
  }
19
20
  /** Compiles this schema into a high performance Validator */
@@ -3,14 +3,15 @@
3
3
  import { Arguments } from '../system/arguments/index.mjs';
4
4
  import * as Build from './build.mjs';
5
5
  import { Errors } from './errors.mjs';
6
- import { ParseError } from './parse.mjs';
6
+ import { ThrowParseError } from './parse.mjs';
7
7
  // ------------------------------------------------------------------
8
8
  // Validator
9
9
  // ------------------------------------------------------------------
10
10
  export class Validator {
11
11
  constructor(context, schema) {
12
- this.buildResult = Build.Build(context, schema);
13
- this.evaluateResult = this.buildResult.Evaluate();
12
+ this.evaluateResult = Build.Build(context, schema).Evaluate();
13
+ this.context = context;
14
+ this.schema = schema;
14
15
  }
15
16
  /** Returns true if this Validator is using JIT acceleration. */
16
17
  IsAccelerated() {
@@ -18,7 +19,7 @@ export class Validator {
18
19
  }
19
20
  /** Returns the underlying Schema used to construct this Validator. */
20
21
  Schema() {
21
- return this.buildResult.Schema();
22
+ return this.schema;
22
23
  }
23
24
  /** Performs a type-guard check on the provided value. */
24
25
  Check(value) {
@@ -28,12 +29,11 @@ export class Validator {
28
29
  Parse(value) {
29
30
  if (this.evaluateResult.Check(value))
30
31
  return value;
31
- const [_result, errors] = Errors(this.buildResult.Context(), this.buildResult.Schema(), value);
32
- throw new ParseError(this.buildResult.Schema(), value, errors);
32
+ ThrowParseError(this.context, this.schema, value);
33
33
  }
34
- /** Inspects a value and returns a detailed list of validation errors. */
34
+ /** Returns an array of validation errors for the given value. */
35
35
  Errors(value) {
36
- return Errors(this.buildResult.Context(), this.buildResult.Schema(), value);
36
+ return Errors(this.context, this.schema, value);
37
37
  }
38
38
  }
39
39
  /** Compiles this schema into a high performance Validator */
@@ -21,15 +21,14 @@ export declare class CheckContext {
21
21
  GetKeys(): Set<string>;
22
22
  Merge(results: CheckContext[]): true;
23
23
  }
24
- export type ErrorContextCallback = (error: TValidationError) => unknown;
25
24
  export declare class ErrorContext extends CheckContext {
26
- private readonly callback;
27
- constructor(callback: ErrorContextCallback);
28
- AddError(error: TValidationError): false;
29
- }
30
- export declare class AccumulatedErrorContext extends ErrorContext {
31
25
  private readonly errors;
32
26
  constructor();
33
- AddError(error: TValidationError): false;
27
+ AtCapacity(): boolean;
28
+ AddError<Keyword extends TValidationError['keyword']>(keyword: Keyword, schemaPath: string, instancePath: string, params: Extract<TValidationError, {
29
+ keyword: Keyword;
30
+ }>['params']): false;
31
+ AddErrors(error: TValidationError[]): false;
34
32
  GetErrors(): TValidationError[];
33
+ private AddErrorObject;
35
34
  }
@@ -1,4 +1,5 @@
1
1
  // deno-fmt-ignore-file
2
+ import { Settings } from '../../system/settings/index.mjs';
2
3
  import * as Schema from '../types/index.mjs';
3
4
  import { Guard as G, EmitGuard as E } from '../../guard/index.mjs';
4
5
  // ------------------------------------------------------------------
@@ -101,29 +102,36 @@ export class CheckContext {
101
102
  return true;
102
103
  }
103
104
  }
104
- export class ErrorContext extends CheckContext {
105
- constructor(callback) {
106
- super();
107
- this.callback = callback;
108
- }
109
- AddError(error) {
110
- this.callback(error);
111
- return false;
112
- }
113
- }
114
105
  // ------------------------------------------------------------------
115
- // AccumulatedErrorContext
106
+ // ErrorContext
116
107
  // ------------------------------------------------------------------
117
- export class AccumulatedErrorContext extends ErrorContext {
108
+ export class ErrorContext extends CheckContext {
118
109
  constructor() {
119
- super(error => this.errors.push(error));
110
+ super();
120
111
  this.errors = [];
121
112
  }
122
- AddError(error) {
123
- this.errors.push(error);
113
+ // ----------------------------------------------------------------
114
+ // Public
115
+ // ----------------------------------------------------------------
116
+ AtCapacity() {
117
+ return this.errors.length >= Settings.Get().maxErrors;
118
+ }
119
+ AddError(keyword, schemaPath, instancePath, params) {
120
+ return this.AddErrorObject({ keyword, schemaPath, instancePath, params });
121
+ }
122
+ AddErrors(error) {
123
+ error.forEach(error => this.AddErrorObject(error));
124
124
  return false;
125
125
  }
126
126
  GetErrors() {
127
127
  return this.errors;
128
128
  }
129
+ // ----------------------------------------------------------------
130
+ // Private
131
+ // ----------------------------------------------------------------
132
+ AddErrorObject(error) {
133
+ if (!this.AtCapacity())
134
+ this.errors.push(error);
135
+ return false;
136
+ }
129
137
  }
@@ -1,6 +1,6 @@
1
1
  import * as Schema from '../types/index.mjs';
2
- import { Stack } from './_stack.mjs';
2
+ import * as Stack from './_stack.mjs';
3
3
  import { BuildContext } from './_context.mjs';
4
4
  export declare function ResetFunctions(): void;
5
5
  export declare function GetFunctions(): string[];
6
- export declare function CreateFunction(stack: Stack, context: BuildContext, schema: Schema.XSchema, value: string): string;
6
+ export declare function CreateFunction(stack: Stack.XStack, context: BuildContext, schema: Schema.XSchema, value: string): string;
@@ -55,7 +55,7 @@ export function GetFunctions() {
55
55
  // CreateFunction
56
56
  // ------------------------------------------------------------------
57
57
  export function CreateFunction(stack, context, schema, value) {
58
- const name = CreateName(schema, stack.BaseURL().href);
58
+ const name = CreateName(schema, stack.lexicalBase);
59
59
  const call = CreateCallExpression(context, schema, name, value);
60
60
  if (funcs.has(name))
61
61
  return call;
@@ -1,4 +1,4 @@
1
- import * as S from '../types/index.mjs';
2
- import { Stack } from './_stack.mjs';
1
+ import * as Schema from '../types/index.mjs';
2
+ import * as Stack from './_stack.mjs';
3
3
  import { BuildContext } from './_context.mjs';
4
- export declare function Reducer(stack: Stack, context: BuildContext, schemas: S.XSchema[], value: string, check: string): string;
4
+ export declare function Reducer(stack: Stack.XStack, context: BuildContext, schemas: Schema.XSchema[], value: string, check: string): string;
@@ -1,6 +1,6 @@
1
- import * as S from '../types/index.mjs';
2
- import { Stack } from './_stack.mjs';
1
+ import * as Schema from '../types/index.mjs';
2
+ import * as Stack from './_stack.mjs';
3
3
  import { BuildContext, CheckContext, ErrorContext } from './_context.mjs';
4
- export declare function BuildRefine(_stack: Stack, _context: BuildContext, schema: S.XRefine, value: string): string;
5
- export declare function CheckRefine(_stack: Stack, _context: CheckContext, schema: S.XRefine, value: unknown): boolean;
6
- export declare function ErrorRefine(_stack: Stack, context: ErrorContext, schemaPath: string, instancePath: string, schema: S.XRefine, value: unknown): boolean;
4
+ export declare function BuildRefine(_stack: Stack.XStack, _context: BuildContext, schema: Schema.XRefine, value: string): string;
5
+ export declare function CheckRefine(_stack: Stack.XStack, _context: CheckContext, schema: Schema.XRefine, value: unknown): boolean;
6
+ export declare function ErrorRefine(_stack: Stack.XStack, context: ErrorContext, schemaPath: string, instancePath: string, schema: Schema.XRefine, value: unknown): boolean;
@@ -1,11 +1,11 @@
1
1
  // deno-fmt-ignore-file
2
- import * as V from './_externals.mjs';
2
+ import * as Externals from './_externals.mjs';
3
3
  import { EmitGuard as E, Guard as G } from '../../guard/index.mjs';
4
4
  // ------------------------------------------------------------------
5
5
  // Build
6
6
  // ------------------------------------------------------------------
7
7
  export function BuildRefine(_stack, _context, schema, value) {
8
- const refinements = V.CreateVariable(schema['~refine'].map((refinement) => refinement));
8
+ const refinements = Externals.CreateVariable(schema['~refine'].map((refinement) => refinement));
9
9
  return E.Every(refinements, E.Constant(0), ['refinement', '_'], E.Call(E.Member('refinement', 'check'), [value]));
10
10
  }
11
11
  // ------------------------------------------------------------------
@@ -19,11 +19,8 @@ export function CheckRefine(_stack, _context, schema, value) {
19
19
  // ------------------------------------------------------------------
20
20
  export function ErrorRefine(_stack, context, schemaPath, instancePath, schema, value) {
21
21
  return G.EveryAll(schema['~refine'], 0, (refinement, index) => {
22
- return refinement.check(value) || context.AddError({
23
- keyword: '~refine',
24
- schemaPath,
25
- instancePath,
26
- params: { index, message: refinement.error(value) },
22
+ return refinement.check(value) || context.AddError('~refine', schemaPath, instancePath, {
23
+ index, message: refinement.error(value)
27
24
  });
28
25
  });
29
26
  }
@@ -1,18 +1,36 @@
1
1
  import * as Schema from '../types/index.mjs';
2
- export declare class Stack {
3
- #private;
4
- private readonly context;
5
- private readonly schema;
6
- private readonly ids;
7
- private readonly anchors;
8
- private readonly recursiveAnchors;
9
- private readonly dynamicAnchors;
10
- constructor(context: Record<PropertyKey, Schema.XSchema>, schema: Schema.XSchema);
11
- BaseURL(): URL;
12
- Base(): Schema.XSchemaObject;
13
- Push(schema: Schema.XSchema): void;
14
- Pop(schema: Schema.XSchema): void;
15
- Ref(ref: Schema.XRef): Schema.XSchema | undefined;
16
- RecursiveRef(recursiveRef: Schema.XRecursiveRef): Schema.XSchema | undefined;
17
- DynamicRef(dynamicRef: Schema.XDynamicRef): Schema.XSchema | undefined;
2
+ export declare const DefaultUri = "urn:typebox:root";
3
+ export interface XStack {
4
+ /** The schema context. */
5
+ readonly context: Record<string, Schema.XSchema>;
6
+ /** The entry schema. */
7
+ readonly schema: Schema.XSchema;
8
+ /** Visited $id schemas, kept only to detect resource re-entry (see resolve.ts FindResolvedResource). */
9
+ readonly ids: Schema.XId[];
10
+ /** Nearest enclosing $id schema, used as the root for local pointer resolution. */
11
+ readonly lexicalSchema: Schema.XSchema;
12
+ /** First $recursiveAnchor: true schema seen on the path. */
13
+ readonly recursiveAnchor: Schema.XRecursiveAnchor | undefined;
14
+ /** Dynamic anchors visible at this point in the traversal. */
15
+ readonly dynamicAnchors: Schema.XDynamicAnchor[];
16
+ /** Lexical base URL, used to resolve relative $id and $ref values within the current schema. */
17
+ readonly lexicalBase: string;
18
+ /** Base URL of the current resource, reset each time a new resource $id is entered. */
19
+ readonly resourceBase: string;
20
+ /** Base URL that $ref values resolve against. */
21
+ readonly referenceBase: string;
22
+ /** Resource entry point bookkeeping: schema -> the base/root to apply when that schema is next pushed. */
23
+ readonly resourceEntries: Map<Schema.XSchemaObject, {
24
+ base: string;
25
+ root: Schema.XSchemaObject;
26
+ }>;
27
+ /** True while referenceBase should track resourceBase rather than lexicalBase. */
28
+ readonly useResourceBaseForReference: boolean;
29
+ /** True until the next $id schema is entered, marking it as a fresh resource root. */
30
+ readonly pendingResource: boolean;
31
+ /** True once traversal has entered a retrieved or legacy resource. */
32
+ readonly enteredResource: boolean;
18
33
  }
34
+ export declare function NextUri(ref: string, base: string): URL;
35
+ export declare function Stack(context: Record<string, Schema.XSchema>, schema: Schema.XSchema): XStack;
36
+ export declare function NextStack(stack: XStack, schema: Schema.XSchema): XStack;
@@ -1,106 +1,150 @@
1
- // deno-fmt-ignore-file
2
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
- };
7
- var _Stack_instances, _Stack_PushResourceAnchors, _Stack_PopResourceAnchors;
1
+ import { Guard } from '../../guard/index.mjs';
8
2
  import * as Schema from '../types/index.mjs';
9
- import { Guard as G } from '../../guard/index.mjs';
10
- import { Resolve } from '../resolve/index.mjs';
11
- export class Stack {
12
- constructor(context, schema) {
13
- _Stack_instances.add(this);
14
- this.context = context;
15
- this.schema = schema;
16
- this.ids = [];
17
- this.anchors = [];
18
- this.recursiveAnchors = [];
19
- this.dynamicAnchors = [];
20
- }
21
- // ----------------------------------------------------------------
22
- // Base
23
- // ----------------------------------------------------------------
24
- BaseURL() {
25
- return this.ids.reduce((result, schema) => new URL(schema.$id, result), Resolve.DefaultBase);
26
- }
27
- Base() {
28
- return this.ids[this.ids.length - 1] ?? this.schema;
29
- }
30
- // ----------------------------------------------------------------
31
- // Stack
32
- // ----------------------------------------------------------------
33
- Push(schema) {
34
- if (!Schema.IsSchemaObject(schema))
35
- return;
36
- if (Schema.IsId(schema)) {
37
- this.ids.push(schema);
38
- __classPrivateFieldGet(this, _Stack_instances, "m", _Stack_PushResourceAnchors).call(this, schema);
39
- }
40
- if (Schema.IsAnchor(schema))
41
- this.anchors.push(schema);
42
- if (Schema.IsRecursiveAnchorTrue(schema))
43
- this.recursiveAnchors.push(schema);
44
- if (Schema.IsDynamicAnchor(schema))
45
- this.dynamicAnchors.push(schema);
46
- }
47
- Pop(schema) {
48
- if (!Schema.IsSchemaObject(schema))
49
- return;
50
- if (Schema.IsId(schema)) {
51
- this.ids.pop();
52
- __classPrivateFieldGet(this, _Stack_instances, "m", _Stack_PopResourceAnchors).call(this, schema);
53
- }
54
- if (Schema.IsAnchor(schema))
55
- this.anchors.pop();
56
- if (Schema.IsRecursiveAnchorTrue(schema))
57
- this.recursiveAnchors.pop();
58
- if (Schema.IsDynamicAnchor(schema))
59
- this.dynamicAnchors.pop();
60
- }
61
- // ----------------------------------------------------------------
62
- // Ref
63
- // ----------------------------------------------------------------
64
- Ref(ref) {
65
- const root = this.schema;
66
- return !ref.$ref.startsWith('#')
67
- ? Resolve.Ref(this.context, root, ref.$ref)
68
- : Resolve.Ref(this.context, this.Base(), ref.$ref);
69
- }
70
- // ----------------------------------------------------------------
71
- // RecursiveRef
72
- // ----------------------------------------------------------------
73
- RecursiveRef(recursiveRef) {
74
- return Schema.IsRecursiveAnchorTrue(this.Base())
75
- ? Resolve.Ref(this.context, this.recursiveAnchors[0], recursiveRef.$recursiveRef)
76
- : Resolve.Ref(this.context, this.Base(), recursiveRef.$recursiveRef);
77
- }
78
- // ----------------------------------------------------------------
79
- // DynamicRef
80
- // ----------------------------------------------------------------
81
- DynamicRef(dynamicRef) {
82
- const root = this.schema;
83
- return Resolve.DynamicRef(this.context, root, this.Base(), dynamicRef, this.dynamicAnchors);
84
- }
85
- }
86
- _Stack_instances = new WeakSet(), _Stack_PushResourceAnchors = function _Stack_PushResourceAnchors(schema, isRoot = true) {
87
- if (!Schema.IsSchemaObject(schema))
88
- return;
89
- const current = schema;
90
- if (!isRoot && Schema.IsId(current))
91
- return;
92
- if (!isRoot && Schema.IsDynamicAnchor(current))
93
- this.dynamicAnchors.push(current);
94
- for (const key of G.Keys(current))
95
- __classPrivateFieldGet(this, _Stack_instances, "m", _Stack_PushResourceAnchors).call(this, current[key], false);
96
- }, _Stack_PopResourceAnchors = function _Stack_PopResourceAnchors(schema, isRoot = true) {
3
+ // ------------------------------------------------------------------
4
+ // DefaultUri
5
+ // ------------------------------------------------------------------
6
+ export const DefaultUri = 'urn:typebox:root';
7
+ // ------------------------------------------------------------------
8
+ // NextUri
9
+ //
10
+ // Resolves a relative or absolute ref against a base URI or URN.
11
+ // Hierarchical bases (http, https, etc.) resolve using the normal
12
+ // URL rules. URN bases have no path to resolve against, so the ref
13
+ // is appended after the base's last ':' segment instead.
14
+ // ------------------------------------------------------------------
15
+ export function NextUri(ref, base) {
16
+ return (URL.canParse(ref, base)) ? new URL(ref, base) : Guard.IsEqual(base, DefaultUri) ? new URL(`${base}:${ref}`) : new URL(`${base.slice(0, base.lastIndexOf(':'))}:${ref}`);
17
+ }
18
+ // ------------------------------------------------------------------
19
+ // Stack
20
+ //
21
+ // Creates the root frame a schema traversal starts from.
22
+ // ------------------------------------------------------------------
23
+ export function Stack(context, schema) {
24
+ const base = Schema.IsSchemaObject(schema) && Schema.IsId(schema) ? NextUri(schema.$id, DefaultUri).href : DefaultUri;
25
+ return {
26
+ context,
27
+ schema,
28
+ lexicalSchema: schema,
29
+ lexicalBase: base,
30
+ resourceBase: base,
31
+ referenceBase: base,
32
+ ids: [],
33
+ useResourceBaseForReference: true,
34
+ recursiveAnchor: undefined,
35
+ dynamicAnchors: [],
36
+ resourceEntries: new Map(),
37
+ pendingResource: true,
38
+ enteredResource: false
39
+ };
40
+ }
41
+ // ------------------------------------------------------------------
42
+ // RegisterResourceAnchors
43
+ //
44
+ // Collects $dynamicAnchor schemas reachable from a resource root,
45
+ // stopping at nested $id boundaries which own their own anchors.
46
+ // ------------------------------------------------------------------
47
+ function RegisterResourceAnchors(anchors, schema, isRoot = true) {
48
+ if (Schema.IsSchemaBoolean(schema))
49
+ return anchors;
50
+ if (Array.isArray(schema))
51
+ return schema.reduce((result, item) => RegisterResourceAnchors(result, item, false), anchors);
97
52
  if (!Schema.IsSchemaObject(schema))
98
- return;
99
- const current = schema;
100
- if (!isRoot && Schema.IsId(current))
101
- return;
102
- if (!isRoot && Schema.IsDynamicAnchor(current))
103
- this.dynamicAnchors.pop();
104
- for (const key of G.Keys(current))
105
- __classPrivateFieldGet(this, _Stack_instances, "m", _Stack_PopResourceAnchors).call(this, current[key], false);
106
- };
53
+ return anchors;
54
+ if (!isRoot && Schema.IsId(schema))
55
+ return anchors;
56
+ const next = !isRoot && Schema.IsDynamicAnchor(schema) ? [...anchors, schema] : anchors;
57
+ return Object.keys(schema).reduce((result, key) => RegisterResourceAnchors(result, schema[key], false), next);
58
+ }
59
+ // ------------------------------------------------------------------
60
+ // ResourceEntry
61
+ //
62
+ // A ref crossing (Resolve.Ref) may have marked `schema` as the entry
63
+ // point of a retrieved/legacy resource. If so the frame resets to
64
+ // that resource's base/root instead of chaining onto the parent.
65
+ // ------------------------------------------------------------------
66
+ function ResourceEntry(stack, schema) {
67
+ return stack.resourceEntries.get(schema);
68
+ }
69
+ function NextEnteredResource(stack, schema) {
70
+ return stack.enteredResource || ResourceEntry(stack, schema) !== undefined;
71
+ }
72
+ // ------------------------------------------------------------------
73
+ // NextStack
74
+ //
75
+ // Each Next* helper below computes one XStack field in isolation from
76
+ // the previous frame and the schema being pushed.
77
+ // ------------------------------------------------------------------
78
+ function IsRelativeId(schema) {
79
+ return !/^[A-Za-z][A-Za-z0-9+.-]*:/.test(schema.$id);
80
+ }
81
+ function NextIds(stack, schema) {
82
+ return Schema.IsId(schema) ? [...stack.ids, schema] : stack.ids;
83
+ }
84
+ function NextRecursiveAnchor(stack, schema) {
85
+ return stack.recursiveAnchor ?? (Schema.IsRecursiveAnchorTrue(schema) ? schema : undefined);
86
+ }
87
+ function NextDynamicAnchors(stack, schema) {
88
+ const registered = Schema.IsId(schema) ? RegisterResourceAnchors(stack.dynamicAnchors, schema) : stack.dynamicAnchors;
89
+ return Schema.IsDynamicAnchor(schema) ? [...registered, schema] : registered;
90
+ }
91
+ function NextPendingResource(stack, schema) {
92
+ return Schema.IsId(schema) ? false : stack.pendingResource;
93
+ }
94
+ function NextLexicalBase(stack, schema) {
95
+ const entry = ResourceEntry(stack, schema);
96
+ if (entry)
97
+ return entry.base;
98
+ return Schema.IsId(schema) ? NextUri(schema.$id, stack.lexicalBase).href : stack.lexicalBase;
99
+ }
100
+ function NextResourceBase(stack, schema) {
101
+ const entry = ResourceEntry(stack, schema);
102
+ if (entry)
103
+ return entry.base;
104
+ return (Schema.IsId(schema) && stack.pendingResource) ? NextUri(schema.$id, stack.resourceBase).href : stack.resourceBase;
105
+ }
106
+ function NextUseResourceBaseForReference(stack, schema) {
107
+ return Schema.IsId(schema) ? !IsRelativeId(schema) : stack.useResourceBaseForReference;
108
+ }
109
+ function NextReferenceBase(stack, schema) {
110
+ const isRetrieved = NextEnteredResource(stack, schema);
111
+ const useResourceBaseForReference = NextUseResourceBaseForReference(stack, schema);
112
+ return (isRetrieved || useResourceBaseForReference) ? NextResourceBase(stack, schema) : NextLexicalBase(stack, schema);
113
+ }
114
+ function NextLexicalSchema(stack, schema) {
115
+ const entry = ResourceEntry(stack, schema);
116
+ if (entry)
117
+ return entry.root;
118
+ return Schema.IsId(schema) ? schema : stack.lexicalSchema;
119
+ }
120
+ // ------------------------------------------------------------------
121
+ // NextStack | HasStackKeywords (Optimization)
122
+ //
123
+ // HasStackKeywords narrows schema to an object and checks whether it
124
+ // carries $id, $dynamicAnchor, a first-seen $recursiveAnchor, or a
125
+ // resource-entry point. When none apply, NextStack skips the frame
126
+ // rebuild and returns the parent stack as-is.
127
+ // ------------------------------------------------------------------
128
+ function HasStackKeywords(stack, schema) {
129
+ return Schema.IsSchemaObject(schema) && (Schema.IsId(schema) ||
130
+ Schema.IsDynamicAnchor(schema) ||
131
+ (Guard.IsUndefined(stack.recursiveAnchor) && Schema.IsRecursiveAnchorTrue(schema)) ||
132
+ !Guard.IsUndefined(ResourceEntry(stack, schema)));
133
+ }
134
+ export function NextStack(stack, schema) {
135
+ return HasStackKeywords(stack, schema)
136
+ ? {
137
+ ...stack,
138
+ ids: NextIds(stack, schema),
139
+ dynamicAnchors: NextDynamicAnchors(stack, schema),
140
+ recursiveAnchor: NextRecursiveAnchor(stack, schema),
141
+ pendingResource: NextPendingResource(stack, schema),
142
+ lexicalBase: NextLexicalBase(stack, schema),
143
+ resourceBase: NextResourceBase(stack, schema),
144
+ useResourceBaseForReference: NextUseResourceBaseForReference(stack, schema),
145
+ referenceBase: NextReferenceBase(stack, schema),
146
+ lexicalSchema: NextLexicalSchema(stack, schema),
147
+ enteredResource: NextEnteredResource(stack, schema)
148
+ }
149
+ : stack;
150
+ }
@@ -1,6 +1,6 @@
1
1
  import * as Schema from '../types/index.mjs';
2
- import { Stack } from './_stack.mjs';
2
+ import * as Stack from './_stack.mjs';
3
3
  import { BuildContext, CheckContext, ErrorContext } from './_context.mjs';
4
- export declare function BuildAdditionalItems(stack: Stack, context: BuildContext, schema: Schema.XAdditionalItems, value: string): string;
5
- export declare function CheckAdditionalItems(stack: Stack, context: CheckContext, schema: Schema.XAdditionalItems, value: unknown[]): boolean;
6
- export declare function ErrorAdditionalItems(stack: Stack, context: ErrorContext, schemaPath: string, instancePath: string, schema: Schema.XAdditionalItems, value: unknown[]): boolean;
4
+ export declare function BuildAdditionalItems(stack: Stack.XStack, context: BuildContext, schema: Schema.XAdditionalItems, value: string): string;
5
+ export declare function CheckAdditionalItems(stack: Stack.XStack, context: CheckContext, schema: Schema.XAdditionalItems, value: unknown[]): boolean;
6
+ export declare function ErrorAdditionalItems(stack: Stack.XStack, context: ErrorContext, schemaPath: string, instancePath: string, schema: Schema.XAdditionalItems, value: unknown[]): boolean;