@sinclair/typebox 0.21.2 → 0.22.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 (4) hide show
  1. package/package.json +8 -8
  2. package/readme.md +10 -8
  3. package/typebox.d.ts +165 -140
  4. package/typebox.js +54 -54
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.21.2",
3
+ "version": "0.22.0",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "json-schema",
@@ -26,14 +26,14 @@
26
26
  "test": "npm run spec"
27
27
  },
28
28
  "devDependencies": {
29
- "@sinclair/hammer": "^0.15.7",
30
- "@types/chai": "^4.2.16",
31
- "@types/mocha": "^8.2.2",
32
- "@types/node": "^14.14.37",
33
- "ajv": "^8.6.2",
34
- "ajv-formats": "^2.0.2",
29
+ "@sinclair/hammer": "^0.15.8",
30
+ "@types/chai": "^4.2.22",
31
+ "@types/mocha": "^9.0.0",
32
+ "@types/node": "^16.11.9",
33
+ "ajv": "^8.8.2",
34
+ "ajv-formats": "^2.1.1",
35
35
  "chai": "^4.3.4",
36
- "mocha": "^9.1.2",
36
+ "mocha": "^9.1.3",
37
37
  "tsd": "^0.19.0",
38
38
  "typescript": "^4.5.2"
39
39
  }
package/readme.md CHANGED
@@ -452,7 +452,7 @@ It can be helpful to organize shared referenced types under a common namespace.
452
452
  ```typescript
453
453
  const Math3D = Type.Namespace({ // const Math3D = {
454
454
  Vector4: Type.Object({ // $id: 'Math3D',
455
- x: Type.Number(), // definitions: {
455
+ x: Type.Number(), // $defs: {
456
456
  y: Type.Number(), // Vector4: {
457
457
  z: Type.Number(), // type: 'object',
458
458
  w: Type.Number() // properties: {
@@ -486,9 +486,9 @@ const Math3D = Type.Namespace({ // const Math3D = {
486
486
  const Vertex = Type.Object({ // const Vertex = {
487
487
  position: Type.Ref(Math3D, 'Vector4'), // type: 'object',
488
488
  normal: Type.Ref(Math3D, 'Vector3'), // properties: {
489
- uv: Type.Ref(Math3D, 'Vector2') // position: { $ref: 'Math3D#/definitions/Vector4' },
490
- }) // normal: { $ref: 'Math3D#/definitions/Vector3' },
491
- // uv: { $ref: 'Math3D#/definitions/Vector2' }
489
+ uv: Type.Ref(Math3D, 'Vector2') // position: { $ref: 'Math3D#/$defs/Vector4' },
490
+ }) // normal: { $ref: 'Math3D#/$defs/Vector3' },
491
+ // uv: { $ref: 'Math3D#/$defs/Vector2' }
492
492
  // },
493
493
  // required: ['position', 'normal', 'uv']
494
494
  // }
@@ -503,8 +503,8 @@ Recursive types can be created with the `Type.Rec(...)` function. The following
503
503
  ```typescript
504
504
  const Node = Type.Rec(Self => Type.Object({ // const Node = {
505
505
  id: Type.String(), // $id: 'Node',
506
- nodes: Type.Array(Self), // $ref: 'Node#/definitions/self',
507
- }), { $id: 'Node' }) // definitions: {
506
+ nodes: Type.Array(Self), // $ref: 'Node#/$defs/self',
507
+ }), { $id: 'Node' }) // $defs: {
508
508
  // self: {
509
509
  // type: 'object',
510
510
  // properties: {
