typebox 1.1.37 → 1.1.39

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.
@@ -45,8 +45,8 @@ function FunctionSection(build) {
45
45
  // ------------------------------------------------------------------
46
46
  function ExportSection(build) {
47
47
  const body = build.UseUnevaluated()
48
- ? `const context = new CheckContext({}, {}); return ${build.Call()}`
49
- : `return ${build.Call()}`;
48
+ ? `const context = new CheckContext({}, {}); return ${build.Entry()}`
49
+ : `return ${build.Entry()}`;
50
50
  return [
51
51
  Separator(),
52
52
  TsIgnore(),
@@ -1,16 +1,14 @@
1
1
  import { type TLocalizedValidationError } from '../error/index.mjs';
2
2
  import { type StaticDecode, type StaticEncode, type TProperties, type TSchema, Base } from '../type/index.mjs';
3
+ import { BuildResult, EvaluateResult } from '../schema/index.mjs';
3
4
  export declare class Validator<Context extends TProperties = TProperties, Type extends TSchema = TSchema, Encode extends unknown = StaticEncode<Type, Context>, Decode extends unknown = StaticDecode<Type, Context>> extends Base<Encode> {
4
- private readonly context;
5
- private readonly type;
6
- private readonly isAccelerated;
7
5
  private readonly hasCodec;
8
- private readonly code;
9
- private readonly check;
6
+ private readonly buildResult;
7
+ private readonly evaluateResult;
10
8
  /** Constructs a Validator with the given Context and Type. */
11
9
  constructor(context: Context, type: Type);
12
10
  /** Constructs a Validator with the given arguments. */
13
- constructor(context: Context, type: Type, isEvaluated: boolean, hasCodec: boolean, code: string, check: (value: unknown) => boolean);
11
+ constructor(hasCodec: boolean, buildResult: BuildResult, evaluateResult: EvaluateResult);
14
12
  /** Returns true if this Validator is using JIT acceleration. */
15
13
  IsAccelerated(): boolean;
16
14
  /** Returns the Context for this validator. */
@@ -21,6 +19,8 @@ export declare class Validator<Context extends TProperties = TProperties, Type e
21
19
  Code(): string;
22
20
  /** Performs a type-guard check on the provided value. */
23
21
  Check(value: unknown): value is Encode;
22
+ /** Validates a value and returns it. Will throw if invalid. */
23
+ Parse(value: unknown): Encode;
24
24
  /** Inspects a value and returns a detailed list of validation errors. */
25
25
  Errors(value: unknown): TLocalizedValidationError[];
26
26
  /** Cleans a value using the Validator type. */
@@ -31,12 +31,13 @@ export declare class Validator<Context extends TProperties = TProperties, Type e
31
31
  Create(): Encode;
32
32
  /** Creates defaults using the Validator type. */
33
33
  Default(value: unknown): unknown;
34
- /** Clones this validator. */
35
- Clone(): Validator<Context, Type>;
36
- /** Validates a value and returns it. Will throw if invalid. */
37
- Parse(value: unknown): Encode;
38
34
  /** Decodes a value */
39
35
  Decode(value: unknown): Decode;
40
36
  /** Encodes a value */
41
37
  Encode(value: unknown): Encode;
38
+ /**
39
+ * @deprecated Validator instances should not support Clone because they are owners of JIT evaluated functions. This function will be
40
+ * removed in the next version of TypeBox (relates to Type.Base deprecation)
41
+ */
42
+ Clone(): Validator<Context, Type>;
42
43
  }
@@ -1,10 +1,9 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { Settings } from '../system/settings/index.mjs';
3
3
  import { Arguments } from '../system/arguments/index.mjs';
4
- import { Environment } from '../system/environment/index.mjs';
5
4
  import { Base } from '../type/index.mjs';
6
5
  import { Errors, Clean, Convert, Create, Default, Decode, Encode, HasCodec, Parser, ParseError } from '../value/index.mjs';
7
- import { Build } from '../schema/index.mjs';
6
+ import { Build, BuildResult, EvaluateResult } from '../schema/index.mjs';
8
7
  // ------------------------------------------------------------------
9
8
  // Validator<...>
10
9
  // ------------------------------------------------------------------
@@ -13,27 +12,24 @@ export class Validator extends Base {
13
12
  constructor(...args) {
14
13
  super();
15
14
  const matched = Arguments.Match(args, {
16
- 6: (context, type, isEvalulated, hasCodec, code, check) => [context, type, isEvalulated, hasCodec, code, check],
15
+ 3: (hasCodec, buildResult, evaluateResult) => [hasCodec, buildResult, evaluateResult],
17
16
  2: (context, type) => [context, type]
18
17
  });
19
- if (matched.length === 6) {
20
- const [context, type, isEvaluated, hasCodec, code, check] = matched;
21
- this.context = context;
22
- this.type = type;
23
- this.isAccelerated = isEvaluated;
18
+ // Note: The Base type requires this Validator to be Clone, but where we cannot safely clone
19
+ // the BuildResult or the EvaluateResult. For now we pass the Validator constructor a shared
20
+ // reference of BuildResult and EvaluateResult to mitigate re-compile on Clone. We must remove
21
+ // this overload when Base is removed (memory-gc-ref)
22
+ if (matched.length === 3 && matched[1] instanceof BuildResult && matched[2] instanceof EvaluateResult) {
23
+ const [hasCodec, buildResult, evaluateResult] = matched;
24
24
  this.hasCodec = hasCodec;
25
- this.code = code;
26
- this.check = check;
25
+ this.buildResult = buildResult;
26
+ this.evaluateResult = evaluateResult;
27
27
  }
28
28
  else {
29
29
  const [context, type] = matched;
30
- const result = Build(context, type).Evaluate();
31
30
  this.hasCodec = HasCodec(context, type);
32
- this.context = context;
33
- this.type = type;
34
- this.isAccelerated = result.IsAccelerated;
35
- this.code = result.Code;
36
- this.check = result.Check;
31
+ this.buildResult = Build(context, type);
32
+ this.evaluateResult = this.buildResult.Evaluate();
37
33
  }
38
34
  }
39
35
  // ----------------------------------------------------------------
@@ -41,76 +37,85 @@ export class Validator extends Base {
41
37
  // ----------------------------------------------------------------
42
38
  /** Returns true if this Validator is using JIT acceleration. */
43
39
  IsAccelerated() {
44
- return this.isAccelerated;
40
+ return this.evaluateResult.IsAccelerated();
45
41
  }
46
42
  // ----------------------------------------------------------------
47
- // Context | Type
43
+ // Context & Type
48
44
  // ----------------------------------------------------------------
49
45
  /** Returns the Context for this validator. */
50
46
  Context() {
51
- return this.context;
47
+ return this.buildResult.Context();
52
48
  }
53
49
  /** Returns the underlying Type used to construct this Validator. */
54
50
  Type() {
55
- return this.type;
51
+ return this.buildResult.Schema();
56
52
  }
57
53
  // ----------------------------------------------------------------
58
54
  // Code
59
55
  // ----------------------------------------------------------------
60
56
  /** Returns the generated code for this validator. */
61
57
  Code() {
62
- return this.code;
58
+ return this.evaluateResult.Code();
63
59
  }
64
60
  // ----------------------------------------------------------------
65
- // Base<...>
61
+ // Standard Validator
66
62
  // ----------------------------------------------------------------
67
63
  /** Performs a type-guard check on the provided value. */
68
64
  Check(value) {
69
- return this.check(value);
65
+ return this.evaluateResult.Check(value);
66
+ }
67
+ /** Validates a value and returns it. Will throw if invalid. */
68
+ Parse(value) {
69
+ const checked = this.Check(value);
70
+ if (checked)
71
+ return value;
72
+ if (Settings.Get().correctiveParse)
73
+ return Parser(this.Context(), this.Type(), value);
74
+ throw new ParseError(value, this.Errors(value));
70
75
  }
71
76
  /** Inspects a value and returns a detailed list of validation errors. */
72
77
  Errors(value) {
73
- if (Environment.CanEvaluate() && this.check(value))
78
+ if (this.IsAccelerated() && this.Check(value))
74
79
  return [];
75
- return Errors(this.context, this.type, value);
80
+ return Errors(this.Context(), this.Type(), value);
76
81
  }
82
+ // ----------------------------------------------------------------
83
+ // Value.* Operations
84
+ // ----------------------------------------------------------------
77
85
  /** Cleans a value using the Validator type. */
78
86
  Clean(value) {
79
- return Clean(this.context, this.type, value);
87
+ return Clean(this.Context(), this.Type(), value);
80
88
  }
81
89
  /** Converts a value using the Validator type. */
82
90
  Convert(value) {
83
- return Convert(this.context, this.type, value);
91
+ return Convert(this.Context(), this.Type(), value);
84
92
  }
85
93
  /** Creates a value using the Validator type. */
86
94
  Create() {
87
- return Create(this.context, this.type);
95
+ return Create(this.Context(), this.Type());
88
96
  }
89
97
  /** Creates defaults using the Validator type. */
90
98
  Default(value) {
91
- return Default(this.context, this.type, value);
92
- }
93
- /** Clones this validator. */
94
- Clone() {
95
- return new Validator(this.context, this.type, this.isAccelerated, this.hasCodec, this.code, this.check);
96
- }
97
- /** Validates a value and returns it. Will throw if invalid. */
98
- Parse(value) {
99
- const checked = this.Check(value);
100
- if (checked)
101
- return value;
102
- if (Settings.Get().correctiveParse)
103
- return Parser(this.context, this.type, value);
104
- throw new ParseError(value, this.Errors(value));
99
+ return Default(this.Context(), this.Type(), value);
105
100
  }
106
101
  /** Decodes a value */
107
102
  Decode(value) {
108
- const result = this.hasCodec ? Decode(this.context, this.type, value) : this.Parse(value);
103
+ const result = this.hasCodec ? Decode(this.Context(), this.Type(), value) : this.Parse(value);
109
104
  return result;
110
105
  }
111
106
  /** Encodes a value */
112
107
  Encode(value) {
113
- const result = this.hasCodec ? Encode(this.context, this.type, value) : this.Parse(value);
108
+ const result = this.hasCodec ? Encode(this.Context(), this.Type(), value) : this.Parse(value);
114
109
  return result;
115
110
  }
111
+ // ----------------------------------------------------------------
112
+ // Deprecations
113
+ // ----------------------------------------------------------------
114
+ /**
115
+ * @deprecated Validator instances should not support Clone because they are owners of JIT evaluated functions. This function will be
116
+ * removed in the next version of TypeBox (relates to Type.Base deprecation)
117
+ */
118
+ Clone() {
119
+ return new Validator(this.hasCodec, this.buildResult, this.evaluateResult);
120
+ }
116
121
  }
@@ -1,19 +1,23 @@
1
1
  import * as Engine from './engine/index.mjs';
2
2
  import * as Schema from './types/index.mjs';
3
3
  export type CheckFunction = (value: unknown) => boolean;
4
- export interface EvaluateResult {
5
- IsAccelerated: boolean;
6
- Code: string;
7
- Check: CheckFunction;
4
+ export declare class EvaluateResult {
5
+ private readonly isAccelerated;
6
+ private readonly code;
7
+ private readonly check;
8
+ constructor(isAccelerated: boolean, code: string, check: CheckFunction);
9
+ IsAccelerated(): boolean;
10
+ Code(): string;
11
+ Check(value: unknown): boolean;
8
12
  }
9
13
  export declare class BuildResult {
10
14
  private readonly context;
11
15
  private readonly schema;
12
16
  private readonly external;
13
17
  private readonly functions;
14
- private readonly call;
18
+ private readonly entry;
15
19
  private readonly useUnevaluated;
16
- constructor(context: Record<PropertyKey, Schema.XSchema>, schema: Schema.XSchema, external: Engine.TExternal, functions: string[], call: string, useUnevaluated: boolean);
20
+ constructor(context: Record<PropertyKey, Schema.XSchema>, schema: Schema.XSchema, external: Engine.TExternal, functions: string[], entry: string, useUnevaluated: boolean);
17
21
  /** Returns the Context used for this build */
18
22
  Context(): Record<PropertyKey, Schema.XSchema>;
19
23
  /** Returns the Schema used for this build */
@@ -25,7 +29,7 @@ export declare class BuildResult {
25
29
  /** Returns check functions */
26
30
  Functions(): string[];
27
31
  /** Return entry function call. */
28
- Call(): string;
32
+ Entry(): string;
29
33
  /** Evaluates the build into a validation function */
30
34
  Evaluate(): EvaluateResult;
31
35
  }
@@ -12,8 +12,8 @@ import * as Engine from './engine/index.mjs';
12
12
  function CreateCode(build) {
13
13
  const functions = build.Functions().join(';\n');
14
14
  const statements = build.UseUnevaluated()
15
- ? ['const context = new CheckContext({}, {})', `return ${build.Call()}`]
16
- : [`return ${build.Call()}`];
15
+ ? ['const context = new CheckContext({}, {})', `return ${build.Entry()}`]
16
+ : [`return ${build.Entry()}`];
17
17
  return `${functions}; return (value) => { ${statements.join('; ')} }`;
18
18
  }
19
19
  // ------------------------------------------------------------------
@@ -40,15 +40,34 @@ function CreateCheck(build, code) {
40
40
  : CreateDynamicCheck(build);
41
41
  }
42
42
  // ------------------------------------------------------------------
43
+ // EvaluateResult
44
+ // ------------------------------------------------------------------
45
+ export class EvaluateResult {
46
+ constructor(isAccelerated, code, check) {
47
+ this.isAccelerated = isAccelerated;
48
+ this.code = code;
49
+ this.check = check;
50
+ }
51
+ IsAccelerated() {
52
+ return this.isAccelerated;
53
+ }
54
+ Code() {
55
+ return this.code;
56
+ }
57
+ Check(value) {
58
+ return this.check(value);
59
+ }
60
+ }
61
+ // ------------------------------------------------------------------
43
62
  // BuildResult
44
63
  // ------------------------------------------------------------------
45
64
  export class BuildResult {
46
- constructor(context, schema, external, functions, call, useUnevaluated) {
65
+ constructor(context, schema, external, functions, entry, useUnevaluated) {
47
66
  this.context = context;
48
67
  this.schema = schema;
49
68
  this.external = external;
50
69
  this.functions = functions;
51
- this.call = call;
70
+ this.entry = entry;
52
71
  this.useUnevaluated = useUnevaluated;
53
72
  }
54
73
  /** Returns the Context used for this build */
@@ -72,14 +91,14 @@ export class BuildResult {
72
91
  return this.functions;
73
92
  }
74
93
  /** Return entry function call. */
75
- Call() {
76
- return this.call;
94
+ Entry() {
95
+ return this.entry;
77
96
  }
78
97
  /** Evaluates the build into a validation function */
79
98
  Evaluate() {
80
- const Code = CreateCode(this);
81
- const Check = CreateCheck(this, Code);
82
- return { IsAccelerated: Environment.CanEvaluate(), Code, Check };
99
+ const code = CreateCode(this);
100
+ const check = CreateCheck(this, code);
101
+ return new EvaluateResult(Environment.CanEvaluate(), code, check);
83
102
  }
84
103
  }
85
104
  /** Builds a schema into a optimized runtime validator */
@@ -2,8 +2,8 @@ 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 build;
6
- private readonly result;
5
+ private readonly buildResult;
6
+ private readonly evaluateResult;
7
7
  constructor(context: Record<string, Schema.XSchema>, schema: Schema);
8
8
  /** Returns true if this Validator is using JIT acceleration. */
9
9
  IsAccelerated(): boolean;
@@ -9,31 +9,31 @@ import { ParseError } from './parse.mjs';
9
9
  // ------------------------------------------------------------------
10
10
  export class Validator {
11
11
  constructor(context, schema) {
12
- this.build = Build.Build(context, schema);
13
- this.result = this.build.Evaluate();
12
+ this.buildResult = Build.Build(context, schema);
13
+ this.evaluateResult = this.buildResult.Evaluate();
14
14
  }
15
15
  /** Returns true if this Validator is using JIT acceleration. */
16
16
  IsAccelerated() {
17
- return this.result.IsAccelerated;
17
+ return this.evaluateResult.IsAccelerated();
18
18
  }
19
19
  /** Returns the underlying Schema used to construct this Validator. */
20
20
  Schema() {
21
- return this.build.Schema();
21
+ return this.buildResult.Schema();
22
22
  }
23
23
  /** Performs a type-guard check on the provided value. */
24
24
  Check(value) {
25
- return this.result.Check(value);
25
+ return this.evaluateResult.Check(value);
26
26
  }
27
27
  /** Validates a value and returns it. Will throw if invalid. */
28
28
  Parse(value) {
29
- if (this.result.Check(value))
29
+ if (this.evaluateResult.Check(value))
30
30
  return value;
31
- const [_result, errors] = Errors(this.build.Context(), this.build.Schema(), value);
32
- throw new ParseError(this.build.Schema(), value, errors);
31
+ const [_result, errors] = Errors(this.buildResult.Context(), this.buildResult.Schema(), value);
32
+ throw new ParseError(this.buildResult.Schema(), value, errors);
33
33
  }
34
34
  /** Inspects a value and returns a detailed list of validation errors. */
35
35
  Errors(value) {
36
- return Errors(this.build.Context(), this.build.Schema(), value);
36
+ return Errors(this.buildResult.Context(), this.buildResult.Schema(), value);
37
37
  }
38
38
  }
39
39
  /** Compiles this schema into a high performance Validator */
@@ -104,7 +104,7 @@ export function ErrorAdditionalProperties(stack, context, schemaPath, instancePa
104
104
  return isAdditionalProperties || context.AddError({
105
105
  keyword: 'additionalProperties',
106
106
  schemaPath,
107
- instancePath: instancePath,
107
+ instancePath,
108
108
  params: { additionalProperties },
109
109
  });
110
110
  }
@@ -164,10 +164,13 @@ export type TExtendsMapping<Input extends [unknown, unknown, unknown, unknown, u
164
164
  export declare function ExtendsMapping(input: [unknown, unknown, unknown, unknown, unknown, unknown] | []): unknown;
165
165
  export type TBaseMapping<Input extends [unknown, unknown, unknown] | unknown> = (Input extends ['(', infer Type extends T.TSchema, ')'] ? Type : Input extends infer Type extends T.TSchema ? Type : never);
166
166
  export declare function BaseMapping(input: [unknown, unknown, unknown] | unknown): unknown;
167
+ export type TWithMapping<Input extends [unknown, unknown] | []> = (Input extends ['with', infer Options extends Record<PropertyKey, unknown>] ? Options : []);
168
+ export declare function WithMapping(input: [unknown, unknown] | []): unknown;
167
169
  type TFactorIndexArray<Type extends T.TSchema, IndexArray extends unknown[]> = (IndexArray extends [infer Left extends T.TSchema[], ...infer Right extends unknown[]] ? (Left extends [infer Indexer extends T.TSchema] ? TFactorIndexArray<C.TIndexDeferred<Type, Indexer>, Right> : Left extends [] ? TFactorIndexArray<T.TArray<Type>, Right> : T.TNever) : Type);
168
170
  type TFactorExtends<Type extends T.TSchema, Extends extends unknown[]> = (Extends extends [infer Right extends T.TSchema, infer True extends T.TSchema, infer False extends T.TSchema] ? C.TConditionalDeferred<Type, Right, True, False> : Type);
169
- export type TFactorMapping<Input extends [unknown, unknown, unknown, unknown]> = (Input extends [infer KeyOf extends boolean, infer Type extends T.TSchema, infer IndexArray extends unknown[], infer Extend extends unknown[]] ? KeyOf extends true ? TFactorExtends<C.TKeyOfDeferred<TFactorIndexArray<Type, IndexArray>>, Extend> : TFactorExtends<TFactorIndexArray<Type, IndexArray>, Extend> : never);
170
- export declare function FactorMapping(input: [unknown, unknown, unknown, unknown]): unknown;
171
+ type TFactorWith<Type extends T.TSchema, With extends unknown> = (With extends Record<PropertyKey, unknown> ? C.TOptionsDeferred<Type, With> : Type);
172
+ export type TFactorMapping<Input extends [unknown, unknown, unknown, unknown, unknown]> = (Input extends [infer KeyOf extends boolean, infer Type extends T.TSchema, infer IndexArray extends unknown[], infer Extend extends unknown[], infer WithClause extends unknown] ? TFactorWith<KeyOf extends true ? TFactorExtends<C.TKeyOfDeferred<TFactorIndexArray<Type, IndexArray>>, Extend> : TFactorExtends<TFactorIndexArray<Type, IndexArray>, Extend>, WithClause> : never);
173
+ export declare function FactorMapping(input: [unknown, unknown, unknown, unknown, unknown]): unknown;
171
174
  type TExprBinaryMapping<Left extends T.TSchema, Rest extends unknown[]> = (Rest extends [infer Operator extends unknown, infer Right extends T.TSchema, infer Next extends unknown[]] ? (TExprBinaryMapping<Right, Next> extends infer Schema extends T.TSchema ? (Operator extends '&' ? (Schema extends T.TIntersect<infer Types extends T.TSchema[]> ? T.TIntersect<[Left, ...Types]> : T.TIntersect<[Left, Schema]>) : Operator extends '|' ? (Schema extends T.TUnion<infer Types extends T.TSchema[]> ? T.TUnion<[Left, ...Types]> : T.TUnion<[Left, Schema]>) : never) : never) : Left);
172
175
  export type TExprTermTailMapping<Input extends [unknown, unknown, unknown] | []> = (Input);
173
176
  export declare function ExprTermTailMapping(input: [unknown, unknown, unknown] | []): unknown;
@@ -185,27 +185,34 @@ export function BaseMapping(input) {
185
185
  ? input[1]
186
186
  : input;
187
187
  }
188
+ export function WithMapping(input) {
189
+ return Guard.IsEqual(input.length, 2) ? input[1] : [];
190
+ }
188
191
  // deno-coverage-ignore-start
189
- // ...
190
- const FactorIndexArray = (Type, indexArray) => {
192
+ function FactorIndexArray(Type, indexArray) {
191
193
  return indexArray.reduce((result, left) => {
192
194
  const _left = left;
193
195
  return (Guard.IsEqual(_left.length, 1) ? C.IndexDeferred(result, _left[0]) :
194
196
  Guard.IsEqual(_left.length, 0) ? T.Array(result) :
195
197
  Unreachable());
196
198
  }, Type);
197
- };
199
+ }
198
200
  // deno-coverage-ignore-stop
199
- const FactorExtends = (type, extend) => {
201
+ function FactorExtends(type, extend) {
200
202
  return Guard.IsEqual(extend.length, 3)
201
203
  ? C.ConditionalDeferred(type, extend[0], extend[1], extend[2])
202
204
  : type;
203
- };
205
+ }
206
+ function FactorWith(type, withClause) {
207
+ return Guard.IsArray(withClause) && Guard.IsEqual(withClause.length, 0)
208
+ ? type
209
+ : C.OptionsDeferred(type, withClause);
210
+ }
204
211
  export function FactorMapping(input) {
205
- const [keyOf, type, indexArray, extend] = input;
206
- return keyOf
212
+ const [keyOf, type, indexArray, extend, withClause] = input;
213
+ return FactorWith(keyOf
207
214
  ? FactorExtends(C.KeyOfDeferred(FactorIndexArray(type, indexArray)), extend)
208
- : FactorExtends(FactorIndexArray(type, indexArray), extend);
215
+ : FactorExtends(FactorIndexArray(type, indexArray), extend), withClause);
209
216
  }
210
217
  // deno-coverage-ignore-start
211
218
  function ExprBinaryMapping(left, rest) {
@@ -43,7 +43,8 @@ export type TIndexArray_0<Input extends string, Result extends unknown[] = []> =
43
43
  export type TIndexArray<Input extends string> = TIndexArray_0<Input> extends [infer _0 extends ([unknown, unknown, unknown] | [unknown, unknown])[], infer Input extends string] ? [S.TIndexArrayMapping<_0>, Input] : [];
44
44
  export type TExtends<Input extends string> = ((Token.TConst<'extends', Input> extends [infer _0, infer Input extends string] ? (TType<Input> extends [infer _1, infer Input extends string] ? (Token.TConst<'?', Input> extends [infer _2, infer Input extends string] ? (TType<Input> extends [infer _3, infer Input extends string] ? (Token.TConst<':', Input> extends [infer _4, infer Input extends string] ? (TType<Input> extends [infer _5, infer Input extends string] ? [[_0, _1, _2, _3, _4, _5], Input] : []) : []) : []) : []) : []) : []) extends [infer _0, infer Input extends string] ? [_0, Input] : [[], Input] extends [infer _0, infer Input extends string] ? [_0, Input] : []) extends [infer _0 extends [unknown, unknown, unknown, unknown, unknown, unknown] | [], infer Input extends string] ? [S.TExtendsMapping<_0>, Input] : [];
45
45
  export type TBase<Input extends string> = ((Token.TConst<'(', Input> extends [infer _0, infer Input extends string] ? (TType<Input> extends [infer _1, infer Input extends string] ? (Token.TConst<')', Input> extends [infer _2, infer Input extends string] ? [[_0, _1, _2], Input] : []) : []) : []) extends [infer _0, infer Input extends string] ? [_0, Input] : TKeyword<Input> extends [infer _0, infer Input extends string] ? [_0, Input] : T_Object_<Input> extends [infer _0, infer Input extends string] ? [_0, Input] : TTuple<Input> extends [infer _0, infer Input extends string] ? [_0, Input] : TTemplateLiteral<Input> extends [infer _0, infer Input extends string] ? [_0, Input] : TLiteral<Input> extends [infer _0, infer Input extends string] ? [_0, Input] : TConstructor<Input> extends [infer _0, infer Input extends string] ? [_0, Input] : T_Function_<Input> extends [infer _0, infer Input extends string] ? [_0, Input] : TMapped<Input> extends [infer _0, infer Input extends string] ? [_0, Input] : TOptions<Input> extends [infer _0, infer Input extends string] ? [_0, Input] : TGenericCall<Input> extends [infer _0, infer Input extends string] ? [_0, Input] : TReference<Input> extends [infer _0, infer Input extends string] ? [_0, Input] : []) extends [infer _0 extends [unknown, unknown, unknown] | unknown, infer Input extends string] ? [S.TBaseMapping<_0>, Input] : [];
46
- export type TFactor<Input extends string> = (TKeyOf<Input> extends [infer _0, infer Input extends string] ? (TBase<Input> extends [infer _1, infer Input extends string] ? (TIndexArray<Input> extends [infer _2, infer Input extends string] ? (TExtends<Input> extends [infer _3, infer Input extends string] ? [[_0, _1, _2, _3], Input] : []) : []) : []) : []) extends [infer _0 extends [unknown, unknown, unknown, unknown], infer Input extends string] ? [S.TFactorMapping<_0>, Input] : [];
46
+ export type TWith<Input extends string> = ((Token.TConst<'with', Input> extends [infer _0, infer Input extends string] ? (TJsonObject<Input> extends [infer _1, infer Input extends string] ? [[_0, _1], Input] : []) : []) extends [infer _0, infer Input extends string] ? [_0, Input] : [[], Input] extends [infer _0, infer Input extends string] ? [_0, Input] : []) extends [infer _0 extends [unknown, unknown] | [], infer Input extends string] ? [S.TWithMapping<_0>, Input] : [];
47
+ export type TFactor<Input extends string> = (TKeyOf<Input> extends [infer _0, infer Input extends string] ? (TBase<Input> extends [infer _1, infer Input extends string] ? (TIndexArray<Input> extends [infer _2, infer Input extends string] ? (TExtends<Input> extends [infer _3, infer Input extends string] ? (TWith<Input> extends [infer _4, infer Input extends string] ? [[_0, _1, _2, _3, _4], Input] : []) : []) : []) : []) : []) extends [infer _0 extends [unknown, unknown, unknown, unknown, unknown], infer Input extends string] ? [S.TFactorMapping<_0>, Input] : [];
47
48
  export type TExprTermTail<Input extends string> = ((Token.TConst<'&', Input> extends [infer _0, infer Input extends string] ? (TFactor<Input> extends [infer _1, infer Input extends string] ? (TExprTermTail<Input> extends [infer _2, infer Input extends string] ? [[_0, _1, _2], Input] : []) : []) : []) extends [infer _0, infer Input extends string] ? [_0, Input] : [[], Input] extends [infer _0, infer Input extends string] ? [_0, Input] : []) extends [infer _0 extends [unknown, unknown, unknown] | [], infer Input extends string] ? [S.TExprTermTailMapping<_0>, Input] : [];
48
49
  export type TExprTerm<Input extends string> = (TFactor<Input> extends [infer _0, infer Input extends string] ? (TExprTermTail<Input> extends [infer _1, infer Input extends string] ? [[_0, _1], Input] : []) : []) extends [infer _0 extends [unknown, unknown], infer Input extends string] ? [S.TExprTermMapping<_0>, Input] : [];
49
50
  export type TExprTail<Input extends string> = ((Token.TConst<'|', Input> extends [infer _0, infer Input extends string] ? (TExprTerm<Input> extends [infer _1, infer Input extends string] ? (TExprTail<Input> extends [infer _2, infer Input extends string] ? [[_0, _1, _2], Input] : []) : []) : []) extends [infer _0, infer Input extends string] ? [_0, Input] : [[], Input] extends [infer _0, infer Input extends string] ? [_0, Input] : []) extends [infer _0 extends [unknown, unknown, unknown] | [], infer Input extends string] ? [S.TExprTailMapping<_0>, Input] : [];
@@ -172,6 +173,7 @@ export declare const IndexArray_0: (input: string, result?: unknown[]) => [unkno
172
173
  export declare const IndexArray: (input: string) => [unknown, string] | [];
173
174
  export declare const Extends: (input: string) => [unknown, string] | [];
174
175
  export declare const Base: (input: string) => [unknown, string] | [];
176
+ export declare const With: (input: string) => [unknown, string] | [];
175
177
  export declare const Factor: (input: string) => [unknown, string] | [];
176
178
  export declare const ExprTermTail: (input: string) => [unknown, string] | [];
177
179
  export declare const ExprTerm: (input: string) => [unknown, string] | [];
@@ -47,7 +47,8 @@ export const IndexArray_0 = (input, result = []) => If(If(If(Token.Const('[', in
47
47
  export const IndexArray = (input) => If(IndexArray_0(input), ([_0, input]) => [S.IndexArrayMapping(_0), input]);
48
48
  export const Extends = (input) => If(If(If(Token.Const('extends', input), ([_0, input]) => If(Type(input), ([_1, input]) => If(Token.Const('?', input), ([_2, input]) => If(Type(input), ([_3, input]) => If(Token.Const(':', input), ([_4, input]) => If(Type(input), ([_5, input]) => [[_0, _1, _2, _3, _4, _5], input])))))), ([_0, input]) => [_0, input], () => If([[], input], ([_0, input]) => [_0, input], () => [])), ([_0, input]) => [S.ExtendsMapping(_0), input]);
49
49
  export const Base = (input) => If(If(If(Token.Const('(', input), ([_0, input]) => If(Type(input), ([_1, input]) => If(Token.Const(')', input), ([_2, input]) => [[_0, _1, _2], input]))), ([_0, input]) => [_0, input], () => If(Keyword(input), ([_0, input]) => [_0, input], () => If(_Object_(input), ([_0, input]) => [_0, input], () => If(Tuple(input), ([_0, input]) => [_0, input], () => If(TemplateLiteral(input), ([_0, input]) => [_0, input], () => If(Literal(input), ([_0, input]) => [_0, input], () => If(Constructor(input), ([_0, input]) => [_0, input], () => If(_Function_(input), ([_0, input]) => [_0, input], () => If(Mapped(input), ([_0, input]) => [_0, input], () => If(Options(input), ([_0, input]) => [_0, input], () => If(GenericCall(input), ([_0, input]) => [_0, input], () => If(Reference(input), ([_0, input]) => [_0, input], () => [])))))))))))), ([_0, input]) => [S.BaseMapping(_0), input]);
50
- export const Factor = (input) => If(If(KeyOf(input), ([_0, input]) => If(Base(input), ([_1, input]) => If(IndexArray(input), ([_2, input]) => If(Extends(input), ([_3, input]) => [[_0, _1, _2, _3], input])))), ([_0, input]) => [S.FactorMapping(_0), input]);
50
+ export const With = (input) => If(If(If(Token.Const('with', input), ([_0, input]) => If(JsonObject(input), ([_1, input]) => [[_0, _1], input])), ([_0, input]) => [_0, input], () => If([[], input], ([_0, input]) => [_0, input], () => [])), ([_0, input]) => [S.WithMapping(_0), input]);
51
+ export const Factor = (input) => If(If(KeyOf(input), ([_0, input]) => If(Base(input), ([_1, input]) => If(IndexArray(input), ([_2, input]) => If(Extends(input), ([_3, input]) => If(With(input), ([_4, input]) => [[_0, _1, _2, _3, _4], input]))))), ([_0, input]) => [S.FactorMapping(_0), input]);
51
52
  export const ExprTermTail = (input) => If(If(If(Token.Const('&', input), ([_0, input]) => If(Factor(input), ([_1, input]) => If(ExprTermTail(input), ([_2, input]) => [[_0, _1, _2], input]))), ([_0, input]) => [_0, input], () => If([[], input], ([_0, input]) => [_0, input], () => [])), ([_0, input]) => [S.ExprTermTailMapping(_0), input]);
52
53
  export const ExprTerm = (input) => If(If(Factor(input), ([_0, input]) => If(ExprTermTail(input), ([_1, input]) => [[_0, _1], input])), ([_0, input]) => [S.ExprTermMapping(_0), input]);
53
54
  export const ExprTail = (input) => If(If(If(Token.Const('|', input), ([_0, input]) => If(ExprTerm(input), ([_1, input]) => If(ExprTail(input), ([_2, input]) => [[_0, _1, _2], input]))), ([_0, input]) => [_0, input], () => If([[], input], ([_0, input]) => [_0, input], () => [])), ([_0, input]) => [S.ExprTailMapping(_0), input]);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "typebox",
3
3
  "description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
4
- "version": "1.1.37",
4
+ "version": "1.1.39",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "jsonschema"
@@ -16,22 +16,6 @@
16
16
  "types": "./build/index.d.mts",
17
17
  "module": "./build/index.mjs",
18
18
  "exports": {
19
- "./guard": {
20
- "import": "./build/guard/index.mjs",
21
- "default": "./build/guard/index.mjs"
22
- },
23
- "./error": {
24
- "import": "./build/error/index.mjs",
25
- "default": "./build/error/index.mjs"
26
- },
27
- "./compile": {
28
- "import": "./build/compile/index.mjs",
29
- "default": "./build/compile/index.mjs"
30
- },
31
- "./system": {
32
- "import": "./build/system/index.mjs",
33
- "default": "./build/system/index.mjs"
34
- },
35
19
  "./format": {
36
20
  "import": "./build/format/index.mjs",
37
21
  "default": "./build/format/index.mjs"
@@ -44,10 +28,26 @@
44
28
  "import": "./build/schema/index.mjs",
45
29
  "default": "./build/schema/index.mjs"
46
30
  },
31
+ "./compile": {
32
+ "import": "./build/compile/index.mjs",
33
+ "default": "./build/compile/index.mjs"
34
+ },
47
35
  "./value": {
48
36
  "import": "./build/value/index.mjs",
49
37
  "default": "./build/value/index.mjs"
50
38
  },
39
+ "./guard": {
40
+ "import": "./build/guard/index.mjs",
41
+ "default": "./build/guard/index.mjs"
42
+ },
43
+ "./system": {
44
+ "import": "./build/system/index.mjs",
45
+ "default": "./build/system/index.mjs"
46
+ },
47
+ "./error": {
48
+ "import": "./build/error/index.mjs",
49
+ "default": "./build/error/index.mjs"
50
+ },
51
51
  ".": {
52
52
  "import": "./build/index.mjs",
53
53
  "default": "./build/index.mjs"
@@ -55,18 +55,6 @@
55
55
  },
56
56
  "typesVersions": {
57
57
  "*": {
58
- "guard": [
59
- "./build/guard/index.d.mts"
60
- ],
61
- "error": [
62
- "./build/error/index.d.mts"
63
- ],
64
- "compile": [
65
- "./build/compile/index.d.mts"
66
- ],
67
- "system": [
68
- "./build/system/index.d.mts"
69
- ],
70
58
  "format": [
71
59
  "./build/format/index.d.mts"
72
60
  ],
@@ -76,9 +64,21 @@
76
64
  "schema": [
77
65
  "./build/schema/index.d.mts"
78
66
  ],
67
+ "compile": [
68
+ "./build/compile/index.d.mts"
69
+ ],
79
70
  "value": [
80
71
  "./build/value/index.d.mts"
81
72
  ],
73
+ "guard": [
74
+ "./build/guard/index.d.mts"
75
+ ],
76
+ "system": [
77
+ "./build/system/index.d.mts"
78
+ ],
79
+ "error": [
80
+ "./build/error/index.d.mts"
81
+ ],
82
82
  ".": [
83
83
  "./build/index.d.mts"
84
84
  ]
package/readme.md CHANGED
@@ -52,7 +52,7 @@ type T = Type.Static<typeof T> // type T = {
52
52
 
53
53
  TypeBox is a runtime type system that creates in-memory JSON Schema objects that infer as TypeScript types. The schematics produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type system that can be statically checked by TypeScript and validated at runtime using standard JSON Schema.
54
54
 
55
- This library is designed to allow JSON Schema to compose similar to how types compose within TypeScript's type system. It can be used as a simple tool to build up complex schematics or integrated into REST, RPC and MCP services to help validate data received over the wire.
55
+ This library is designed to allow JSON Schema to compose similar to how types compose within TypeScript's type system. It can be used as a simple tool to build up complex schematics or integrated into REST and RPC services to help validate data received over the wire.
56
56
 
57
57
  License: MIT
58
58
 
@@ -281,29 +281,29 @@ The following table shows compile performance for various JSON Schema structures
281
281
  ┌──────────────────────┬─────────────┬─────────────┐
282
282
  │ Compile │ TB1X │ AJV8 │
283
283
  ├──────────────────────┼─────────────┼─────────────┤
284
- │ Boolean │ 28.4K ops/s │ 7K ops/s │
285
- │ Number │ 21.8K ops/s │ 7.7K ops/s │
286
- │ String │ 47.8K ops/s │ 7.3K ops/s │
287
- │ Null │ 35.6K ops/s │ 7.8K ops/s │
288
- │ Literal_String │ 28.6K ops/s │ 6.3K ops/s │
289
- │ Literal_Number │ 46.6K ops/s │ 6.2K ops/s │
290
- │ Literal_Boolean │ 40.8K ops/s │ 6.6K ops/s │
291
- │ Pattern │ 29.7K ops/s │ 4.9K ops/s │
292
- │ Object_Open │ 6.8K ops/s │ 1.1K ops/s │
293
- │ Object_Close │ 7.4K ops/s │ 833 ops/s │
294
- │ Object_Vector3 │ 19.4K ops/s │ 2.1K ops/s │
295
- │ Object_Basis3 │ 6K ops/s │ 895 ops/s │
296
- │ Intersect_And │ 12K ops/s │ 3.5K ops/s │
297
- │ Intersect_Structural │ 8.4K ops/s │ 1.1K ops/s │
298
- │ Union_Or │ 18.2K ops/s │ 2.5K ops/s │
299
- │ Union_Structural │ 10.9K ops/s │ 1.3K ops/s │
300
- │ Tuple_Values │ 7.3K ops/s │ 1.6K ops/s │
301
- │ Tuple_Objects │ 1.9K ops/s │ 339 ops/s │
302
- │ Array_Numbers_4 │ 29.9K ops/s │ 3.4K ops/s │
303
- │ Array_Numbers_8 │ 20.3K ops/s │ 3.4K ops/s │
304
- │ Array_Numbers_16 │ 29.4K ops/s │ 3.3K ops/s │
305
- │ Array_Objects_Open │ 6.3K ops/s │ 684 ops/s │
306
- │ Array_Objects_Close │ 7.3K ops/s │ 762 ops/s │
284
+ │ Boolean │ 29.2K ops/s │ 7.1K ops/s │
285
+ │ Number │ 34.5K ops/s │ 7.6K ops/s │
286
+ │ String │ 48.9K ops/s │ 8.7K ops/s │
287
+ │ Null │ 39.6K ops/s │ 7.6K ops/s │
288
+ │ Literal_String │ 46.8K ops/s │ 6.8K ops/s │
289
+ │ Literal_Number │ 48.3K ops/s │ 7.4K ops/s │
290
+ │ Literal_Boolean │ 48.8K ops/s │ 7.3K ops/s │
291
+ │ Pattern │ 32.5K ops/s │ 6.1K ops/s │
292
+ │ Object_Open │ 6.6K ops/s │ 1.4K ops/s │
293
+ │ Object_Close │ 7.6K ops/s │ 1K ops/s │
294
+ │ Object_Vector3 │ 20.8K ops/s │ 2.8K ops/s │
295
+ │ Object_Basis3 │ 7.5K ops/s │ 1K ops/s │
296
+ │ Intersect_And │ 23K ops/s │ 3.9K ops/s │
297
+ │ Intersect_Structural │ 8.7K ops/s │ 1.2K ops/s │
298
+ │ Union_Or │ 17.9K ops/s │ 3.4K ops/s │
299
+ │ Union_Structural │ 11.3K ops/s │ 2.1K ops/s │
300
+ │ Tuple_Values │ 9.6K ops/s │ 2.1K ops/s │
301
+ │ Tuple_Objects │ 2.1K ops/s │ 350 ops/s │
302
+ │ Array_Numbers_4 │ 33.6K ops/s │ 4.2K ops/s │
303
+ │ Array_Numbers_8 │ 39K ops/s │ 3.7K ops/s │
304
+ │ Array_Numbers_16 │ 29.6K ops/s │ 3.8K ops/s │
305
+ │ Array_Objects_Open │ 7.7K ops/s │ 833 ops/s │
306
+ │ Array_Objects_Close │ 7.6K ops/s │ 860 ops/s │
307
307
  └──────────────────────┴─────────────┴─────────────┘
308
308
  ```
309
309
 
@@ -314,29 +314,29 @@ The following tables shows validation performance for various JSON Schema struct
314
314
  ┌──────────────────────┬──────────────┬──────────────┐
315
315
  │ Validate │ TB1X │ AJV8 │
316
316
  ├──────────────────────┼──────────────┼──────────────┤
317
- │ Boolean │ 164.1M ops/s │ 181.5M ops/s │
318
- │ Number │ 107M ops/s │ 50.2M ops/s │
319
- │ String │ 102.2M ops/s │ 61.9M ops/s │
320
- │ Null │ 112.1M ops/s │ 48.2M ops/s │
321
- │ Literal_String │ 102.8M ops/s │ 61.5M ops/s │
322
- │ Literal_Number │ 109.1M ops/s │ 46.4M ops/s │
323
- │ Literal_Boolean │ 109.6M ops/s │ 63.3M ops/s │
324
- │ Pattern │ 24.7M ops/s │ 20.3M ops/s │
325
- │ Object_Open │ 75.4M ops/s │ 37.3M ops/s │
326
- │ Object_Close │ 35.9M ops/s │ 21.9M ops/s │
327
- │ Object_Vector3 │ 77.6M ops/s │ 47.4M ops/s │
328
- │ Object_Basis3 │ 37M ops/s │ 24.3M ops/s │
329
- │ Intersect_And │ 93.3M ops/s │ 61.1M ops/s │
330
- │ Intersect_Structural │ 83M ops/s │ 36.4M ops/s │
331
- │ Union_Or │ 99.7M ops/s │ 8.6M ops/s │
332
- │ Union_Structural │ 81.3M ops/s │ 43.5M ops/s │
333
- │ Tuple_Values │ 72.4M ops/s │ 41.7M ops/s │
334
- │ Tuple_Objects │ 32.6M ops/s │ 22.4M ops/s │
335
- │ Array_Numbers_4 │ 94.1M ops/s │ 42.8M ops/s │
336
- │ Array_Numbers_8 │ 90.6M ops/s │ 42.3M ops/s │
337
- │ Array_Numbers_16 │ 77.5M ops/s │ 40.2M ops/s │
338
- │ Array_Objects_Open │ 26.3M ops/s │ 19.6M ops/s │
339
- │ Array_Objects_Close │ 9.1M ops/s │ 10M ops/s │
317
+ │ Boolean │ 192.2M ops/s │ 189.5M ops/s │
318
+ │ Number │ 112.4M ops/s │ 61M ops/s │
319
+ │ String │ 113.7M ops/s │ 64.1M ops/s │
320
+ │ Null │ 112.8M ops/s │ 64.9M ops/s │
321
+ │ Literal_String │ 108M ops/s │ 62.9M ops/s │
322
+ │ Literal_Number │ 113.5M ops/s │ 63.2M ops/s │
323
+ │ Literal_Boolean │ 109.2M ops/s │ 64.1M ops/s │
324
+ │ Pattern │ 26.5M ops/s │ 22.4M ops/s │
325
+ │ Object_Open │ 78M ops/s │ 47.2M ops/s │
326
+ │ Object_Close │ 38.6M ops/s │ 27.6M ops/s │
327
+ │ Object_Vector3 │ 91M ops/s │ 51.3M ops/s │
328
+ │ Object_Basis3 │ 41.1M ops/s │ 27.4M ops/s │
329
+ │ Intersect_And │ 107.6M ops/s │ 59.9M ops/s │
330
+ │ Intersect_Structural │ 83.6M ops/s │ 46.3M ops/s │
331
+ │ Union_Or │ 95M ops/s │ 7.9M ops/s │
332
+ │ Union_Structural │ 84.5M ops/s │ 52.3M ops/s │
333
+ │ Tuple_Values │ 74.7M ops/s │ 53M ops/s │
334
+ │ Tuple_Objects │ 32.9M ops/s │ 22.3M ops/s │
335
+ │ Array_Numbers_4 │ 93.3M ops/s │ 55.1M ops/s │
336
+ │ Array_Numbers_8 │ 90.3M ops/s │ 50.8M ops/s │
337
+ │ Array_Numbers_16 │ 76.8M ops/s │ 39.6M ops/s │
338
+ │ Array_Objects_Open │ 28.7M ops/s │ 20.4M ops/s │
339
+ │ Array_Objects_Close │ 10.3M ops/s │ 10.8M ops/s │
340
340
  └──────────────────────┴──────────────┴──────────────┘
341
341
  ```
342
342