typebox 1.1.39 → 1.2.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/build/guard/guard.d.mts +2 -2
- package/build/guard/guard.mjs +2 -2
- package/build/type/action/index.d.mts +1 -1
- package/build/type/action/index.mjs +1 -1
- package/build/type/action/with.d.mts +21 -0
- package/build/type/action/with.mjs +19 -0
- package/build/type/engine/enum/index.d.mts +0 -1
- package/build/type/engine/enum/index.mjs +0 -1
- package/build/type/engine/evaluate/evaluate.d.mts +15 -3
- package/build/type/engine/evaluate/evaluate.mjs +30 -6
- package/build/type/engine/exclude/operation.d.mts +5 -6
- package/build/type/engine/exclude/operation.mjs +11 -12
- package/build/type/engine/extract/operation.d.mts +5 -6
- package/build/type/engine/extract/operation.mjs +11 -12
- package/build/type/engine/index.d.mts +1 -1
- package/build/type/engine/index.mjs +1 -1
- package/build/type/engine/indexable/from_dependent.d.mts +5 -0
- package/build/type/engine/indexable/from_dependent.mjs +8 -0
- package/build/type/engine/indexable/from_enum.d.mts +3 -3
- package/build/type/engine/indexable/from_enum.mjs +4 -4
- package/build/type/engine/indexable/from_template_literal.d.mts +2 -2
- package/build/type/engine/indexable/from_template_literal.mjs +3 -3
- package/build/type/engine/indexable/from_type.d.mts +3 -1
- package/build/type/engine/indexable/from_type.mjs +9 -6
- package/build/type/engine/indexed/instantiate.d.mts +2 -1
- package/build/type/engine/indexed/instantiate.mjs +2 -1
- package/build/type/engine/instantiate.d.mts +7 -6
- package/build/type/engine/instantiate.mjs +23 -21
- package/build/type/engine/intrinsics/from_template_literal.d.mts +2 -2
- package/build/type/engine/intrinsics/from_template_literal.mjs +3 -3
- package/build/type/engine/keyof/instantiate.d.mts +2 -1
- package/build/type/engine/keyof/instantiate.mjs +2 -1
- package/build/type/engine/mapped/mapped_operation.d.mts +4 -4
- package/build/type/engine/mapped/mapped_operation.mjs +3 -3
- package/build/type/engine/mapped/mapped_variants.d.mts +5 -4
- package/build/type/engine/mapped/mapped_variants.mjs +10 -5
- package/build/type/engine/object/from_dependent.d.mts +5 -0
- package/build/type/engine/object/from_dependent.mjs +8 -0
- package/build/type/engine/object/from_type.d.mts +3 -1
- package/build/type/engine/object/from_type.mjs +8 -5
- package/build/type/engine/partial/from_dependent.d.mts +5 -0
- package/build/type/engine/partial/from_dependent.mjs +8 -0
- package/build/type/engine/partial/from_intersect.d.mts +1 -1
- package/build/type/engine/partial/from_intersect.mjs +3 -2
- package/build/type/engine/partial/from_type.d.mts +3 -1
- package/build/type/engine/partial/from_type.mjs +7 -4
- package/build/type/engine/readonly_object/from_dependent.d.mts +5 -0
- package/build/type/engine/readonly_object/from_dependent.mjs +8 -0
- package/build/type/engine/readonly_object/from_intersect.d.mts +1 -1
- package/build/type/engine/readonly_object/from_intersect.mjs +3 -2
- package/build/type/engine/readonly_object/from_type.d.mts +3 -1
- package/build/type/engine/readonly_object/from_type.mjs +8 -5
- package/build/type/engine/record/from_key_enum.d.mts +2 -2
- package/build/type/engine/record/from_key_enum.mjs +2 -2
- package/build/type/engine/record/from_key_template_literal.d.mts +2 -2
- package/build/type/engine/record/from_key_template_literal.mjs +2 -2
- package/build/type/engine/required/from_dependent.d.mts +5 -0
- package/build/type/engine/required/from_dependent.mjs +8 -0
- package/build/type/engine/required/from_intersect.d.mts +1 -1
- package/build/type/engine/required/from_intersect.mjs +3 -2
- package/build/type/engine/required/from_type.d.mts +3 -1
- package/build/type/engine/required/from_type.mjs +7 -4
- package/build/type/engine/template_literal/encode.d.mts +2 -2
- package/build/type/engine/template_literal/encode.mjs +4 -4
- package/build/type/engine/with/instantiate.d.mts +8 -0
- package/build/type/engine/{options → with}/instantiate.mjs +5 -5
- package/build/type/extends/dependent.d.mts +6 -0
- package/build/type/extends/dependent.mjs +6 -0
- package/build/type/extends/enum.d.mts +4 -4
- package/build/type/extends/enum.mjs +3 -2
- package/build/type/extends/extends_left.d.mts +3 -1
- package/build/type/extends/extends_left.mjs +23 -20
- package/build/type/extends/extends_right.d.mts +7 -5
- package/build/type/extends/extends_right.mjs +18 -13
- package/build/type/extends/template_literal.d.mts +3 -3
- package/build/type/extends/template_literal.mjs +3 -3
- package/build/type/script/mapping.d.mts +4 -7
- package/build/type/script/mapping.mjs +16 -16
- package/build/type/script/parser.d.mts +3 -3
- package/build/type/script/parser.mjs +2 -2
- package/build/type/types/dependent.d.mts +17 -0
- package/build/type/types/dependent.mjs +24 -0
- package/build/type/types/enum.d.mts +1 -1
- package/build/type/types/index.d.mts +1 -0
- package/build/type/types/index.mjs +1 -0
- package/build/type/types/static.d.mts +2 -1
- package/build/typebox.d.mts +2 -1
- package/build/typebox.mjs +2 -1
- package/build/value/clean/from_intersect.mjs +2 -2
- package/build/value/clone/clone.mjs +17 -3
- package/build/value/convert/from_enum.mjs +3 -4
- package/build/value/convert/from_template_literal.d.mts +1 -1
- package/build/value/convert/from_template_literal.mjs +2 -3
- package/build/value/create/from_enum.mjs +2 -2
- package/build/value/repair/from_enum.mjs +2 -3
- package/package.json +1 -1
- package/readme.md +51 -20
- package/build/type/action/options.d.mts +0 -11
- package/build/type/action/options.mjs +0 -11
- package/build/type/engine/enum/enum_to_union.d.mts +0 -14
- package/build/type/engine/enum/enum_to_union.mjs +0 -24
- package/build/type/engine/options/instantiate.d.mts +0 -8
- /package/build/type/engine/{options → with}/index.d.mts +0 -0
- /package/build/type/engine/{options → with}/index.mjs +0 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// deno-fmt-ignore-file
|
|
2
|
+
import { Memory } from '../../system/memory/index.mjs';
|
|
3
|
+
import { IsKind } from './schema.mjs';
|
|
4
|
+
// ------------------------------------------------------------------
|
|
5
|
+
// Factory
|
|
6
|
+
// ------------------------------------------------------------------
|
|
7
|
+
/** Creates a Dependent type */
|
|
8
|
+
export function Dependent(if_, then_, else_, options = {}) {
|
|
9
|
+
return Memory.Create({ '~kind': 'Dependent' }, { if: if_, then: then_, else: else_ }, options);
|
|
10
|
+
}
|
|
11
|
+
// ------------------------------------------------------------------
|
|
12
|
+
// Guard
|
|
13
|
+
// ------------------------------------------------------------------
|
|
14
|
+
/** Returns true if the given value is TDependent. */
|
|
15
|
+
export function IsDependent(value) {
|
|
16
|
+
return IsKind(value, 'Dependent');
|
|
17
|
+
}
|
|
18
|
+
// ------------------------------------------------------------------
|
|
19
|
+
// Options
|
|
20
|
+
// ------------------------------------------------------------------
|
|
21
|
+
/** Extracts options from a IsDependent. */
|
|
22
|
+
export function DependentOptions(type) {
|
|
23
|
+
return Memory.Discard(type, ['~kind', 'if', 'then', 'else']);
|
|
24
|
+
}
|
|
@@ -2,7 +2,7 @@ import { type TSchema, type TSchemaOptions } from './schema.mjs';
|
|
|
2
2
|
import { type TTypeScriptEnumLike } from '../engine/enum/typescript_enum_to_enum_values.mjs';
|
|
3
3
|
import { type TTypeScriptEnumToEnumValues } from '../engine/enum/typescript_enum_to_enum_values.mjs';
|
|
4
4
|
export type StaticEnum<Values extends TEnumValue[]> = (Values[number]);
|
|
5
|
-
export type TEnumValue = string | number
|
|
5
|
+
export type TEnumValue = string | number;
|
|
6
6
|
/** Represents an Enum type. */
|
|
7
7
|
export interface TEnum<Values extends TEnumValue[] = TEnumValue[]> extends TSchema {
|
|
8
8
|
'~kind': 'Enum';
|
|
@@ -17,6 +17,7 @@ export * from './enum.mjs';
|
|
|
17
17
|
export * from './function.mjs';
|
|
18
18
|
export * from './generic.mjs';
|
|
19
19
|
export * from './identifier.mjs';
|
|
20
|
+
export * from './dependent.mjs';
|
|
20
21
|
export * from './infer.mjs';
|
|
21
22
|
export * from './integer.mjs';
|
|
22
23
|
export * from './intersect.mjs';
|
|
@@ -23,6 +23,7 @@ export * from './enum.mjs';
|
|
|
23
23
|
export * from './function.mjs';
|
|
24
24
|
export * from './generic.mjs';
|
|
25
25
|
export * from './identifier.mjs';
|
|
26
|
+
export * from './dependent.mjs';
|
|
26
27
|
export * from './infer.mjs';
|
|
27
28
|
export * from './integer.mjs';
|
|
28
29
|
export * from './intersect.mjs';
|
|
@@ -10,6 +10,7 @@ import type { StaticCyclic, TCyclic } from './cyclic.mjs';
|
|
|
10
10
|
import type { StaticEnum, TEnum, TEnumValue } from './enum.mjs';
|
|
11
11
|
import type { StaticFunction, TFunction } from './function.mjs';
|
|
12
12
|
import type { StaticInteger, TInteger } from './integer.mjs';
|
|
13
|
+
import type { StaticDependent, TDependent } from './dependent.mjs';
|
|
13
14
|
import type { StaticIntersect, TIntersect } from './intersect.mjs';
|
|
14
15
|
import type { StaticIterator, TIterator } from './iterator.mjs';
|
|
15
16
|
import type { StaticLiteral, TLiteral, TLiteralValue } from './literal.mjs';
|
|
@@ -37,7 +38,7 @@ export type StaticEvaluate<T> = {
|
|
|
37
38
|
[K in keyof T]: T[K];
|
|
38
39
|
} & {};
|
|
39
40
|
export type StaticDirection = 'Encode' | 'Decode';
|
|
40
|
-
export type StaticType<Stack extends string[], Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Type extends TSchema> = (Type extends TCodec<infer Type extends TSchema, infer Decoded extends unknown> ? StaticCodec<Stack, Direction, Context, This, Type, Decoded> : Type extends TAny ? StaticAny : Type extends TArray<infer Items extends TSchema> ? StaticArray<Stack, Direction, Context, This, Type, Items> : Type extends TAsyncIterator<infer Type extends TSchema> ? StaticAsyncIterator<Stack, Direction, Context, This, Type> : Type extends Base<infer Value extends unknown> ? StaticBase<Value> : Type extends TBigInt ? StaticBigInt : Type extends TBoolean ? StaticBoolean : Type extends TConstructor<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? StaticConstructor<Stack, Direction, Context, This, Parameters, ReturnType> : Type extends TEnum<infer Values extends TEnumValue[]> ? StaticEnum<Values> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? StaticFunction<Stack, Direction, Context, This, Parameters, ReturnType> : Type extends TInteger ? StaticInteger : Type extends TIntersect<infer Types extends TSchema[]> ? StaticIntersect<Stack, Direction, Context, This, Types> : Type extends TIterator<infer Types extends TSchema> ? StaticIterator<Stack, Direction, Context, This, Types> : Type extends TLiteral<infer Value extends TLiteralValue> ? StaticLiteral<Value> : Type extends TNever ? StaticNever : Type extends TNull ? StaticNull : Type extends TNumber ? StaticNumber : Type extends TObject<infer Properties extends TProperties> ? StaticObject<Stack, Direction, Context, This, Properties> : Type extends TPromise<infer Type extends TSchema> ? StaticPromise<Stack, Direction, Context, This, Type> : Type extends TRecord<infer Key extends string, infer Value extends TSchema> ? StaticRecord<Stack, Direction, Context, This, Key, Value> : Type extends TCyclic<infer Defs extends TProperties, infer Ref extends string> ? StaticCyclic<Stack, Direction, Context, This, Defs, Ref> : Type extends TRef<infer Ref extends string> ? StaticRef<Stack, Direction, Context, This, Ref> : Type extends TString ? StaticString : Type extends TSymbol ? StaticSymbol : Type extends TTemplateLiteral<infer Pattern extends string> ? StaticTemplateLiteral<Pattern> : Type extends TThis ? StaticThis<Stack, Direction, Context, This> : Type extends TTuple<infer Items extends TSchema[]> ? StaticTuple<Stack, Direction, Context, This, Type, Items> : Type extends TUndefined ? StaticUndefined : Type extends TUnion<infer Types extends TSchema[]> ? StaticUnion<Stack, Direction, Context, This, Types> : Type extends TUnknown ? StaticUnknown : Type extends TUnsafe<infer Type extends unknown> ? StaticUnsafe<Type> : Type extends TVoid ? StaticVoid : XStatic<Type>);
|
|
41
|
+
export type StaticType<Stack extends string[], Direction extends StaticDirection, Context extends TProperties, This extends TProperties, Type extends TSchema> = (Type extends TCodec<infer Type extends TSchema, infer Decoded extends unknown> ? StaticCodec<Stack, Direction, Context, This, Type, Decoded> : Type extends TAny ? StaticAny : Type extends TArray<infer Items extends TSchema> ? StaticArray<Stack, Direction, Context, This, Type, Items> : Type extends TAsyncIterator<infer Type extends TSchema> ? StaticAsyncIterator<Stack, Direction, Context, This, Type> : Type extends Base<infer Value extends unknown> ? StaticBase<Value> : Type extends TBigInt ? StaticBigInt : Type extends TBoolean ? StaticBoolean : Type extends TConstructor<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? StaticConstructor<Stack, Direction, Context, This, Parameters, ReturnType> : Type extends TEnum<infer Values extends TEnumValue[]> ? StaticEnum<Values> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? StaticFunction<Stack, Direction, Context, This, Parameters, ReturnType> : Type extends TDependent<infer If extends TSchema, infer Then extends TSchema, infer Else extends TSchema> ? StaticDependent<Stack, Direction, Context, This, If, Then, Else> : Type extends TInteger ? StaticInteger : Type extends TIntersect<infer Types extends TSchema[]> ? StaticIntersect<Stack, Direction, Context, This, Types> : Type extends TIterator<infer Types extends TSchema> ? StaticIterator<Stack, Direction, Context, This, Types> : Type extends TLiteral<infer Value extends TLiteralValue> ? StaticLiteral<Value> : Type extends TNever ? StaticNever : Type extends TNull ? StaticNull : Type extends TNumber ? StaticNumber : Type extends TObject<infer Properties extends TProperties> ? StaticObject<Stack, Direction, Context, This, Properties> : Type extends TPromise<infer Type extends TSchema> ? StaticPromise<Stack, Direction, Context, This, Type> : Type extends TRecord<infer Key extends string, infer Value extends TSchema> ? StaticRecord<Stack, Direction, Context, This, Key, Value> : Type extends TCyclic<infer Defs extends TProperties, infer Ref extends string> ? StaticCyclic<Stack, Direction, Context, This, Defs, Ref> : Type extends TRef<infer Ref extends string> ? StaticRef<Stack, Direction, Context, This, Ref> : Type extends TString ? StaticString : Type extends TSymbol ? StaticSymbol : Type extends TTemplateLiteral<infer Pattern extends string> ? StaticTemplateLiteral<Pattern> : Type extends TThis ? StaticThis<Stack, Direction, Context, This> : Type extends TTuple<infer Items extends TSchema[]> ? StaticTuple<Stack, Direction, Context, This, Type, Items> : Type extends TUndefined ? StaticUndefined : Type extends TUnion<infer Types extends TSchema[]> ? StaticUnion<Stack, Direction, Context, This, Types> : Type extends TUnknown ? StaticUnknown : Type extends TUnsafe<infer Type extends unknown> ? StaticUnsafe<Type> : Type extends TVoid ? StaticVoid : XStatic<Type>);
|
|
41
42
|
/** Infers a static type from a TypeBox type using Parse logic. */
|
|
42
43
|
export type StaticParse<Type extends TSchema, Context extends TProperties = {}, Result extends unknown = StaticType<[], 'Encode', Context, {}, Type>> = Result;
|
|
43
44
|
/** Infers a static type from a TypeBox type using Decode logic. */
|
package/build/typebox.d.mts
CHANGED
|
@@ -17,7 +17,6 @@ export { Mapped, type TMapped, type TMappedDeferred } from './type/action/mapped
|
|
|
17
17
|
export { Module, type TModule, type TModuleDeferred } from './type/action/module.mjs';
|
|
18
18
|
export { NonNullable, type TNonNullable, type TNonNullableDeferred } from './type/action/non_nullable.mjs';
|
|
19
19
|
export { Omit, type TOmit, type TOmitDeferred } from './type/action/omit.mjs';
|
|
20
|
-
export { Options, type TOptions } from './type/action/options.mjs';
|
|
21
20
|
export { Parameters, type TParameters, type TParametersDeferred } from './type/action/parameters.mjs';
|
|
22
21
|
export { Partial, type TPartial, type TPartialDeferred } from './type/action/partial.mjs';
|
|
23
22
|
export { Pick, type TPick, type TPickDeferred } from './type/action/pick.mjs';
|
|
@@ -26,6 +25,7 @@ export { Required, type TRequired, type TRequiredDeferred } from './type/action/
|
|
|
26
25
|
export { ReturnType, type TReturnType, type TReturnTypeDeferred } from './type/action/return_type.mjs';
|
|
27
26
|
export { type TUncapitalize, type TUncapitalizeDeferred, Uncapitalize } from './type/action/uncapitalize.mjs';
|
|
28
27
|
export { type TUppercase, type TUppercaseDeferred, Uppercase } from './type/action/uppercase.mjs';
|
|
28
|
+
export { Options, type TOptions, type TWith, With } from './type/action/with.mjs';
|
|
29
29
|
export { Codec, Decode, DecodeBuilder, Encode, EncodeBuilder, IsCodec, type TCodec } from './type/types/_codec.mjs';
|
|
30
30
|
export { Immutable, IsImmutable, type TImmutable } from './type/types/_immutable.mjs';
|
|
31
31
|
export { IsOptional, Optional, type TOptional } from './type/types/_optional.mjs';
|
|
@@ -44,6 +44,7 @@ export { Enum, IsEnum, type TEnum, type TEnumValue } from './type/types/enum.mjs
|
|
|
44
44
|
export { Function, IsFunction, type TFunction } from './type/types/function.mjs';
|
|
45
45
|
export { Generic, IsGeneric, type TGeneric } from './type/types/generic.mjs';
|
|
46
46
|
export { Identifier, IsIdentifier, type TIdentifier } from './type/types/identifier.mjs';
|
|
47
|
+
export { Dependent, IsDependent, type TDependent } from './type/types/dependent.mjs';
|
|
47
48
|
export { Infer, IsInfer, type TInfer } from './type/types/infer.mjs';
|
|
48
49
|
export { Integer, IsInteger, type TInteger } from './type/types/integer.mjs';
|
|
49
50
|
export { Intersect, IsIntersect, type TIntersect } from './type/types/intersect.mjs';
|
package/build/typebox.mjs
CHANGED
|
@@ -29,7 +29,6 @@ export { Mapped } from './type/action/mapped.mjs';
|
|
|
29
29
|
export { Module } from './type/action/module.mjs';
|
|
30
30
|
export { NonNullable } from './type/action/non_nullable.mjs';
|
|
31
31
|
export { Omit } from './type/action/omit.mjs';
|
|
32
|
-
export { Options } from './type/action/options.mjs';
|
|
33
32
|
export { Parameters } from './type/action/parameters.mjs';
|
|
34
33
|
export { Partial } from './type/action/partial.mjs';
|
|
35
34
|
export { Pick } from './type/action/pick.mjs';
|
|
@@ -38,6 +37,7 @@ export { Required } from './type/action/required.mjs';
|
|
|
38
37
|
export { ReturnType } from './type/action/return_type.mjs';
|
|
39
38
|
export { Uncapitalize } from './type/action/uncapitalize.mjs';
|
|
40
39
|
export { Uppercase } from './type/action/uppercase.mjs';
|
|
40
|
+
export { Options, With } from './type/action/with.mjs';
|
|
41
41
|
// ------------------------------------------------------------------
|
|
42
42
|
// Extension
|
|
43
43
|
// ------------------------------------------------------------------
|
|
@@ -62,6 +62,7 @@ export { Enum, IsEnum } from './type/types/enum.mjs';
|
|
|
62
62
|
export { Function, IsFunction } from './type/types/function.mjs';
|
|
63
63
|
export { Generic, IsGeneric } from './type/types/generic.mjs';
|
|
64
64
|
export { Identifier, IsIdentifier } from './type/types/identifier.mjs';
|
|
65
|
+
export { Dependent, IsDependent } from './type/types/dependent.mjs';
|
|
65
66
|
export { Infer, IsInfer } from './type/types/infer.mjs';
|
|
66
67
|
export { Integer, IsInteger } from './type/types/integer.mjs';
|
|
67
68
|
export { Intersect, IsIntersect } from './type/types/intersect.mjs';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
-
import { Evaluate, Instantiate, IsObject,
|
|
2
|
+
import { Evaluate, Instantiate, IsObject, With } from '../../type/index.mjs';
|
|
3
3
|
import { Guard } from '../../guard/index.mjs';
|
|
4
4
|
import { FromType } from './from_type.mjs';
|
|
5
5
|
// ------------------------------------------------------------------
|
|
@@ -13,7 +13,7 @@ function EvaluateIntersection(context, type) {
|
|
|
13
13
|
const instantiated = Instantiate(context, type);
|
|
14
14
|
const evaluated = Evaluate(instantiated);
|
|
15
15
|
return IsObject(evaluated)
|
|
16
|
-
?
|
|
16
|
+
? With(evaluated, additionalProperties)
|
|
17
17
|
: evaluated;
|
|
18
18
|
}
|
|
19
19
|
// ------------------------------------------------------------------
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
2
|
import { Guard, GlobalsGuard } from '../../guard/index.mjs';
|
|
3
|
+
import { Clone as SystemClone } from '../../system/memory/clone.mjs';
|
|
3
4
|
// ------------------------------------------------------------------
|
|
4
5
|
// ClassInstance
|
|
5
6
|
//
|
|
@@ -14,6 +15,19 @@ function FromClassInstance(value) {
|
|
|
14
15
|
return value; // atomic
|
|
15
16
|
}
|
|
16
17
|
// ------------------------------------------------------------------
|
|
18
|
+
// TypeInstance
|
|
19
|
+
//
|
|
20
|
+
// TypeBox types have non-enumerable properties that should be
|
|
21
|
+
// retained on Clone. We need to use a SystemClone for this
|
|
22
|
+
// to ensure the non-enumerable properties are preserved.
|
|
23
|
+
// ------------------------------------------------------------------
|
|
24
|
+
function IsTypeInstance(value) {
|
|
25
|
+
return Guard.HasPropertyKey(value, '~kind');
|
|
26
|
+
}
|
|
27
|
+
function FromTypeInstance(value) {
|
|
28
|
+
return SystemClone(value);
|
|
29
|
+
}
|
|
30
|
+
// ------------------------------------------------------------------
|
|
17
31
|
// ObjectInstance
|
|
18
32
|
// ------------------------------------------------------------------
|
|
19
33
|
function FromObjectInstance(value) {
|
|
@@ -32,9 +46,9 @@ function FromObjectInstance(value) {
|
|
|
32
46
|
// Object
|
|
33
47
|
// ------------------------------------------------------------------
|
|
34
48
|
function FromObject(value) {
|
|
35
|
-
return (Guard.IsClassInstance(value)
|
|
36
|
-
?
|
|
37
|
-
|
|
49
|
+
return (Guard.IsClassInstance(value) ? FromClassInstance(value) :
|
|
50
|
+
IsTypeInstance(value) ? FromTypeInstance(value) :
|
|
51
|
+
FromObjectInstance(value));
|
|
38
52
|
}
|
|
39
53
|
// ------------------------------------------------------------------
|
|
40
54
|
// Array
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { Evaluate } from '../../type/index.mjs';
|
|
3
|
+
import { FromType } from './from_type.mjs';
|
|
4
4
|
export function FromEnum(context, type, value) {
|
|
5
|
-
|
|
6
|
-
return FromUnion(context, union, value);
|
|
5
|
+
return FromType(context, Evaluate(type), value);
|
|
7
6
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type TTemplateLiteral, type TProperties } from '../../type/index.mjs';
|
|
2
2
|
export declare function FromTemplateLiteral(context: TProperties, type: TTemplateLiteral, value: unknown): unknown;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
-
import {
|
|
2
|
+
import { Evaluate } from '../../type/index.mjs';
|
|
3
3
|
import { FromType } from './from_type.mjs';
|
|
4
4
|
export function FromTemplateLiteral(context, type, value) {
|
|
5
|
-
|
|
6
|
-
return FromType(context, decoded, value);
|
|
5
|
+
return FromType(context, Evaluate(type), value);
|
|
7
6
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
-
import {
|
|
2
|
+
import { Evaluate } from '../../type/index.mjs';
|
|
3
3
|
import { FromType } from './from_type.mjs';
|
|
4
4
|
export function FromEnum(context, type) {
|
|
5
|
-
return FromType(context,
|
|
5
|
+
return FromType(context, Evaluate(type));
|
|
6
6
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
-
import {
|
|
2
|
+
import { Evaluate } from '../../type/index.mjs';
|
|
3
3
|
import { FromType } from './from_type.mjs';
|
|
4
4
|
export function FromEnum(context, type, value) {
|
|
5
|
-
|
|
6
|
-
return FromType(context, union, value);
|
|
5
|
+
return FromType(context, Evaluate(type), value);
|
|
7
6
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -50,7 +50,7 @@ type T = Type.Static<typeof T> // type T = {
|
|
|
50
50
|
|
|
51
51
|
[Documentation](https://sinclairzx81.github.io/typebox/)
|
|
52
52
|
|
|
53
|
-
TypeBox is a runtime type system that creates in-memory JSON Schema objects that infer as TypeScript types. The schematics produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type
|
|
53
|
+
TypeBox is a runtime type system that creates in-memory JSON Schema objects that infer as TypeScript types. The schematics 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 and validated at runtime using standard JSON Schema.
|
|
54
54
|
|
|
55
55
|
This library is designed to allow JSON Schema to compose similar to how types compose within TypeScript's type system. It can be used as a simple tool to build up complex schematics or integrated into REST and RPC services to help validate data received over the wire.
|
|
56
56
|
|
|
@@ -113,11 +113,11 @@ type User = Type.Static<typeof User> // type User = {
|
|
|
113
113
|
|
|
114
114
|
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/script/overview) | [Example 1](https://www.typescriptlang.org/play/#code/JYWwDg9gTgLgBAFQJ5gKZwGZQiOByGFVAIwgA88AoSgehrgFonmXW32POvueHb6AygGMowMDH6Ne0mbLnNqQiADsAzvADecAKqrUUADQ69UbWAAmAQxjoAvnAC8iIgDpho8QAoABtThxgZRsoDEshdABRIOBCOA1Kf39gcwAuOHVRZQBzBLhbPwCg-VDw4304VDIbZXNVOCiYGKQ4uFz-ZUsQVDSMwKyjNoqQS2AAGx6YTKzW-3zcwjQy0wtrdCcIgDdLUYBXVYAeQYAFYCEAa33dfSMz1CQIDHrowgA+OAAyGcSjy1hgbf2AHkQDFLiYbncHk9Gq8Xrk4ZRvABKah0KTyDGYzGSABKqAwo1QQgkaKxZPJvEUKlUEEJLlGECyniuUCRiXZHM5XK5aPi3P5AsFgrR-gW3XwEGIACsiTA8AZBkKlcrOSK4GBsGg-qhVGk+SqDYa1UlUi0xWk8L1sng8grDfblca4B0unq4Ob8FasjbbHaHf7+U7UMMxm6PZbJn0fYqA7G4GrfTG4-61VBUABHHbANOmgDaSeT9qdeGS8q+hdjxZdqDLBYrKuLwZGoyo9cr9H8AF0622hWi5ko1LTUPTGcyTGYrDY2YXeT3ewK1eHJTLifL5wvuWqNRAtY0dXqN5vVR3Eskw0QLV6fX7jw3T+1OuKtOHr7aj3fEkGQ+MzZfPZG1p5B+n7xqeiagY6p5ppm2aoHmJbmHg3aQUq-aovQFJYdhLCSAIMDWKckg4SRFLUGKSyOM4aBuARjRCPsYpQiybxofQFEslR+qocKp7nukgH9CBoFqtWExTLePGBqeTahgJUzCZ+6GUBxE4rDYVHIDR+GEQxTGPCyk6rKxaKqfoRkaU43FSVufGml6kk2SeiTVgA-OJfSOU57JqrJozufJfSKXe6FAA) | [Example 2](https://www.typescriptlang.org/play/#code/JYWwDg9gTgLgBAFQJ5gKZwGZQiOByGFVAIwgA88AoSgehrgFonmXW32POvnb7k1ejbsJGj21AMYQAdgGd4AUWkxghOAF5ERAHQBJZaigYAhhNQAKANoBdADRwA3pThxgAEwBcWtNoDKMKGBpAHNzAEpKAF8Iyik5eABVWUMNb1Q9AyNTC0slFUI7R2c4aWMQVC9+dP9AkPDbYtQQY2AAG0qdGqDQiOjJGXk4JMMEsDdjGHRNKu0FADdjVoBXCYsZ-UmoZIkYK2KZgAVgCQBrc2GoexmAaVQkAHkMczzVJDCwhpdD41hgRfMZvcQKpzslLmltLcHk8XoR3hFrO9qHQhGI0eiMdxBAAlVAYVqoHaCTEk0mY-pyCAE7StCChC5hFxM5ks1lslkopzs7k83m8lEuQhoLx4CDEABWhJgeE+fLl8u5ArgYGwaF+qFkXi5Cp1uqVLncWrgQoq+HktWCeDgkVlurtfP1JTKpocxqIIvN3StNuK9r97MdTRa7UcbuFZoCXutvv9saZSp9caTLiVUFQAEclsA0544JYY8m-Y68O4ZXAC4W7cXSuUyxXKzri0G2lQG-6ldZ6225SjIhTZFT0rT6WDRuNJozk5yu93+fRBe78GLJTsZTPZ4r58rVYYVBqteuN2zHYbQyaPZGQt7bUee1uXDWXWHTXhPVfrTfb3OWc2Q67zxGFreoeX7xluiagQ6W5ppm2aoLmlglm4eCdpBUHRsSZJYdhnCCP4EzHJhOHEcR1AmkMYKpDM+EqBIAA8JoQBgFGGAAfPKKLkRcqTamhPJKqeb7BJ+fGskqj5eEJImicySq-pJl7BCBaG9mRRAsVAY6rFRnQwAR9GMcxFxaZM7FwJx6nGWM2maLxMliVugmKdJ9nmVuj4APwKRaLn2XJzRtF5cBCcpkG9kAA)
|
|
115
115
|
|
|
116
|
-
|
|
116
|
+
TypeBox includes a micro DSL for composing JSON Schema with TypeScript syntax. The DSL offers a full syntactic frontend to Type.* and supports many advanced type-level constructs such as Conditional, Mapped, Indexed, Infer, Generics, Distributed types and more. This feature is implemented symmetrically at runtime and statically via TypeScript Template Literal types.
|
|
117
117
|
|
|
118
118
|
### Example
|
|
119
119
|
|
|
120
|
-
The following uses
|
|
120
|
+
The following uses Script to parse a TypeScript definition module into JSON Schema.
|
|
121
121
|
|
|
122
122
|
```typescript
|
|
123
123
|
import Type from 'typebox'
|
|
@@ -189,35 +189,66 @@ type UserUpdate = Type.Static<typeof UserUpdate> // type UserUpdate = {
|
|
|
189
189
|
|
|
190
190
|
## Schema
|
|
191
191
|
|
|
192
|
-
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/schema/overview) | [Example 1](https://www.typescriptlang.org/play
|
|
192
|
+
[Documentation](https://sinclairzx81.github.io/typebox/#/docs/schema/overview) | [Example 1](https://www.typescriptlang.org/play/#code/JYWwDg9gTgLgBAZQMYAsCmICGcBmUIhwDkMAnmGgEYQAeA9AM6oaZEBQbddcAtH-wMFDhI0WPESenbgGECYYABs003pPUbNW-hyQQAdg3gBVBmihwAvImZYAdHPBK0ACgDebOHDIUAXMQhKACs0JBgiABpPODB8ClhgNAZ-Dy8vYAATFO9yNH8iIyhgfQBzIjgAXyi0uH1MEDy4Nxy-YkLissrqtJYlbJ9Ggpgi0sjcaCwYfN7FcoroquioNABHAFdgZay4AG1oryJMsf3iOobjmqIZ9i8AXTYKgEoOLjVtd4+P1QAFTCgzVSfIHAyS6AxGOBrMwWaymcx2X7-VzNGqotHouCvPSGeBQ8wpaKZfIANgArDgAIyYABMlAAzEgACwZUloYk4ADsmAAHJQAJxIAAMGQpYwx6Ne6W27VK3TOgzxUDF4pVqq8ktq9UaMpK3Rm+UVAAEMgRMMU7HoQOU1SqNfq4DqHo8bS7XajXhUrHA7D6gA) | [Example 2](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAZQMYAsCmICGcBmUIhwDkMAnmGgEYQAeA9AM6oaZEBQokscAKuWrnyES-ajXZs6dOAFo58hYqXKVqteo0zJ0gMIEwwADZptszeYuWr8tmyQQAdg3gBVBmihwAvImZYAdHrgRmgAFHwU-gDylABWaEgwoQDebHBwwAAmAFy8-P4IMFDADgDmoQCUADRpcA6YIGi5EWgFRSXl1bUsRs35hcVlKbjQWDC5RD2GRHAAvhVs8wum1qtr61pScAAKmFDuKxtHx6q29k7wAK7unj5uHv67+2HJ6W-vH5-vW+fOcNceXKpdJZCYANgArDgAIyYABMlAAzEgACyZCFoME4ADsmAAHJQAJxIAAMmWhRCqX2pcC2IJycGcg1KNXS9UaEwBUEpNN5fO+0jZDSajPaZVZcCmnJuAAFMgRMCV-PYQDN+by6ZKsL1RczFhV1YajR8trNvHB-JagA)
|
|
193
193
|
|
|
194
|
-
TypeBox includes a high-performance
|
|
194
|
+
TypeBox includes a high-performance JIT compiler that supports JSON Schema Draft 3 through to 2020-12. It is designed to be a lightweight industry-grade alternative to Ajv and offers improved compilation and validation performance. It also provides automatic fallback to dynamic validation in JIT restricted environments such as Cloudflare Workers.
|
|
195
195
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
The following uses the Schema submodule to compile a TypeBox type.
|
|
196
|
+
The compiler is available via optional sub module import.
|
|
199
197
|
|
|
200
198
|
```typescript
|
|
201
199
|
import Schema from 'typebox/schema'
|
|
200
|
+
```
|
|
202
201
|
|
|
203
|
-
|
|
202
|
+
### Compile
|
|
204
203
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
204
|
+
The compiler accepts JSON Schema and returns Validator instances.
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
const Vector = Schema.Compile(Type.Object({ // const Vector: Validator<TObject<{
|
|
208
|
+
x: Type.Number(), // x: TNumber
|
|
209
|
+
y: Type.Number(), // y: TNumber
|
|
210
|
+
z: Type.Number() // z: TNumber
|
|
211
|
+
})) // }>>
|
|
212
|
+
```
|
|
213
|
+
With JSON Schema
|
|
214
|
+
```typescript
|
|
215
|
+
const Vector = Schema.Compile({ // const Vector: Validator<{
|
|
216
|
+
type: 'object', // type: "object";
|
|
217
|
+
required: ['x', 'y', 'z'], // required: ["x", "y", "z"];
|
|
218
|
+
properties: { // properties: { ... };
|
|
219
|
+
x: { type: 'number' }, // }, { ... }>
|
|
220
|
+
y: { type: 'number' },
|
|
221
|
+
z: { type: 'number' }
|
|
222
|
+
}
|
|
223
|
+
})
|
|
224
|
+
```
|
|
210
225
|
|
|
211
|
-
|
|
226
|
+
### Validate
|
|
212
227
|
|
|
213
|
-
|
|
214
|
-
id: '65f1a2b3c4d5e6f7a8b9c0d1', // id: string,
|
|
215
|
-
name: 'user', // name: string,
|
|
216
|
-
email: 'user@domain.com' // email: string
|
|
217
|
-
}) // } = ...
|
|
228
|
+
Validator instances provide functions to Check and Parse values.
|
|
218
229
|
|
|
230
|
+
```typescript
|
|
231
|
+
const Vector = Schema.Compile(Type.Script(`{
|
|
232
|
+
x: number
|
|
233
|
+
y: number
|
|
234
|
+
z: number
|
|
235
|
+
}`))
|
|
236
|
+
|
|
237
|
+
const valid = Vector.Check({ // const valid: boolean
|
|
238
|
+
x: 1,
|
|
239
|
+
y: 0,
|
|
240
|
+
z: 0
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
const value = Vector.Parse({ // const value: {
|
|
244
|
+
x: 1, // x: number
|
|
245
|
+
y: 0, // y: number
|
|
246
|
+
z: 0 // z: number
|
|
247
|
+
}) // }
|
|
219
248
|
```
|
|
220
249
|
|
|
250
|
+
|
|
251
|
+
|
|
221
252
|
### Coverage
|
|
222
253
|
|
|
223
254
|
The following table shows specification coverage implemented by TypeBox.
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { type TSchema } from '../types/schema.mjs';
|
|
2
|
-
import { type TDeferred } from '../types/deferred.mjs';
|
|
3
|
-
import { type TOptionsAction } from '../engine/options/instantiate.mjs';
|
|
4
|
-
/** Creates a deferred Options action. */
|
|
5
|
-
export type TOptionsDeferred<Type extends TSchema, Options extends TSchema> = (TDeferred<'Options', [Type, Options]>);
|
|
6
|
-
/** Creates a deferred Options action. */
|
|
7
|
-
export declare function OptionsDeferred<Type extends TSchema, Options extends TSchema>(type: Type, options: Options): TOptionsDeferred<Type, Options>;
|
|
8
|
-
/** Applies an immediate Options action to the given type. */
|
|
9
|
-
export type TOptions<Type extends TSchema, Options extends TSchema> = (Type & Options);
|
|
10
|
-
/** Applies an immediate Options action to the given type. */
|
|
11
|
-
export declare function Options<Type extends TSchema, const Options extends TSchema>(type: Type, options: Options): TOptionsAction<Type, Options>;
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
// deno-fmt-ignore-file
|
|
2
|
-
import { Deferred } from '../types/deferred.mjs';
|
|
3
|
-
import { OptionsAction } from '../engine/options/instantiate.mjs';
|
|
4
|
-
/** Creates a deferred Options action. */
|
|
5
|
-
export function OptionsDeferred(type, options) {
|
|
6
|
-
return Deferred('Options', [type, options], {});
|
|
7
|
-
}
|
|
8
|
-
/** Applies an immediate Options action to the given type. */
|
|
9
|
-
export function Options(type, options) {
|
|
10
|
-
return OptionsAction(type, options);
|
|
11
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { type TSchema } from '../../types/schema.mjs';
|
|
2
|
-
import { type TEnum, type TEnumValue } from '../../types/enum.mjs';
|
|
3
|
-
import { type TUnion } from '../../types/union.mjs';
|
|
4
|
-
import { type TLiteral } from '../../types/literal.mjs';
|
|
5
|
-
import { type TNull } from '../../types/null.mjs';
|
|
6
|
-
import { type TNever } from '../../types/never.mjs';
|
|
7
|
-
type TFromEnumValue<Value extends TEnumValue, Result extends TSchema = Value extends string | number ? TLiteral<Value> : Value extends null ? TNull : TNever> = Result;
|
|
8
|
-
export type TEnumValuesToVariants<Values extends TEnumValue[], Result extends TSchema[] = []> = (Values extends [infer Left extends TEnumValue, ...infer Right extends TEnumValue[]] ? TEnumValuesToVariants<Right, [...Result, TFromEnumValue<Left>]> : Result);
|
|
9
|
-
export declare function EnumValuesToVariants<Values extends TEnumValue[]>(values: [...Values]): TEnumValuesToVariants<Values>;
|
|
10
|
-
export type TEnumValuesToUnion<Values extends TEnumValue[], Variants extends TSchema[] = TEnumValuesToVariants<Values>, Results extends TSchema = TUnion<Variants>> = Results;
|
|
11
|
-
export declare function EnumValuesToUnion<Values extends TEnumValue[]>(values: [...Values]): TEnumValuesToUnion<Values>;
|
|
12
|
-
export type TEnumToUnion<Type extends TEnum, Result extends TSchema = TEnumValuesToUnion<Type['enum']>> = Result;
|
|
13
|
-
export declare function EnumToUnion<Type extends TEnum>(type: Type): TEnumToUnion<Type>;
|
|
14
|
-
export {};
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
// deno-fmt-ignore-file
|
|
2
|
-
import { Guard } from '../../../guard/index.mjs';
|
|
3
|
-
import { Union } from '../../types/union.mjs';
|
|
4
|
-
import { Literal } from '../../types/literal.mjs';
|
|
5
|
-
import { Null } from '../../types/null.mjs';
|
|
6
|
-
import { Never } from '../../types/never.mjs';
|
|
7
|
-
function FromEnumValue(value) {
|
|
8
|
-
return (Guard.IsString(value) || Guard.IsNumber(value) ? Literal(value) :
|
|
9
|
-
Guard.IsNull(value) ? Null() :
|
|
10
|
-
Never());
|
|
11
|
-
}
|
|
12
|
-
export function EnumValuesToVariants(values) {
|
|
13
|
-
const result = values.map(value => FromEnumValue(value));
|
|
14
|
-
return result;
|
|
15
|
-
}
|
|
16
|
-
export function EnumValuesToUnion(values) {
|
|
17
|
-
const variants = EnumValuesToVariants(values);
|
|
18
|
-
const result = Union(variants);
|
|
19
|
-
return result;
|
|
20
|
-
}
|
|
21
|
-
export function EnumToUnion(type) {
|
|
22
|
-
const result = EnumValuesToUnion(type.enum);
|
|
23
|
-
return result;
|
|
24
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { type TProperties } from '../../types/properties.mjs';
|
|
2
|
-
import { type TSchema } from '../../types/schema.mjs';
|
|
3
|
-
import { type TState, type TInstantiateType, type TCanInstantiate } from '../instantiate.mjs';
|
|
4
|
-
import { type TOptionsDeferred, type TOptions } from '../../action/options.mjs';
|
|
5
|
-
export type TOptionsAction<Type extends TSchema, Options extends TSchema, Result extends TSchema = TCanInstantiate<[Type]> extends true ? TOptions<Type, Options> : TOptionsDeferred<Type, Options>> = Result;
|
|
6
|
-
export declare function OptionsAction<Type extends TSchema, Options extends TSchema>(type: Type, options: Options): TOptionsAction<Type, Options>;
|
|
7
|
-
export type TOptionsInstantiate<Context extends TProperties, State extends TState, Type extends TSchema, Options extends TSchema, InstantiatedType extends TSchema = TInstantiateType<Context, State, Type>> = TOptionsAction<InstantiatedType, Options>;
|
|
8
|
-
export declare function OptionsInstantiate<Context extends TProperties, State extends TState, Type extends TSchema, Options extends TSchema>(context: Context, state: State, type: Type, options: Options): TOptionsInstantiate<Context, State, Type, Options>;
|
|
File without changes
|
|
File without changes
|