@sinclair/typebox 0.30.0-dev-4 → 0.30.0-dev-5
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 +12 -12
- package/typebox.d.ts +19 -7
- package/typebox.js +36 -4
- package/value/convert.js +2 -2
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -63,7 +63,7 @@ type T = Static<typeof T> // type T = {
|
|
|
63
63
|
|
|
64
64
|
## Overview
|
|
65
65
|
|
|
66
|
-
TypeBox is a runtime type builder that creates in-memory JSON Schema objects that can be statically inferred as TypeScript types. The schemas produced by this library are designed to match the static type assertion rules of the TypeScript
|
|
66
|
+
TypeBox is a runtime type builder that creates in-memory JSON Schema objects that can be statically inferred as TypeScript types. The schemas produced by this library are designed to match the static type assertion rules of the TypeScript language. TypeBox allows one to create a unified type that can be statically checked by TypeScript and runtime asserted using standard JSON Schema validation.
|
|
67
67
|
|
|
68
68
|
This library is designed to enable JSON schema to compose with the same flexibility as TypeScript's type system. It can be used as a simple tool to build up complex schemas or integrated into REST or RPC services to help validate data received over the wire.
|
|
69
69
|
|
|
@@ -72,6 +72,7 @@ License MIT
|
|
|
72
72
|
## Contents
|
|
73
73
|
- [Install](#install)
|
|
74
74
|
- [Overview](#overview)
|
|
75
|
+
- [Features](#features)
|
|
75
76
|
- [Usage](#usage)
|
|
76
77
|
- [Types](#types)
|
|
77
78
|
- [Standard](#types-standard)
|
|
@@ -102,7 +103,6 @@ License MIT
|
|
|
102
103
|
- [Errors](#values-errors)
|
|
103
104
|
- [Mutate](#values-mutate)
|
|
104
105
|
- [Pointer](#values-pointer)
|
|
105
|
-
- [Transform](#values-transform)
|
|
106
106
|
- [TypeCheck](#typecheck)
|
|
107
107
|
- [Ajv](#typecheck-ajv)
|
|
108
108
|
- [TypeCompiler](#typecheck-typecompiler)
|
|
@@ -110,7 +110,7 @@ License MIT
|
|
|
110
110
|
- [Types](#typesystem-types)
|
|
111
111
|
- [Formats](#typesystem-formats)
|
|
112
112
|
- [Policies](#typesystem-policies)
|
|
113
|
-
- [
|
|
113
|
+
- [Transform](#Transform)
|
|
114
114
|
- [Ecosystem](#ecosystem)
|
|
115
115
|
- [Benchmark](#benchmark)
|
|
116
116
|
- [Compile](#benchmark-compile)
|
|
@@ -1457,13 +1457,13 @@ TypeSystem.AllowArrayObjects = true
|
|
|
1457
1457
|
TypeSystem.AllowNaN = true
|
|
1458
1458
|
```
|
|
1459
1459
|
|
|
1460
|
-
<a name='
|
|
1460
|
+
<a name='transform'></a>
|
|
1461
1461
|
|
|
1462
|
-
##
|
|
1462
|
+
## TypeBox Transform
|
|
1463
1463
|
|
|
1464
|
-
TypeBox offers a web based code generation tool that can be used to convert TypeScript types into TypeBox types as well as a variety of other runtime type representations.
|
|
1464
|
+
TypeBox offers a small web based code generation tool that can be used to convert TypeScript types into TypeBox types as well as a variety of other runtime type representations.
|
|
1465
1465
|
|
|
1466
|
-
[
|
|
1466
|
+
[TypeBox Transform Link Here](https://sinclairzx81.github.io/typebox-transform/)
|
|
1467
1467
|
|
|
1468
1468
|
<a name='ecosystem'></a>
|
|
1469
1469
|
|
|
@@ -1586,11 +1586,11 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
|
|
|
1586
1586
|
┌──────────────────────┬────────────┬────────────┬─────────────┐
|
|
1587
1587
|
│ (index) │ Compiled │ Minified │ Compression │
|
|
1588
1588
|
├──────────────────────┼────────────┼────────────┼─────────────┤
|
|
1589
|
-
│ typebox/compiler │ '
|
|
1590
|
-
│ typebox/errors │ '
|
|
1591
|
-
│ typebox/system │ '
|
|
1592
|
-
│ typebox/value │ '
|
|
1593
|
-
│ typebox │ '
|
|
1589
|
+
│ typebox/compiler │ '129.4 kb' │ ' 58.6 kb' │ '2.21 x' │
|
|
1590
|
+
│ typebox/errors │ '111.6 kb' │ ' 50.1 kb' │ '2.23 x' │
|
|
1591
|
+
│ typebox/system │ ' 76.5 kb' │ ' 31.7 kb' │ '2.41 x' │
|
|
1592
|
+
│ typebox/value │ '180.7 kb' │ ' 79.3 kb' │ '2.28 x' │
|
|
1593
|
+
│ typebox │ ' 75.4 kb' │ ' 31.3 kb' │ '2.41 x' │
|
|
1594
1594
|
└──────────────────────┴────────────┴────────────┴─────────────┘
|
|
1595
1595
|
```
|
|
1596
1596
|
|
package/typebox.d.ts
CHANGED
|
@@ -104,6 +104,8 @@ export interface TAsyncIterator<T extends TSchema = TSchema> extends TSchema {
|
|
|
104
104
|
type: 'AsyncIterator';
|
|
105
105
|
items: T;
|
|
106
106
|
}
|
|
107
|
+
export type TAwaitedRest<T extends TSchema[]> = T extends [infer L, ...infer R] ? [TAwaited<AssertType<L>>, ...TAwaitedRest<AssertRest<R>>] : [];
|
|
108
|
+
export type TAwaited<T extends TSchema> = T extends TIntersect<infer S> ? TIntersect<TAwaitedRest<S>> : T extends TUnion<infer S> ? TUnion<TAwaitedRest<S>> : T extends TPromise<infer S> ? TAwaited<S> : T;
|
|
107
109
|
export interface TBigInt extends TSchema, NumericOptions<bigint> {
|
|
108
110
|
[Kind]: 'BigInt';
|
|
109
111
|
static: bigint;
|
|
@@ -276,11 +278,11 @@ export interface TObject<T extends TProperties = TProperties> extends TSchema, O
|
|
|
276
278
|
properties: T;
|
|
277
279
|
required?: string[];
|
|
278
280
|
}
|
|
279
|
-
export type
|
|
281
|
+
export type TOmitProperties<T extends TProperties, K extends keyof any> = Evaluate<AssertProperties<Omit<T, K>>>;
|
|
282
|
+
export type TOmitRest<T extends TSchema[], K extends keyof any> = AssertRest<{
|
|
280
283
|
[K2 in keyof T]: TOmit<AssertType<T[K2]>, K>;
|
|
281
284
|
}>;
|
|
282
|
-
export type
|
|
283
|
-
export type TOmit<T extends TSchema = TSchema, K extends keyof any = keyof any> = T extends TRecursive<infer S> ? TRecursive<TOmit<S, K>> : T extends TIntersect<infer S> ? TIntersect<TOmitArray<S, K>> : T extends TUnion<infer S> ? TUnion<TOmitArray<S, K>> : T extends TObject<infer S> ? TObject<TOmitProperties<S, K>> : T;
|
|
285
|
+
export type TOmit<T extends TSchema = TSchema, K extends keyof any = keyof any> = T extends TRecursive<infer S> ? TRecursive<TOmit<S, K>> : T extends TIntersect<infer S> ? TIntersect<TOmitRest<S, K>> : T extends TUnion<infer S> ? TUnion<TOmitRest<S, K>> : T extends TObject<infer S> ? TObject<TOmitProperties<S, K>> : T;
|
|
284
286
|
export type TParameters<T extends TFunction> = Ensure<TTuple<T['parameters']>>;
|
|
285
287
|
export type TPartialObjectArray<T extends TObject[]> = AssertRest<{
|
|
286
288
|
[K in keyof T]: TPartial<AssertType<T[K], TObject>>;
|
|
@@ -292,13 +294,13 @@ export type TPartialProperties<T extends TProperties> = Evaluate<AssertPropertie
|
|
|
292
294
|
[K in keyof T]: TOptional<T[K]>;
|
|
293
295
|
}>>;
|
|
294
296
|
export type TPartial<T extends TSchema> = T extends TRecursive<infer S> ? TRecursive<TPartial<S>> : T extends TIntersect<infer S> ? TIntersect<TPartialArray<S>> : T extends TUnion<infer S> ? TUnion<TPartialArray<S>> : T extends TObject<infer S> ? TObject<TPartialProperties<S>> : T;
|
|
295
|
-
export type TPickArray<T extends TSchema[], K extends keyof any> = {
|
|
296
|
-
[K2 in keyof T]: TPick<AssertType<T[K2]>, K>;
|
|
297
|
-
};
|
|
298
297
|
export type TPickProperties<T extends TProperties, K extends keyof any> = Pick<T, Assert<Extract<K, keyof T>, keyof T>> extends infer R ? ({
|
|
299
298
|
[K in keyof R]: AssertType<R[K]> extends TSchema ? R[K] : never;
|
|
300
299
|
}) : never;
|
|
301
|
-
export type
|
|
300
|
+
export type TPickRest<T extends TSchema[], K extends keyof any> = {
|
|
301
|
+
[K2 in keyof T]: TPick<AssertType<T[K2]>, K>;
|
|
302
|
+
};
|
|
303
|
+
export type TPick<T extends TSchema = TSchema, K extends keyof any = keyof any> = T extends TRecursive<infer S> ? TRecursive<TPick<S, K>> : T extends TIntersect<infer S> ? TIntersect<TPickRest<S, K>> : T extends TUnion<infer S> ? TUnion<TPickRest<S, K>> : T extends TObject<infer S> ? TObject<TPickProperties<S, K>> : T;
|
|
302
304
|
export interface TPromise<T extends TSchema = TSchema> extends TSchema {
|
|
303
305
|
[Kind]: 'Promise';
|
|
304
306
|
static: Promise<Static<T, this['params']>>;
|
|
@@ -685,6 +687,8 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
685
687
|
Array<T extends TSchema>(items: T, options?: ArrayOptions): TArray<T>;
|
|
686
688
|
/** `[Standard]` Creates a Boolean type */
|
|
687
689
|
Boolean(options?: SchemaOptions): TBoolean;
|
|
690
|
+
/** `[Standard]` Maps a literal strings first character to uppercase */
|
|
691
|
+
Capitalize<T extends TLiteral<string>>(schema: T, options?: SchemaOptions): TLiteral<Capitalize<T['const']>>;
|
|
688
692
|
/** `[Standard]` Creates a Composite object type. */
|
|
689
693
|
Composite<T extends TObject[]>(objects: [...T], options?: ObjectOptions): TComposite<T>;
|
|
690
694
|
/** `[Standard]` Creates a Enum type */
|
|
@@ -720,6 +724,8 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
720
724
|
KeyOf<T extends TSchema>(schema: T, options?: SchemaOptions): TKeyOf<T>;
|
|
721
725
|
/** `[Standard]` Creates a Literal type */
|
|
722
726
|
Literal<T extends TLiteralValue>(value: T, options?: SchemaOptions): TLiteral<T>;
|
|
727
|
+
/** `[Standard]` Maps a literal string to lowercase */
|
|
728
|
+
Lowercase<T extends TLiteral<string>>(schema: T, options?: SchemaOptions): TLiteral<Lowercase<T['const']>>;
|
|
723
729
|
/** `[Standard]` Creates a Never type */
|
|
724
730
|
Never(options?: SchemaOptions): TNever;
|
|
725
731
|
/** `[Standard]` Creates a Not type */
|
|
@@ -780,6 +786,8 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
780
786
|
TemplateLiteral<T extends TTemplateLiteralKind[]>(kinds: [...T], options?: SchemaOptions): TTemplateLiteral<T>;
|
|
781
787
|
/** `[Standard]` Creates a Tuple type */
|
|
782
788
|
Tuple<T extends TSchema[]>(items: [...T], options?: SchemaOptions): TTuple<T>;
|
|
789
|
+
/** `[Standard]` Maps a literal strings first character to lowercase */
|
|
790
|
+
Uncapitalize<T extends TLiteral<string>>(schema: T, options?: SchemaOptions): TLiteral<Uncapitalize<T['const']>>;
|
|
783
791
|
/** `[Standard]` Creates a Union type */
|
|
784
792
|
Union(anyOf: [], options?: SchemaOptions): TNever;
|
|
785
793
|
/** `[Standard]` Creates a Union type */
|
|
@@ -792,10 +800,14 @@ export declare class StandardTypeBuilder extends TypeBuilder {
|
|
|
792
800
|
Unknown(options?: SchemaOptions): TUnknown;
|
|
793
801
|
/** `[Standard]` Creates a Unsafe type that infers for the generic argument */
|
|
794
802
|
Unsafe<T>(options?: UnsafeOptions): TUnsafe<T>;
|
|
803
|
+
/** `[Standard]` Maps a literal string to uppercase */
|
|
804
|
+
Uppercase<T extends TLiteral<string>>(schema: T, options?: SchemaOptions): TLiteral<Uppercase<T['const']>>;
|
|
795
805
|
}
|
|
796
806
|
export declare class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
797
807
|
/** `[Extended]` Creates a AsyncIterator type */
|
|
798
808
|
AsyncIterator<T extends TSchema>(items: T, options?: SchemaOptions): TAsyncIterator<T>;
|
|
809
|
+
/** `[Extended]` Recursively unwraps Promise from the given type. */
|
|
810
|
+
Awaited<T extends TSchema>(schema: T, options?: SchemaOptions): TAwaited<T>;
|
|
799
811
|
/** `[Extended]` Creates a BigInt type */
|
|
800
812
|
BigInt(options?: NumericOptions<bigint>): TBigInt;
|
|
801
813
|
/** `[Extended]` Extracts the ConstructorParameters from the given Constructor type */
|
package/typebox.js
CHANGED
|
@@ -222,12 +222,12 @@ var TypeGuard;
|
|
|
222
222
|
schema.type === 'array' &&
|
|
223
223
|
IsOptionalString(schema.$id) &&
|
|
224
224
|
TSchema(schema.items) &&
|
|
225
|
-
IsOptionalSchema(schema.contains) &&
|
|
226
|
-
IsOptionalNumber(schema.minContains) &&
|
|
227
|
-
IsOptionalNumber(schema.maxContains) &&
|
|
228
225
|
IsOptionalNumber(schema.minItems) &&
|
|
229
226
|
IsOptionalNumber(schema.maxItems) &&
|
|
230
|
-
IsOptionalBoolean(schema.uniqueItems)
|
|
227
|
+
IsOptionalBoolean(schema.uniqueItems) &&
|
|
228
|
+
IsOptionalSchema(schema.contains) &&
|
|
229
|
+
IsOptionalNumber(schema.minContains) &&
|
|
230
|
+
IsOptionalNumber(schema.maxContains));
|
|
231
231
|
}
|
|
232
232
|
TypeGuard.TArray = TArray;
|
|
233
233
|
/** Returns true if the given schema is TAsyncIterator */
|
|
@@ -1952,6 +1952,11 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1952
1952
|
Boolean(options = {}) {
|
|
1953
1953
|
return this.Create({ ...options, [exports.Kind]: 'Boolean', type: 'boolean' });
|
|
1954
1954
|
}
|
|
1955
|
+
/** `[Standard]` Maps a literal strings first character to uppercase */
|
|
1956
|
+
Capitalize(schema, options = {}) {
|
|
1957
|
+
const [first, rest] = [schema.const.slice(0, 1), schema.const.slice(1)];
|
|
1958
|
+
return exports.Type.Literal(`${first.toUpperCase()}${rest}`, options);
|
|
1959
|
+
}
|
|
1955
1960
|
/** `[Standard]` Creates a Composite object type. */
|
|
1956
1961
|
Composite(objects, options) {
|
|
1957
1962
|
const intersect = exports.Type.Intersect(objects, {});
|
|
@@ -2070,6 +2075,10 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2070
2075
|
Literal(value, options = {}) {
|
|
2071
2076
|
return this.Create({ ...options, [exports.Kind]: 'Literal', const: value, type: typeof value });
|
|
2072
2077
|
}
|
|
2078
|
+
/** `[Standard]` Maps a literal string to lowercase */
|
|
2079
|
+
Lowercase(schema, options = {}) {
|
|
2080
|
+
return exports.Type.Literal(schema.const.toLowerCase(), options);
|
|
2081
|
+
}
|
|
2073
2082
|
/** `[Standard]` Creates a Never type */
|
|
2074
2083
|
Never(options = {}) {
|
|
2075
2084
|
return this.Create({ ...options, [exports.Kind]: 'Never', not: {} });
|
|
@@ -2237,6 +2246,11 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2237
2246
|
{ ...options, [exports.Kind]: 'Tuple', type: 'array', minItems, maxItems });
|
|
2238
2247
|
return this.Create(schema);
|
|
2239
2248
|
}
|
|
2249
|
+
/** `[Standard]` Maps a literal strings first character to lowercase */
|
|
2250
|
+
Uncapitalize(schema, options = {}) {
|
|
2251
|
+
const [first, rest] = [schema.const.slice(0, 1), schema.const.slice(1)];
|
|
2252
|
+
return exports.Type.Literal(`${first.toLocaleLowerCase()}${rest}`, options);
|
|
2253
|
+
}
|
|
2240
2254
|
Union(union, options = {}) {
|
|
2241
2255
|
if (TypeGuard.TTemplateLiteral(union)) {
|
|
2242
2256
|
return TemplateLiteralResolver.Resolve(union);
|
|
@@ -2259,6 +2273,10 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2259
2273
|
Unsafe(options = {}) {
|
|
2260
2274
|
return this.Create({ ...options, [exports.Kind]: options[exports.Kind] || 'Unsafe' });
|
|
2261
2275
|
}
|
|
2276
|
+
/** `[Standard]` Maps a literal string to uppercase */
|
|
2277
|
+
Uppercase(schema, options = {}) {
|
|
2278
|
+
return exports.Type.Literal(schema.const.toUpperCase(), options);
|
|
2279
|
+
}
|
|
2262
2280
|
}
|
|
2263
2281
|
exports.StandardTypeBuilder = StandardTypeBuilder;
|
|
2264
2282
|
// --------------------------------------------------------------------------
|
|
@@ -2269,6 +2287,20 @@ class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
|
2269
2287
|
AsyncIterator(items, options = {}) {
|
|
2270
2288
|
return this.Create({ ...options, [exports.Kind]: 'AsyncIterator', type: 'AsyncIterator', items: TypeClone.Clone(items) });
|
|
2271
2289
|
}
|
|
2290
|
+
/** `[Extended]` Recursively unwraps Promise from the given type. */
|
|
2291
|
+
Awaited(schema, options = {}) {
|
|
2292
|
+
const AwaitedRest = (rest) => {
|
|
2293
|
+
if (rest.length === 0)
|
|
2294
|
+
return rest;
|
|
2295
|
+
const [L, ...R] = rest;
|
|
2296
|
+
return [this.Awaited(L), ...AwaitedRest(R)];
|
|
2297
|
+
};
|
|
2298
|
+
// prettier-ignore
|
|
2299
|
+
return (TypeGuard.TIntersect(schema) ? exports.Type.Intersect(AwaitedRest(schema.allOf)) :
|
|
2300
|
+
TypeGuard.TUnion(schema) ? exports.Type.Union(AwaitedRest(schema.anyOf)) :
|
|
2301
|
+
TypeGuard.TPromise(schema) ? this.Awaited(schema.item) :
|
|
2302
|
+
TypeClone.Clone(schema, options));
|
|
2303
|
+
}
|
|
2272
2304
|
/** `[Extended]` Creates a BigInt type */
|
|
2273
2305
|
BigInt(options = {}) {
|
|
2274
2306
|
return this.Create({ ...options, [exports.Kind]: 'BigInt', type: 'bigint' });
|
package/value/convert.js
CHANGED
|
@@ -280,7 +280,7 @@ function TUnknown(schema, references, value) {
|
|
|
280
280
|
function TVoid(schema, references, value) {
|
|
281
281
|
return value;
|
|
282
282
|
}
|
|
283
|
-
function
|
|
283
|
+
function TKind(schema, references, value) {
|
|
284
284
|
return value;
|
|
285
285
|
}
|
|
286
286
|
function Visit(schema, references, value) {
|
|
@@ -348,7 +348,7 @@ function Visit(schema, references, value) {
|
|
|
348
348
|
default:
|
|
349
349
|
if (!Types.TypeRegistry.Has(schema_[Types.Kind]))
|
|
350
350
|
throw new ValueConvertUnknownTypeError(schema_);
|
|
351
|
-
return
|
|
351
|
+
return TKind(schema_, references_, value);
|
|
352
352
|
}
|
|
353
353
|
}
|
|
354
354
|
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|