@@ -514,7 +514,7 @@ const Node = Type.Rec(Self => Type.Object({ // const Node = {
514
514
  // nodes: {
515
515
  // type: 'array',
516
516
  // items: {
517
- // $ref: 'Node#/definitions/self'
517
+ // $ref: 'Node#/$defs/self'
518
518
  // }
519
519
  // }
520
520
  // }
@@ -774,7 +774,9 @@ type T = Static<typeof T> // type T = string | null
774
774
  //
775
775
  //--------------------------------------------------------------------------------------------
776
776
 
777
- function StringUnion<T extends string[]>(values: [...T]): TUnion<{[K in keyof T]: T[K] }[number]> {
777
+ type IntoStringUnion<T> = {[K in keyof T]: T[K] extends string ? TLiteral<T[K]>: never }
778
+
779
+ function StringUnion<T extends string[]>(values: [...T]): TUnion<IntoStringUnion<T>> {
778
780
  return { enum: values } as any
779
781
  }
780
782
 
package/typebox.d.ts CHANGED
@@ -68,243 +68,268 @@ export declare type TDefinitions = {
68
68
  };
69
69
  export declare type TNamespace<T extends TDefinitions> = {
70
70
  kind: typeof BoxKind;
71
- definitions: T;
71
+ $defs: T;
72
72
  } & CustomOptions;
73
- export declare type Infer<T> = {
74
- '_infer': T;
75
- };
73
+ export interface TSchema {
74
+ 'typebox:output': unknown;
75
+ }
76
76
  export declare type TEnumType = Record<string, string | number>;
77
- export declare type TKey = string | number;
77
+ export declare type TKey = string | number | symbol;
78
78
  export declare type TValue = string | number | boolean;
79
- export declare type TRecordKey = TString | TNumber | TKeyOf<any> | TUnion<string | number>;
79
+ export declare type TRecordKey = TString | TNumber | TKeyOf<any> | TUnion<any>;
80
80
  export declare type TEnumKey<T = TKey> = {
81
81
  type: 'number' | 'string';
82
82
  const: T;
83
83
  };
84
- export declare type TProperties = {
84
+ export interface TProperties {
85
85
  [key: string]: TSchema;
86
- };
87
- export declare type TTuple<I> = Infer<I> & {
86
+ }
87
+ export interface TRecord<K extends TRecordKey, T extends TSchema> extends TSchema, ObjectOptions {
88
+ 'typebox:output': StaticRecord<K, T>;
89
+ kind: typeof RecordKind;
90
+ type: 'object';
91
+ patternProperties: {
92
+ [pattern: string]: T;
93
+ };
94
+ }
95
+ export interface TTuple<T extends TSchema[]> extends TSchema, CustomOptions {
96
+ 'typebox:output': StaticTuple<T>;
88
97
  kind: typeof TupleKind;
89
98
  type: 'array';
90
- items?: TSchema[];
99
+ items?: T;
91
100
  additionalItems?: false;
92
101
  minItems: number;
93
102
  maxItems: number;
94
- } & CustomOptions;
95
- export declare type TObject<I> = Infer<I> & {
103
+ }
104
+ export interface TObject<T extends TProperties> extends TSchema, ObjectOptions {
105
+ 'typebox:output': StaticObject<T>;
96
106
  kind: typeof ObjectKind;
97
107
  type: 'object';
98
- properties: TProperties;
108
+ properties: T;
99
109
  required?: string[];
100
- } & ObjectOptions;
101
- export declare type TUnion<I> = Infer<I> & {
110
+ }
111
+ export interface TUnion<T extends TSchema[]> extends TSchema, CustomOptions {
112
+ 'typebox:output': StaticUnion<T>;
102
113
  kind: typeof UnionKind;
103
- anyOf: TSchema[];
104
- } & CustomOptions;
105
- export declare type TIntersect<I> = Infer<I> & {
114
+ anyOf: T;
115
+ }
116
+ export interface TIntersect<T extends TSchema[]> extends TSchema, IntersectOptions {
117
+ 'typebox:output': StaticIntersect<T>;
106
118
  kind: typeof IntersectKind;
107
119
  type: 'object';
108
- allOf: TSchema[];
109
- } & IntersectOptions;
110
- export declare type TKeyOf<I> = Infer<I> & {
120
+ allOf: T;
121
+ }
122
+ export interface TKeyOf<T extends TKey[]> extends TSchema, CustomOptions {
123
+ 'typebox:output': StaticKeyOf<T>;
111
124
  kind: typeof KeyOfKind;
112
125
  type: 'string';
113
- enum: string[];
114
- } & CustomOptions;
115
- export declare type TRecord<I> = Infer<I> & {
116
- kind: typeof RecordKind;
117
- type: 'object';
118
- patternProperties: {
119
- [pattern: string]: TSchema;
120
- };
121
- } & ObjectOptions;
122
- export declare type TArray<I> = Infer<I> & {
126
+ enum: T;
127
+ }
128
+ export interface TArray<T extends TSchema> extends TSchema, ArrayOptions {
129
+ 'typebox:output': StaticArray<T>;
123
130
  kind: typeof ArrayKind;
124
131
  type: 'array';
125
- items: any;
126
- } & ArrayOptions;
127
- export declare type TLiteral<I> = Infer<I> & {
132
+ items: T;
133
+ }
134
+ export interface TLiteral<T extends TValue> extends TSchema, CustomOptions {
135
+ 'typebox:output': StaticLiteral<T>;
128
136
  kind: typeof LiteralKind;
129
- const: TValue;
130
- } & CustomOptions;
131
- export declare type TEnum<I> = Infer<I> & {
137
+ const: T;
138
+ }
139
+ export interface TEnum<T extends TEnumKey[]> extends TSchema, CustomOptions {
140
+ 'typebox:output': StaticEnum<T>;
132
141
  kind: typeof EnumKind;
133
- anyOf: TSchema;
134
- } & CustomOptions;
135
- export declare type TString = Infer<string> & {
142
+ anyOf: T;
143
+ }
144
+ export interface TString extends TSchema, StringOptions<string> {
145
+ 'typebox:output': string;
136
146
  kind: typeof StringKind;
137
147
  type: 'string';
138
- } & StringOptions<string>;
139
- export declare type TNumber = Infer<number> & {
148
+ }
149
+ export interface TNumber extends TSchema, NumberOptions {
150
+ 'typebox:output': number;
140
151
  kind: typeof NumberKind;
141
152
  type: 'number';
142
- } & NumberOptions;
143
- export declare type TInteger = Infer<number> & {
153
+ }
154
+ export interface TInteger extends TSchema, NumberOptions {
155
+ 'typebox:output': number;
144
156
  kind: typeof IntegerKind;
145
157
  type: 'integer';
146
- } & NumberOptions;
147
- export declare type TBoolean = Infer<boolean> & {
158
+ }
159
+ export interface TBoolean extends TSchema, CustomOptions {
160
+ 'typebox:output': boolean;
148
161
  kind: typeof BooleanKind;
149
162
  type: 'boolean';
150
- } & CustomOptions;
151
- export declare type TNull = Infer<null> & {
163
+ }
164
+ export interface TNull extends TSchema, CustomOptions {
165
+ 'typebox:output': null;
152
166
  kind: typeof NullKind;
153
167
  type: 'null';
154
- } & CustomOptions;
155
- export declare type TUnknown = Infer<unknown> & {
168
+ }
169
+ export interface TUnknown extends TSchema, CustomOptions {
170
+ 'typebox:output': unknown;
156
171
  kind: typeof UnknownKind;
157
- } & CustomOptions;
158
- export declare type TAny = Infer<any> & {
172
+ }
173
+ export interface TAny extends TSchema, CustomOptions {
174
+ 'typebox:output': any;
159
175
  kind: typeof AnyKind;
160
- } & CustomOptions;
176
+ }
161
177
  export declare const ConstructorKind: unique symbol;
162
178
  export declare const FunctionKind: unique symbol;
163
179
  export declare const PromiseKind: unique symbol;
164
180
  export declare const UndefinedKind: unique symbol;
165
181
  export declare const VoidKind: unique symbol;
166
- export declare type TConstructor<T> = Infer<T> & {
182
+ export interface TConstructor<T extends TSchema[], U extends TSchema> extends TSchema, CustomOptions {
183
+ 'typebox:output': StaticConstructor<T, U>;
167
184
  kind: typeof ConstructorKind;
168
185
  type: 'constructor';
169
186
  arguments: TSchema[];
170
187
  returns: TSchema;
171
- } & CustomOptions;
172
- export declare type TFunction<T> = Infer<T> & {
188
+ }
189
+ export interface TFunction<T extends TSchema[], U extends TSchema> extends TSchema, CustomOptions {
190
+ 'typebox:output': StaticFunction<T, U>;
173
191
  kind: typeof FunctionKind;
174
192
  type: 'function';
175
193
  arguments: TSchema[];
176
194
  returns: TSchema;
177
- } & CustomOptions;
178
- export declare type TPromise<T> = Infer<T> & {
195
+ }
196
+ export interface TPromise<T extends TSchema> extends TSchema, CustomOptions {
197
+ 'typebox:output': StaticPromise<T>;
179
198
  kind: typeof PromiseKind;
180
199
  type: 'promise';
181
200
  item: TSchema;
182
- } & CustomOptions;
183
- export declare type TUndefined = Infer<undefined> & {
201
+ }
202
+ export interface TUndefined extends TSchema, CustomOptions {
203
+ 'typebox:output': undefined;
184
204
  kind: typeof UndefinedKind;
185
205
  type: 'undefined';
186
- } & CustomOptions;
187
- export declare type TVoid = Infer<void> & {
206
+ }
207
+ export interface TVoid extends TSchema, CustomOptions {
208
+ 'typebox:output': void;
188
209
  kind: typeof VoidKind;
189
210
  type: 'void';
190
- } & CustomOptions;
191
- export declare type TSchema = TIntersect<any> | TUnion<any> | TTuple<any> | TObject<any> | TKeyOf<any> | TRecord<any> | TArray<any> | TEnum<any> | TLiteral<any> | TString | TNumber | TInteger | TBoolean | TNull | TUnknown | TAny | TConstructor<any> | TFunction<any> | TPromise<any> | TUndefined | TVoid;
211
+ }
192
212
  export declare type UnionToIntersect<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
193
- export declare type IntersectEvaluate<T extends readonly TSchema[]> = {
194
- [K in keyof T]: Static<T[K]>;
195
- };
196
- export declare type IntersectReduce<I extends unknown, T extends readonly any[]> = T extends [infer A, ...infer B] ? IntersectReduce<I & A, B> : I;
197
- export declare type ReadonlyOptionalPropertyKeys<T extends TProperties> = {
213
+ export declare type StaticReadonlyOptionalPropertyKeys<T extends TProperties> = {
198
214
  [K in keyof T]: T[K] extends TReadonlyOptional<TSchema> ? K : never;
199
215
  }[keyof T];
200
- export declare type ReadonlyPropertyKeys<T extends TProperties> = {
216
+ export declare type StaticReadonlyPropertyKeys<T extends TProperties> = {
201
217
  [K in keyof T]: T[K] extends TReadonly<TSchema> ? K : never;
202
218
  }[keyof T];
203
- export declare type OptionalPropertyKeys<T extends TProperties> = {
219
+ export declare type StaticOptionalPropertyKeys<T extends TProperties> = {
204
220
  [K in keyof T]: T[K] extends TOptional<TSchema> ? K : never;
205
221
  }[keyof T];
206
- export declare type RequiredPropertyKeys<T extends TProperties> = keyof Omit<T, ReadonlyOptionalPropertyKeys<T> | ReadonlyPropertyKeys<T> | OptionalPropertyKeys<T>>;
222
+ export declare type StaticRequiredPropertyKeys<T extends TProperties> = keyof Omit<T, StaticReadonlyOptionalPropertyKeys<T> | StaticReadonlyPropertyKeys<T> | StaticOptionalPropertyKeys<T>>;
223
+ export declare type StaticIntersectEvaluate<T extends readonly TSchema[]> = {
224
+ [K in keyof T]: Static<T[K]>;
225
+ };
226
+ export declare type StaticIntersectReduce<I extends unknown, T extends readonly any[]> = T extends [infer A, ...infer B] ? StaticIntersectReduce<I & A, B> : I;
227
+ export declare type StaticRequired<T extends TProperties> = {
228
+ [K in keyof T]: T[K] extends TReadonlyOptional<infer U> ? TReadonly<U> : T[K] extends TReadonly<infer U> ? TReadonly<U> : T[K] extends TOptional<infer U> ? U : T[K];
229
+ };
230
+ export declare type StaticPartial<T extends TProperties> = {
231
+ [K in keyof T]: T[K] extends TReadonlyOptional<infer U> ? TReadonlyOptional<U> : T[K] extends TReadonly<infer U> ? TReadonlyOptional<U> : T[K] extends TOptional<infer U> ? TOptional<U> : TOptional<T[K]>;
232
+ };
207
233
  export declare type StaticProperties<T extends TProperties> = {
208
- readonly [K in ReadonlyOptionalPropertyKeys<T>]?: Static<T[K]>;
234
+ readonly [K in StaticReadonlyOptionalPropertyKeys<T>]?: Static<T[K]>;
209
235
  } & {
210
- readonly [K in ReadonlyPropertyKeys<T>]: Static<T[K]>;
236
+ readonly [K in StaticReadonlyPropertyKeys<T>]: Static<T[K]>;
211
237
  } & {
212
- [K in OptionalPropertyKeys<T>]?: Static<T[K]>;
238
+ [K in StaticOptionalPropertyKeys<T>]?: Static<T[K]>;
213
239
  } & {
214
- [K in RequiredPropertyKeys<T>]: Static<T[K]>;
240
+ [K in StaticRequiredPropertyKeys<T>]: Static<T[K]>;
215
241
  };
216
- export declare type StaticRecord<K extends TRecordKey, T extends TSchema> = K extends TString ? Record<string, Static<T>> : K extends TNumber ? Record<number, Static<T>> : K extends TKeyOf<any> ? Record<K['_infer'], Static<T>> : K extends TUnion<any> ? Record<K['_infer'], Static<T>> : never;
242
+ export declare type StaticRecord<K extends TRecordKey, T extends TSchema> = K extends TString ? Record<string, Static<T>> : K extends TNumber ? Record<number, Static<T>> : K extends TKeyOf<TKey[]> ? Record<K['typebox:output'], Static<T>> : K extends TUnion<TSchema[]> ? Record<K['typebox:output'], Static<T>> : never;
217
243
  export declare type StaticEnum<T> = T extends TEnumKey<infer U>[] ? U : never;
218
244
  export declare type StaticKeyOf<T extends TKey[]> = T extends Array<infer K> ? K : never;
219
- export declare type StaticIntersect<T extends readonly TSchema[]> = IntersectReduce<unknown, IntersectEvaluate<T>>;
245
+ export declare type StaticIntersect<T extends readonly TSchema[]> = StaticIntersectReduce<unknown, StaticIntersectEvaluate<T>>;
220
246
  export declare type StaticUnion<T extends readonly TSchema[]> = {
221
247
  [K in keyof T]: Static<T[K]>;
222
248
  }[number];
223
249
  export declare type StaticTuple<T extends readonly TSchema[]> = {
224
250
  [K in keyof T]: Static<T[K]>;
225
251
  };
226
- export declare type StaticObject<T extends TProperties> = StaticProperties<StaticProperties<T>>;
252
+ export declare type StaticObject<T extends TProperties> = StaticProperties<T> extends infer I ? {
253
+ [K in keyof I]: I[K];
254
+ } : never;
227
255
  export declare type StaticArray<T extends TSchema> = Array<Static<T>>;
228
256
  export declare type StaticLiteral<T extends TValue> = T;
229
- export declare type StaticConstructor<T extends readonly TSchema[], U extends TSchema> = new (...args: [...{
257
+ export declare type StaticParameters<T extends readonly TSchema[]> = {
230
258
  [K in keyof T]: Static<T[K]>;
231
- }]) => Static<U>;
232
- export declare type StaticFunction<T extends readonly TSchema[], U extends TSchema> = (...args: [...{
233
- [K in keyof T]: Static<T[K]>;
234
- }]) => Static<U>;
259
+ };
260
+ export declare type StaticConstructor<T extends readonly TSchema[], U extends TSchema> = new (...args: [...StaticParameters<T>]) => Static<U>;
261
+ export declare type StaticFunction<T extends readonly TSchema[], U extends TSchema> = (...args: [...StaticParameters<T>]) => Static<U>;
235
262
  export declare type StaticPromise<T extends TSchema> = Promise<Static<T>>;
236
- export declare type Static<T> = T extends TKeyOf<infer I> ? I : T extends TIntersect<infer I> ? I : T extends TUnion<infer I> ? I : T extends TTuple<infer I> ? I : T extends TObject<infer I> ? {
237
- [K in keyof I]: I[K];
238
- } : T extends TRecord<infer I> ? I : T extends TArray<infer I> ? I : T extends TEnum<infer I> ? I : T extends TLiteral<infer I> ? I : T extends TString ? T['_infer'] : T extends TNumber ? T['_infer'] : T extends TInteger ? T['_infer'] : T extends TBoolean ? T['_infer'] : T extends TNull ? T['_infer'] : T extends TUnknown ? T['_infer'] : T extends TAny ? T['_infer'] : T extends TConstructor<infer I> ? I : T extends TFunction<infer I> ? I : T extends TPromise<infer I> ? I : T extends TUndefined ? T['_infer'] : T extends TVoid ? T['_infer'] : never;
263
+ export declare type Static<T> = T extends TSchema ? T['typebox:output'] : never;
239
264
  export declare class TypeBuilder {
240
- /** `standard` Modifies an object property to be both readonly and optional */
265
+ /** `Standard` Modifies an object property to be both readonly and optional */
241
266
  ReadonlyOptional<T extends TSchema>(item: T): TReadonlyOptional<T>;
242
- /** `standard` Modifies an object property to be readonly */
267
+ /** `Standard` Modifies an object property to be readonly */
243
268
  Readonly<T extends TSchema>(item: T): TReadonly<T>;
244
- /** `standard` Modifies an object property to be optional */
269
+ /** `Standard` Modifies an object property to be optional */
245
270
  Optional<T extends TSchema>(item: T): TOptional<T>;
246
- /** `standard` Creates a type type */
247
- Tuple<T extends TSchema[]>(items: [...T], options?: CustomOptions): TTuple<StaticTuple<T>>;
248
- /** `standard` Creates an object type with the given properties */
249
- Object<T extends TProperties>(properties: T, options?: ObjectOptions): TObject<StaticProperties<T>>;
250
- /** `standard` Creates an intersection type. Note this function requires draft `2019-09` to constrain with `unevaluatedProperties` */
251
- Intersect<T extends TSchema[]>(items: [...T], options?: IntersectOptions): TIntersect<StaticIntersect<T>>;
252
- /** `standard` Creates a union type */
253
- Union<T extends TSchema[]>(items: [...T], options?: CustomOptions): TUnion<StaticUnion<T>>;
254
- /** `standard` Creates an array type */
255
- Array<T extends TSchema>(items: T, options?: ArrayOptions): TArray<StaticArray<T>>;
256
- /** `standard` Creates an enum type from a TypeScript enum */
257
- Enum<T extends TEnumType>(item: T, options?: CustomOptions): TEnum<StaticEnum<TEnumKey<T[keyof T]>[]>>;
258
- /** `standard` Creates a literal type. Supports string, number and boolean values only */
259
- Literal<T extends TValue>(value: T, options?: CustomOptions): TLiteral<StaticLiteral<T>>;
260
- /** `standard` Creates a string type */
271
+ /** `Standard` Creates a type type */
272
+ Tuple<T extends TSchema[]>(items: [...T], options?: CustomOptions): TTuple<T>;
273
+ /** `Standard` Creates an object type with the given properties */
274
+ Object<T extends TProperties>(properties: T, options?: ObjectOptions): TObject<T>;
275
+ /** `Standard` Creates an intersect type. */
276
+ Intersect<T extends TSchema[]>(items: [...T], options?: IntersectOptions): TIntersect<T>;
277
+ /** `Standard` Creates a union type */
278
+ Union<T extends TSchema[]>(items: [...T], options?: CustomOptions): TUnion<T>;
279
+ /** `Standard` Creates an array type */
280
+ Array<T extends TSchema>(items: T, options?: ArrayOptions): TArray<T>;
281
+ /** `Standard` Creates an enum type from a TypeScript enum */
282
+ Enum<T extends TEnumType>(item: T, options?: CustomOptions): TEnum<TEnumKey<T[keyof T]>[]>;
283
+ /** `Standard` Creates a literal type. Supports string, number and boolean values only */
284
+ Literal<T extends TValue>(value: T, options?: CustomOptions): TLiteral<T>;
285
+ /** `Standard` Creates a string type */
261
286
  String<TCustomFormatOption extends string>(options?: StringOptions<StringFormatOption | TCustomFormatOption>): TString;
262
- /** `standard` Creates a string type from a regular expression */
287
+ /** `Standard` Creates a string type from a regular expression */
263
288
  RegEx(regex: RegExp, options?: CustomOptions): TString;
264
- /** `standard` Creates a number type */
289
+ /** `Standard` Creates a number type */
265
290
  Number(options?: NumberOptions): TNumber;
266
- /** `standard` Creates an integer type */
291
+ /** `Standard` Creates an integer type */
267
292
  Integer(options?: NumberOptions): TInteger;
268
- /** `standard` Creates a boolean type */
293
+ /** `Standard` Creates a boolean type */
269
294
  Boolean(options?: CustomOptions): TBoolean;
270
- /** `standard` Creates a null type */
295
+ /** `Standard` Creates a null type */
271
296
  Null(options?: CustomOptions): TNull;
272
- /** `standard` Creates an unknown type */
297
+ /** `Standard` Creates an unknown type */
273
298
  Unknown(options?: CustomOptions): TUnknown;
274
- /** `standard` Creates an any type */
299
+ /** `Standard` Creates an any type */
275
300
  Any(options?: CustomOptions): TAny;
276
- /** `standard` Creates a keyof type from the given object */
277
- KeyOf<T extends TObject<any>>(schema: T, options?: CustomOptions): TKeyOf<keyof T['_infer']>;
278
- /** `standard` Creates a record type */
279
- Record<K extends TRecordKey, T extends TSchema>(key: K, value: T, options?: ObjectOptions): TRecord<StaticRecord<K, T>>;
280
- /** `standard` Makes all properties in the given object type required */
281
- Required<T extends TObject<any>>(schema: T, options?: ObjectOptions): TObject<Required<T['_infer']>>;
282
- /** `standard` Makes all properties in the given object type optional */
283
- Partial<T extends TObject<any>>(schema: T, options?: ObjectOptions): TObject<Partial<T['_infer']>>;
284
- /** `standard` Picks property keys from the given object type */
285
- Pick<T extends TObject<any>, K extends (keyof T['_infer'])[]>(schema: T, keys: [...K], options?: ObjectOptions): TObject<Pick<T['_infer'], K[number]>>;
286
- /** `standard` Omits property keys from the given object type */
287
- Omit<T extends TObject<any>, K extends (keyof T['_infer'])[]>(schema: T, keys: [...K], options?: ObjectOptions): TObject<Omit<T['_infer'], K[number]>>;
288
- /** `standard` Omits the `kind` and `modifier` properties from the underlying schema */
301
+ /** `Standard` Creates a keyof type from the given object */
302
+ KeyOf<T extends TObject<TProperties>>(schema: T, options?: CustomOptions): TKeyOf<(keyof T['properties'])[]>;
303
+ /** `Standard` Creates a record type */
304
+ Record<K extends TRecordKey, T extends TSchema>(key: K, value: T, options?: ObjectOptions): TRecord<K, T>;
305
+ /** `Standard` Makes all properties in the given object type required */
306
+ Required<T extends TObject<any>>(schema: T, options?: ObjectOptions): TObject<StaticRequired<T['properties']>>;
307
+ /** `Standard` Makes all properties in the given object type optional */
308
+ Partial<T extends TObject<any>>(schema: T, options?: ObjectOptions): TObject<StaticPartial<T['properties']>>;
309
+ /** `Standard` Picks property keys from the given object type */
310
+ Pick<T extends TObject<TProperties>, K extends (keyof T['properties'])[]>(schema: T, keys: [...K], options?: ObjectOptions): TObject<Pick<T['properties'], K[number]>>;
311
+ /** `Standard` Omits property keys from the given object type */
312
+ Omit<T extends TObject<any>, K extends (keyof T['properties'])[]>(schema: T, keys: [...K], options?: ObjectOptions): TObject<Omit<T['properties'], K[number]>>;
313
+ /** `Standard` Omits the `kind` and `modifier` properties from the underlying schema */
289
314
  Strict<T extends TSchema>(schema: T, options?: CustomOptions): T;
290
- /** `extended` Creates a constructor type */
291
- Constructor<T extends TSchema[], U extends TSchema>(args: [...T], returns: U, options?: CustomOptions): TConstructor<StaticConstructor<T, U>>;
292
- /** `extended` Creates a function type */
293
- Function<T extends TSchema[], U extends TSchema>(args: [...T], returns: U, options?: CustomOptions): TFunction<StaticFunction<T, U>>;
294
- /** `extended` Creates a promise type */
295
- Promise<T extends TSchema>(item: T, options?: CustomOptions): TPromise<StaticPromise<T>>;
296
- /** `extended` Creates a undefined type */
315
+ /** `Extended` Creates a constructor type */
316
+ Constructor<T extends TSchema[], U extends TSchema>(args: [...T], returns: U, options?: CustomOptions): TConstructor<T, U>;
317
+ /** `Extended` Creates a function type */
318
+ Function<T extends TSchema[], U extends TSchema>(args: [...T], returns: U, options?: CustomOptions): TFunction<T, U>;
319
+ /** `Extended` Creates a promise type */
320
+ Promise<T extends TSchema>(item: T, options?: CustomOptions): TPromise<T>;
321
+ /** `Extended` Creates a undefined type */
297
322
  Undefined(options?: CustomOptions): TUndefined;
298
- /** `extended` Creates a void type */
323
+ /** `Extended` Creates a void type */
299
324
  Void(options?: CustomOptions): TVoid;
300
- /** `experimental` Creates a recursive type */
325
+ /** `Experimental` Creates a recursive type */
301
326
  Rec<T extends TSchema>(callback: (self: TAny) => T, options?: CustomOptions): T;
302
- /** `experimental` Creates a recursive type. Pending https://github.com/ajv-validator/ajv/issues/1709 */
303
- /** `experimental` Creates a namespace for a set of related types */
304
- Namespace<T extends TDefinitions>(definitions: T, options?: CustomOptions): TNamespace<T>;
305
- /** `experimental` References a type within a namespace. The referenced namespace must specify an `$id` */
306
- Ref<T extends TNamespace<TDefinitions>, K extends keyof T['definitions']>(box: T, key: K): T['definitions'][K];
307
- /** `experimental` References type. The referenced type must specify an `$id` */
327
+ /** `Experimental` Creates a recursive type. Pending https://github.com/ajv-validator/ajv/issues/1709 */
328
+ /** `Experimental` Creates a namespace for a set of related types */
329
+ Namespace<T extends TDefinitions>($defs: T, options?: CustomOptions): TNamespace<T>;
330
+ /** `Experimental` References a type within a namespace. The referenced namespace must specify an `$id` */
331
+ Ref<T extends TNamespace<TDefinitions>, K extends keyof T['$defs']>(box: T, key: K): T['$defs'][K];
332
+ /** `Experimental` References type. The referenced type must specify an `$id` */
308
333
  Ref<T extends TSchema>(schema: T): T;
309
334
  }
310
335
  export declare const Type: TypeBuilder;
package/typebox.js CHANGED
@@ -28,15 +28,15 @@ THE SOFTWARE.
28
28
  ---------------------------------------------------------------------------*/
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
30
  exports.Type = exports.TypeBuilder = exports.VoidKind = exports.UndefinedKind = exports.PromiseKind = exports.FunctionKind = exports.ConstructorKind = exports.AnyKind = exports.UnknownKind = exports.NullKind = exports.BooleanKind = exports.IntegerKind = exports.NumberKind = exports.StringKind = exports.LiteralKind = exports.EnumKind = exports.ArrayKind = exports.RecordKind = exports.ObjectKind = exports.TupleKind = exports.UnionKind = exports.IntersectKind = exports.KeyOfKind = exports.BoxKind = exports.ReadonlyModifier = exports.OptionalModifier = exports.ReadonlyOptionalModifier = void 0;
31
- // ------------------------------------------------------------------------
31
+ // --------------------------------------------------------------------------
32
32
  // Modifiers
33
- // ------------------------------------------------------------------------
33
+ // --------------------------------------------------------------------------
34
34
  exports.ReadonlyOptionalModifier = Symbol('ReadonlyOptionalModifier');
35
35
  exports.OptionalModifier = Symbol('OptionalModifier');
36
36
  exports.ReadonlyModifier = Symbol('ReadonlyModifier');
37
- // ------------------------------------------------------------------------
37
+ // --------------------------------------------------------------------------
38
38
  // Schema Standard
39
- // ------------------------------------------------------------------------
39
+ // --------------------------------------------------------------------------
40
40
  exports.BoxKind = Symbol('BoxKind');
41
41
  exports.KeyOfKind = Symbol('KeyOfKind');
42
42
  exports.IntersectKind = Symbol('IntersectKind');
@@ -54,17 +54,17 @@ exports.BooleanKind = Symbol('BooleanKind');
54
54
  exports.NullKind = Symbol('NullKind');
55
55
  exports.UnknownKind = Symbol('UnknownKind');
56
56
  exports.AnyKind = Symbol('AnyKind');
57
- // ------------------------------------------------------------------------
57
+ // --------------------------------------------------------------------------
58
58
  // Extended Schema Types
59
- // ------------------------------------------------------------------------
59
+ // --------------------------------------------------------------------------
60
60
  exports.ConstructorKind = Symbol('ConstructorKind');
61
61
  exports.FunctionKind = Symbol('FunctionKind');
62
62
  exports.PromiseKind = Symbol('PromiseKind');
63
63
  exports.UndefinedKind = Symbol('UndefinedKind');
64
64
  exports.VoidKind = Symbol('VoidKind');
65
- // ------------------------------------------------------------------------
65
+ // --------------------------------------------------------------------------
66
66
  // Utility
67
- // ------------------------------------------------------------------------
67
+ // --------------------------------------------------------------------------
68
68
  function isObject(object) {
69
69
  return typeof object === 'object' && object !== null && !Array.isArray(object);
70
70
  }
@@ -78,32 +78,32 @@ function clone(object) {
78
78
  return object.map((item) => clone(item));
79
79
  return object;
80
80
  }
81
- // ------------------------------------------------------------------------
81
+ // --------------------------------------------------------------------------
82
82
  // TypeBuilder
83
- // ------------------------------------------------------------------------
83
+ // --------------------------------------------------------------------------
84
84
  class TypeBuilder {
85
- /** `standard` Modifies an object property to be both readonly and optional */
85
+ /** `Standard` Modifies an object property to be both readonly and optional */
86
86
  ReadonlyOptional(item) {
87
87
  return { ...item, modifier: exports.ReadonlyOptionalModifier };
88
88
  }
89
- /** `standard` Modifies an object property to be readonly */
89
+ /** `Standard` Modifies an object property to be readonly */
90
90
  Readonly(item) {
91
91
  return { ...item, modifier: exports.ReadonlyModifier };
92
92
  }
93
- /** `standard` Modifies an object property to be optional */
93
+ /** `Standard` Modifies an object property to be optional */
94
94
  Optional(item) {
95
95
  return { ...item, modifier: exports.OptionalModifier };
96
96
  }
97
- /** `standard` Creates a type type */
97
+ /** `Standard` Creates a type type */
98
98
  Tuple(items, options = {}) {
99
99
  const additionalItems = false;
100
100
  const minItems = items.length;
101
101
  const maxItems = items.length;
102
- return (items.length > 0)
102
+ return ((items.length > 0)
103
103
  ? { ...options, kind: exports.TupleKind, type: 'array', items, additionalItems, minItems, maxItems }
104
- : { ...options, kind: exports.TupleKind, type: 'array', minItems, maxItems };
104
+ : { ...options, kind: exports.TupleKind, type: 'array', minItems, maxItems });
105
105
  }
106
- /** `standard` Creates an object type with the given properties */
106
+ /** `Standard` Creates an object type with the given properties */
107
107
  Object(properties, options = {}) {
108
108
  const property_names = Object.keys(properties);
109
109
  const optional = property_names.filter(name => {
@@ -114,74 +114,74 @@ class TypeBuilder {
114
114
  });
115
115
  const required_names = property_names.filter(name => !optional.includes(name));
116
116
  const required = (required_names.length > 0) ? required_names : undefined;
117
- return (required) ?
118
- { ...options, kind: exports.ObjectKind, type: 'object', properties, required } :
119
- { ...options, kind: exports.ObjectKind, type: 'object', properties };
117
+ return ((required)
118
+ ? { ...options, kind: exports.ObjectKind, type: 'object', properties, required }
119
+ : { ...options, kind: exports.ObjectKind, type: 'object', properties });
120
120
  }
121
- /** `standard` Creates an intersection type. Note this function requires draft `2019-09` to constrain with `unevaluatedProperties` */
121
+ /** `Standard` Creates an intersect type. */
122
122
  Intersect(items, options = {}) {
123
123
  return { ...options, kind: exports.IntersectKind, type: 'object', allOf: items };
124
124
  }
125
- /** `standard` Creates a union type */
125
+ /** `Standard` Creates a union type */
126
126
  Union(items, options = {}) {
127
127
  return { ...options, kind: exports.UnionKind, anyOf: items };
128
128
  }
129
- /** `standard` Creates an array type */
129
+ /** `Standard` Creates an array type */
130
130
  Array(items, options = {}) {
131
131
  return { ...options, kind: exports.ArrayKind, type: 'array', items };
132
132
  }
133
- /** `standard` Creates an enum type from a TypeScript enum */
133
+ /** `Standard` Creates an enum type from a TypeScript enum */
134
134
  Enum(item, options = {}) {
135
135
  const values = Object.keys(item).filter(key => isNaN(key)).map(key => item[key]);
136
136
  const anyOf = values.map(value => typeof value === 'string' ? { type: 'string', const: value } : { type: 'number', const: value });
137
137
  return { ...options, kind: exports.EnumKind, anyOf };
138
138
  }
139
- /** `standard` Creates a literal type. Supports string, number and boolean values only */
139
+ /** `Standard` Creates a literal type. Supports string, number and boolean values only */
140
140
  Literal(value, options = {}) {
141
141
  return { ...options, kind: exports.LiteralKind, const: value, type: typeof value };
142
142
  }
143
- /** `standard` Creates a string type */
143
+ /** `Standard` Creates a string type */
144
144
  String(options = {}) {
145
145
  return { ...options, kind: exports.StringKind, type: 'string' };
146
146
  }
147
- /** `standard` Creates a string type from a regular expression */
147
+ /** `Standard` Creates a string type from a regular expression */
148
148
  RegEx(regex, options = {}) {
149
149
  return this.String({ ...options, pattern: regex.source });
150
150
  }
151
- /** `standard` Creates a number type */
151
+ /** `Standard` Creates a number type */
152
152
  Number(options = {}) {
153
153
  return { ...options, kind: exports.NumberKind, type: 'number' };
154
154
  }
155
- /** `standard` Creates an integer type */
155
+ /** `Standard` Creates an integer type */
156
156
  Integer(options = {}) {
157
157
  return { ...options, kind: exports.IntegerKind, type: 'integer' };
158
158
  }
159
- /** `standard` Creates a boolean type */
159
+ /** `Standard` Creates a boolean type */
160
160
  Boolean(options = {}) {
161
161
  return { ...options, kind: exports.BooleanKind, type: 'boolean' };
162
162
  }
163
- /** `standard` Creates a null type */
163
+ /** `Standard` Creates a null type */
164
164
  Null(options = {}) {
165
165
  return { ...options, kind: exports.NullKind, type: 'null' };
166
166
  }
167
- /** `standard` Creates an unknown type */
167
+ /** `Standard` Creates an unknown type */
168
168
  Unknown(options = {}) {
169
169
  return { ...options, kind: exports.UnknownKind };
170
170
  }
171
- /** `standard` Creates an any type */
171
+ /** `Standard` Creates an any type */
172
172
  Any(options = {}) {
173
173
  return { ...options, kind: exports.AnyKind };
174
174
  }
175
- /** `standard` Creates a keyof type from the given object */
175
+ /** `Standard` Creates a keyof type from the given object */
176
176
  KeyOf(schema, options = {}) {
177
177
  const keys = Object.keys(schema.properties);
178
178
  return { ...options, kind: exports.KeyOfKind, type: 'string', enum: keys };
179
179
  }
180
- /** `standard` Creates a record type */
180
+ /** `Standard` Creates a record type */
181
181
  Record(key, value, options = {}) {
182
182
  const pattern = (() => {
183
183
  switch (key.kind) {
184
- case exports.UnionKind: return `^${key.anyOf.map(literal => literal.const).join('|')}$`;
184
+ case exports.UnionKind: return `^${key.anyOf.map((literal) => literal.const).join('|')}$`;
185
185
  case exports.KeyOfKind: return `^${key.enum.join('|')}$`;
186
186
  case exports.NumberKind: return '^(0|[1-9][0-9]*)$';
187
187
  case exports.StringKind: return key.pattern ? key.pattern : '^.*$';
@@ -190,7 +190,7 @@ class TypeBuilder {
190
190
  })();
191
191
  return { ...options, kind: exports.RecordKind, type: 'object', patternProperties: { [pattern]: value } };
192
192
  }
193
- /** `standard` Makes all properties in the given object type required */
193
+ /** `Standard` Makes all properties in the given object type required */
194
194
  Required(schema, options = {}) {
195
195
  const next = { ...clone(schema), ...options };
196
196
  next.required = Object.keys(next.properties);
@@ -213,7 +213,7 @@ class TypeBuilder {
213
213
  }
214
214
  return next;
215
215
  }
216
- /** `standard` Makes all properties in the given object type optional */
216
+ /** `Standard` Makes all properties in the given object type optional */
217
217
  Partial(schema, options = {}) {
218
218
  const next = { ...clone(schema), ...options };
219
219
  delete next.required;
@@ -236,7 +236,7 @@ class TypeBuilder {
236
236
  }
237
237
  return next;
238
238
  }
239
- /** `standard` Picks property keys from the given object type */
239
+ /** `Standard` Picks property keys from the given object type */
240
240
  Pick(schema, keys, options = {}) {
241
241
  const next = { ...clone(schema), ...options };
242
242
  next.required = next.required ? next.required.filter((key) => keys.includes(key)) : undefined;
@@ -246,7 +246,7 @@ class TypeBuilder {
246
246
  }
247
247
  return next;
248
248
  }
249
- /** `standard` Omits property keys from the given object type */
249
+ /** `Standard` Omits property keys from the given object type */
250
250
  Omit(schema, keys, options = {}) {
251
251
  const next = { ...clone(schema), ...options };
252
252
  next.required = next.required ? next.required.filter((key) => !keys.includes(key)) : undefined;
@@ -256,49 +256,49 @@ class TypeBuilder {
256
256
  }
257
257
  return next;
258
258
  }
259
- /** `standard` Omits the `kind` and `modifier` properties from the underlying schema */
259
+ /** `Standard` Omits the `kind` and `modifier` properties from the underlying schema */
260
260
  Strict(schema, options = {}) {
261
261
  return JSON.parse(JSON.stringify({ ...options, ...schema }));
262
262
  }
263
- /** `extended` Creates a constructor type */
263
+ /** `Extended` Creates a constructor type */
264
264
  Constructor(args, returns, options = {}) {
265
265
  return { ...options, kind: exports.ConstructorKind, type: 'constructor', arguments: args, returns };
266
266
  }
267
- /** `extended` Creates a function type */
267
+ /** `Extended` Creates a function type */
268
268
  Function(args, returns, options = {}) {
269
269
  return { ...options, kind: exports.FunctionKind, type: 'function', arguments: args, returns };
270
270
  }
271
- /** `extended` Creates a promise type */
271
+ /** `Extended` Creates a promise type */
272
272
  Promise(item, options = {}) {
273
273
  return { ...options, type: 'promise', kind: exports.PromiseKind, item };
274
274
  }
275
- /** `extended` Creates a undefined type */
275
+ /** `Extended` Creates a undefined type */
276
276
  Undefined(options = {}) {
277
277
  return { ...options, type: 'undefined', kind: exports.UndefinedKind };
278
278
  }
279
- /** `extended` Creates a void type */
279
+ /** `Extended` Creates a void type */
280
280
  Void(options = {}) {
281
281
  return { ...options, type: 'void', kind: exports.VoidKind };
282
282
  }
283
- /** `experimental` Creates a recursive type */
283
+ /** `Experimental` Creates a recursive type */
284
284
  Rec(callback, options = {}) {
285
285
  const $id = options.$id || '';
286
- const self = callback({ $ref: `${$id}#/definitions/self` });
287
- return { ...options, $ref: `${$id}#/definitions/self`, definitions: { self } };
286
+ const self = callback({ $ref: `${$id}#/$defs/self` });
287
+ return { ...options, $ref: `${$id}#/$defs/self`, $defs: { self } };
288
288
  }
289
- /** `experimental` Creates a recursive type. Pending https://github.com/ajv-validator/ajv/issues/1709 */
289
+ /** `Experimental` Creates a recursive type. Pending https://github.com/ajv-validator/ajv/issues/1709 */
290
290
  // public Rec<T extends TProperties>($id: string, callback: (self: TAny) => T, options: ObjectOptions = {}): TObject<T> {
291
291
  // const properties = callback({ $recursiveRef: `${$id}` } as any)
292
292
  // return { ...options, kind: ObjectKind, $id, $recursiveAnchor: true, type: 'object', properties }
293
293
  // }
294
- /** `experimental` Creates a namespace for a set of related types */
295
- Namespace(definitions, options = {}) {
296
- return { ...options, kind: exports.BoxKind, definitions };
294
+ /** `Experimental` Creates a namespace for a set of related types */
295
+ Namespace($defs, options = {}) {
296
+ return { ...options, kind: exports.BoxKind, $defs };
297
297
  }
298
298
  Ref(...args) {
299
299
  const $id = args[0]['$id'] || '';
300
300
  const key = args[1];
301
- return (args.length === 2) ? { $ref: `${$id}#/definitions/${key}` } : { $ref: $id };
301
+ return (args.length === 2) ? { $ref: `${$id}#/$defs/${key}` } : { $ref: $id };
302
302
  }
303
303
  }
304
304
  exports.TypeBuilder = TypeBuilder;