@sinclair/typebox 0.20.6 → 0.21.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.
- package/package.json +1 -1
- package/readme.md +4 -4
- package/typebox.d.ts +115 -123
- package/typebox.js +35 -36
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -447,10 +447,10 @@ const R = Type.Ref(T) // const R = {
|
|
|
447
447
|
// }
|
|
448
448
|
```
|
|
449
449
|
|
|
450
|
-
It can be helpful to organize shared referenced types under a common namespace. The `Type.
|
|
450
|
+
It can be helpful to organize shared referenced types under a common namespace. The `Type.Namespace(...)` function can be used to create a shared definition container for related types. The following creates a `Math3D` container and a `Vertex` structure that references types in the container.
|
|
451
451
|
|
|
452
452
|
```typescript
|
|
453
|
-
const Math3D = Type.
|
|
453
|
+
const Math3D = Type.Namespace({ // const Math3D = {
|
|
454
454
|
Vector4: Type.Object({ // $id: 'Math3D',
|
|
455
455
|
x: Type.Number(), // definitions: {
|
|
456
456
|
y: Type.Number(), // Vector4: {
|
|
@@ -691,7 +691,7 @@ const ok = ajv.validate(User, {
|
|
|
691
691
|
|
|
692
692
|
#### Reference Types
|
|
693
693
|
|
|
694
|
-
Referenced types can be added to AJV with the `ajv.addSchema(...)` function. The following moves the `userId` and `email` property types into a `Type.
|
|
694
|
+
Referenced types can be added to AJV with the `ajv.addSchema(...)` function. The following moves the `userId` and `email` property types into a `Type.Namespace(...)` and registers the box with AJV.
|
|
695
695
|
|
|
696
696
|
```typescript
|
|
697
697
|
//--------------------------------------------------------------------------------------------
|
|
@@ -700,7 +700,7 @@ Referenced types can be added to AJV with the `ajv.addSchema(...)` function. The
|
|
|
700
700
|
//
|
|
701
701
|
//--------------------------------------------------------------------------------------------
|
|
702
702
|
|
|
703
|
-
const Shared = Type.
|
|
703
|
+
const Shared = Type.Namespace({
|
|
704
704
|
UserId: Type.String({ format: 'uuid' }),
|
|
705
705
|
Email: Type.String({ format: 'email' })
|
|
706
706
|
}, { $id: 'Shared' })
|
package/typebox.d.ts
CHANGED
|
@@ -63,96 +63,99 @@ export declare type IntersectOptions = {
|
|
|
63
63
|
export declare type ObjectOptions = {
|
|
64
64
|
additionalProperties?: boolean;
|
|
65
65
|
} & CustomOptions;
|
|
66
|
+
export declare type TNamespace<T extends TDefinitions> = {
|
|
67
|
+
kind: typeof BoxKind;
|
|
68
|
+
definitions: T;
|
|
69
|
+
} & CustomOptions;
|
|
70
|
+
export declare type TDefinitions = {
|
|
71
|
+
[key: string]: TSchema;
|
|
72
|
+
};
|
|
73
|
+
export declare type Infer<T> = {
|
|
74
|
+
'_infer': T;
|
|
75
|
+
};
|
|
66
76
|
export declare type TEnumType = Record<string, string | number>;
|
|
67
77
|
export declare type TKey = string | number;
|
|
68
78
|
export declare type TValue = string | number | boolean;
|
|
69
|
-
export declare type TRecordKey = TString | TNumber |
|
|
79
|
+
export declare type TRecordKey = TString | TNumber | TKeyOf<any>;
|
|
70
80
|
export declare type TEnumKey<T = TKey> = {
|
|
71
81
|
type: 'number' | 'string';
|
|
72
82
|
const: T;
|
|
73
83
|
};
|
|
74
|
-
export declare type TDefinitions = {
|
|
75
|
-
[key: string]: TSchema;
|
|
76
|
-
};
|
|
77
84
|
export declare type TProperties = {
|
|
78
85
|
[key: string]: TSchema;
|
|
79
86
|
};
|
|
80
|
-
export declare type
|
|
81
|
-
kind: typeof BoxKind;
|
|
82
|
-
definitions: T;
|
|
83
|
-
} & CustomOptions;
|
|
84
|
-
export declare type TTuple<T extends TSchema[]> = {
|
|
87
|
+
export declare type TTuple<T> = Infer<T> & {
|
|
85
88
|
kind: typeof TupleKind;
|
|
86
89
|
type: 'array';
|
|
87
|
-
items?: [
|
|
90
|
+
items?: TSchema[];
|
|
88
91
|
additionalItems?: false;
|
|
89
92
|
minItems: number;
|
|
90
93
|
maxItems: number;
|
|
91
94
|
} & CustomOptions;
|
|
92
|
-
export declare type TObject<T
|
|
95
|
+
export declare type TObject<T> = Infer<T> & {
|
|
93
96
|
kind: typeof ObjectKind;
|
|
94
97
|
type: 'object';
|
|
95
|
-
properties:
|
|
98
|
+
properties: TProperties;
|
|
96
99
|
required?: string[];
|
|
97
100
|
} & ObjectOptions;
|
|
98
|
-
export declare type TUnion<T
|
|
101
|
+
export declare type TUnion<T> = Infer<T> & {
|
|
99
102
|
kind: typeof UnionKind;
|
|
100
|
-
anyOf: [
|
|
103
|
+
anyOf: TSchema[];
|
|
101
104
|
} & CustomOptions;
|
|
102
|
-
export declare type TIntersect<T
|
|
105
|
+
export declare type TIntersect<T> = Infer<T> & {
|
|
103
106
|
kind: typeof IntersectKind;
|
|
104
107
|
type: 'object';
|
|
105
|
-
allOf: [
|
|
108
|
+
allOf: TSchema[];
|
|
106
109
|
} & IntersectOptions;
|
|
107
|
-
export declare type TKeyOf<T
|
|
110
|
+
export declare type TKeyOf<T> = Infer<T> & {
|
|
108
111
|
kind: typeof KeyOfKind;
|
|
109
112
|
type: 'string';
|
|
110
|
-
enum: [
|
|
113
|
+
enum: string[];
|
|
111
114
|
} & CustomOptions;
|
|
112
|
-
export declare type TRecord<
|
|
115
|
+
export declare type TRecord<T> = Infer<T> & {
|
|
113
116
|
kind: typeof RecordKind;
|
|
114
117
|
type: 'object';
|
|
115
118
|
patternProperties: {
|
|
116
|
-
[pattern: string]:
|
|
119
|
+
[pattern: string]: TSchema;
|
|
117
120
|
};
|
|
118
121
|
} & ObjectOptions;
|
|
119
|
-
export declare type TArray<T
|
|
122
|
+
export declare type TArray<T> = Infer<T> & {
|
|
120
123
|
kind: typeof ArrayKind;
|
|
121
124
|
type: 'array';
|
|
122
|
-
items:
|
|
125
|
+
items: any;
|
|
123
126
|
} & ArrayOptions;
|
|
124
|
-
export declare type TLiteral<T
|
|
127
|
+
export declare type TLiteral<T> = Infer<T> & {
|
|
125
128
|
kind: typeof LiteralKind;
|
|
126
|
-
const:
|
|
129
|
+
const: TSchema;
|
|
127
130
|
} & CustomOptions;
|
|
128
|
-
export declare type TEnum<T
|
|
131
|
+
export declare type TEnum<T> = Infer<T> & {
|
|
129
132
|
kind: typeof EnumKind;
|
|
130
|
-
anyOf:
|
|
133
|
+
anyOf: TSchema;
|
|
131
134
|
} & CustomOptions;
|
|
132
|
-
export declare type TString = {
|
|
135
|
+
export declare type TString = Infer<string> & {
|
|
133
136
|
kind: typeof StringKind;
|
|
134
137
|
type: 'string';
|
|
135
138
|
} & StringOptions<string>;
|
|
136
|
-
export declare type TNumber = {
|
|
139
|
+
export declare type TNumber = Infer<number> & {
|
|
137
140
|
kind: typeof NumberKind;
|
|
138
141
|
type: 'number';
|
|
139
142
|
} & NumberOptions;
|
|
140
|
-
export declare type TInteger = {
|
|
143
|
+
export declare type TInteger = Infer<number> & {
|
|
141
144
|
kind: typeof IntegerKind;
|
|
142
145
|
type: 'integer';
|
|
143
146
|
} & NumberOptions;
|
|
144
|
-
export declare type TBoolean = {
|
|
147
|
+
export declare type TBoolean = Infer<boolean> & {
|
|
145
148
|
kind: typeof BooleanKind;
|
|
146
149
|
type: 'boolean';
|
|
147
150
|
} & CustomOptions;
|
|
148
|
-
export declare type TNull = {
|
|
151
|
+
export declare type TNull = Infer<null> & {
|
|
149
152
|
kind: typeof NullKind;
|
|
150
153
|
type: 'null';
|
|
151
154
|
} & CustomOptions;
|
|
152
|
-
export declare type TUnknown = {
|
|
155
|
+
export declare type TUnknown = Infer<unknown> & {
|
|
153
156
|
kind: typeof UnknownKind;
|
|
154
157
|
} & CustomOptions;
|
|
155
|
-
export declare type TAny = {
|
|
158
|
+
export declare type TAny = Infer<any> & {
|
|
156
159
|
kind: typeof AnyKind;
|
|
157
160
|
} & CustomOptions;
|
|
158
161
|
export declare const ConstructorKind: unique symbol;
|
|
@@ -160,41 +163,33 @@ export declare const FunctionKind: unique symbol;
|
|
|
160
163
|
export declare const PromiseKind: unique symbol;
|
|
161
164
|
export declare const UndefinedKind: unique symbol;
|
|
162
165
|
export declare const VoidKind: unique symbol;
|
|
163
|
-
export declare type TConstructor<T
|
|
166
|
+
export declare type TConstructor<T> = Infer<T> & {
|
|
164
167
|
kind: typeof ConstructorKind;
|
|
165
168
|
type: 'constructor';
|
|
166
|
-
arguments:
|
|
167
|
-
returns:
|
|
169
|
+
arguments: TSchema[];
|
|
170
|
+
returns: TSchema;
|
|
168
171
|
} & CustomOptions;
|
|
169
|
-
export declare type TFunction<T
|
|
172
|
+
export declare type TFunction<T> = Infer<T> & {
|
|
170
173
|
kind: typeof FunctionKind;
|
|
171
174
|
type: 'function';
|
|
172
|
-
arguments:
|
|
173
|
-
returns:
|
|
175
|
+
arguments: TSchema[];
|
|
176
|
+
returns: TSchema;
|
|
174
177
|
} & CustomOptions;
|
|
175
|
-
export declare type TPromise<T
|
|
178
|
+
export declare type TPromise<T> = Infer<T> & {
|
|
176
179
|
kind: typeof PromiseKind;
|
|
177
180
|
type: 'promise';
|
|
178
|
-
item:
|
|
181
|
+
item: TSchema;
|
|
179
182
|
} & CustomOptions;
|
|
180
|
-
export declare type TUndefined = {
|
|
183
|
+
export declare type TUndefined = Infer<undefined> & {
|
|
181
184
|
kind: typeof UndefinedKind;
|
|
182
185
|
type: 'undefined';
|
|
183
186
|
} & CustomOptions;
|
|
184
|
-
export declare type TVoid = {
|
|
187
|
+
export declare type TVoid = Infer<void> & {
|
|
185
188
|
kind: typeof VoidKind;
|
|
186
189
|
type: 'void';
|
|
187
190
|
} & CustomOptions;
|
|
188
|
-
export declare type TSchema = TIntersect<any> | TUnion<any> | TTuple<any> | TObject<any> | TKeyOf<any> | TRecord<any
|
|
189
|
-
export declare type TRequired<T extends TProperties> = {
|
|
190
|
-
[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];
|
|
191
|
-
};
|
|
192
|
-
export declare type TPartial<T extends TProperties> = {
|
|
193
|
-
[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]>;
|
|
194
|
-
};
|
|
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;
|
|
195
192
|
export declare type UnionToIntersect<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
196
|
-
export declare type ObjectPropertyKeys<T> = T extends TObject<infer U> ? PropertyKeys<U> : never;
|
|
197
|
-
export declare type PropertyKeys<T extends TProperties> = keyof T;
|
|
198
193
|
export declare type ReadonlyOptionalPropertyKeys<T extends TProperties> = {
|
|
199
194
|
[K in keyof T]: T[K] extends TReadonlyOptional<TSchema> ? K : never;
|
|
200
195
|
}[keyof T];
|
|
@@ -205,15 +200,12 @@ export declare type OptionalPropertyKeys<T extends TProperties> = {
|
|
|
205
200
|
[K in keyof T]: T[K] extends TOptional<TSchema> ? K : never;
|
|
206
201
|
}[keyof T];
|
|
207
202
|
export declare type RequiredPropertyKeys<T extends TProperties> = keyof Omit<T, ReadonlyOptionalPropertyKeys<T> | ReadonlyPropertyKeys<T> | OptionalPropertyKeys<T>>;
|
|
208
|
-
export declare type
|
|
209
|
-
[K in
|
|
210
|
-
};
|
|
211
|
-
export declare type StaticModifiers<T extends TProperties> = {
|
|
212
|
-
readonly [K in ReadonlyOptionalPropertyKeys<T>]?: T[K] extends TReadonlyOptional<infer U> ? Static<U> : never;
|
|
203
|
+
export declare type StaticProperties<T extends TProperties> = {
|
|
204
|
+
readonly [K in ReadonlyOptionalPropertyKeys<T>]?: Static<T[K]>;
|
|
213
205
|
} & {
|
|
214
|
-
readonly [K in ReadonlyPropertyKeys<T>]: T[K]
|
|
206
|
+
readonly [K in ReadonlyPropertyKeys<T>]: Static<T[K]>;
|
|
215
207
|
} & {
|
|
216
|
-
[K in OptionalPropertyKeys<T>]?: T[K]
|
|
208
|
+
[K in OptionalPropertyKeys<T>]?: Static<T[K]>;
|
|
217
209
|
} & {
|
|
218
210
|
[K in RequiredPropertyKeys<T>]: Static<T[K]>;
|
|
219
211
|
};
|
|
@@ -226,89 +218,89 @@ export declare type StaticUnion<T extends readonly TSchema[]> = {
|
|
|
226
218
|
export declare type StaticTuple<T extends readonly TSchema[]> = {
|
|
227
219
|
[K in keyof T]: Static<T[K]>;
|
|
228
220
|
};
|
|
229
|
-
export declare type StaticObject<T extends TProperties> =
|
|
230
|
-
export declare type StaticRecord<K extends TRecordKey, T extends TSchema> = K extends TString ?
|
|
231
|
-
[key: string]: Static<T>;
|
|
232
|
-
} : K extends TNumber ? {
|
|
233
|
-
[key: number]: Static<T>;
|
|
234
|
-
} : K extends TUnion<infer L> ? L extends TLiteral<any>[] ? {
|
|
235
|
-
[K in StaticUnion<L>]: Static<T>;
|
|
236
|
-
} : never : never;
|
|
221
|
+
export declare type StaticObject<T extends TProperties> = StaticProperties<StaticProperties<T>>;
|
|
222
|
+
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>> : never;
|
|
237
223
|
export declare type StaticArray<T extends TSchema> = Array<Static<T>>;
|
|
238
224
|
export declare type StaticLiteral<T extends TValue> = T;
|
|
239
|
-
export declare type StaticConstructor<T extends readonly TSchema[], U extends TSchema> = new (...args:
|
|
240
|
-
|
|
225
|
+
export declare type StaticConstructor<T extends readonly TSchema[], U extends TSchema> = new (...args: [...{
|
|
226
|
+
[K in keyof T]: Static<T[K]>;
|
|
227
|
+
}]) => Static<U>;
|
|
228
|
+
export declare type StaticFunction<T extends readonly TSchema[], U extends TSchema> = (...args: [...{
|
|
229
|
+
[K in keyof T]: Static<T[K]>;
|
|
230
|
+
}]) => Static<U>;
|
|
241
231
|
export declare type StaticPromise<T extends TSchema> = Promise<Static<T>>;
|
|
242
|
-
export declare type Static<T> = T extends TKeyOf<infer
|
|
232
|
+
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> ? {
|
|
233
|
+
[K in keyof I]: I[K];
|
|
234
|
+
} : 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;
|
|
243
235
|
export declare class TypeBuilder {
|
|
244
|
-
/** `
|
|
236
|
+
/** `standard` Modifies an object property to be both readonly and optional */
|
|
245
237
|
ReadonlyOptional<T extends TSchema>(item: T): TReadonlyOptional<T>;
|
|
246
|
-
/** `
|
|
238
|
+
/** `standard` Modifies an object property to be readonly */
|
|
247
239
|
Readonly<T extends TSchema>(item: T): TReadonly<T>;
|
|
248
|
-
/** `
|
|
240
|
+
/** `standard` Modifies an object property to be optional */
|
|
249
241
|
Optional<T extends TSchema>(item: T): TOptional<T>;
|
|
250
|
-
/** `
|
|
251
|
-
Tuple<T extends TSchema[]>(items: [...T], options?: CustomOptions): TTuple<T
|
|
252
|
-
/** `
|
|
253
|
-
Object<T extends TProperties>(properties: T, options?: ObjectOptions): TObject<T
|
|
254
|
-
/** `
|
|
255
|
-
Intersect<T extends TSchema[]>(items: [...T], options?: IntersectOptions): TIntersect<T
|
|
256
|
-
/** `
|
|
257
|
-
Union<T extends TSchema[]>(items: [...T], options?: CustomOptions): TUnion<T
|
|
258
|
-
/** `
|
|
259
|
-
Array<T extends TSchema>(items: T, options?: ArrayOptions): TArray<T
|
|
260
|
-
/** `
|
|
261
|
-
Enum<T extends TEnumType>(item: T, options?: CustomOptions): TEnum<TEnumKey<T[keyof T]>[]
|
|
262
|
-
/** `
|
|
263
|
-
Literal<T extends TValue>(value: T, options?: CustomOptions): TLiteral<T
|
|
264
|
-
/** `
|
|
242
|
+
/** `standard` Creates a type type */
|
|
243
|
+
Tuple<T extends TSchema[]>(items: [...T], options?: CustomOptions): TTuple<StaticTuple<T>>;
|
|
244
|
+
/** `standard` Creates an object type with the given properties */
|
|
245
|
+
Object<T extends TProperties>(properties: T, options?: ObjectOptions): TObject<StaticProperties<T>>;
|
|
246
|
+
/** `standard` Creates an intersection type. Note this function requires draft `2019-09` to constrain with `unevaluatedProperties` */
|
|
247
|
+
Intersect<T extends TSchema[]>(items: [...T], options?: IntersectOptions): TIntersect<StaticIntersect<T>>;
|
|
248
|
+
/** `standard` Creates a union type */
|
|
249
|
+
Union<T extends TSchema[]>(items: [...T], options?: CustomOptions): TUnion<StaticUnion<T>>;
|
|
250
|
+
/** `standard` Creates an array type */
|
|
251
|
+
Array<T extends TSchema>(items: T, options?: ArrayOptions): TArray<StaticArray<T>>;
|
|
252
|
+
/** `standard` Creates an enum type from a TypeScript enum */
|
|
253
|
+
Enum<T extends TEnumType>(item: T, options?: CustomOptions): TEnum<StaticEnum<TEnumKey<T[keyof T]>[]>>;
|
|
254
|
+
/** `standard` Creates a literal type. Supports string, number and boolean values only */
|
|
255
|
+
Literal<T extends TValue>(value: T, options?: CustomOptions): TLiteral<StaticLiteral<T>>;
|
|
256
|
+
/** `standard` Creates a string type */
|
|
265
257
|
String<TCustomFormatOption extends string>(options?: StringOptions<StringFormatOption | TCustomFormatOption>): TString;
|
|
266
|
-
/** `
|
|
258
|
+
/** `standard` Creates a string type from a regular expression */
|
|
267
259
|
RegEx(regex: RegExp, options?: CustomOptions): TString;
|
|
268
|
-
/** `
|
|
260
|
+
/** `standard` Creates a number type */
|
|
269
261
|
Number(options?: NumberOptions): TNumber;
|
|
270
|
-
/** `
|
|
262
|
+
/** `standard` Creates an integer type */
|
|
271
263
|
Integer(options?: NumberOptions): TInteger;
|
|
272
|
-
/** `
|
|
264
|
+
/** `standard` Creates a boolean type */
|
|
273
265
|
Boolean(options?: CustomOptions): TBoolean;
|
|
274
|
-
/** `
|
|
266
|
+
/** `standard` Creates a null type */
|
|
275
267
|
Null(options?: CustomOptions): TNull;
|
|
276
|
-
/** `
|
|
268
|
+
/** `standard` Creates an unknown type */
|
|
277
269
|
Unknown(options?: CustomOptions): TUnknown;
|
|
278
|
-
/** `
|
|
270
|
+
/** `standard` Creates an any type */
|
|
279
271
|
Any(options?: CustomOptions): TAny;
|
|
280
|
-
/** `
|
|
281
|
-
KeyOf<T extends TObject<
|
|
282
|
-
/** `
|
|
283
|
-
Record<K extends TRecordKey, T extends TSchema>(key: K, value: T, options?: ObjectOptions): TRecord<K, T
|
|
284
|
-
/** `
|
|
285
|
-
Required<T extends TObject<
|
|
286
|
-
/** `
|
|
287
|
-
Partial<T extends TObject<
|
|
288
|
-
/** `
|
|
289
|
-
Pick<T extends TObject<
|
|
290
|
-
/** `
|
|
291
|
-
Omit<T extends TObject<
|
|
292
|
-
/** `
|
|
272
|
+
/** `standard` Creates a keyof type from the given object */
|
|
273
|
+
KeyOf<T extends TObject<any>>(schema: T, options?: CustomOptions): TKeyOf<keyof T['_infer']>;
|
|
274
|
+
/** `standard` Creates a record type */
|
|
275
|
+
Record<K extends TRecordKey, T extends TSchema>(key: K, value: T, options?: ObjectOptions): TRecord<StaticRecord<K, T>>;
|
|
276
|
+
/** `standard` Makes all properties in the given object type required */
|
|
277
|
+
Required<T extends TObject<any>>(schema: T, options?: ObjectOptions): TObject<Required<T['_infer']>>;
|
|
278
|
+
/** `standard` Makes all properties in the given object type optional */
|
|
279
|
+
Partial<T extends TObject<any>>(schema: T, options?: ObjectOptions): TObject<Partial<T['_infer']>>;
|
|
280
|
+
/** `standard` Picks property keys from the given object type */
|
|
281
|
+
Pick<T extends TObject<any>, K extends (keyof T['_infer'])[]>(schema: T, keys: [...K], options?: ObjectOptions): TObject<Pick<T['_infer'], K[number]>>;
|
|
282
|
+
/** `standard` Omits property keys from the given object type */
|
|
283
|
+
Omit<T extends TObject<any>, K extends (keyof T['_infer'])[]>(schema: T, keys: [...K], options?: ObjectOptions): TObject<Omit<T['_infer'], K[number]>>;
|
|
284
|
+
/** `standard` Omits the `kind` and `modifier` properties from the underlying schema */
|
|
293
285
|
Strict<T extends TSchema>(schema: T, options?: CustomOptions): T;
|
|
294
|
-
/** `
|
|
295
|
-
Constructor<T extends TSchema[], U extends TSchema>(args: [...T], returns: U, options?: CustomOptions): TConstructor<T, U
|
|
296
|
-
/** `
|
|
297
|
-
Function<T extends TSchema[], U extends TSchema>(args: [...T], returns: U, options?: CustomOptions): TFunction<T, U
|
|
298
|
-
/** `
|
|
299
|
-
Promise<T extends TSchema>(item: T, options?: CustomOptions): TPromise<T
|
|
300
|
-
/** `
|
|
286
|
+
/** `extended` Creates a constructor type */
|
|
287
|
+
Constructor<T extends TSchema[], U extends TSchema>(args: [...T], returns: U, options?: CustomOptions): TConstructor<StaticConstructor<T, U>>;
|
|
288
|
+
/** `extended` Creates a function type */
|
|
289
|
+
Function<T extends TSchema[], U extends TSchema>(args: [...T], returns: U, options?: CustomOptions): TFunction<StaticFunction<T, U>>;
|
|
290
|
+
/** `extended` Creates a promise type */
|
|
291
|
+
Promise<T extends TSchema>(item: T, options?: CustomOptions): TPromise<StaticPromise<T>>;
|
|
292
|
+
/** `extended` Creates a undefined type */
|
|
301
293
|
Undefined(options?: CustomOptions): TUndefined;
|
|
302
|
-
/** `
|
|
294
|
+
/** `extended` Creates a void type */
|
|
303
295
|
Void(options?: CustomOptions): TVoid;
|
|
304
|
-
/** `
|
|
296
|
+
/** `experimental` Creates a recursive type */
|
|
305
297
|
Rec<T extends TSchema>(callback: (self: TAny) => T, options?: CustomOptions): T;
|
|
306
|
-
/** `
|
|
307
|
-
/** `
|
|
308
|
-
|
|
309
|
-
/** `
|
|
310
|
-
Ref<T extends
|
|
311
|
-
/** `
|
|
298
|
+
/** `experimental` Creates a recursive type. Pending https://github.com/ajv-validator/ajv/issues/1709 */
|
|
299
|
+
/** `experimental` Creates a namespace for a set of related types */
|
|
300
|
+
Namespace<T extends TDefinitions>(definitions: T, options?: CustomOptions): TNamespace<T>;
|
|
301
|
+
/** `experimental` References a type within a namespace. The referenced namespace must specify an `$id` */
|
|
302
|
+
Ref<T extends TNamespace<TDefinitions>, K extends keyof T['definitions']>(box: T, key: K): T['definitions'][K];
|
|
303
|
+
/** `experimental` References type. The referenced type must specify an `$id` */
|
|
312
304
|
Ref<T extends TSchema>(schema: T): T;
|
|
313
305
|
}
|
|
314
306
|
export declare const Type: TypeBuilder;
|
package/typebox.js
CHANGED
|
@@ -82,19 +82,19 @@ function clone(object) {
|
|
|
82
82
|
// TypeBuilder
|
|
83
83
|
// ------------------------------------------------------------------------
|
|
84
84
|
class TypeBuilder {
|
|
85
|
-
/** `
|
|
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
|
-
/** `
|
|
89
|
+
/** `standard` Modifies an object property to be readonly */
|
|
90
90
|
Readonly(item) {
|
|
91
91
|
return { ...item, modifier: exports.ReadonlyModifier };
|
|
92
92
|
}
|
|
93
|
-
/** `
|
|
93
|
+
/** `standard` Modifies an object property to be optional */
|
|
94
94
|
Optional(item) {
|
|
95
95
|
return { ...item, modifier: exports.OptionalModifier };
|
|
96
96
|
}
|
|
97
|
-
/** `
|
|
97
|
+
/** `standard` Creates a type type */
|
|
98
98
|
Tuple(items, options = {}) {
|
|
99
99
|
const additionalItems = false;
|
|
100
100
|
const minItems = items.length;
|
|
@@ -103,7 +103,7 @@ class TypeBuilder {
|
|
|
103
103
|
? { ...options, kind: exports.TupleKind, type: 'array', items, additionalItems, minItems, maxItems }
|
|
104
104
|
: { ...options, kind: exports.TupleKind, type: 'array', minItems, maxItems };
|
|
105
105
|
}
|
|
106
|
-
/** `
|
|
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 => {
|
|
@@ -118,73 +118,73 @@ class TypeBuilder {
|
|
|
118
118
|
{ ...options, kind: exports.ObjectKind, type: 'object', properties, required } :
|
|
119
119
|
{ ...options, kind: exports.ObjectKind, type: 'object', properties };
|
|
120
120
|
}
|
|
121
|
-
/** `
|
|
121
|
+
/** `standard` Creates an intersection type. Note this function requires draft `2019-09` to constrain with `unevaluatedProperties` */
|
|
122
122
|
Intersect(items, options = {}) {
|
|
123
123
|
return { ...options, kind: exports.IntersectKind, type: 'object', allOf: items };
|
|
124
124
|
}
|
|
125
|
-
/** `
|
|
125
|
+
/** `standard` Creates a union type */
|
|
126
126
|
Union(items, options = {}) {
|
|
127
127
|
return { ...options, kind: exports.UnionKind, anyOf: items };
|
|
128
128
|
}
|
|
129
|
-
/** `
|
|
129
|
+
/** `standard` Creates an array type */
|
|
130
130
|
Array(items, options = {}) {
|
|
131
131
|
return { ...options, kind: exports.ArrayKind, type: 'array', items };
|
|
132
132
|
}
|
|
133
|
-
/** `
|
|
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
|
-
/** `
|
|
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
|
-
/** `
|
|
143
|
+
/** `standard` Creates a string type */
|
|
144
144
|
String(options = {}) {
|
|
145
145
|
return { ...options, kind: exports.StringKind, type: 'string' };
|
|
146
146
|
}
|
|
147
|
-
/** `
|
|
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
|
-
/** `
|
|
151
|
+
/** `standard` Creates a number type */
|
|
152
152
|
Number(options = {}) {
|
|
153
153
|
return { ...options, kind: exports.NumberKind, type: 'number' };
|
|
154
154
|
}
|
|
155
|
-
/** `
|
|
155
|
+
/** `standard` Creates an integer type */
|
|
156
156
|
Integer(options = {}) {
|
|
157
157
|
return { ...options, kind: exports.IntegerKind, type: 'integer' };
|
|
158
158
|
}
|
|
159
|
-
/** `
|
|
159
|
+
/** `standard` Creates a boolean type */
|
|
160
160
|
Boolean(options = {}) {
|
|
161
161
|
return { ...options, kind: exports.BooleanKind, type: 'boolean' };
|
|
162
162
|
}
|
|
163
|
-
/** `
|
|
163
|
+
/** `standard` Creates a null type */
|
|
164
164
|
Null(options = {}) {
|
|
165
165
|
return { ...options, kind: exports.NullKind, type: 'null' };
|
|
166
166
|
}
|
|
167
|
-
/** `
|
|
167
|
+
/** `standard` Creates an unknown type */
|
|
168
168
|
Unknown(options = {}) {
|
|
169
169
|
return { ...options, kind: exports.UnknownKind };
|
|
170
170
|
}
|
|
171
|
-
/** `
|
|
171
|
+
/** `standard` Creates an any type */
|
|
172
172
|
Any(options = {}) {
|
|
173
173
|
return { ...options, kind: exports.AnyKind };
|
|
174
174
|
}
|
|
175
|
-
/** `
|
|
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
|
-
/** `
|
|
180
|
+
/** `standard` Creates a record type */
|
|
181
181
|
Record(key, value, options = {}) {
|
|
182
|
-
const pattern = key.kind === exports.
|
|
182
|
+
const pattern = key.kind === exports.KeyOfKind ? `^${key.enum.join('|')}$` :
|
|
183
183
|
key.kind === exports.NumberKind ? '^(0|[1-9][0-9]*)$' :
|
|
184
184
|
key.pattern ? key.pattern : '^.*$';
|
|
185
185
|
return { ...options, kind: exports.RecordKind, type: 'object', patternProperties: { [pattern]: value } };
|
|
186
186
|
}
|
|
187
|
-
/** `
|
|
187
|
+
/** `standard` Makes all properties in the given object type required */
|
|
188
188
|
Required(schema, options = {}) {
|
|
189
189
|
const next = { ...clone(schema), ...options };
|
|
190
190
|
next.required = Object.keys(next.properties);
|
|
@@ -207,7 +207,7 @@ class TypeBuilder {
|
|
|
207
207
|
}
|
|
208
208
|
return next;
|
|
209
209
|
}
|
|
210
|
-
/** `
|
|
210
|
+
/** `standard` Makes all properties in the given object type optional */
|
|
211
211
|
Partial(schema, options = {}) {
|
|
212
212
|
const next = { ...clone(schema), ...options };
|
|
213
213
|
delete next.required;
|
|
@@ -230,7 +230,7 @@ class TypeBuilder {
|
|
|
230
230
|
}
|
|
231
231
|
return next;
|
|
232
232
|
}
|
|
233
|
-
/** `
|
|
233
|
+
/** `standard` Picks property keys from the given object type */
|
|
234
234
|
Pick(schema, keys, options = {}) {
|
|
235
235
|
const next = { ...clone(schema), ...options };
|
|
236
236
|
next.required = next.required ? next.required.filter((key) => keys.includes(key)) : undefined;
|
|
@@ -240,7 +240,7 @@ class TypeBuilder {
|
|
|
240
240
|
}
|
|
241
241
|
return next;
|
|
242
242
|
}
|
|
243
|
-
/** `
|
|
243
|
+
/** `standard` Omits property keys from the given object type */
|
|
244
244
|
Omit(schema, keys, options = {}) {
|
|
245
245
|
const next = { ...clone(schema), ...options };
|
|
246
246
|
next.required = next.required ? next.required.filter((key) => !keys.includes(key)) : undefined;
|
|
@@ -250,46 +250,45 @@ class TypeBuilder {
|
|
|
250
250
|
}
|
|
251
251
|
return next;
|
|
252
252
|
}
|
|
253
|
-
/** `
|
|
253
|
+
/** `standard` Omits the `kind` and `modifier` properties from the underlying schema */
|
|
254
254
|
Strict(schema, options = {}) {
|
|
255
255
|
return JSON.parse(JSON.stringify({ ...options, ...schema }));
|
|
256
256
|
}
|
|
257
|
-
/** `
|
|
257
|
+
/** `extended` Creates a constructor type */
|
|
258
258
|
Constructor(args, returns, options = {}) {
|
|
259
259
|
return { ...options, kind: exports.ConstructorKind, type: 'constructor', arguments: args, returns };
|
|
260
260
|
}
|
|
261
|
-
/** `
|
|
261
|
+
/** `extended` Creates a function type */
|
|
262
262
|
Function(args, returns, options = {}) {
|
|
263
263
|
return { ...options, kind: exports.FunctionKind, type: 'function', arguments: args, returns };
|
|
264
264
|
}
|
|
265
|
-
/** `
|
|
265
|
+
/** `extended` Creates a promise type */
|
|
266
266
|
Promise(item, options = {}) {
|
|
267
267
|
return { ...options, type: 'promise', kind: exports.PromiseKind, item };
|
|
268
268
|
}
|
|
269
|
-
/** `
|
|
269
|
+
/** `extended` Creates a undefined type */
|
|
270
270
|
Undefined(options = {}) {
|
|
271
271
|
return { ...options, type: 'undefined', kind: exports.UndefinedKind };
|
|
272
272
|
}
|
|
273
|
-
/** `
|
|
273
|
+
/** `extended` Creates a void type */
|
|
274
274
|
Void(options = {}) {
|
|
275
275
|
return { ...options, type: 'void', kind: exports.VoidKind };
|
|
276
276
|
}
|
|
277
|
-
/** `
|
|
277
|
+
/** `experimental` Creates a recursive type */
|
|
278
278
|
Rec(callback, options = {}) {
|
|
279
279
|
const $id = options.$id || '';
|
|
280
280
|
const self = callback({ $ref: `${$id}#/definitions/self` });
|
|
281
281
|
return { ...options, $ref: `${$id}#/definitions/self`, definitions: { self } };
|
|
282
282
|
}
|
|
283
|
-
/** `
|
|
283
|
+
/** `experimental` Creates a recursive type. Pending https://github.com/ajv-validator/ajv/issues/1709 */
|
|
284
284
|
// public Rec<T extends TProperties>($id: string, callback: (self: TAny) => T, options: ObjectOptions = {}): TObject<T> {
|
|
285
285
|
// const properties = callback({ $recursiveRef: `${$id}` } as any)
|
|
286
286
|
// return { ...options, kind: ObjectKind, $id, $recursiveAnchor: true, type: 'object', properties }
|
|
287
287
|
// }
|
|
288
|
-
/** `
|
|
289
|
-
|
|
288
|
+
/** `experimental` Creates a namespace for a set of related types */
|
|
289
|
+
Namespace(definitions, options = {}) {
|
|
290
290
|
return { ...options, kind: exports.BoxKind, definitions };
|
|
291
291
|
}
|
|
292
|
-
/** `EXPERIMENTAL` References a schema. */
|
|
293
292
|
Ref(...args) {
|
|
294
293
|
const $id = args[0]['$id'] || '';
|
|
295
294
|
const key = args[1];
|