typebox 1.0.43 → 1.0.45

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 (130) hide show
  1. package/build/schema/build.d.mts +6 -6
  2. package/build/schema/build.mjs +6 -4
  3. package/build/schema/check.d.mts +3 -3
  4. package/build/schema/check.mjs +4 -2
  5. package/build/schema/engine/_context.d.mts +6 -16
  6. package/build/schema/engine/_context.mjs +11 -34
  7. package/build/schema/engine/_externals.d.mts +1 -1
  8. package/build/schema/engine/_externals.mjs +2 -2
  9. package/build/schema/engine/_functions.d.mts +3 -2
  10. package/build/schema/engine/_functions.mjs +6 -5
  11. package/build/schema/engine/_guard.d.mts +5 -4
  12. package/build/schema/engine/_guard.mjs +5 -5
  13. package/build/schema/engine/_reducer.d.mts +2 -1
  14. package/build/schema/engine/_reducer.mjs +3 -3
  15. package/build/schema/engine/_refine.d.mts +4 -3
  16. package/build/schema/engine/_refine.mjs +4 -4
  17. package/build/schema/engine/_stack.d.mts +18 -0
  18. package/build/schema/engine/_stack.mjs +73 -0
  19. package/build/schema/engine/_unique.d.mts +2 -0
  20. package/build/schema/engine/_unique.mjs +5 -0
  21. package/build/schema/engine/additionalItems.d.mts +5 -4
  22. package/build/schema/engine/additionalItems.mjs +15 -13
  23. package/build/schema/engine/additionalProperties.d.mts +5 -4
  24. package/build/schema/engine/additionalProperties.mjs +19 -16
  25. package/build/schema/engine/allOf.d.mts +5 -4
  26. package/build/schema/engine/allOf.mjs +15 -15
  27. package/build/schema/engine/anyOf.d.mts +5 -4
  28. package/build/schema/engine/anyOf.mjs +15 -15
  29. package/build/schema/engine/boolean.d.mts +4 -3
  30. package/build/schema/engine/boolean.mjs +4 -4
  31. package/build/schema/engine/const.d.mts +5 -4
  32. package/build/schema/engine/const.mjs +6 -6
  33. package/build/schema/engine/contains.d.mts +5 -4
  34. package/build/schema/engine/contains.mjs +10 -8
  35. package/build/schema/engine/dependencies.d.mts +5 -4
  36. package/build/schema/engine/dependencies.mjs +6 -6
  37. package/build/schema/engine/dependentRequired.d.mts +5 -4
  38. package/build/schema/engine/dependentRequired.mjs +3 -3
  39. package/build/schema/engine/dependentSchemas.d.mts +5 -4
  40. package/build/schema/engine/dependentSchemas.mjs +6 -6
  41. package/build/schema/engine/enum.d.mts +5 -4
  42. package/build/schema/engine/enum.mjs +6 -6
  43. package/build/schema/engine/exclusiveMaximum.d.mts +5 -4
  44. package/build/schema/engine/exclusiveMaximum.mjs +4 -4
  45. package/build/schema/engine/exclusiveMinimum.d.mts +5 -4
  46. package/build/schema/engine/exclusiveMinimum.mjs +4 -4
  47. package/build/schema/engine/format.d.mts +5 -4
  48. package/build/schema/engine/format.mjs +5 -5
  49. package/build/schema/engine/if.d.mts +5 -4
  50. package/build/schema/engine/if.mjs +19 -19
  51. package/build/schema/engine/index.d.mts +12 -11
  52. package/build/schema/engine/index.mjs +13 -12
  53. package/build/schema/engine/items.d.mts +5 -4
  54. package/build/schema/engine/items.mjs +22 -22
  55. package/build/schema/engine/maxContains.d.mts +5 -4
  56. package/build/schema/engine/maxContains.mjs +11 -9
  57. package/build/schema/engine/maxItems.d.mts +5 -4
  58. package/build/schema/engine/maxItems.mjs +4 -4
  59. package/build/schema/engine/maxLength.d.mts +5 -4
  60. package/build/schema/engine/maxLength.mjs +4 -4
  61. package/build/schema/engine/maxProperties.d.mts +5 -4
  62. package/build/schema/engine/maxProperties.mjs +4 -4
  63. package/build/schema/engine/maximum.d.mts +5 -4
  64. package/build/schema/engine/maximum.mjs +4 -4
  65. package/build/schema/engine/minContains.d.mts +5 -4
  66. package/build/schema/engine/minContains.mjs +10 -8
  67. package/build/schema/engine/minItems.d.mts +5 -4
  68. package/build/schema/engine/minItems.mjs +4 -4
  69. package/build/schema/engine/minLength.d.mts +5 -4
  70. package/build/schema/engine/minLength.mjs +4 -4
  71. package/build/schema/engine/minProperties.d.mts +5 -4
  72. package/build/schema/engine/minProperties.mjs +4 -4
  73. package/build/schema/engine/minimum.d.mts +5 -4
  74. package/build/schema/engine/minimum.mjs +4 -4
  75. package/build/schema/engine/multipleOf.d.mts +5 -4
  76. package/build/schema/engine/multipleOf.mjs +4 -4
  77. package/build/schema/engine/not.d.mts +5 -4
  78. package/build/schema/engine/not.mjs +14 -13
  79. package/build/schema/engine/oneOf.d.mts +5 -4
  80. package/build/schema/engine/oneOf.mjs +14 -14
  81. package/build/schema/engine/pattern.d.mts +5 -4
  82. package/build/schema/engine/pattern.mjs +6 -6
  83. package/build/schema/engine/patternProperties.d.mts +5 -4
  84. package/build/schema/engine/patternProperties.mjs +14 -12
  85. package/build/schema/engine/prefixItems.d.mts +5 -4
  86. package/build/schema/engine/prefixItems.mjs +6 -6
  87. package/build/schema/engine/properties.d.mts +5 -4
  88. package/build/schema/engine/properties.mjs +10 -10
  89. package/build/schema/engine/propertyNames.d.mts +5 -4
  90. package/build/schema/engine/propertyNames.mjs +11 -9
  91. package/build/schema/engine/recursiveRef.d.mts +5 -4
  92. package/build/schema/engine/recursiveRef.mjs +11 -22
  93. package/build/schema/engine/ref.d.mts +5 -4
  94. package/build/schema/engine/ref.mjs +44 -29
  95. package/build/schema/engine/required.d.mts +5 -4
  96. package/build/schema/engine/required.mjs +3 -3
  97. package/build/schema/engine/schema.d.mts +5 -4
  98. package/build/schema/engine/schema.mjs +216 -207
  99. package/build/schema/engine/type.d.mts +5 -4
  100. package/build/schema/engine/type.mjs +12 -12
  101. package/build/schema/engine/unevaluatedItems.d.mts +5 -4
  102. package/build/schema/engine/unevaluatedItems.mjs +15 -13
  103. package/build/schema/engine/unevaluatedProperties.d.mts +5 -4
  104. package/build/schema/engine/unevaluatedProperties.mjs +15 -13
  105. package/build/schema/engine/uniqueItems.d.mts +5 -4
  106. package/build/schema/engine/uniqueItems.mjs +3 -3
  107. package/build/schema/errors.d.mts +3 -3
  108. package/build/schema/errors.mjs +4 -4
  109. package/build/schema/resolve/index.d.mts +1 -0
  110. package/build/schema/resolve/index.mjs +1 -0
  111. package/build/schema/resolve/ref.d.mts +2 -0
  112. package/build/schema/{resolver → resolve}/ref.mjs +43 -45
  113. package/build/schema/resolve/resolve.d.mts +1 -0
  114. package/build/schema/resolve/resolve.mjs +1 -0
  115. package/build/schema/schema.d.mts +1 -1
  116. package/build/schema/schema.mjs +1 -1
  117. package/build/schema/types/recursiveAnchor.d.mts +4 -0
  118. package/build/schema/types/recursiveAnchor.mjs +7 -0
  119. package/build/type/types/_codec.d.mts +1 -1
  120. package/build/type/types/_codec.mjs +1 -0
  121. package/build/type/types/static.d.mts +4 -4
  122. package/package.json +1 -1
  123. package/readme.md +1 -76
  124. package/build/schema/resolver/index.d.mts +0 -1
  125. package/build/schema/resolver/index.mjs +0 -1
  126. package/build/schema/resolver/recursiveRef.d.mts +0 -2
  127. package/build/schema/resolver/recursiveRef.mjs +0 -47
  128. package/build/schema/resolver/ref.d.mts +0 -1
  129. package/build/schema/resolver/resolver.d.mts +0 -2
  130. package/build/schema/resolver/resolver.mjs +0 -2
