@sinclair/typebox 0.31.7 → 0.31.9
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 +3 -3
- package/typebox.d.ts +11 -13
- package/typebox.js +18 -16
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -58,11 +58,11 @@ type T = Static<typeof T> // type T = {
|
|
|
58
58
|
|
|
59
59
|
## Overview
|
|
60
60
|
|
|
61
|
-
TypeBox is a runtime type builder that creates Json Schema objects that infer as TypeScript types. The schemas produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type that can be statically checked by TypeScript or runtime
|
|
61
|
+
TypeBox is a runtime type builder that creates Json Schema objects that infer as TypeScript types. The schemas produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type that can be statically checked by TypeScript or runtime asserted using standard Json Schema validation.
|
|
62
62
|
|
|
63
|
-
TypeBox
|
|
63
|
+
TypeBox uses industry standard schematics for runtime type representation; enabling types to be reflected, serialized and published directly. Its type system is fully extensible and able to support type representation for multiple schema specifications. It also provides a high performance validation compiler, various tools for working with dynamic data and offers detailed structured error reporting.
|
|
64
64
|
|
|
65
|
-
TypeBox can be used as a simple tool to build up complex schemas or integrated into applications to enable high performance runtime
|
|
65
|
+
TypeBox can be used as a simple tool to build up complex schemas or integrated into applications and frameworks to enable high performance runtime type checking for data received over the wire.
|
|
66
66
|
|
|
67
67
|
License MIT
|
|
68
68
|
|
package/typebox.d.ts
CHANGED
|
@@ -72,7 +72,7 @@ export interface TSchema extends SchemaOptions, TKind {
|
|
|
72
72
|
params: unknown[];
|
|
73
73
|
static: unknown;
|
|
74
74
|
}
|
|
75
|
-
export type TAnySchema = TSchema | TAny | TArray | TAsyncIterator | TBigInt | TBoolean | TConstructor | TDate |
|
|
75
|
+
export type TAnySchema = TSchema | TAny | TArray | TAsyncIterator | TBigInt | TBoolean | TConstructor | TDate | TFunction | TInteger | TIntersect | TIterator | TLiteral | TNot | TNull | TNumber | TObject | TPromise | TRecord | TRef | TString | TSymbol | TTemplateLiteral | TThis | TTuple | TUndefined | TUnion | TUint8Array | TUnknown | TVoid;
|
|
76
76
|
export interface NumericOptions<N extends number | bigint> extends SchemaOptions {
|
|
77
77
|
exclusiveMaximum?: N;
|
|
78
78
|
exclusiveMinimum?: N;
|
|
@@ -157,15 +157,13 @@ export interface TDate extends TSchema, DateOptions {
|
|
|
157
157
|
static: Date;
|
|
158
158
|
type: 'date';
|
|
159
159
|
}
|
|
160
|
-
export
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
export
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
anyOf: TLiteral<T[keyof T]>[];
|
|
168
|
-
}
|
|
160
|
+
export type TEnumRecord = Record<TEnumKey, TEnumValue>;
|
|
161
|
+
export type TEnumValue = string | number;
|
|
162
|
+
export type TEnumKey = string;
|
|
163
|
+
export type TEnumToLiteralUnion<T extends TEnumValue> = T extends TEnumValue ? TLiteral<T> : never;
|
|
164
|
+
export type TEnumToLiteralTuple<T extends TEnumValue> = UnionToTuple<TEnumToLiteralUnion<T>>;
|
|
165
|
+
export type TEnumToUnion<T extends TEnumValue, R = UnionType<AssertRest<TEnumToLiteralTuple<T>>>> = R extends TLiteralString ? TNever : R;
|
|
166
|
+
export type TEnum<T extends TEnumValue> = Ensure<TEnumToUnion<T>>;
|
|
169
167
|
export type TExtends<L extends TSchema, R extends TSchema, T extends TSchema, U extends TSchema> = (Static<L> extends Static<R> ? T : U) extends infer O ? UnionToTuple<O> extends [infer X, infer Y] ? TUnion<[AssertType<X>, AssertType<Y>]> : AssertType<O> : never;
|
|
170
168
|
export type TExcludeTemplateLiteralResult<T extends string> = UnionType<AssertRest<UnionToTuple<{
|
|
171
169
|
[K in T]: TLiteral<K>;
|
|
@@ -776,8 +774,8 @@ export declare class TypeBuilder {
|
|
|
776
774
|
protected Create<T>(schema: Omit<T, 'static' | 'params'>): T;
|
|
777
775
|
/** `[Internal]` Throws a TypeBuilder error with the given message */
|
|
778
776
|
protected Throw(message: string): never;
|
|
779
|
-
/** `[Internal]` Discards
|
|
780
|
-
protected Discard(
|
|
777
|
+
/** `[Internal]` Discards property keys from the given record type */
|
|
778
|
+
protected Discard(record: Record<PropertyKey, any>, keys: PropertyKey[]): any;
|
|
781
779
|
/** `[Json]` Omits compositing symbols from this schema */
|
|
782
780
|
Strict<T extends TSchema>(schema: T): T;
|
|
783
781
|
}
|
|
@@ -799,7 +797,7 @@ export declare class JsonTypeBuilder extends TypeBuilder {
|
|
|
799
797
|
/** `[Json]` Creates a Composite object type */
|
|
800
798
|
Composite<T extends TObject[]>(objects: [...T], options?: ObjectOptions): TComposite<T>;
|
|
801
799
|
/** `[Json]` Creates a Enum type */
|
|
802
|
-
Enum<V extends
|
|
800
|
+
Enum<V extends TEnumValue, T extends Record<TEnumKey, V>>(item: T, options?: SchemaOptions): TEnum<T[keyof T]>;
|
|
803
801
|
/** `[Json]` Creates a Conditional type */
|
|
804
802
|
Extends<L extends TSchema, R extends TSchema, T extends TSchema, U extends TSchema>(left: L, right: R, trueType: T, falseType: U, options?: SchemaOptions): TExtends<L, R, T, U>;
|
|
805
803
|
/** `[Json]` Constructs a type by excluding from unionType all union members that are assignable to excludedMembers */
|
package/typebox.js
CHANGED
|
@@ -1874,10 +1874,12 @@ class TypeBuilder {
|
|
|
1874
1874
|
Throw(message) {
|
|
1875
1875
|
throw new TypeBuilderError(message);
|
|
1876
1876
|
}
|
|
1877
|
-
/** `[Internal]` Discards
|
|
1878
|
-
Discard(
|
|
1879
|
-
|
|
1880
|
-
|
|
1877
|
+
/** `[Internal]` Discards property keys from the given record type */
|
|
1878
|
+
Discard(record, keys) {
|
|
1879
|
+
return keys.reduce((acc, key) => {
|
|
1880
|
+
const { [key]: _, ...rest } = acc;
|
|
1881
|
+
return rest;
|
|
1882
|
+
}, record);
|
|
1881
1883
|
}
|
|
1882
1884
|
/** `[Json]` Omits compositing symbols from this schema */
|
|
1883
1885
|
Strict(schema) {
|
|
@@ -1932,13 +1934,13 @@ class JsonTypeBuilder extends TypeBuilder {
|
|
|
1932
1934
|
}
|
|
1933
1935
|
/** `[Json]` Creates a Enum type */
|
|
1934
1936
|
Enum(item, options = {}) {
|
|
1937
|
+
if (ValueGuard.IsUndefined(item))
|
|
1938
|
+
return this.Union([], options);
|
|
1935
1939
|
// prettier-ignore
|
|
1936
|
-
const
|
|
1937
|
-
//
|
|
1938
|
-
const anyOf =
|
|
1939
|
-
|
|
1940
|
-
: { [exports.Kind]: 'Literal', type: 'number', const: value });
|
|
1941
|
-
return this.Create({ ...options, [exports.Kind]: 'Union', anyOf });
|
|
1940
|
+
const values1 = Object.getOwnPropertyNames(item).filter((key) => isNaN(key)).map((key) => item[key]);
|
|
1941
|
+
const values2 = [...new Set(values1)]; // distinct
|
|
1942
|
+
const anyOf = values2.map((value) => exports.Type.Literal(value));
|
|
1943
|
+
return this.Union(anyOf, options);
|
|
1942
1944
|
}
|
|
1943
1945
|
/** `[Json]` Creates a Conditional type */
|
|
1944
1946
|
Extends(left, right, trueType, falseType, options = {}) {
|
|
@@ -2076,7 +2078,7 @@ class JsonTypeBuilder extends TypeBuilder {
|
|
|
2076
2078
|
Omit(schema, unresolved, options = {}) {
|
|
2077
2079
|
const keys = KeyArrayResolver.Resolve(unresolved);
|
|
2078
2080
|
// prettier-ignore
|
|
2079
|
-
return ObjectMap.Map(this.Discard(TypeClone.Type(schema), exports.Transform), (object) => {
|
|
2081
|
+
return ObjectMap.Map(this.Discard(TypeClone.Type(schema), ['$id', exports.Transform]), (object) => {
|
|
2080
2082
|
if (ValueGuard.IsArray(object.required)) {
|
|
2081
2083
|
object.required = object.required.filter((key) => !keys.includes(key));
|
|
2082
2084
|
if (object.required.length === 0)
|
|
@@ -2092,18 +2094,18 @@ class JsonTypeBuilder extends TypeBuilder {
|
|
|
2092
2094
|
/** `[Json]` Constructs a type where all properties are optional */
|
|
2093
2095
|
Partial(schema, options = {}) {
|
|
2094
2096
|
// prettier-ignore
|
|
2095
|
-
return ObjectMap.Map(this.Discard(TypeClone.Type(schema), exports.Transform), (object) => {
|
|
2097
|
+
return ObjectMap.Map(this.Discard(TypeClone.Type(schema), ['$id', exports.Transform]), (object) => {
|
|
2096
2098
|
const properties = Object.getOwnPropertyNames(object.properties).reduce((acc, key) => {
|
|
2097
2099
|
return { ...acc, [key]: this.Optional(object.properties[key]) };
|
|
2098
2100
|
}, {});
|
|
2099
|
-
return this.Object(properties, this.Discard(object, 'required') /* object used as options to retain other constraints */);
|
|
2101
|
+
return this.Object(properties, this.Discard(object, ['required']) /* object used as options to retain other constraints */);
|
|
2100
2102
|
}, options);
|
|
2101
2103
|
}
|
|
2102
2104
|
/** `[Json]` Constructs a type whose keys are picked from the given type */
|
|
2103
2105
|
Pick(schema, unresolved, options = {}) {
|
|
2104
2106
|
const keys = KeyArrayResolver.Resolve(unresolved);
|
|
2105
2107
|
// prettier-ignore
|
|
2106
|
-
return ObjectMap.Map(this.Discard(TypeClone.Type(schema), exports.Transform), (object) => {
|
|
2108
|
+
return ObjectMap.Map(this.Discard(TypeClone.Type(schema), ['$id', exports.Transform]), (object) => {
|
|
2107
2109
|
if (ValueGuard.IsArray(object.required)) {
|
|
2108
2110
|
object.required = object.required.filter((key) => keys.includes(key));
|
|
2109
2111
|
if (object.required.length === 0)
|
|
@@ -2169,9 +2171,9 @@ class JsonTypeBuilder extends TypeBuilder {
|
|
|
2169
2171
|
/** `[Json]` Constructs a type where all properties are required */
|
|
2170
2172
|
Required(schema, options = {}) {
|
|
2171
2173
|
// prettier-ignore
|
|
2172
|
-
return ObjectMap.Map(this.Discard(TypeClone.Type(schema), exports.Transform), (object) => {
|
|
2174
|
+
return ObjectMap.Map(this.Discard(TypeClone.Type(schema), ['$id', exports.Transform]), (object) => {
|
|
2173
2175
|
const properties = Object.getOwnPropertyNames(object.properties).reduce((acc, key) => {
|
|
2174
|
-
return { ...acc, [key]: this.Discard(object.properties[key], exports.Optional) };
|
|
2176
|
+
return { ...acc, [key]: this.Discard(object.properties[key], [exports.Optional]) };
|
|
2175
2177
|
}, {});
|
|
2176
2178
|
return this.Object(properties, object /* object used as options to retain other constraints */);
|
|
2177
2179
|
}, options);
|