@sinclair/typebox 0.24.50 → 0.25.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/compiler/compiler.d.ts +1 -1
- package/compiler/compiler.js +15 -2
- package/conditional/structural.js +23 -0
- package/errors/errors.d.ts +43 -38
- package/errors/errors.js +62 -38
- package/guard/guard.d.ts +2 -0
- package/guard/guard.js +96 -14
- package/package.json +1 -1
- package/readme.md +245 -161
- package/typebox.d.ts +68 -49
- package/typebox.js +48 -43
- package/value/cast.js +6 -1
- package/value/check.js +21 -1
- package/value/clone.js +10 -4
- package/value/create.js +13 -0
- package/value/delta.js +8 -0
- package/value/equal.js +6 -0
- package/value/is.d.ts +1 -0
- package/value/is.js +5 -1
package/typebox.d.ts
CHANGED
|
@@ -32,7 +32,7 @@ export interface TSchema extends SchemaOptions {
|
|
|
32
32
|
params: unknown[];
|
|
33
33
|
static: unknown;
|
|
34
34
|
}
|
|
35
|
-
export declare type TAnySchema = TSchema | TAny | TArray | TBoolean | TConstructor | TEnum | TFunction | TInteger | TLiteral | TNull | TNumber | TObject | TPromise | TRecord | TSelf | TRef | TString | TTuple | TUndefined | TUnion | TUint8Array | TUnknown | TVoid;
|
|
35
|
+
export declare type TAnySchema = TSchema | TAny | TArray | TBoolean | TConstructor | TDate | TEnum | TFunction | TInteger | TLiteral | TNull | TNumber | TObject | TPromise | TRecord | TSelf | TRef | TString | TTuple | TUndefined | TUnion | TUint8Array | TUnknown | TVoid;
|
|
36
36
|
export interface NumericOptions extends SchemaOptions {
|
|
37
37
|
exclusiveMaximum?: number;
|
|
38
38
|
exclusiveMinimum?: number;
|
|
@@ -69,10 +69,23 @@ export declare type StaticContructorParameters<T extends readonly TSchema[], P e
|
|
|
69
69
|
export interface TConstructor<T extends TSchema[] = TSchema[], U extends TSchema = TSchema> extends TSchema {
|
|
70
70
|
[Kind]: 'Constructor';
|
|
71
71
|
static: new (...param: StaticContructorParameters<T, this['params']>) => Static<U, this['params']>;
|
|
72
|
-
type: '
|
|
72
|
+
type: 'object';
|
|
73
|
+
instanceOf: 'Constructor';
|
|
73
74
|
parameters: T;
|
|
74
75
|
returns: U;
|
|
75
76
|
}
|
|
77
|
+
export interface DateOptions extends SchemaOptions {
|
|
78
|
+
exclusiveMaximumTimestamp?: number;
|
|
79
|
+
exclusiveMinimumTimestamp?: number;
|
|
80
|
+
maximumTimestamp?: number;
|
|
81
|
+
minimumTimestamp?: number;
|
|
82
|
+
}
|
|
83
|
+
export interface TDate extends TSchema, DateOptions {
|
|
84
|
+
[Kind]: 'Date';
|
|
85
|
+
static: Date;
|
|
86
|
+
type: 'object';
|
|
87
|
+
instanceOf: 'Date';
|
|
88
|
+
}
|
|
76
89
|
export interface TEnumOption<T> {
|
|
77
90
|
type: 'number' | 'string';
|
|
78
91
|
const: T;
|
|
@@ -90,7 +103,8 @@ export declare type StaticFunctionParameters<T extends readonly TSchema[], P ext
|
|
|
90
103
|
export interface TFunction<T extends readonly TSchema[] = TSchema[], U extends TSchema = TSchema> extends TSchema {
|
|
91
104
|
[Kind]: 'Function';
|
|
92
105
|
static: (...param: StaticFunctionParameters<T, this['params']>) => Static<U, this['params']>;
|
|
93
|
-
type: '
|
|
106
|
+
type: 'object';
|
|
107
|
+
instanceOf: 'Function';
|
|
94
108
|
parameters: T;
|
|
95
109
|
returns: U;
|
|
96
110
|
}
|
|
@@ -207,10 +221,11 @@ export declare type TPick<T extends TObject, Properties extends ObjectPropertyKe
|
|
|
207
221
|
export interface TPromise<T extends TSchema = TSchema> extends TSchema {
|
|
208
222
|
[Kind]: 'Promise';
|
|
209
223
|
static: Promise<Static<T, this['params']>>;
|
|
210
|
-
type: '
|
|
224
|
+
type: 'object';
|
|
225
|
+
instanceOf: 'Promise';
|
|
211
226
|
item: TSchema;
|
|
212
227
|
}
|
|
213
|
-
export declare type TRecordKey = TString |
|
|
228
|
+
export declare type TRecordKey = TString | TNumeric | TUnion<TLiteral<any>[]>;
|
|
214
229
|
export interface TRecord<K extends TRecordKey = TRecordKey, T extends TSchema = TSchema> extends TSchema {
|
|
215
230
|
[Kind]: 'Record';
|
|
216
231
|
static: Record<Static<K>, Static<T, this['params']>>;
|
|
@@ -268,9 +283,9 @@ export interface TTuple<T extends TSchema[] = TSchema[]> extends TSchema {
|
|
|
268
283
|
}
|
|
269
284
|
export interface TUndefined extends TSchema {
|
|
270
285
|
[Kind]: 'Undefined';
|
|
271
|
-
specialized: 'Undefined';
|
|
272
286
|
static: undefined;
|
|
273
|
-
type: '
|
|
287
|
+
type: 'null';
|
|
288
|
+
typeOf: 'Undefined';
|
|
274
289
|
}
|
|
275
290
|
export interface TUnion<T extends TSchema[] = TSchema[]> extends TSchema {
|
|
276
291
|
[Kind]: 'Union';
|
|
@@ -286,7 +301,7 @@ export interface Uint8ArrayOptions extends SchemaOptions {
|
|
|
286
301
|
export interface TUint8Array extends TSchema, Uint8ArrayOptions {
|
|
287
302
|
[Kind]: 'Uint8Array';
|
|
288
303
|
static: Uint8Array;
|
|
289
|
-
|
|
304
|
+
instanceOf: 'Uint8Array';
|
|
290
305
|
type: 'object';
|
|
291
306
|
}
|
|
292
307
|
export interface TUnknown extends TSchema {
|
|
@@ -304,6 +319,7 @@ export interface TVoid extends TSchema {
|
|
|
304
319
|
[Kind]: 'Void';
|
|
305
320
|
static: void;
|
|
306
321
|
type: 'null';
|
|
322
|
+
typeOf: 'Void';
|
|
307
323
|
}
|
|
308
324
|
/** Creates a static type from a TypeBox type */
|
|
309
325
|
export declare type Static<T extends TSchema, P extends unknown[] = []> = (T & {
|
|
@@ -316,88 +332,91 @@ export declare class TypeBuilder {
|
|
|
316
332
|
Readonly<T extends TSchema>(item: T): TReadonly<T>;
|
|
317
333
|
/** Creates a optional property */
|
|
318
334
|
Optional<T extends TSchema>(item: T): TOptional<T>;
|
|
319
|
-
/** Creates a any type */
|
|
335
|
+
/** `Standard` Creates a any type */
|
|
320
336
|
Any(options?: SchemaOptions): TAny;
|
|
321
|
-
/** Creates a array type */
|
|
337
|
+
/** `Standard` Creates a array type */
|
|
322
338
|
Array<T extends TSchema>(items: T, options?: ArrayOptions): TArray<T>;
|
|
323
|
-
/** Creates a boolean type */
|
|
339
|
+
/** `Standard` Creates a boolean type */
|
|
324
340
|
Boolean(options?: SchemaOptions): TBoolean;
|
|
325
|
-
/** Creates a tuple type from this constructors parameters */
|
|
341
|
+
/** `Extended` Creates a tuple type from this constructors parameters */
|
|
326
342
|
ConstructorParameters<T extends TConstructor<any[], any>>(schema: T, options?: SchemaOptions): TConstructorParameters<T>;
|
|
327
|
-
/** Creates a constructor type */
|
|
343
|
+
/** `Extended` Creates a constructor type */
|
|
328
344
|
Constructor<T extends TTuple<TSchema[]>, U extends TSchema>(parameters: T, returns: U, options?: SchemaOptions): TConstructor<TupleToArray<T>, U>;
|
|
329
|
-
/** Creates a constructor type */
|
|
345
|
+
/** `Extended` Creates a constructor type */
|
|
330
346
|
Constructor<T extends TSchema[], U extends TSchema>(parameters: [...T], returns: U, options?: SchemaOptions): TConstructor<T, U>;
|
|
331
|
-
/** Creates a
|
|
347
|
+
/** `Extended` Creates a Date type */
|
|
348
|
+
Date(options?: DateOptions): TDate;
|
|
349
|
+
/** `Standard` Creates a enum type */
|
|
332
350
|
Enum<T extends Record<string, string | number>>(item: T, options?: SchemaOptions): TEnum<T>;
|
|
333
|
-
/** Creates a function type */
|
|
351
|
+
/** `Extended` Creates a function type */
|
|
334
352
|
Function<T extends TTuple<TSchema[]>, U extends TSchema>(parameters: T, returns: U, options?: SchemaOptions): TFunction<TupleToArray<T>, U>;
|
|
335
|
-
/** Creates a function type */
|
|
353
|
+
/** `Extended` Creates a function type */
|
|
336
354
|
Function<T extends TSchema[], U extends TSchema>(parameters: [...T], returns: U, options?: SchemaOptions): TFunction<T, U>;
|
|
337
|
-
/** Creates a type from this constructors instance type */
|
|
355
|
+
/** `Extended` Creates a type from this constructors instance type */
|
|
338
356
|
InstanceType<T extends TConstructor<any[], any>>(schema: T, options?: SchemaOptions): TInstanceType<T>;
|
|
339
|
-
/** Creates a integer type */
|
|
357
|
+
/** `Standard` Creates a integer type */
|
|
340
358
|
Integer(options?: NumericOptions): TInteger;
|
|
341
|
-
/** Creates a intersect type. */
|
|
359
|
+
/** `Standard` Creates a intersect type. */
|
|
342
360
|
Intersect<T extends TObject[]>(objects: [...T], options?: ObjectOptions): TIntersect<T>;
|
|
343
|
-
/** Creates a keyof type */
|
|
361
|
+
/** `Standard` Creates a keyof type */
|
|
344
362
|
KeyOf<T extends TObject>(object: T, options?: SchemaOptions): TKeyOf<T>;
|
|
345
|
-
/** Creates a literal type. */
|
|
363
|
+
/** `Standard` Creates a literal type. */
|
|
346
364
|
Literal<T extends TLiteralValue>(value: T, options?: SchemaOptions): TLiteral<T>;
|
|
347
|
-
/** Creates a never type */
|
|
365
|
+
/** `Standard` Creates a never type */
|
|
348
366
|
Never(options?: SchemaOptions): TNever;
|
|
349
|
-
/** Creates a null type */
|
|
367
|
+
/** `Standard` Creates a null type */
|
|
350
368
|
Null(options?: SchemaOptions): TNull;
|
|
351
|
-
/** Creates a number type */
|
|
369
|
+
/** `Standard` Creates a number type */
|
|
352
370
|
Number(options?: NumericOptions): TNumber;
|
|
353
|
-
/** Creates an object type
|
|
371
|
+
/** `Standard` Creates an object type */
|
|
354
372
|
Object<T extends TProperties>(properties: T, options?: ObjectOptions): TObject<T>;
|
|
355
|
-
/** Creates a new object whose
|
|
373
|
+
/** `Standard` Creates a new object type whose keys are omitted from the given source type */
|
|
356
374
|
Omit<T extends TObject, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: ObjectOptions): TOmit<T, UnionStringLiteralToTuple<K>>;
|
|
357
|
-
/** Creates a new object whose
|
|
375
|
+
/** `Standard` Creates a new object type whose keys are omitted from the given source type */
|
|
358
376
|
Omit<T extends TObject, K extends ObjectPropertyKeys<T>[]>(schema: T, keys: readonly [...K], options?: ObjectOptions): TOmit<T, K>;
|
|
359
|
-
/** Creates a tuple type from this functions parameters */
|
|
377
|
+
/** `Extended` Creates a tuple type from this functions parameters */
|
|
360
378
|
Parameters<T extends TFunction<any[], any>>(schema: T, options?: SchemaOptions): TParameters<T>;
|
|
361
|
-
/** Creates an object type whose properties are all optional */
|
|
379
|
+
/** `Standard` Creates an object type whose properties are all optional */
|
|
362
380
|
Partial<T extends TObject>(schema: T, options?: ObjectOptions): TPartial<T>;
|
|
363
|
-
/** Creates a object whose
|
|
381
|
+
/** `Standard` Creates a new object type whose keys are picked from the given source type */
|
|
364
382
|
Pick<T extends TObject, K extends TUnion<TLiteral<string>[]>>(schema: T, keys: K, options?: ObjectOptions): TPick<T, UnionStringLiteralToTuple<K>>;
|
|
365
|
-
/** Creates a object whose
|
|
383
|
+
/** `Standard` Creates a new object type whose keys are picked from the given source type */
|
|
366
384
|
Pick<T extends TObject, K extends ObjectPropertyKeys<T>[]>(schema: T, keys: readonly [...K], options?: ObjectOptions): TPick<T, K>;
|
|
367
|
-
/** Creates a
|
|
385
|
+
/** `Extended` Creates a Promise type */
|
|
368
386
|
Promise<T extends TSchema>(item: T, options?: SchemaOptions): TPromise<T>;
|
|
369
|
-
/** Creates an object whose properties are derived from the given string literal union. */
|
|
387
|
+
/** `Standard` Creates an object whose properties are derived from the given string literal union. */
|
|
370
388
|
Record<K extends TUnion<TLiteral[]>, T extends TSchema>(key: K, schema: T, options?: ObjectOptions): TObject<TRecordProperties<K, T>>;
|
|
371
|
-
/** Creates a record type */
|
|
372
|
-
Record<K extends TString |
|
|
373
|
-
/** Creates
|
|
389
|
+
/** `Standard` Creates a record type */
|
|
390
|
+
Record<K extends TString | TNumeric, T extends TSchema>(key: K, schema: T, options?: ObjectOptions): TRecord<K, T>;
|
|
391
|
+
/** `Standard` Creates recursive type */
|
|
374
392
|
Recursive<T extends TSchema>(callback: (self: TSelf) => T, options?: SchemaOptions): TRecursive<T>;
|
|
375
|
-
/** Creates a reference
|
|
393
|
+
/** `Standard` Creates a reference type. The referenced type must contain a $id. */
|
|
376
394
|
Ref<T extends TSchema>(schema: T, options?: SchemaOptions): TRef<T>;
|
|
377
|
-
/** Creates a string type from a regular expression */
|
|
395
|
+
/** `Standard` Creates a string type from a regular expression */
|
|
378
396
|
RegEx(regex: RegExp, options?: SchemaOptions): TString;
|
|
379
|
-
/** Creates an object type whose properties are all required */
|
|
397
|
+
/** `Standard` Creates an object type whose properties are all required */
|
|
380
398
|
Required<T extends TObject>(schema: T, options?: SchemaOptions): TRequired<T>;
|
|
381
|
-
/** Creates a type from this functions return type */
|
|
399
|
+
/** `Extended` Creates a type from this functions return type */
|
|
382
400
|
ReturnType<T extends TFunction<any[], any>>(schema: T, options?: SchemaOptions): TReturnType<T>;
|
|
383
401
|
/** Removes Kind and Modifier symbol property keys from this schema */
|
|
384
402
|
Strict<T extends TSchema>(schema: T): T;
|
|
385
|
-
/** Creates a string type */
|
|
403
|
+
/** `Standard` Creates a string type */
|
|
386
404
|
String<Format extends string>(options?: StringOptions<StringFormatOption | Format>): TString<Format>;
|
|
387
|
-
/** Creates a tuple type */
|
|
405
|
+
/** `Standard` Creates a tuple type */
|
|
388
406
|
Tuple<T extends TSchema[]>(items: [...T], options?: SchemaOptions): TTuple<T>;
|
|
389
|
-
/** Creates a undefined type */
|
|
407
|
+
/** `Extended` Creates a undefined type */
|
|
390
408
|
Undefined(options?: SchemaOptions): TUndefined;
|
|
391
|
-
/** Creates a union type */
|
|
409
|
+
/** `Standard` Creates a union type */
|
|
392
410
|
Union(items: [], options?: SchemaOptions): TNever;
|
|
411
|
+
/** `Standard` Creates a union type */
|
|
393
412
|
Union<T extends TSchema[]>(items: [...T], options?: SchemaOptions): TUnion<T>;
|
|
394
|
-
/** Creates a Uint8Array type */
|
|
413
|
+
/** `Extended` Creates a Uint8Array type */
|
|
395
414
|
Uint8Array(options?: Uint8ArrayOptions): TUint8Array;
|
|
396
|
-
/** Creates an unknown type */
|
|
415
|
+
/** `Standard` Creates an unknown type */
|
|
397
416
|
Unknown(options?: SchemaOptions): TUnknown;
|
|
398
|
-
/** Creates a user defined schema that infers as type T */
|
|
417
|
+
/** `Standard` Creates a user defined schema that infers as type T */
|
|
399
418
|
Unsafe<T>(options?: UnsafeOptions): TUnsafe<T>;
|
|
400
|
-
/** Creates a void type */
|
|
419
|
+
/** `Extended` Creates a void type */
|
|
401
420
|
Void(options?: SchemaOptions): TVoid;
|
|
402
421
|
/** Use this function to return TSchema with static and params omitted */
|
|
403
422
|
protected Create<T>(schema: Omit<T, 'static' | 'params'>): T;
|
package/typebox.js
CHANGED
|
@@ -57,36 +57,40 @@ class TypeBuilder {
|
|
|
57
57
|
// ----------------------------------------------------------------------
|
|
58
58
|
// Types
|
|
59
59
|
// ----------------------------------------------------------------------
|
|
60
|
-
/** Creates a any type */
|
|
60
|
+
/** `Standard` Creates a any type */
|
|
61
61
|
Any(options = {}) {
|
|
62
62
|
return this.Create({ ...options, [exports.Kind]: 'Any' });
|
|
63
63
|
}
|
|
64
|
-
/** Creates a array type */
|
|
64
|
+
/** `Standard` Creates a array type */
|
|
65
65
|
Array(items, options = {}) {
|
|
66
66
|
return this.Create({ ...options, [exports.Kind]: 'Array', type: 'array', items });
|
|
67
67
|
}
|
|
68
|
-
/** Creates a boolean type */
|
|
68
|
+
/** `Standard` Creates a boolean type */
|
|
69
69
|
Boolean(options = {}) {
|
|
70
70
|
return this.Create({ ...options, [exports.Kind]: 'Boolean', type: 'boolean' });
|
|
71
71
|
}
|
|
72
|
-
/** Creates a tuple type from this constructors parameters */
|
|
72
|
+
/** `Extended` Creates a tuple type from this constructors parameters */
|
|
73
73
|
ConstructorParameters(schema, options = {}) {
|
|
74
74
|
return this.Tuple([...schema.parameters], { ...options });
|
|
75
75
|
}
|
|
76
|
-
/** Creates a constructor type */
|
|
76
|
+
/** `Extended` Creates a constructor type */
|
|
77
77
|
Constructor(parameters, returns, options = {}) {
|
|
78
78
|
if (parameters[exports.Kind] === 'Tuple') {
|
|
79
79
|
const inner = parameters.items === undefined ? [] : parameters.items;
|
|
80
|
-
return this.Create({ ...options, [exports.Kind]: 'Constructor', type: '
|
|
80
|
+
return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'object', instanceOf: 'Constructor', parameters: inner, returns });
|
|
81
81
|
}
|
|
82
82
|
else if (globalThis.Array.isArray(parameters)) {
|
|
83
|
-
return this.Create({ ...options, [exports.Kind]: 'Constructor', type: '
|
|
83
|
+
return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'object', instanceOf: 'Constructor', parameters, returns });
|
|
84
84
|
}
|
|
85
85
|
else {
|
|
86
86
|
throw new Error('TypeBuilder.Constructor: Invalid parameters');
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
|
-
/** Creates a
|
|
89
|
+
/** `Extended` Creates a Date type */
|
|
90
|
+
Date(options = {}) {
|
|
91
|
+
return this.Create({ ...options, [exports.Kind]: 'Date', type: 'object', instanceOf: 'Date' });
|
|
92
|
+
}
|
|
93
|
+
/** `Standard` Creates a enum type */
|
|
90
94
|
Enum(item, options = {}) {
|
|
91
95
|
const values = Object.keys(item)
|
|
92
96
|
.filter((key) => isNaN(key))
|
|
@@ -94,28 +98,28 @@ class TypeBuilder {
|
|
|
94
98
|
const anyOf = values.map((value) => (typeof value === 'string' ? { [exports.Kind]: 'Literal', type: 'string', const: value } : { [exports.Kind]: 'Literal', type: 'number', const: value }));
|
|
95
99
|
return this.Create({ ...options, [exports.Kind]: 'Union', [exports.Hint]: 'Enum', anyOf });
|
|
96
100
|
}
|
|
97
|
-
/** Creates a function type */
|
|
101
|
+
/** `Extended` Creates a function type */
|
|
98
102
|
Function(parameters, returns, options = {}) {
|
|
99
103
|
if (parameters[exports.Kind] === 'Tuple') {
|
|
100
104
|
const inner = parameters.items === undefined ? [] : parameters.items;
|
|
101
|
-
return this.Create({ ...options, [exports.Kind]: 'Function', type: '
|
|
105
|
+
return this.Create({ ...options, [exports.Kind]: 'Function', type: 'object', instanceOf: 'Function', parameters: inner, returns });
|
|
102
106
|
}
|
|
103
107
|
else if (globalThis.Array.isArray(parameters)) {
|
|
104
|
-
return this.Create({ ...options, [exports.Kind]: 'Function', type: '
|
|
108
|
+
return this.Create({ ...options, [exports.Kind]: 'Function', type: 'object', instanceOf: 'Function', parameters, returns });
|
|
105
109
|
}
|
|
106
110
|
else {
|
|
107
111
|
throw new Error('TypeBuilder.Function: Invalid parameters');
|
|
108
112
|
}
|
|
109
113
|
}
|
|
110
|
-
/** Creates a type from this constructors instance type */
|
|
114
|
+
/** `Extended` Creates a type from this constructors instance type */
|
|
111
115
|
InstanceType(schema, options = {}) {
|
|
112
116
|
return { ...options, ...this.Clone(schema.returns) };
|
|
113
117
|
}
|
|
114
|
-
/** Creates a integer type */
|
|
118
|
+
/** `Standard` Creates a integer type */
|
|
115
119
|
Integer(options = {}) {
|
|
116
120
|
return this.Create({ ...options, [exports.Kind]: 'Integer', type: 'integer' });
|
|
117
121
|
}
|
|
118
|
-
/** Creates a intersect type. */
|
|
122
|
+
/** `Standard` Creates a intersect type. */
|
|
119
123
|
Intersect(objects, options = {}) {
|
|
120
124
|
const isOptional = (schema) => (schema[exports.Modifier] && schema[exports.Modifier] === 'Optional') || schema[exports.Modifier] === 'ReadonlyOptional';
|
|
121
125
|
const [required, optional] = [new Set(), new Set()];
|
|
@@ -144,16 +148,16 @@ class TypeBuilder {
|
|
|
144
148
|
return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties });
|
|
145
149
|
}
|
|
146
150
|
}
|
|
147
|
-
/** Creates a keyof type */
|
|
151
|
+
/** `Standard` Creates a keyof type */
|
|
148
152
|
KeyOf(object, options = {}) {
|
|
149
153
|
const items = Object.keys(object.properties).map((key) => this.Create({ ...options, [exports.Kind]: 'Literal', type: 'string', const: key }));
|
|
150
154
|
return this.Create({ ...options, [exports.Kind]: 'Union', [exports.Hint]: 'KeyOf', anyOf: items });
|
|
151
155
|
}
|
|
152
|
-
/** Creates a literal type. */
|
|
156
|
+
/** `Standard` Creates a literal type. */
|
|
153
157
|
Literal(value, options = {}) {
|
|
154
158
|
return this.Create({ ...options, [exports.Kind]: 'Literal', const: value, type: typeof value });
|
|
155
159
|
}
|
|
156
|
-
/** Creates a never type */
|
|
160
|
+
/** `Standard` Creates a never type */
|
|
157
161
|
Never(options = {}) {
|
|
158
162
|
return this.Create({
|
|
159
163
|
...options,
|
|
@@ -164,15 +168,15 @@ class TypeBuilder {
|
|
|
164
168
|
],
|
|
165
169
|
});
|
|
166
170
|
}
|
|
167
|
-
/** Creates a null type */
|
|
171
|
+
/** `Standard` Creates a null type */
|
|
168
172
|
Null(options = {}) {
|
|
169
173
|
return this.Create({ ...options, [exports.Kind]: 'Null', type: 'null' });
|
|
170
174
|
}
|
|
171
|
-
/** Creates a number type */
|
|
175
|
+
/** `Standard` Creates a number type */
|
|
172
176
|
Number(options = {}) {
|
|
173
177
|
return this.Create({ ...options, [exports.Kind]: 'Number', type: 'number' });
|
|
174
178
|
}
|
|
175
|
-
/** Creates an object type
|
|
179
|
+
/** `Standard` Creates an object type */
|
|
176
180
|
Object(properties, options = {}) {
|
|
177
181
|
const property_names = Object.keys(properties);
|
|
178
182
|
const optional = property_names.filter((name) => {
|
|
@@ -188,7 +192,7 @@ class TypeBuilder {
|
|
|
188
192
|
return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties });
|
|
189
193
|
}
|
|
190
194
|
}
|
|
191
|
-
/** Creates a new object whose
|
|
195
|
+
/** `Standard` Creates a new object type whose keys are omitted from the given source type */
|
|
192
196
|
Omit(schema, keys, options = {}) {
|
|
193
197
|
const select = keys[exports.Kind] === 'Union' ? keys.anyOf.map((schema) => schema.const) : keys;
|
|
194
198
|
const next = { ...this.Clone(schema), ...options, [exports.Hint]: 'Omit' };
|
|
@@ -203,11 +207,11 @@ class TypeBuilder {
|
|
|
203
207
|
}
|
|
204
208
|
return this.Create(next);
|
|
205
209
|
}
|
|
206
|
-
/** Creates a tuple type from this functions parameters */
|
|
210
|
+
/** `Extended` Creates a tuple type from this functions parameters */
|
|
207
211
|
Parameters(schema, options = {}) {
|
|
208
212
|
return exports.Type.Tuple(schema.parameters, { ...options });
|
|
209
213
|
}
|
|
210
|
-
/** Creates an object type whose properties are all optional */
|
|
214
|
+
/** `Standard` Creates an object type whose properties are all optional */
|
|
211
215
|
Partial(schema, options = {}) {
|
|
212
216
|
const next = { ...this.Clone(schema), ...options, [exports.Hint]: 'Partial' };
|
|
213
217
|
delete next.required;
|
|
@@ -231,7 +235,7 @@ class TypeBuilder {
|
|
|
231
235
|
}
|
|
232
236
|
return this.Create(next);
|
|
233
237
|
}
|
|
234
|
-
/** Creates a object whose
|
|
238
|
+
/** `Standard` Creates a new object type whose keys are picked from the given source type */
|
|
235
239
|
Pick(schema, keys, options = {}) {
|
|
236
240
|
const select = keys[exports.Kind] === 'Union' ? keys.anyOf.map((schema) => schema.const) : keys;
|
|
237
241
|
const next = { ...this.Clone(schema), ...options, [exports.Hint]: 'Pick' };
|
|
@@ -246,11 +250,11 @@ class TypeBuilder {
|
|
|
246
250
|
}
|
|
247
251
|
return this.Create(next);
|
|
248
252
|
}
|
|
249
|
-
/** Creates a
|
|
253
|
+
/** `Extended` Creates a Promise type */
|
|
250
254
|
Promise(item, options = {}) {
|
|
251
|
-
return this.Create({ ...options, [exports.Kind]: 'Promise', type: '
|
|
255
|
+
return this.Create({ ...options, [exports.Kind]: 'Promise', type: 'object', instanceOf: 'Promise', item });
|
|
252
256
|
}
|
|
253
|
-
/** Creates a record type */
|
|
257
|
+
/** `Standard` Creates a record type */
|
|
254
258
|
Record(key, value, options = {}) {
|
|
255
259
|
// If string literal union return TObject with properties extracted from union.
|
|
256
260
|
if (key[exports.Kind] === 'Union') {
|
|
@@ -259,7 +263,7 @@ class TypeBuilder {
|
|
|
259
263
|
}, {}), { ...options, [exports.Hint]: 'Record' });
|
|
260
264
|
}
|
|
261
265
|
// otherwise return TRecord with patternProperties
|
|
262
|
-
const pattern = key[exports.Kind]
|
|
266
|
+
const pattern = ['Integer', 'Number'].includes(key[exports.Kind]) ? '^(0|[1-9][0-9]*)$' : key[exports.Kind] === 'String' && key.pattern ? key.pattern : '^.*$';
|
|
263
267
|
return this.Create({
|
|
264
268
|
...options,
|
|
265
269
|
[exports.Kind]: 'Record',
|
|
@@ -268,7 +272,7 @@ class TypeBuilder {
|
|
|
268
272
|
additionalProperties: false,
|
|
269
273
|
});
|
|
270
274
|
}
|
|
271
|
-
/** Creates
|
|
275
|
+
/** `Standard` Creates recursive type */
|
|
272
276
|
Recursive(callback, options = {}) {
|
|
273
277
|
if (options.$id === undefined)
|
|
274
278
|
options.$id = `T${TypeOrdinal++}`;
|
|
@@ -276,17 +280,17 @@ class TypeBuilder {
|
|
|
276
280
|
self.$id = options.$id;
|
|
277
281
|
return this.Create({ ...options, ...self });
|
|
278
282
|
}
|
|
279
|
-
/** Creates a reference
|
|
283
|
+
/** `Standard` Creates a reference type. The referenced type must contain a $id. */
|
|
280
284
|
Ref(schema, options = {}) {
|
|
281
285
|
if (schema.$id === undefined)
|
|
282
286
|
throw Error('TypeBuilder.Ref: Referenced schema must specify an $id');
|
|
283
287
|
return this.Create({ ...options, [exports.Kind]: 'Ref', $ref: schema.$id });
|
|
284
288
|
}
|
|
285
|
-
/** Creates a string type from a regular expression */
|
|
289
|
+
/** `Standard` Creates a string type from a regular expression */
|
|
286
290
|
RegEx(regex, options = {}) {
|
|
287
291
|
return this.Create({ ...options, [exports.Kind]: 'String', type: 'string', pattern: regex.source });
|
|
288
292
|
}
|
|
289
|
-
/** Creates an object type whose properties are all required */
|
|
293
|
+
/** `Standard` Creates an object type whose properties are all required */
|
|
290
294
|
Required(schema, options = {}) {
|
|
291
295
|
const next = { ...this.Clone(schema), ...options, [exports.Hint]: 'Required' };
|
|
292
296
|
next.required = Object.keys(next.properties);
|
|
@@ -310,7 +314,7 @@ class TypeBuilder {
|
|
|
310
314
|
}
|
|
311
315
|
return this.Create(next);
|
|
312
316
|
}
|
|
313
|
-
/** Creates a type from this functions return type */
|
|
317
|
+
/** `Extended` Creates a type from this functions return type */
|
|
314
318
|
ReturnType(schema, options = {}) {
|
|
315
319
|
return { ...options, ...this.Clone(schema.returns) };
|
|
316
320
|
}
|
|
@@ -318,11 +322,11 @@ class TypeBuilder {
|
|
|
318
322
|
Strict(schema) {
|
|
319
323
|
return JSON.parse(JSON.stringify(schema));
|
|
320
324
|
}
|
|
321
|
-
/** Creates a string type */
|
|
325
|
+
/** `Standard` Creates a string type */
|
|
322
326
|
String(options = {}) {
|
|
323
327
|
return this.Create({ ...options, [exports.Kind]: 'String', type: 'string' });
|
|
324
328
|
}
|
|
325
|
-
/** Creates a tuple type */
|
|
329
|
+
/** `Standard` Creates a tuple type */
|
|
326
330
|
Tuple(items, options = {}) {
|
|
327
331
|
const additionalItems = false;
|
|
328
332
|
const minItems = items.length;
|
|
@@ -330,28 +334,29 @@ class TypeBuilder {
|
|
|
330
334
|
const schema = (items.length > 0 ? { ...options, [exports.Kind]: 'Tuple', type: 'array', items, additionalItems, minItems, maxItems } : { ...options, [exports.Kind]: 'Tuple', type: 'array', minItems, maxItems });
|
|
331
335
|
return this.Create(schema);
|
|
332
336
|
}
|
|
333
|
-
/** Creates a undefined type */
|
|
337
|
+
/** `Extended` Creates a undefined type */
|
|
334
338
|
Undefined(options = {}) {
|
|
335
|
-
return this.Create({ ...options, [exports.Kind]: 'Undefined', type: '
|
|
339
|
+
return this.Create({ ...options, [exports.Kind]: 'Undefined', type: 'null', typeOf: 'Undefined' });
|
|
336
340
|
}
|
|
341
|
+
/** `Standard` Creates a union type */
|
|
337
342
|
Union(items, options = {}) {
|
|
338
343
|
return items.length === 0 ? exports.Type.Never({ ...options }) : this.Create({ ...options, [exports.Kind]: 'Union', anyOf: items });
|
|
339
344
|
}
|
|
340
|
-
/** Creates a Uint8Array type */
|
|
345
|
+
/** `Extended` Creates a Uint8Array type */
|
|
341
346
|
Uint8Array(options = {}) {
|
|
342
|
-
return this.Create({ ...options, [exports.Kind]: 'Uint8Array', type: 'object',
|
|
347
|
+
return this.Create({ ...options, [exports.Kind]: 'Uint8Array', type: 'object', instanceOf: 'Uint8Array' });
|
|
343
348
|
}
|
|
344
|
-
/** Creates an unknown type */
|
|
349
|
+
/** `Standard` Creates an unknown type */
|
|
345
350
|
Unknown(options = {}) {
|
|
346
351
|
return this.Create({ ...options, [exports.Kind]: 'Unknown' });
|
|
347
352
|
}
|
|
348
|
-
/** Creates a user defined schema that infers as type T */
|
|
353
|
+
/** `Standard` Creates a user defined schema that infers as type T */
|
|
349
354
|
Unsafe(options = {}) {
|
|
350
355
|
return this.Create({ ...options, [exports.Kind]: options[exports.Kind] || 'Unsafe' });
|
|
351
356
|
}
|
|
352
|
-
/** Creates a void type */
|
|
357
|
+
/** `Extended` Creates a void type */
|
|
353
358
|
Void(options = {}) {
|
|
354
|
-
return this.Create({ ...options, [exports.Kind]: 'Void', type: 'null' });
|
|
359
|
+
return this.Create({ ...options, [exports.Kind]: 'Void', type: 'null', typeOf: 'Void' });
|
|
355
360
|
}
|
|
356
361
|
/** Use this function to return TSchema with static and params omitted */
|
|
357
362
|
Create(schema) {
|
package/value/cast.js
CHANGED
|
@@ -195,6 +195,9 @@ var ValueCast;
|
|
|
195
195
|
}
|
|
196
196
|
return result;
|
|
197
197
|
}
|
|
198
|
+
function Date(schema, references, value) {
|
|
199
|
+
return check_1.ValueCheck.Check(schema, references, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema, references);
|
|
200
|
+
}
|
|
198
201
|
function Enum(schema, references, value) {
|
|
199
202
|
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|
|
200
203
|
}
|
|
@@ -247,7 +250,7 @@ var ValueCast;
|
|
|
247
250
|
function Record(schema, references, value) {
|
|
248
251
|
if (check_1.ValueCheck.Check(schema, references, value))
|
|
249
252
|
return clone_1.ValueClone.Clone(value);
|
|
250
|
-
if (value === null || typeof value !== 'object' || globalThis.Array.isArray(value))
|
|
253
|
+
if (value === null || typeof value !== 'object' || globalThis.Array.isArray(value) || value instanceof globalThis.Date)
|
|
251
254
|
return create_1.ValueCreate.Create(schema, references);
|
|
252
255
|
const subschemaKey = globalThis.Object.keys(schema.patternProperties)[0];
|
|
253
256
|
const subschema = schema.patternProperties[subschemaKey];
|
|
@@ -312,6 +315,8 @@ var ValueCast;
|
|
|
312
315
|
return Boolean(anySchema, anyReferences, value);
|
|
313
316
|
case 'Constructor':
|
|
314
317
|
return Constructor(anySchema, anyReferences, value);
|
|
318
|
+
case 'Date':
|
|
319
|
+
return Date(anySchema, anyReferences, value);
|
|
315
320
|
case 'Enum':
|
|
316
321
|
return Enum(anySchema, anyReferences, value);
|
|
317
322
|
case 'Function':
|
package/value/check.js
CHANGED
|
@@ -63,6 +63,24 @@ var ValueCheck;
|
|
|
63
63
|
function Constructor(schema, references, value) {
|
|
64
64
|
return Visit(schema.returns, references, value.prototype);
|
|
65
65
|
}
|
|
66
|
+
function Date(schema, references, value) {
|
|
67
|
+
if (!(value instanceof globalThis.Date)) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
if (schema.exclusiveMinimumTimestamp && !(value.getTime() > schema.exclusiveMinimumTimestamp)) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
if (schema.exclusiveMaximumTimestamp && !(value.getTime() < schema.exclusiveMaximumTimestamp)) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
if (schema.minimumTimestamp && !(value.getTime() >= schema.minimumTimestamp)) {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
if (schema.maximumTimestamp && !(value.getTime() <= schema.maximumTimestamp)) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
66
84
|
function Function(schema, references, value) {
|
|
67
85
|
return typeof value === 'function';
|
|
68
86
|
}
|
|
@@ -175,7 +193,7 @@ var ValueCheck;
|
|
|
175
193
|
return typeof value === 'object' && typeof value.then === 'function';
|
|
176
194
|
}
|
|
177
195
|
function Record(schema, references, value) {
|
|
178
|
-
if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value))) {
|
|
196
|
+
if (!(typeof value === 'object' && value !== null && !globalThis.Array.isArray(value) && !(value instanceof globalThis.Date))) {
|
|
179
197
|
return false;
|
|
180
198
|
}
|
|
181
199
|
const [keyPattern, valueSchema] = globalThis.Object.entries(schema.patternProperties)[0];
|
|
@@ -281,6 +299,8 @@ var ValueCheck;
|
|
|
281
299
|
return Boolean(anySchema, anyReferences, value);
|
|
282
300
|
case 'Constructor':
|
|
283
301
|
return Constructor(anySchema, anyReferences, value);
|
|
302
|
+
case 'Date':
|
|
303
|
+
return Date(anySchema, anyReferences, value);
|
|
284
304
|
case 'Function':
|
|
285
305
|
return Function(anySchema, anyReferences, value);
|
|
286
306
|
case 'Integer':
|
package/value/clone.js
CHANGED
|
@@ -31,13 +31,16 @@ exports.ValueClone = void 0;
|
|
|
31
31
|
const is_1 = require("./is");
|
|
32
32
|
var ValueClone;
|
|
33
33
|
(function (ValueClone) {
|
|
34
|
+
function Array(value) {
|
|
35
|
+
return value.map((element) => Clone(element));
|
|
36
|
+
}
|
|
37
|
+
function Date(value) {
|
|
38
|
+
return new globalThis.Date(value.toISOString());
|
|
39
|
+
}
|
|
34
40
|
function Object(value) {
|
|
35
41
|
const keys = [...globalThis.Object.keys(value), ...globalThis.Object.getOwnPropertySymbols(value)];
|
|
36
42
|
return keys.reduce((acc, key) => ({ ...acc, [key]: Clone(value[key]) }), {});
|
|
37
43
|
}
|
|
38
|
-
function Array(value) {
|
|
39
|
-
return value.map((element) => Clone(element));
|
|
40
|
-
}
|
|
41
44
|
function TypedArray(value) {
|
|
42
45
|
return value.slice();
|
|
43
46
|
}
|
|
@@ -45,7 +48,10 @@ var ValueClone;
|
|
|
45
48
|
return value;
|
|
46
49
|
}
|
|
47
50
|
function Clone(value) {
|
|
48
|
-
if (is_1.Is.
|
|
51
|
+
if (is_1.Is.Date(value)) {
|
|
52
|
+
return Date(value);
|
|
53
|
+
}
|
|
54
|
+
else if (is_1.Is.Object(value)) {
|
|
49
55
|
return Object(value);
|
|
50
56
|
}
|
|
51
57
|
else if (is_1.Is.Array(value)) {
|
package/value/create.js
CHANGED
|
@@ -99,6 +99,17 @@ var ValueCreate;
|
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
101
|
}
|
|
102
|
+
function Date(schema, references) {
|
|
103
|
+
if (schema.default !== undefined) {
|
|
104
|
+
return schema.default;
|
|
105
|
+
}
|
|
106
|
+
else if (schema.minimumTimestamp !== undefined) {
|
|
107
|
+
return new globalThis.Date(schema.minimumTimestamp);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
return new globalThis.Date(0);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
102
113
|
function Enum(schema, references) {
|
|
103
114
|
if (schema.default !== undefined) {
|
|
104
115
|
return schema.default;
|
|
@@ -305,6 +316,8 @@ var ValueCreate;
|
|
|
305
316
|
return Boolean(anySchema, anyReferences);
|
|
306
317
|
case 'Constructor':
|
|
307
318
|
return Constructor(anySchema, anyReferences);
|
|
319
|
+
case 'Date':
|
|
320
|
+
return Date(anySchema, anyReferences);
|
|
308
321
|
case 'Enum':
|
|
309
322
|
return Enum(anySchema, anyReferences);
|
|
310
323
|
case 'Function':
|
package/value/delta.js
CHANGED
|
@@ -79,6 +79,11 @@ var ValueDelta;
|
|
|
79
79
|
yield Delete(`${path}/${String(key)}`);
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
|
+
function* Date(path, current, next) {
|
|
83
|
+
if (is_1.Is.Date(next) && current.getTime() === next.getTime())
|
|
84
|
+
return;
|
|
85
|
+
yield Update(path, next);
|
|
86
|
+
}
|
|
82
87
|
function* Array(path, current, next) {
|
|
83
88
|
if (!is_1.Is.Array(next))
|
|
84
89
|
return yield Update(path, next);
|
|
@@ -112,6 +117,9 @@ var ValueDelta;
|
|
|
112
117
|
if (is_1.Is.Object(current)) {
|
|
113
118
|
return yield* Object(path, current, next);
|
|
114
119
|
}
|
|
120
|
+
else if (is_1.Is.Date(current)) {
|
|
121
|
+
return yield* Date(path, current, next);
|
|
122
|
+
}
|
|
115
123
|
else if (is_1.Is.Array(current)) {
|
|
116
124
|
return yield* Array(path, current, next);
|
|
117
125
|
}
|