@@ -1,5 +1,5 @@
1
1
  import * as Engine from './engine/index.mjs';
2
- import { type XSchema } from './types/index.mjs';
2
+ import * as Schema from './types/index.mjs';
3
3
  export type CheckFunction = (value: unknown) => boolean;
4
4
  export interface EvaluateResult {
5
5
  IsEvaluated: boolean;
@@ -13,11 +13,11 @@ export declare class BuildResult {
13
13
  private readonly functions;
14
14
  private readonly call;
15
15
  private readonly useUnevaluated;
16
- constructor(context: Record<PropertyKey, XSchema>, schema: XSchema, external: Engine.TExternal, functions: string[], call: string, useUnevaluated: boolean);
16
+ constructor(context: Record<PropertyKey, Schema.XSchema>, schema: Schema.XSchema, external: Engine.TExternal, functions: string[], call: string, useUnevaluated: boolean);
17
17
  /** Returns the Context used for this build */
18
- Context(): Record<PropertyKey, XSchema>;
18
+ Context(): Record<PropertyKey, Schema.XSchema>;
19
19
  /** Returns the Schema used for this build */
20
- Schema(): XSchema;
20
+ Schema(): Schema.XSchema;
21
21
  /** Returns true if this build requires a Unevaluated context */
22
22
  UseUnevaluated(): boolean;
23
23
  /** Returns external variables */
@@ -30,6 +30,6 @@ export declare class BuildResult {
30
30
  Evaluate(): EvaluateResult;
31
31
  }
32
32
  /** Builds a schema into a optimized runtime validator */
33
- export declare function Build(schema: XSchema): BuildResult;
33
+ export declare function Build(schema: Schema.XSchema): BuildResult;
34
34
  /** Builds a schema into a optimized runtime validator */
35
- export declare function Build(context: Record<PropertyKey, XSchema>, schema: XSchema): BuildResult;
35
+ export declare function Build(context: Record<PropertyKey, Schema.XSchema>, schema: Schema.XSchema): BuildResult;
@@ -27,8 +27,9 @@ function CreateEvaluatedCheck(build, code) {
27
27
  // CreateDynamicCheck
28
28
  // ------------------------------------------------------------------
29
29
  function CreateDynamicCheck(build) {
30
- const context = new Engine.CheckContext(build.Context(), build.Schema());
31
- return (value) => Engine.CheckSchema(context, build.Schema(), value);
30
+ const stack = new Engine.Stack(build.Context(), build.Schema());
31
+ const context = new Engine.CheckContext();
32
+ return (value) => Engine.CheckSchema(stack, context, build.Schema(), value);
32
33
  }
33
34
  // ------------------------------------------------------------------
34
35
  // CreateCheck
@@ -89,8 +90,9 @@ export function Build(...args) {
89
90
  });
90
91
  Engine.ResetExternal();
91
92
  Engine.ResetFunctions();
92
- const build = new Engine.BuildContext(context, schema, Engine.HasUnevaluated(context, schema));
93
- const call = Engine.CreateFunction(build, schema, 'value');
93
+ const stack = new Engine.Stack(context, schema);
94
+ const build = new Engine.BuildContext(Engine.HasUnevaluated(context, schema));
95
+ const call = Engine.CreateFunction(stack, build, schema, 'value');
94
96
  const functions = Engine.GetFunctions();
95
97
  const externals = Engine.GetExternal();
96
98
  return new BuildResult(context, schema, externals, functions, call, build.UseUnevaluated());
@@ -1,5 +1,5 @@
1
- import { XSchema } from './types/index.mjs';
1
+ import * as Schema from './types/index.mjs';
2
2
  /** Checks a value against the provided schema */
3
- export declare function Check(schema: XSchema, value: unknown): boolean;
3
+ export declare function Check(schema: Schema.XSchema, value: unknown): boolean;
4
4
  /** Checks a value against the provided schema */
5
- export declare function Check(context: Record<PropertyKey, XSchema>, schema: XSchema, value: unknown): boolean;
5
+ export declare function Check(context: Record<PropertyKey, Schema.XSchema>, schema: Schema.XSchema, value: unknown): boolean;
@@ -1,12 +1,14 @@
1
1
  // deno-fmt-ignore-file
2
2
  // deno-lint-ignore-file
3
3
  import { Arguments } from '../system/arguments/index.mjs';
4
- import { CheckSchema, CheckContext } from './engine/index.mjs';
4
+ import * as Engine from './engine/index.mjs';
5
5
  /** Checks a value against the provided schema */
6
6
  export function Check(...args) {
7
7
  const [context, schema, value] = Arguments.Match(args, {
8
8
  3: (context, schema, value) => [context, schema, value],
9
9
  2: (schema, value) => [{}, schema, value]
10
10
  });
11
- return CheckSchema(new CheckContext(context, schema), schema, value);
11
+ const stack = new Engine.Stack(context, schema);
12
+ const checkContext = new Engine.CheckContext();
13
+ return Engine.CheckSchema(stack, checkContext, schema, value);
12
14
  }
@@ -1,42 +1,32 @@
1
- import * as S from '../types/index.mjs';
2
1
  import type { TValidationError } from '../../error/index.mjs';
3
2
  export declare function HasUnevaluated(context: Record<PropertyKey, unknown>, schema: unknown): boolean;
4
- export declare class BaseContext {
5
- protected readonly context: Record<PropertyKey, S.XSchema>;
6
- protected readonly schema: S.XSchema;
7
- constructor(context: Record<PropertyKey, S.XSchema>, schema: S.XSchema);
8
- GetContext(): Record<PropertyKey, object | boolean>;
9
- GetSchema(): object | boolean;
10
- }
11
- export declare class BuildContext extends BaseContext {
3
+ export declare class BuildContext {
12
4
  private readonly hasUnevaluated;
13
- constructor(context: Record<PropertyKey, S.XSchema>, schema: S.XSchema, hasUnevaluated: boolean);
5
+ constructor(hasUnevaluated: boolean);
14
6
  UseUnevaluated(): boolean;
15
7
  AddIndex(index: string): string;
16
8
  AddKey(key: string): string;
17
- Clone(): string;
18
9
  Merge(results: string): string;
19
10
  }
20
- export declare class CheckContext extends BaseContext {
11
+ export declare class CheckContext {
21
12
  private readonly indices;
22
13
  private readonly keys;
23
- constructor(context: Record<PropertyKey, S.XSchema>, schema: S.XSchema);
14
+ constructor();
24
15
  AddIndex(index: number): true;
25
16
  AddKey(key: string): true;
26
17
  GetIndices(): Set<number>;
27
18
  GetKeys(): Set<string>;
28
- Clone(): CheckContext;
29
19
  Merge(results: CheckContext[]): true;
30
20
  }
31
21
  export type ErrorContextCallback = (error: TValidationError) => unknown;
32
22
  export declare class ErrorContext extends CheckContext {
33
23
  private readonly callback;
34
- constructor(context: Record<PropertyKey, S.XSchema>, schema: S.XSchema, callback: ErrorContextCallback);
24
+ constructor(callback: ErrorContextCallback);
35
25
  AddError(error: TValidationError): false;
36
26
  }
37
27
  export declare class AccumulatedErrorContext extends ErrorContext {
38
28
  private readonly errors;
39
- constructor(context: Record<PropertyKey, S.XSchema>, schema: S.XSchema);
29
+ constructor();
40
30
  AddError(error: TValidationError): false;
41
31
  GetErrors(): TValidationError[];
42
32
  }
@@ -1,12 +1,12 @@
1
1
  // deno-fmt-ignore-file
2
- import * as S from '../types/index.mjs';
2
+ import * as Schema from '../types/index.mjs';
3
3
  import { Guard as G, EmitGuard as E } from '../../guard/index.mjs';
4
4
  // ------------------------------------------------------------------
5
5
  // HasUnevaluated
6
6
  // ------------------------------------------------------------------
7
7
  function HasUnevaluatedFromObject(value) {
8
- return (S.IsUnevaluatedItems(value)
9
- || S.IsUnevaluatedProperties(value)
8
+ return (Schema.IsUnevaluatedItems(value)
9
+ || Schema.IsUnevaluatedProperties(value)
10
10
  || G.Keys(value).some(key => HasUnevaluatedFromUnknown(value[key])));
11
11
  }
12
12
  function HasUnevaluatedFromArray(value) {
@@ -21,26 +21,10 @@ export function HasUnevaluated(context, schema) {
21
21
  return HasUnevaluatedFromUnknown(schema) || G.Keys(context).some(key => HasUnevaluatedFromUnknown(context[key]));
22
22
  }
23
23
  // ------------------------------------------------------------------
24
- // BaseContext
25
- // ------------------------------------------------------------------
26
- export class BaseContext {
27
- constructor(context, schema) {
28
- this.context = context;
29
- this.schema = schema;
30
- }
31
- GetContext() {
32
- return this.context;
33
- }
34
- GetSchema() {
35
- return this.schema;
36
- }
37
- }
38
- // ------------------------------------------------------------------
39
24
  // BuildContext
40
25
  // ------------------------------------------------------------------
41
- export class BuildContext extends BaseContext {
42
- constructor(context, schema, hasUnevaluated) {
43
- super(context, schema);
26
+ export class BuildContext {
27
+ constructor(hasUnevaluated) {
44
28
  this.hasUnevaluated = hasUnevaluated;
45
29
  }
46
30
  UseUnevaluated() {
@@ -52,9 +36,6 @@ export class BuildContext extends BaseContext {
52
36
  AddKey(key) {
53
37
  return E.Call(E.Member('context', 'AddKey'), [key]);
54
38
  }
55
- Clone() {
56
- return E.Call(E.Member('context', 'Clone'), []);
57
- }
58
39
  Merge(results) {
59
40
  return E.Call(E.Member('context', 'Merge'), [results]);
60
41
  }
@@ -62,9 +43,8 @@ export class BuildContext extends BaseContext {
62
43
  // ------------------------------------------------------------------
63
44
  // CheckContext
64
45
  // ------------------------------------------------------------------
65
- export class CheckContext extends BaseContext {
66
- constructor(context, schema) {
67
- super(context, schema);
46
+ export class CheckContext {
47
+ constructor() {
68
48
  this.indices = new Set();
69
49
  this.keys = new Set();
70
50
  }
@@ -82,9 +62,6 @@ export class CheckContext extends BaseContext {
82
62
  GetKeys() {
83
63
  return this.keys;
84
64
  }
85
- Clone() {
86
- return new CheckContext(this.context, this.schema);
87
- }
88
65
  Merge(results) {
89
66
  for (const context of results) {
90
67
  context.indices.forEach(value => this.indices.add(value));
@@ -94,8 +71,8 @@ export class CheckContext extends BaseContext {
94
71
  }
95
72
  }
96
73
  export class ErrorContext extends CheckContext {
97
- constructor(context, schema, callback) {
98
- super(context, schema);
74
+ constructor(callback) {
75
+ super();
99
76
  this.callback = callback;
100
77
  }
101
78
  AddError(error) {
@@ -107,8 +84,8 @@ export class ErrorContext extends CheckContext {
107
84
  // AccumulatedErrorContext
108
85
  // ------------------------------------------------------------------
109
86
  export class AccumulatedErrorContext extends ErrorContext {
110
- constructor(context, schema) {
111
- super(context, schema, error => this.errors.push(error));
87
+ constructor() {
88
+ super(error => this.errors.push(error));
112
89
  this.errors = [];
113
90
  }
114
91
  AddError(error) {
@@ -5,5 +5,5 @@ export interface TExternal {
5
5
  variables: unknown[];
6
6
  }
7
7
  export declare function ResetExternal(): void;
8
- export declare function CreateExternalVariable(value: unknown): string;
8
+ export declare function CreateVariable(value: unknown): string;
9
9
  export declare function GetExternal(): TExternal;
@@ -19,9 +19,9 @@ export function ResetExternal() {
19
19
  resetCount += 1;
20
20
  }
21
21
  // ------------------------------------------------------------------
22
- // CreateExternalVariable
22
+ // CreateVariable
23
23
  // ------------------------------------------------------------------
24
- export function CreateExternalVariable(value) {
24
+ export function CreateVariable(value) {
25
25
  const call = `${state.identifier}[${state.variables.length}]`;
26
26
  state.variables.push(value);
27
27
  return call;
@@ -1,5 +1,6 @@
1
- import * as S from '../types/index.mjs';
1
+ import * as Schema from '../types/index.mjs';
2
+ import { Stack } from './_stack.mjs';
2
3
  import { BuildContext } from './_context.mjs';
3
4
  export declare function ResetFunctions(): void;
4
5
  export declare function GetFunctions(): string[];
5
- export declare function CreateFunction(context: BuildContext, schema: S.XSchema, value: string): string;
6
+ export declare function CreateFunction(stack: Stack, context: BuildContext, schema: Schema.XSchema, value: string): string;
@@ -1,4 +1,5 @@
1
1
  // deno-fmt-ignore-file
2
+ import * as Schema from '../types/index.mjs';
2
3
  import { Hashing } from '../../system/hashing/index.mjs';
3
4
  import { EmitGuard as E } from '../../guard/index.mjs';
4
5
  import { BuildSchema } from './schema.mjs';
@@ -14,8 +15,8 @@ function CreateCallExpression(context, schema, hash, value) {
14
15
  // ------------------------------------------------------------------
15
16
  // CreateFunctionExpression
16
17
  // ------------------------------------------------------------------
17
- function CreateFunctionExpression(context, schema, hash) {
18
- const expression = BuildSchema(context, schema, 'value');
18
+ function CreateFunctionExpression(stack, context, schema, hash) {
19
+ const expression = BuildSchema(stack, context, schema, 'value');
19
20
  return context.UseUnevaluated()
20
21
  ? E.ConstDeclaration(`check_${hash}`, E.ArrowFunction(['context', 'value'], expression))
21
22
  : E.ConstDeclaration(`check_${hash}`, E.ArrowFunction(['value'], expression));
@@ -35,12 +36,12 @@ export function GetFunctions() {
35
36
  // ------------------------------------------------------------------
36
37
  // CreateFunction
37
38
  // ------------------------------------------------------------------
38
- export function CreateFunction(context, schema, value) {
39
- const hash = Hashing.Hash(schema);
39
+ export function CreateFunction(stack, context, schema, value) {
40
+ const hash = Schema.IsSchemaObject(schema) ? Hashing.Hash({ __baseURL: stack.BaseURL().href, ...schema }) : Hashing.Hash(schema);
40
41
  const call = CreateCallExpression(context, schema, hash, value);
41
42
  if (functions.has(hash))
42
43
  return call;
43
44
  functions.set(hash, '');
44
- functions.set(hash, CreateFunctionExpression(context, schema, hash));
45
+ functions.set(hash, CreateFunctionExpression(stack, context, schema, hash));
45
46
  return call;
46
47
  }
@@ -1,5 +1,6 @@
1
- import * as S from '../types/index.mjs';
1
+ import * as Schema from '../types/index.mjs';
2
+ import { Stack } from './_stack.mjs';
2
3
  import { BuildContext, CheckContext, ErrorContext } from './_context.mjs';
3
- export declare function BuildGuard(context: BuildContext, schema: S.XGuard, value: string): string;
4
- export declare function CheckGuard(context: CheckContext, schema: S.XGuard, value: unknown): boolean;
5
- export declare function ErrorGuard(context: ErrorContext, schemaPath: string, instancePath: string, schema: S.XGuard, value: unknown): boolean;
4
+ export declare function BuildGuard(stack: Stack, context: BuildContext, schema: Schema.XGuard, value: string): string;
5
+ export declare function CheckGuard(stack: Stack, context: CheckContext, schema: Schema.XGuard, value: unknown): boolean;
6
+ export declare function ErrorGuard(stack: Stack, context: ErrorContext, schemaPath: string, instancePath: string, schema: Schema.XGuard, value: unknown): boolean;
@@ -1,22 +1,22 @@
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 } from '../../guard/index.mjs';
4
4
  // ------------------------------------------------------------------
5
5
  // Build
6
6
  // ------------------------------------------------------------------
7
- export function BuildGuard(context, schema, value) {
8
- return E.Call(E.Member(E.Member(V.CreateExternalVariable(schema), '~guard'), 'check'), [value]);
7
+ export function BuildGuard(stack, context, schema, value) {
8
+ return E.Call(E.Member(E.Member(Externals.CreateVariable(schema), '~guard'), 'check'), [value]);
9
9
  }
10
10
  // ------------------------------------------------------------------
11
11
  // Check
12
12
  // ------------------------------------------------------------------
13
- export function CheckGuard(context, schema, value) {
13
+ export function CheckGuard(stack, context, schema, value) {
14
14
  return schema['~guard'].check(value);
15
15
  }
16
16
  // ------------------------------------------------------------------
17
17
  // Error
18
18
  // ------------------------------------------------------------------
19
- export function ErrorGuard(context, schemaPath, instancePath, schema, value) {
19
+ export function ErrorGuard(stack, context, schemaPath, instancePath, schema, value) {
20
20
  return schema['~guard'].check(value) || context.AddError({
21
21
  keyword: '~guard',
22
22
  schemaPath,
@@ -1,3 +1,4 @@
1
1
  import * as S from '../types/index.mjs';
2
+ import { Stack } from './_stack.mjs';
2
3
  import { BuildContext } from './_context.mjs';
3
- export declare function Reducer(context: BuildContext, schemas: S.XSchema[], value: string, check: string): string;
4
+ export declare function Reducer(stack: Stack, context: BuildContext, schemas: S.XSchema[], value: string, check: string): string;
@@ -35,10 +35,10 @@ import { BuildSchema } from './schema.mjs';
35
35
  // })()
36
36
  //
37
37
  // ------------------------------------------------------------------
38
- export function Reducer(context, schemas, value, check) {
38
+ export function Reducer(stack, context, schemas, value, check) {
39
39
  const results = E.ConstDeclaration('results', '[]');
40
- const context_n = schemas.map((_schema, index) => E.ConstDeclaration(`context_${index}`, context.Clone()));
41
- const condition_n = schemas.map((schema, index) => E.ConstDeclaration(`condition_${index}`, E.Call(E.ArrowFunction(['context'], BuildSchema(context, schema, value)), [`context_${index}`])));
40
+ const context_n = schemas.map((_schema, index) => E.ConstDeclaration(`context_${index}`, E.New('CheckContext', [])));
41
+ const condition_n = schemas.map((schema, index) => E.ConstDeclaration(`condition_${index}`, E.Call(E.ArrowFunction(['context'], BuildSchema(stack, context, schema, value)), [`context_${index}`])));
42
42
  const checks = schemas.map((_schema, index) => E.If(`condition_${index}`, E.Call(E.Member('results', 'push'), [`context_${index}`])));
43
43
  const returns = E.Return(E.And(check, context.Merge('results')));
44
44
  return E.Call(E.ArrowFunction([], E.Statements([results, ...context_n, ...condition_n, ...checks, returns])), []);
@@ -1,5 +1,6 @@
1
1
  import * as S from '../types/index.mjs';
2
+ import { Stack } from './_stack.mjs';
2
3
  import { BuildContext, CheckContext, ErrorContext } from './_context.mjs';
3
- export declare function BuildRefine(context: BuildContext, schema: S.XRefine, value: string): string;
4
- export declare function CheckRefine(context: CheckContext, schema: S.XRefine, value: unknown): boolean;
5
- export declare function ErrorRefine(context: ErrorContext, schemaPath: string, instancePath: string, schema: S.XRefine, value: unknown): boolean;
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,20 +4,20 @@ import { EmitGuard as E, Guard as G } from '../../guard/index.mjs';
4
4
  // ------------------------------------------------------------------
5
5
  // Build
6
6
  // ------------------------------------------------------------------
7
- export function BuildRefine(context, schema, value) {
8
- const refinements = V.CreateExternalVariable(schema['~refine'].map((refinement) => refinement));
7
+ export function BuildRefine(stack, context, schema, value) {
8
+ const refinements = V.CreateVariable(schema['~refine'].map((refinement) => refinement));
9
9
  return E.Every(refinements, E.Constant(0), ['refinement', '_'], E.Call(E.Member('refinement', 'refine'), [value]));
10
10
  }
11
11
  // ------------------------------------------------------------------
12
12
  // Check
13
13
  // ------------------------------------------------------------------
14
- export function CheckRefine(context, schema, value) {
14
+ export function CheckRefine(stack, context, schema, value) {
15
15
  return G.Every(schema['~refine'], 0, (refinement, _) => refinement.refine(value));
16
16
  }
17
17
  // ------------------------------------------------------------------
18
18
  // Error
19
19
  // ------------------------------------------------------------------
20
- export function ErrorRefine(context, schemaPath, instancePath, schema, value) {
20
+ export function ErrorRefine(stack, context, schemaPath, instancePath, schema, value) {
21
21
  return G.EveryAll(schema['~refine'], 0, (refinement, index) => {
22
22
  return refinement.refine(value) || context.AddError({
23
23
  keyword: '~refine',
@@ -0,0 +1,18 @@
1
+ import * as Schema from '../types/index.mjs';
2
+ export declare class Stack {
3
+ private readonly context;
4
+ private readonly schema;
5
+ private readonly ids;
6
+ private readonly anchors;
7
+ private readonly recursiveAnchors;
8
+ private readonly dynamicAnchors;
9
+ constructor(context: Record<PropertyKey, Schema.XSchema>, schema: Schema.XSchema);
10
+ BaseURL(): URL;
11
+ Base(): Schema.XSchemaObject;
12
+ Push(schema: Schema.XSchema): void;
13
+ Pop(schema: Schema.XSchema): void;
14
+ private FromContext;
15
+ private FromRef;
16
+ Ref(ref: string): Schema.XSchema | undefined;
17
+ RecursiveRef(recursiveRef: string): Schema.XSchema | undefined;
18
+ }
@@ -0,0 +1,73 @@
1
+ // deno-fmt-ignore-file
2
+ import * as Schema from '../types/index.mjs';
3
+ import { Guard as G } from '../../guard/index.mjs';
4
+ import { Resolve } from '../resolve/index.mjs';
5
+ export class Stack {
6
+ constructor(context, schema) {
7
+ this.context = context;
8
+ this.schema = schema;
9
+ this.ids = [];
10
+ this.anchors = [];
11
+ this.recursiveAnchors = [];
12
+ this.dynamicAnchors = [];
13
+ }
14
+ // ----------------------------------------------------------------
15
+ // Base
16
+ // ----------------------------------------------------------------
17
+ BaseURL() {
18
+ return this.ids.reduce((result, schema) => new URL(schema.$id, result), new URL('root', 'memory://'));
19
+ }
20
+ Base() {
21
+ return this.ids[this.ids.length - 1] ?? this.schema;
22
+ }
23
+ // ----------------------------------------------------------------
24
+ // Stack
25
+ // ----------------------------------------------------------------
26
+ Push(schema) {
27
+ if (!Schema.IsSchemaObject(schema))
28
+ return;
29
+ if (Schema.IsId(schema))
30
+ this.ids.push(schema);
31
+ if (Schema.IsAnchor(schema))
32
+ this.anchors.push(schema);
33
+ if (Schema.IsRecursiveAnchorTrue(schema))
34
+ this.recursiveAnchors.push(schema);
35
+ if (Schema.IsDynamicAnchor(schema))
36
+ this.dynamicAnchors.push(schema);
37
+ }
38
+ Pop(schema) {
39
+ if (!Schema.IsSchemaObject(schema))
40
+ return;
41
+ if (Schema.IsId(schema))
42
+ this.ids.pop();
43
+ if (Schema.IsAnchor(schema))
44
+ this.anchors.pop();
45
+ if (Schema.IsRecursiveAnchorTrue(schema))
46
+ this.recursiveAnchors.pop();
47
+ if (Schema.IsDynamicAnchor(schema))
48
+ this.dynamicAnchors.pop();
49
+ }
50
+ // ----------------------------------------------------------------
51
+ // Ref
52
+ // ----------------------------------------------------------------
53
+ FromContext(ref) {
54
+ return G.HasPropertyKey(this.context, ref) ? this.context[ref] : undefined;
55
+ }
56
+ FromRef(ref) {
57
+ return !ref.startsWith('#')
58
+ ? Resolve.Ref(this.schema, ref)
59
+ : Resolve.Ref(this.Base(), ref);
60
+ }
61
+ Ref(ref) {
62
+ return this.FromContext(ref) ?? this.FromRef(ref);
63
+ }
64
+ // ----------------------------------------------------------------
65
+ // RecursiveRef
66
+ // ----------------------------------------------------------------
67
+ RecursiveRef(recursiveRef) {
68
+ if (Schema.IsRecursiveAnchorTrue(this.Base())) {
69
+ return Resolve.Ref(this.recursiveAnchors[0], recursiveRef);
70
+ }
71
+ return Resolve.Ref(this.Base(), recursiveRef);
72
+ }
73
+ }
@@ -0,0 +1,2 @@
1
+ /** Returns a Unique Variable Name */
2
+ export declare function Unique(): string;
@@ -0,0 +1,5 @@
1
+ let index = 0;
2
+ /** Returns a Unique Variable Name */
3
+ export function Unique() {
4
+ return `var_${index++}`;
5
+ }
@@ -1,5 +1,6 @@
1
- import * as S from '../types/index.mjs';
1
+ import * as Schema from '../types/index.mjs';
2
+ import { Stack } from './_stack.mjs';
2
3
  import { BuildContext, CheckContext, ErrorContext } from './_context.mjs';
3
- export declare function BuildAdditionalItems(context: BuildContext, schema: S.XAdditionalItems, value: string): string;
4
- export declare function CheckAdditionalItems(context: CheckContext, schema: S.XAdditionalItems, value: unknown[]): boolean;
5
- export declare function ErrorAdditionalItems(context: ErrorContext, schemaPath: string, instancePath: string, schema: S.XAdditionalItems, value: unknown[]): boolean;
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;
@@ -1,48 +1,50 @@
1
1
  // deno-fmt-ignore-file
2
- import * as S from '../types/index.mjs';
2
+ import * as Schema from '../types/index.mjs';
3
+ import { Unique } from './_unique.mjs';
3
4
  import { Guard as G, EmitGuard as E } from '../../guard/index.mjs';
4
5
  import { BuildSchema, CheckSchema, ErrorSchema } from './schema.mjs';
5
6
  // ------------------------------------------------------------------
6
7
  // Valid
7
8
  // ------------------------------------------------------------------
8
9
  function IsValid(schema) {
9
- return S.IsItems(schema) && G.IsArray(schema.items);
10
+ return Schema.IsItems(schema) && G.IsArray(schema.items);
10
11
  }
11
12
  // ------------------------------------------------------------------
12
13
  // Build
13
14
  // ------------------------------------------------------------------
14
- export function BuildAdditionalItems(context, schema, value) {
15
+ export function BuildAdditionalItems(stack, context, schema, value) {
15
16
  if (!IsValid(schema))
16
17
  return E.Constant(true);
17
- const isSchema = BuildSchema(context, schema.additionalItems, 'element');
18
- const isLength = E.IsLessThan('index', E.Constant(schema.items.length));
19
- const addIndex = context.AddIndex('index');
18
+ const [item, index] = [Unique(), Unique()];
19
+ const isSchema = BuildSchema(stack, context, schema.additionalItems, item);
20
+ const isLength = E.IsLessThan(index, E.Constant(schema.items.length));
21
+ const addIndex = context.AddIndex(index);
20
22
  const guarded = context.UseUnevaluated() ? E.Or(isLength, E.And(isSchema, addIndex)) : E.Or(isLength, isSchema);
21
- return E.Call(E.Member(value, 'every'), [E.ArrowFunction(['element', 'index'], guarded)]);
23
+ return E.Call(E.Member(value, 'every'), [E.ArrowFunction([item, index], guarded)]);
22
24
  }
23
25
  // ------------------------------------------------------------------
24
26
  // Check
25
27
  // ------------------------------------------------------------------
26
- export function CheckAdditionalItems(context, schema, value) {
28
+ export function CheckAdditionalItems(stack, context, schema, value) {
27
29
  if (!IsValid(schema))
28
30
  return true;
29
- const isAdditionalItems = value.every((element, index) => {
31
+ const isAdditionalItems = value.every((item, index) => {
30
32
  return G.IsLessThan(index, schema.items.length)
31
- || (CheckSchema(context, schema.additionalItems, element) && context.AddIndex(index));
33
+ || (CheckSchema(stack, context, schema.additionalItems, item) && context.AddIndex(index));
32
34
  });
33
35
  return isAdditionalItems;
34
36
  }
35
37
  // ------------------------------------------------------------------
36
38
  // Error
37
39
  // ------------------------------------------------------------------
38
- export function ErrorAdditionalItems(context, schemaPath, instancePath, schema, value) {
40
+ export function ErrorAdditionalItems(stack, context, schemaPath, instancePath, schema, value) {
39
41
  if (!IsValid(schema))
40
42
  return true;
41
- const isAdditionalItems = value.every((element, index) => {
43
+ const isAdditionalItems = value.every((item, index) => {
42
44
  const nextSchemaPath = `${schemaPath}/additionalItems`;
43
45
  const nextInstancePath = `${instancePath}/${index}`;
44
46
  return G.IsLessThan(index, schema.items.length) ||
45
- (ErrorSchema(context, nextSchemaPath, nextInstancePath, schema.additionalItems, element) && context.AddIndex(index));
47
+ (ErrorSchema(stack, context, nextSchemaPath, nextInstancePath, schema.additionalItems, item) && context.AddIndex(index));
46
48
  });
47
49
  return isAdditionalItems;
48
50
  }
@@ -1,8 +1,9 @@
1
1
  import * as S from '../types/index.mjs';
2
+ import { Stack } from './_stack.mjs';
2
3
  import { BuildContext, CheckContext, ErrorContext } from './_context.mjs';
3
4
  export declare function CanAdditionalPropertiesFast(context: BuildContext, schema: S.XAdditionalProperties, value: string): schema is S.XAdditionalProperties & S.XRequired;
4
5
  export declare function BuildAdditionalPropertiesFast(context: BuildContext, schema: S.XAdditionalProperties & S.XRequired, value: string): string;
5
- export declare function BuildAdditionalPropertiesStandard(context: BuildContext, schema: S.XAdditionalProperties, value: string): string;
6
- export declare function BuildAdditionalProperties(context: BuildContext, schema: S.XAdditionalProperties, value: string): string;
7
- export declare function CheckAdditionalProperties(context: CheckContext, schema: S.XAdditionalProperties, value: Record<PropertyKey, unknown>): boolean;
8
- export declare function ErrorAdditionalProperties(context: ErrorContext, schemaPath: string, instancePath: string, schema: S.XAdditionalProperties, value: Record<PropertyKey, unknown>): boolean;
6
+ export declare function BuildAdditionalPropertiesStandard(stack: Stack, context: BuildContext, schema: S.XAdditionalProperties, value: string): string;
7
+ export declare function BuildAdditionalProperties(stack: Stack, context: BuildContext, schema: S.XAdditionalProperties, value: string): string;
8
+ export declare function CheckAdditionalProperties(stack: Stack, context: CheckContext, schema: S.XAdditionalProperties, value: Record<PropertyKey, unknown>): boolean;
9
+ export declare function ErrorAdditionalProperties(stack: Stack, context: ErrorContext, schemaPath: string, instancePath: string, schema: S.XAdditionalProperties, value: Record<PropertyKey, unknown>): boolean;