@sinclair/typebox 0.34.7 → 0.34.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/build/cjs/compiler/compiler.d.ts +4 -0
- package/build/cjs/compiler/compiler.js +8 -0
- package/build/cjs/type/module/compute.d.ts +3 -1
- package/build/cjs/type/module/compute.js +59 -52
- package/build/cjs/value/parse/parse.d.ts +20 -4
- package/build/cjs/value/parse/parse.js +73 -17
- package/build/esm/compiler/compiler.d.mts +4 -0
- package/build/esm/compiler/compiler.mjs +8 -0
- package/build/esm/type/module/compute.d.mts +3 -1
- package/build/esm/type/module/compute.mjs +21 -14
- package/build/esm/value/parse/parse.d.mts +20 -4
- package/build/esm/value/parse/parse.mjs +70 -16
- package/package.json +1 -1
- package/readme.md +39 -73
|
@@ -12,6 +12,10 @@ export declare class TypeCheck<T extends TSchema> {
|
|
|
12
12
|
constructor(schema: T, references: TSchema[], checkFunc: CheckFunction, code: string);
|
|
13
13
|
/** Returns the generated assertion code used to validate this type. */
|
|
14
14
|
Code(): string;
|
|
15
|
+
/** Returns the schema type used to validate */
|
|
16
|
+
Schema(): T;
|
|
17
|
+
/** Returns reference types used to validate */
|
|
18
|
+
References(): TSchema[];
|
|
15
19
|
/** Returns an iterator for each error in this value. */
|
|
16
20
|
Errors(value: unknown): ValueErrorIterator;
|
|
17
21
|
/** Returns true if the value matches the compiled type. */
|
|
@@ -36,6 +36,14 @@ class TypeCheck {
|
|
|
36
36
|
Code() {
|
|
37
37
|
return this.code;
|
|
38
38
|
}
|
|
39
|
+
/** Returns the schema type used to validate */
|
|
40
|
+
Schema() {
|
|
41
|
+
return this.schema;
|
|
42
|
+
}
|
|
43
|
+
/** Returns reference types used to validate */
|
|
44
|
+
References() {
|
|
45
|
+
return this.references;
|
|
46
|
+
}
|
|
39
47
|
/** Returns an iterator for each error in this value. */
|
|
40
48
|
Errors(value) {
|
|
41
49
|
return (0, index_2.Errors)(this.schema, this.references, value);
|
|
@@ -13,9 +13,11 @@ import { type TIterator } from '../iterator/index';
|
|
|
13
13
|
import { type TKeyOf } from '../keyof/index';
|
|
14
14
|
import { type TObject, type TProperties } from '../object/index';
|
|
15
15
|
import { type TOmit } from '../omit/index';
|
|
16
|
+
import { type TOptional } from '../optional/index';
|
|
16
17
|
import { type TPick } from '../pick/index';
|
|
17
18
|
import { type TNever } from '../never/index';
|
|
18
19
|
import { TPartial } from '../partial/index';
|
|
20
|
+
import { type TReadonly } from '../readonly/index';
|
|
19
21
|
import { type TRecordOrObject } from '../record/index';
|
|
20
22
|
import { type TRef } from '../ref/index';
|
|
21
23
|
import { TRequired } from '../required/index';
|
|
@@ -44,7 +46,7 @@ type TFromArray<ModuleProperties extends TProperties, Type extends TSchema> = (E
|
|
|
44
46
|
type TFromAsyncIterator<ModuleProperties extends TProperties, Type extends TSchema> = (TAsyncIterator<TFromType<ModuleProperties, Type>>);
|
|
45
47
|
type TFromIterator<ModuleProperties extends TProperties, Type extends TSchema> = (TIterator<TFromType<ModuleProperties, Type>>);
|
|
46
48
|
type TFromRest<ModuleProperties extends TProperties, Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TFromRest<ModuleProperties, Right, [...Result, TFromType<ModuleProperties, Left>]> : Result);
|
|
47
|
-
export type TFromType<ModuleProperties extends TProperties, Type extends TSchema> = (Type extends TArray<infer Type extends TSchema> ? TFromArray<ModuleProperties, Type> : Type extends TAsyncIterator<infer Type extends TSchema> ? TFromAsyncIterator<ModuleProperties, Type> : Type extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TFromComputed<ModuleProperties, Target, Parameters> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TFromConstructor<ModuleProperties, Parameters, InstanceType> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFromFunction<ModuleProperties, Parameters, ReturnType> : Type extends TIntersect<infer Types extends TSchema[]> ? TFromIntersect<ModuleProperties, Types> : Type extends TIterator<infer Type extends TSchema> ? TFromIterator<ModuleProperties, Type> : Type extends TObject<infer Properties extends TProperties> ? TFromObject<ModuleProperties, Properties> : Type extends TTuple<infer Types extends TSchema[]> ? TFromTuple<ModuleProperties, Types> : Type extends TEnum<infer _ extends TEnumRecord> ? Type : Type extends TUnion<infer Types extends TSchema[]> ? TFromUnion<ModuleProperties, Types> : Type);
|
|
49
|
+
export type TFromType<ModuleProperties extends TProperties, Type extends TSchema> = (Type extends TOptional<infer Type extends TSchema> ? TOptional<TFromType<ModuleProperties, Type>> : Type extends TReadonly<infer Type extends TSchema> ? TReadonly<TFromType<ModuleProperties, Type>> : Type extends TArray<infer Type extends TSchema> ? TFromArray<ModuleProperties, Type> : Type extends TAsyncIterator<infer Type extends TSchema> ? TFromAsyncIterator<ModuleProperties, Type> : Type extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TFromComputed<ModuleProperties, Target, Parameters> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TFromConstructor<ModuleProperties, Parameters, InstanceType> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFromFunction<ModuleProperties, Parameters, ReturnType> : Type extends TIntersect<infer Types extends TSchema[]> ? TFromIntersect<ModuleProperties, Types> : Type extends TIterator<infer Type extends TSchema> ? TFromIterator<ModuleProperties, Type> : Type extends TObject<infer Properties extends TProperties> ? TFromObject<ModuleProperties, Properties> : Type extends TTuple<infer Types extends TSchema[]> ? TFromTuple<ModuleProperties, Types> : Type extends TEnum<infer _ extends TEnumRecord> ? Type : Type extends TUnion<infer Types extends TSchema[]> ? TFromUnion<ModuleProperties, Types> : Type);
|
|
48
50
|
export declare function FromType<ModuleProperties extends TProperties, Type extends TSchema>(moduleProperties: ModuleProperties, type: Type): TFromType<ModuleProperties, Type>;
|
|
49
51
|
export type TComputeType<ModuleProperties extends TProperties, Key extends PropertyKey> = (Key extends keyof ModuleProperties ? TFromType<ModuleProperties, ModuleProperties[Key]> : TNever);
|
|
50
52
|
export declare function ComputeType<ModuleProperties extends TProperties, Key extends PropertyKey>(moduleProperties: ModuleProperties, key: Key): TComputeType<ModuleProperties, Key>;
|
|
@@ -5,24 +5,26 @@ exports.FromType = FromType;
|
|
|
5
5
|
exports.ComputeType = ComputeType;
|
|
6
6
|
exports.ComputeModuleProperties = ComputeModuleProperties;
|
|
7
7
|
const index_1 = require("../create/index");
|
|
8
|
-
const index_2 = require("../
|
|
9
|
-
const index_3 = require("../
|
|
10
|
-
const index_4 = require("../
|
|
11
|
-
const index_5 = require("../
|
|
12
|
-
const index_6 = require("../
|
|
13
|
-
const index_7 = require("../
|
|
14
|
-
const index_8 = require("../
|
|
15
|
-
const index_9 = require("../
|
|
16
|
-
const index_10 = require("../
|
|
17
|
-
const index_11 = require("../
|
|
18
|
-
const index_12 = require("../
|
|
19
|
-
const index_13 = require("../
|
|
20
|
-
const index_14 = require("../
|
|
21
|
-
const index_15 = require("../
|
|
22
|
-
const index_16 = require("../
|
|
23
|
-
const index_17 = require("../
|
|
24
|
-
const index_18 = require("../
|
|
25
|
-
const index_19 = require("../
|
|
8
|
+
const index_2 = require("../discard/index");
|
|
9
|
+
const index_3 = require("../array/index");
|
|
10
|
+
const index_4 = require("../awaited/index");
|
|
11
|
+
const index_5 = require("../async-iterator/index");
|
|
12
|
+
const index_6 = require("../constructor/index");
|
|
13
|
+
const index_7 = require("../indexed/index");
|
|
14
|
+
const index_8 = require("../function/index");
|
|
15
|
+
const index_9 = require("../intersect/index");
|
|
16
|
+
const index_10 = require("../iterator/index");
|
|
17
|
+
const index_11 = require("../keyof/index");
|
|
18
|
+
const index_12 = require("../object/index");
|
|
19
|
+
const index_13 = require("../omit/index");
|
|
20
|
+
const index_14 = require("../pick/index");
|
|
21
|
+
const index_15 = require("../never/index");
|
|
22
|
+
const index_16 = require("../partial/index");
|
|
23
|
+
const index_17 = require("../record/index");
|
|
24
|
+
const index_18 = require("../required/index");
|
|
25
|
+
const index_19 = require("../tuple/index");
|
|
26
|
+
const index_20 = require("../union/index");
|
|
27
|
+
const index_21 = require("../symbols/index");
|
|
26
28
|
// ------------------------------------------------------------------
|
|
27
29
|
// KindGuard
|
|
28
30
|
// ------------------------------------------------------------------
|
|
@@ -41,39 +43,39 @@ function Deref(moduleProperties, ref) {
|
|
|
41
43
|
? KindGuard.IsRef(moduleProperties[ref])
|
|
42
44
|
? Deref(moduleProperties, moduleProperties[ref].$ref)
|
|
43
45
|
: FromType(moduleProperties, moduleProperties[ref])
|
|
44
|
-
: (0,
|
|
46
|
+
: (0, index_15.Never)());
|
|
45
47
|
}
|
|
46
48
|
// prettier-ignore
|
|
47
49
|
function FromAwaited(parameters) {
|
|
48
|
-
return (0,
|
|
50
|
+
return (0, index_4.Awaited)(parameters[0]);
|
|
49
51
|
}
|
|
50
52
|
// prettier-ignore
|
|
51
53
|
function FromIndex(parameters) {
|
|
52
|
-
return (0,
|
|
54
|
+
return (0, index_7.Index)(parameters[0], parameters[1]);
|
|
53
55
|
}
|
|
54
56
|
// prettier-ignore
|
|
55
57
|
function FromKeyOf(parameters) {
|
|
56
|
-
return (0,
|
|
58
|
+
return (0, index_11.KeyOf)(parameters[0]);
|
|
57
59
|
}
|
|
58
60
|
// prettier-ignore
|
|
59
61
|
function FromPartial(parameters) {
|
|
60
|
-
return (0,
|
|
62
|
+
return (0, index_16.Partial)(parameters[0]);
|
|
61
63
|
}
|
|
62
64
|
// prettier-ignore
|
|
63
65
|
function FromOmit(parameters) {
|
|
64
|
-
return (0,
|
|
66
|
+
return (0, index_13.Omit)(parameters[0], parameters[1]);
|
|
65
67
|
}
|
|
66
68
|
// prettier-ignore
|
|
67
69
|
function FromPick(parameters) {
|
|
68
|
-
return (0,
|
|
70
|
+
return (0, index_14.Pick)(parameters[0], parameters[1]);
|
|
69
71
|
}
|
|
70
72
|
// prettier-ignore
|
|
71
73
|
function FromRecord(parameters) {
|
|
72
|
-
return (0,
|
|
74
|
+
return (0, index_17.Record)(parameters[0], parameters[1]);
|
|
73
75
|
}
|
|
74
76
|
// prettier-ignore
|
|
75
77
|
function FromRequired(parameters) {
|
|
76
|
-
return (0,
|
|
78
|
+
return (0, index_18.Required)(parameters[0]);
|
|
77
79
|
}
|
|
78
80
|
// prettier-ignore
|
|
79
81
|
function FromComputed(moduleProperties, target, parameters) {
|
|
@@ -86,64 +88,69 @@ function FromComputed(moduleProperties, target, parameters) {
|
|
|
86
88
|
target === 'Pick' ? FromPick(dereferenced) :
|
|
87
89
|
target === 'Record' ? FromRecord(dereferenced) :
|
|
88
90
|
target === 'Required' ? FromRequired(dereferenced) :
|
|
89
|
-
(0,
|
|
91
|
+
(0, index_15.Never)());
|
|
90
92
|
}
|
|
91
93
|
function FromObject(moduleProperties, properties) {
|
|
92
|
-
return (0,
|
|
94
|
+
return (0, index_12.Object)(globalThis.Object.keys(properties).reduce((result, key) => {
|
|
93
95
|
return { ...result, [key]: FromType(moduleProperties, properties[key]) };
|
|
94
96
|
}, {}));
|
|
95
97
|
}
|
|
96
98
|
// prettier-ignore
|
|
97
99
|
function FromConstructor(moduleProperties, parameters, instanceType) {
|
|
98
|
-
return (0,
|
|
100
|
+
return (0, index_6.Constructor)(FromRest(moduleProperties, parameters), FromType(moduleProperties, instanceType));
|
|
99
101
|
}
|
|
100
102
|
// prettier-ignore
|
|
101
103
|
function FromFunction(moduleProperties, parameters, returnType) {
|
|
102
|
-
return (0,
|
|
104
|
+
return (0, index_8.Function)(FromRest(moduleProperties, parameters), FromType(moduleProperties, returnType));
|
|
103
105
|
}
|
|
104
106
|
function FromTuple(moduleProperties, types) {
|
|
105
|
-
return (0,
|
|
107
|
+
return (0, index_19.Tuple)(FromRest(moduleProperties, types));
|
|
106
108
|
}
|
|
107
109
|
function FromIntersect(moduleProperties, types) {
|
|
108
|
-
return (0,
|
|
110
|
+
return (0, index_9.Intersect)(FromRest(moduleProperties, types));
|
|
109
111
|
}
|
|
110
112
|
function FromUnion(moduleProperties, types) {
|
|
111
|
-
return (0,
|
|
113
|
+
return (0, index_20.Union)(FromRest(moduleProperties, types));
|
|
112
114
|
}
|
|
113
115
|
function FromArray(moduleProperties, type) {
|
|
114
|
-
return (0,
|
|
116
|
+
return (0, index_3.Array)(FromType(moduleProperties, type));
|
|
115
117
|
}
|
|
116
118
|
function FromAsyncIterator(moduleProperties, type) {
|
|
117
|
-
return (0,
|
|
119
|
+
return (0, index_5.AsyncIterator)(FromType(moduleProperties, type));
|
|
118
120
|
}
|
|
119
121
|
function FromIterator(moduleProperties, type) {
|
|
120
|
-
return (0,
|
|
122
|
+
return (0, index_10.Iterator)(FromType(moduleProperties, type));
|
|
121
123
|
}
|
|
122
124
|
function FromRest(moduleProperties, types) {
|
|
123
125
|
return types.map((type) => FromType(moduleProperties, type));
|
|
124
126
|
}
|
|
125
127
|
// prettier-ignore
|
|
126
128
|
function FromType(moduleProperties, type) {
|
|
127
|
-
return (
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
//
|
|
132
|
-
KindGuard.
|
|
133
|
-
KindGuard.
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
129
|
+
return (
|
|
130
|
+
// Modifier Unwrap - Reapplied via CreateType Options
|
|
131
|
+
KindGuard.IsOptional(type) ? (0, index_1.CreateType)(FromType(moduleProperties, (0, index_2.Discard)(type, [index_21.OptionalKind])), type) :
|
|
132
|
+
KindGuard.IsReadonly(type) ? (0, index_1.CreateType)(FromType(moduleProperties, (0, index_2.Discard)(type, [index_21.ReadonlyKind])), type) :
|
|
133
|
+
// Traveral
|
|
134
|
+
KindGuard.IsArray(type) ? (0, index_1.CreateType)(FromArray(moduleProperties, type.items), type) :
|
|
135
|
+
KindGuard.IsAsyncIterator(type) ? (0, index_1.CreateType)(FromAsyncIterator(moduleProperties, type.items), type) :
|
|
136
|
+
// Note: The 'as never' is required due to excessive resolution of TIndex. In fact TIndex, TPick, TOmit and
|
|
137
|
+
// all need re-implementation to remove the PropertyKey[] selector. Reimplementation of these types should
|
|
138
|
+
// be a priority as there is a potential for the current inference to break on TS compiler changes.
|
|
139
|
+
KindGuard.IsComputed(type) ? (0, index_1.CreateType)(FromComputed(moduleProperties, type.target, type.parameters)) :
|
|
140
|
+
KindGuard.IsConstructor(type) ? (0, index_1.CreateType)(FromConstructor(moduleProperties, type.parameters, type.returns), type) :
|
|
141
|
+
KindGuard.IsFunction(type) ? (0, index_1.CreateType)(FromFunction(moduleProperties, type.parameters, type.returns), type) :
|
|
142
|
+
KindGuard.IsIntersect(type) ? (0, index_1.CreateType)(FromIntersect(moduleProperties, type.allOf), type) :
|
|
143
|
+
KindGuard.IsIterator(type) ? (0, index_1.CreateType)(FromIterator(moduleProperties, type.items), type) :
|
|
144
|
+
KindGuard.IsObject(type) ? (0, index_1.CreateType)(FromObject(moduleProperties, type.properties), type) :
|
|
145
|
+
KindGuard.IsTuple(type) ? (0, index_1.CreateType)(FromTuple(moduleProperties, type.items || []), type) :
|
|
146
|
+
KindGuard.IsUnion(type) ? (0, index_1.CreateType)(FromUnion(moduleProperties, type.anyOf), type) :
|
|
147
|
+
type);
|
|
141
148
|
}
|
|
142
149
|
// prettier-ignore
|
|
143
150
|
function ComputeType(moduleProperties, key) {
|
|
144
151
|
return (key in moduleProperties
|
|
145
152
|
? FromType(moduleProperties, moduleProperties[key])
|
|
146
|
-
: (0,
|
|
153
|
+
: (0, index_15.Never)());
|
|
147
154
|
}
|
|
148
155
|
// prettier-ignore
|
|
149
156
|
function ComputeModuleProperties(moduleProperties) {
|
|
@@ -1,6 +1,22 @@
|
|
|
1
|
+
import { TypeBoxError } from '../../type/error/index';
|
|
1
2
|
import { TSchema } from '../../type/schema/index';
|
|
2
3
|
import { StaticDecode } from '../../type/static/index';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export
|
|
4
|
+
export declare class ParseError extends TypeBoxError {
|
|
5
|
+
constructor(message: string);
|
|
6
|
+
}
|
|
7
|
+
export type TParseOperation = 'Clone' | 'Clean' | 'Default' | 'Convert' | 'Assert' | 'Decode' | ({} & string);
|
|
8
|
+
export type TParseFunction = (type: TSchema, references: TSchema[], value: unknown) => unknown;
|
|
9
|
+
export declare namespace ParseRegistry {
|
|
10
|
+
function Delete(key: string): void;
|
|
11
|
+
function Set(key: string, callback: TParseFunction): void;
|
|
12
|
+
function Get(key: string): TParseFunction | undefined;
|
|
13
|
+
}
|
|
14
|
+
export declare const ParseDefault: readonly ["Clone", "Clean", "Default", "Convert", "Assert", "Decode"];
|
|
15
|
+
/** Parses a value using the default parse pipeline. Will throws an `AssertError` if invalid. */
|
|
16
|
+
export declare function Parse<Type extends TSchema, Output = StaticDecode<Type>, Result extends Output = Output>(schema: Type, references: TSchema[], value: unknown): Result;
|
|
17
|
+
/** Parses a value using the default parse pipeline. Will throws an `AssertError` if invalid. */
|
|
18
|
+
export declare function Parse<Type extends TSchema, Output = StaticDecode<Type>, Result extends Output = Output>(schema: Type, value: unknown): Result;
|
|
19
|
+
/** Parses a value using the specified operations. */
|
|
20
|
+
export declare function Parse<Type extends TSchema>(operations: TParseOperation[], schema: Type, references: TSchema[], value: unknown): unknown;
|
|
21
|
+
/** Parses a value using the specified operations. */
|
|
22
|
+
export declare function Parse<Type extends TSchema>(operations: TParseOperation[], schema: Type, value: unknown): unknown;
|
|
@@ -1,29 +1,85 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.ParseDefault = exports.ParseRegistry = exports.ParseError = void 0;
|
|
4
5
|
exports.Parse = Parse;
|
|
5
|
-
const index_1 = require("
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
6
|
+
const index_1 = require("../../type/error/index");
|
|
7
|
+
const index_2 = require("../transform/index");
|
|
8
|
+
const index_3 = require("../assert/index");
|
|
9
|
+
const index_4 = require("../default/index");
|
|
10
|
+
const index_5 = require("../convert/index");
|
|
11
|
+
const index_6 = require("../clean/index");
|
|
12
|
+
const index_7 = require("../clone/index");
|
|
13
|
+
// ------------------------------------------------------------------
|
|
14
|
+
// Guards
|
|
15
|
+
// ------------------------------------------------------------------
|
|
16
|
+
const index_8 = require("../guard/index");
|
|
17
|
+
// ------------------------------------------------------------------
|
|
18
|
+
// Error
|
|
19
|
+
// ------------------------------------------------------------------
|
|
20
|
+
class ParseError extends index_1.TypeBoxError {
|
|
21
|
+
constructor(message) {
|
|
22
|
+
super(message);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
exports.ParseError = ParseError;
|
|
26
|
+
// prettier-ignore
|
|
27
|
+
var ParseRegistry;
|
|
28
|
+
(function (ParseRegistry) {
|
|
29
|
+
const registry = new Map([
|
|
30
|
+
['Clone', (_type, _references, value) => (0, index_7.Clone)(value)],
|
|
31
|
+
['Clean', (type, references, value) => (0, index_6.Clean)(type, references, value)],
|
|
32
|
+
['Default', (type, references, value) => (0, index_4.Default)(type, references, value)],
|
|
33
|
+
['Convert', (type, references, value) => (0, index_5.Convert)(type, references, value)],
|
|
34
|
+
['Assert', (type, references, value) => { (0, index_3.Assert)(type, references, value); return value; }],
|
|
35
|
+
['Decode', (type, references, value) => ((0, index_2.HasTransform)(type, references) ? (0, index_2.TransformDecode)(type, references, value) : value)],
|
|
36
|
+
['Encode', (type, references, value) => ((0, index_2.HasTransform)(type, references) ? (0, index_2.TransformEncode)(type, references, value) : value)],
|
|
37
|
+
]);
|
|
38
|
+
// Deletes an entry from the registry
|
|
39
|
+
function Delete(key) {
|
|
40
|
+
registry.delete(key);
|
|
41
|
+
}
|
|
42
|
+
ParseRegistry.Delete = Delete;
|
|
43
|
+
// Sets an entry in the registry
|
|
44
|
+
function Set(key, callback) {
|
|
45
|
+
registry.set(key, callback);
|
|
46
|
+
}
|
|
47
|
+
ParseRegistry.Set = Set;
|
|
48
|
+
// Gets an entry in the registry
|
|
49
|
+
function Get(key) {
|
|
50
|
+
return registry.get(key);
|
|
51
|
+
}
|
|
52
|
+
ParseRegistry.Get = Get;
|
|
53
|
+
})(ParseRegistry || (exports.ParseRegistry = ParseRegistry = {}));
|
|
54
|
+
// ------------------------------------------------------------------
|
|
55
|
+
// Default Parse Sequence
|
|
56
|
+
// ------------------------------------------------------------------
|
|
11
57
|
// prettier-ignore
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
58
|
+
exports.ParseDefault = [
|
|
59
|
+
'Clone',
|
|
60
|
+
'Clean',
|
|
61
|
+
'Default',
|
|
62
|
+
'Convert',
|
|
63
|
+
'Assert',
|
|
64
|
+
'Decode'
|
|
19
65
|
];
|
|
20
66
|
// ------------------------------------------------------------------
|
|
21
67
|
// ParseValue
|
|
22
68
|
// ------------------------------------------------------------------
|
|
23
|
-
function ParseValue(
|
|
24
|
-
return
|
|
69
|
+
function ParseValue(operations, type, references, value) {
|
|
70
|
+
return operations.reduce((value, operationKey) => {
|
|
71
|
+
const operation = ParseRegistry.Get(operationKey);
|
|
72
|
+
if ((0, index_8.IsUndefined)(operation))
|
|
73
|
+
throw new ParseError(`Unable to find Parse operation '${operationKey}'`);
|
|
74
|
+
return operation(type, references, value);
|
|
75
|
+
}, value);
|
|
25
76
|
}
|
|
26
|
-
/** Parses a value
|
|
77
|
+
/** Parses a value */
|
|
27
78
|
function Parse(...args) {
|
|
28
|
-
|
|
79
|
+
// prettier-ignore
|
|
80
|
+
const [operations, schema, references, value] = (args.length === 4 ? [args[0], args[1], args[2], args[3]] :
|
|
81
|
+
args.length === 3 ? (0, index_8.IsArray)(args[0]) ? [args[0], args[1], [], args[2]] : [exports.ParseDefault, args[0], args[1], args[2]] :
|
|
82
|
+
args.length === 2 ? [exports.ParseDefault, args[0], [], args[1]] :
|
|
83
|
+
(() => { throw new ParseError('Invalid Arguments'); })());
|
|
84
|
+
return ParseValue(operations, schema, references, value);
|
|
29
85
|
}
|
|
@@ -12,6 +12,10 @@ export declare class TypeCheck<T extends TSchema> {
|
|
|
12
12
|
constructor(schema: T, references: TSchema[], checkFunc: CheckFunction, code: string);
|
|
13
13
|
/** Returns the generated assertion code used to validate this type. */
|
|
14
14
|
Code(): string;
|
|
15
|
+
/** Returns the schema type used to validate */
|
|
16
|
+
Schema(): T;
|
|
17
|
+
/** Returns reference types used to validate */
|
|
18
|
+
References(): TSchema[];
|
|
15
19
|
/** Returns an iterator for each error in this value. */
|
|
16
20
|
Errors(value: unknown): ValueErrorIterator;
|
|
17
21
|
/** Returns true if the value matches the compiled type. */
|
|
@@ -32,6 +32,14 @@ export class TypeCheck {
|
|
|
32
32
|
Code() {
|
|
33
33
|
return this.code;
|
|
34
34
|
}
|
|
35
|
+
/** Returns the schema type used to validate */
|
|
36
|
+
Schema() {
|
|
37
|
+
return this.schema;
|
|
38
|
+
}
|
|
39
|
+
/** Returns reference types used to validate */
|
|
40
|
+
References() {
|
|
41
|
+
return this.references;
|
|
42
|
+
}
|
|
35
43
|
/** Returns an iterator for each error in this value. */
|
|
36
44
|
Errors(value) {
|
|
37
45
|
return Errors(this.schema, this.references, value);
|
|
@@ -13,9 +13,11 @@ import { type TIterator } from '../iterator/index.mjs';
|
|
|
13
13
|
import { type TKeyOf } from '../keyof/index.mjs';
|
|
14
14
|
import { type TObject, type TProperties } from '../object/index.mjs';
|
|
15
15
|
import { type TOmit } from '../omit/index.mjs';
|
|
16
|
+
import { type TOptional } from '../optional/index.mjs';
|
|
16
17
|
import { type TPick } from '../pick/index.mjs';
|
|
17
18
|
import { type TNever } from '../never/index.mjs';
|
|
18
19
|
import { TPartial } from '../partial/index.mjs';
|
|
20
|
+
import { type TReadonly } from '../readonly/index.mjs';
|
|
19
21
|
import { type TRecordOrObject } from '../record/index.mjs';
|
|
20
22
|
import { type TRef } from '../ref/index.mjs';
|
|
21
23
|
import { TRequired } from '../required/index.mjs';
|
|
@@ -44,7 +46,7 @@ type TFromArray<ModuleProperties extends TProperties, Type extends TSchema> = (E
|
|
|
44
46
|
type TFromAsyncIterator<ModuleProperties extends TProperties, Type extends TSchema> = (TAsyncIterator<TFromType<ModuleProperties, Type>>);
|
|
45
47
|
type TFromIterator<ModuleProperties extends TProperties, Type extends TSchema> = (TIterator<TFromType<ModuleProperties, Type>>);
|
|
46
48
|
type TFromRest<ModuleProperties extends TProperties, Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TFromRest<ModuleProperties, Right, [...Result, TFromType<ModuleProperties, Left>]> : Result);
|
|
47
|
-
export type TFromType<ModuleProperties extends TProperties, Type extends TSchema> = (Type extends TArray<infer Type extends TSchema> ? TFromArray<ModuleProperties, Type> : Type extends TAsyncIterator<infer Type extends TSchema> ? TFromAsyncIterator<ModuleProperties, Type> : Type extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TFromComputed<ModuleProperties, Target, Parameters> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TFromConstructor<ModuleProperties, Parameters, InstanceType> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFromFunction<ModuleProperties, Parameters, ReturnType> : Type extends TIntersect<infer Types extends TSchema[]> ? TFromIntersect<ModuleProperties, Types> : Type extends TIterator<infer Type extends TSchema> ? TFromIterator<ModuleProperties, Type> : Type extends TObject<infer Properties extends TProperties> ? TFromObject<ModuleProperties, Properties> : Type extends TTuple<infer Types extends TSchema[]> ? TFromTuple<ModuleProperties, Types> : Type extends TEnum<infer _ extends TEnumRecord> ? Type : Type extends TUnion<infer Types extends TSchema[]> ? TFromUnion<ModuleProperties, Types> : Type);
|
|
49
|
+
export type TFromType<ModuleProperties extends TProperties, Type extends TSchema> = (Type extends TOptional<infer Type extends TSchema> ? TOptional<TFromType<ModuleProperties, Type>> : Type extends TReadonly<infer Type extends TSchema> ? TReadonly<TFromType<ModuleProperties, Type>> : Type extends TArray<infer Type extends TSchema> ? TFromArray<ModuleProperties, Type> : Type extends TAsyncIterator<infer Type extends TSchema> ? TFromAsyncIterator<ModuleProperties, Type> : Type extends TComputed<infer Target extends string, infer Parameters extends TSchema[]> ? TFromComputed<ModuleProperties, Target, Parameters> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TFromConstructor<ModuleProperties, Parameters, InstanceType> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFromFunction<ModuleProperties, Parameters, ReturnType> : Type extends TIntersect<infer Types extends TSchema[]> ? TFromIntersect<ModuleProperties, Types> : Type extends TIterator<infer Type extends TSchema> ? TFromIterator<ModuleProperties, Type> : Type extends TObject<infer Properties extends TProperties> ? TFromObject<ModuleProperties, Properties> : Type extends TTuple<infer Types extends TSchema[]> ? TFromTuple<ModuleProperties, Types> : Type extends TEnum<infer _ extends TEnumRecord> ? Type : Type extends TUnion<infer Types extends TSchema[]> ? TFromUnion<ModuleProperties, Types> : Type);
|
|
48
50
|
export declare function FromType<ModuleProperties extends TProperties, Type extends TSchema>(moduleProperties: ModuleProperties, type: Type): TFromType<ModuleProperties, Type>;
|
|
49
51
|
export type TComputeType<ModuleProperties extends TProperties, Key extends PropertyKey> = (Key extends keyof ModuleProperties ? TFromType<ModuleProperties, ModuleProperties[Key]> : TNever);
|
|
50
52
|
export declare function ComputeType<ModuleProperties extends TProperties, Key extends PropertyKey>(moduleProperties: ModuleProperties, key: Key): TComputeType<ModuleProperties, Key>;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { CreateType } from '../create/index.mjs';
|
|
2
|
+
import { Discard } from '../discard/index.mjs';
|
|
2
3
|
import { Array } from '../array/index.mjs';
|
|
3
4
|
import { Awaited } from '../awaited/index.mjs';
|
|
4
5
|
import { AsyncIterator } from '../async-iterator/index.mjs';
|
|
@@ -17,6 +18,7 @@ import { Record } from '../record/index.mjs';
|
|
|
17
18
|
import { Required } from '../required/index.mjs';
|
|
18
19
|
import { Tuple } from '../tuple/index.mjs';
|
|
19
20
|
import { Union } from '../union/index.mjs';
|
|
21
|
+
import { OptionalKind, ReadonlyKind } from '../symbols/index.mjs';
|
|
20
22
|
// ------------------------------------------------------------------
|
|
21
23
|
// KindGuard
|
|
22
24
|
// ------------------------------------------------------------------
|
|
@@ -118,20 +120,25 @@ function FromRest(moduleProperties, types) {
|
|
|
118
120
|
}
|
|
119
121
|
// prettier-ignore
|
|
120
122
|
export function FromType(moduleProperties, type) {
|
|
121
|
-
return (
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
//
|
|
126
|
-
KindGuard.
|
|
127
|
-
KindGuard.
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
123
|
+
return (
|
|
124
|
+
// Modifier Unwrap - Reapplied via CreateType Options
|
|
125
|
+
KindGuard.IsOptional(type) ? CreateType(FromType(moduleProperties, Discard(type, [OptionalKind])), type) :
|
|
126
|
+
KindGuard.IsReadonly(type) ? CreateType(FromType(moduleProperties, Discard(type, [ReadonlyKind])), type) :
|
|
127
|
+
// Traveral
|
|
128
|
+
KindGuard.IsArray(type) ? CreateType(FromArray(moduleProperties, type.items), type) :
|
|
129
|
+
KindGuard.IsAsyncIterator(type) ? CreateType(FromAsyncIterator(moduleProperties, type.items), type) :
|
|
130
|
+
// Note: The 'as never' is required due to excessive resolution of TIndex. In fact TIndex, TPick, TOmit and
|
|
131
|
+
// all need re-implementation to remove the PropertyKey[] selector. Reimplementation of these types should
|
|
132
|
+
// be a priority as there is a potential for the current inference to break on TS compiler changes.
|
|
133
|
+
KindGuard.IsComputed(type) ? CreateType(FromComputed(moduleProperties, type.target, type.parameters)) :
|
|
134
|
+
KindGuard.IsConstructor(type) ? CreateType(FromConstructor(moduleProperties, type.parameters, type.returns), type) :
|
|
135
|
+
KindGuard.IsFunction(type) ? CreateType(FromFunction(moduleProperties, type.parameters, type.returns), type) :
|
|
136
|
+
KindGuard.IsIntersect(type) ? CreateType(FromIntersect(moduleProperties, type.allOf), type) :
|
|
137
|
+
KindGuard.IsIterator(type) ? CreateType(FromIterator(moduleProperties, type.items), type) :
|
|
138
|
+
KindGuard.IsObject(type) ? CreateType(FromObject(moduleProperties, type.properties), type) :
|
|
139
|
+
KindGuard.IsTuple(type) ? CreateType(FromTuple(moduleProperties, type.items || []), type) :
|
|
140
|
+
KindGuard.IsUnion(type) ? CreateType(FromUnion(moduleProperties, type.anyOf), type) :
|
|
141
|
+
type);
|
|
135
142
|
}
|
|
136
143
|
// prettier-ignore
|
|
137
144
|
export function ComputeType(moduleProperties, key) {
|
|
@@ -1,6 +1,22 @@
|
|
|
1
|
+
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
1
2
|
import { TSchema } from '../../type/schema/index.mjs';
|
|
2
3
|
import { StaticDecode } from '../../type/static/index.mjs';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export
|
|
4
|
+
export declare class ParseError extends TypeBoxError {
|
|
5
|
+
constructor(message: string);
|
|
6
|
+
}
|
|
7
|
+
export type TParseOperation = 'Clone' | 'Clean' | 'Default' | 'Convert' | 'Assert' | 'Decode' | ({} & string);
|
|
8
|
+
export type TParseFunction = (type: TSchema, references: TSchema[], value: unknown) => unknown;
|
|
9
|
+
export declare namespace ParseRegistry {
|
|
10
|
+
function Delete(key: string): void;
|
|
11
|
+
function Set(key: string, callback: TParseFunction): void;
|
|
12
|
+
function Get(key: string): TParseFunction | undefined;
|
|
13
|
+
}
|
|
14
|
+
export declare const ParseDefault: readonly ["Clone", "Clean", "Default", "Convert", "Assert", "Decode"];
|
|
15
|
+
/** Parses a value using the default parse pipeline. Will throws an `AssertError` if invalid. */
|
|
16
|
+
export declare function Parse<Type extends TSchema, Output = StaticDecode<Type>, Result extends Output = Output>(schema: Type, references: TSchema[], value: unknown): Result;
|
|
17
|
+
/** Parses a value using the default parse pipeline. Will throws an `AssertError` if invalid. */
|
|
18
|
+
export declare function Parse<Type extends TSchema, Output = StaticDecode<Type>, Result extends Output = Output>(schema: Type, value: unknown): Result;
|
|
19
|
+
/** Parses a value using the specified operations. */
|
|
20
|
+
export declare function Parse<Type extends TSchema>(operations: TParseOperation[], schema: Type, references: TSchema[], value: unknown): unknown;
|
|
21
|
+
/** Parses a value using the specified operations. */
|
|
22
|
+
export declare function Parse<Type extends TSchema>(operations: TParseOperation[], schema: Type, value: unknown): unknown;
|
|
@@ -1,25 +1,79 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
2
|
+
import { TransformDecode, TransformEncode, HasTransform } from '../transform/index.mjs';
|
|
3
|
+
import { Assert } from '../assert/index.mjs';
|
|
4
|
+
import { Default } from '../default/index.mjs';
|
|
5
|
+
import { Convert } from '../convert/index.mjs';
|
|
6
|
+
import { Clean } from '../clean/index.mjs';
|
|
6
7
|
import { Clone } from '../clone/index.mjs';
|
|
8
|
+
// ------------------------------------------------------------------
|
|
9
|
+
// Guards
|
|
10
|
+
// ------------------------------------------------------------------
|
|
11
|
+
import { IsArray, IsUndefined } from '../guard/index.mjs';
|
|
12
|
+
// ------------------------------------------------------------------
|
|
13
|
+
// Error
|
|
14
|
+
// ------------------------------------------------------------------
|
|
15
|
+
export class ParseError extends TypeBoxError {
|
|
16
|
+
constructor(message) {
|
|
17
|
+
super(message);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
// prettier-ignore
|
|
21
|
+
export var ParseRegistry;
|
|
22
|
+
(function (ParseRegistry) {
|
|
23
|
+
const registry = new Map([
|
|
24
|
+
['Clone', (_type, _references, value) => Clone(value)],
|
|
25
|
+
['Clean', (type, references, value) => Clean(type, references, value)],
|
|
26
|
+
['Default', (type, references, value) => Default(type, references, value)],
|
|
27
|
+
['Convert', (type, references, value) => Convert(type, references, value)],
|
|
28
|
+
['Assert', (type, references, value) => { Assert(type, references, value); return value; }],
|
|
29
|
+
['Decode', (type, references, value) => (HasTransform(type, references) ? TransformDecode(type, references, value) : value)],
|
|
30
|
+
['Encode', (type, references, value) => (HasTransform(type, references) ? TransformEncode(type, references, value) : value)],
|
|
31
|
+
]);
|
|
32
|
+
// Deletes an entry from the registry
|
|
33
|
+
function Delete(key) {
|
|
34
|
+
registry.delete(key);
|
|
35
|
+
}
|
|
36
|
+
ParseRegistry.Delete = Delete;
|
|
37
|
+
// Sets an entry in the registry
|
|
38
|
+
function Set(key, callback) {
|
|
39
|
+
registry.set(key, callback);
|
|
40
|
+
}
|
|
41
|
+
ParseRegistry.Set = Set;
|
|
42
|
+
// Gets an entry in the registry
|
|
43
|
+
function Get(key) {
|
|
44
|
+
return registry.get(key);
|
|
45
|
+
}
|
|
46
|
+
ParseRegistry.Get = Get;
|
|
47
|
+
})(ParseRegistry || (ParseRegistry = {}));
|
|
48
|
+
// ------------------------------------------------------------------
|
|
49
|
+
// Default Parse Sequence
|
|
50
|
+
// ------------------------------------------------------------------
|
|
7
51
|
// prettier-ignore
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
52
|
+
export const ParseDefault = [
|
|
53
|
+
'Clone',
|
|
54
|
+
'Clean',
|
|
55
|
+
'Default',
|
|
56
|
+
'Convert',
|
|
57
|
+
'Assert',
|
|
58
|
+
'Decode'
|
|
15
59
|
];
|
|
16
60
|
// ------------------------------------------------------------------
|
|
17
61
|
// ParseValue
|
|
18
62
|
// ------------------------------------------------------------------
|
|
19
|
-
function ParseValue(
|
|
20
|
-
return
|
|
63
|
+
function ParseValue(operations, type, references, value) {
|
|
64
|
+
return operations.reduce((value, operationKey) => {
|
|
65
|
+
const operation = ParseRegistry.Get(operationKey);
|
|
66
|
+
if (IsUndefined(operation))
|
|
67
|
+
throw new ParseError(`Unable to find Parse operation '${operationKey}'`);
|
|
68
|
+
return operation(type, references, value);
|
|
69
|
+
}, value);
|
|
21
70
|
}
|
|
22
|
-
/** Parses a value
|
|
71
|
+
/** Parses a value */
|
|
23
72
|
export function Parse(...args) {
|
|
24
|
-
|
|
73
|
+
// prettier-ignore
|
|
74
|
+
const [operations, schema, references, value] = (args.length === 4 ? [args[0], args[1], args[2], args[3]] :
|
|
75
|
+
args.length === 3 ? IsArray(args[0]) ? [args[0], args[1], [], args[2]] : [ParseDefault, args[0], args[1], args[2]] :
|
|
76
|
+
args.length === 2 ? [ParseDefault, args[0], [], args[1]] :
|
|
77
|
+
(() => { throw new ParseError('Invalid Arguments'); })());
|
|
78
|
+
return ParseValue(operations, schema, references, value);
|
|
25
79
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -78,9 +78,6 @@ License MIT
|
|
|
78
78
|
- [Unsafe](#types-unsafe)
|
|
79
79
|
- [Syntax](#syntax)
|
|
80
80
|
- [Parse](#syntax-parse)
|
|
81
|
-
- [Compose](#syntax-compose)
|
|
82
|
-
- [Context](#syntax-context)
|
|
83
|
-
- [Module](#syntax-module)
|
|
84
81
|
- [Static](#syntax-static)
|
|
85
82
|
- [Limits](#syntax-limits)
|
|
86
83
|
- [Values](#values)
|
|
@@ -1025,9 +1022,9 @@ if(TypeGuard.IsString(T)) {
|
|
|
1025
1022
|
|
|
1026
1023
|
## Syntax Types
|
|
1027
1024
|
|
|
1028
|
-
|
|
1025
|
+
Syntax Types is a feature that enables TypeBox to parse TypeScript type annotations directly into Json Schema. This feature works both at runtime and statically within the type system. Syntax Types use Json Schema as the Abstract Syntax Tree (AST) parse target for TypeScript types. Syntax Types are designed to offer a syntactical frontend to the standard Type Builder API.
|
|
1029
1026
|
|
|
1030
|
-
|
|
1027
|
+
This feature is available via optional import.
|
|
1031
1028
|
|
|
1032
1029
|
```typescript
|
|
1033
1030
|
import { Parse } from '@sinclair/typebox/syntax'
|
|
@@ -1037,7 +1034,7 @@ import { Parse } from '@sinclair/typebox/syntax'
|
|
|
1037
1034
|
|
|
1038
1035
|
### Parse
|
|
1039
1036
|
|
|
1040
|
-
Use the Parse function to
|
|
1037
|
+
Use the Parse function to transform a TypeScript type annotation into a TypeBox type. This function will return the parsed TypeBox type or undefined on error.
|
|
1041
1038
|
|
|
1042
1039
|
```typescript
|
|
1043
1040
|
const A = Parse('string') // const A: TString
|
|
@@ -1054,45 +1051,46 @@ const C = Parse(`{ x: number, y: number }`) // const C: TObject<{
|
|
|
1054
1051
|
// }>
|
|
1055
1052
|
```
|
|
1056
1053
|
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
<a name='syntax-compose'></a>
|
|
1060
|
-
|
|
1061
|
-
### Compose
|
|
1062
|
-
|
|
1063
|
-
Syntax Types are designed to be interchangeable with standard Types.
|
|
1054
|
+
Syntax Types can be composed with Standard Types.
|
|
1064
1055
|
|
|
1065
1056
|
```typescript
|
|
1066
1057
|
const T = Type.Object({ // const T: TObject<{
|
|
1067
1058
|
x: Parse('number'), // x: TNumber,
|
|
1068
|
-
y: Parse('
|
|
1069
|
-
z: Parse('
|
|
1059
|
+
y: Parse('string'), // y: TString,
|
|
1060
|
+
z: Parse('boolean') // z: TBoolean
|
|
1070
1061
|
}) // }>
|
|
1071
1062
|
```
|
|
1072
1063
|
|
|
1073
|
-
|
|
1064
|
+
It is also possible to pass Standard Types to Syntax Types in the following way.
|
|
1074
1065
|
|
|
1075
|
-
|
|
1066
|
+
```typescript
|
|
1067
|
+
const X = Type.Number()
|
|
1068
|
+
const Y = Type.String()
|
|
1069
|
+
const Z = Type.Boolean()
|
|
1070
|
+
|
|
1071
|
+
const T = Parse({ X, Y, Z }, `{
|
|
1072
|
+
x: X,
|
|
1073
|
+
y: Y,
|
|
1074
|
+
z: Z
|
|
1075
|
+
}`)
|
|
1076
|
+
```
|
|
1076
1077
|
|
|
1077
|
-
Syntax Types
|
|
1078
|
+
Syntax Types can also be used to parse Module types.
|
|
1078
1079
|
|
|
1079
1080
|
```typescript
|
|
1080
|
-
const
|
|
1081
|
-
|
|
1081
|
+
const Foo = Parse(`module Foo {
|
|
1082
|
+
|
|
1083
|
+
export type PartialUser = Pick<User, 'id'> & Partial<Omit<User, 'id'>>
|
|
1084
|
+
|
|
1082
1085
|
export interface User {
|
|
1083
1086
|
id: string
|
|
1084
1087
|
name: string
|
|
1085
1088
|
email: string
|
|
1086
1089
|
}
|
|
1087
|
-
|
|
1088
|
-
export type PartialUser = (
|
|
1089
|
-
Pick<User, 'id'> &
|
|
1090
|
-
Partial<Omit<User, 'id'>>
|
|
1091
|
-
)
|
|
1092
1090
|
|
|
1093
1091
|
}`)
|
|
1094
1092
|
|
|
1095
|
-
const PartialUser =
|
|
1093
|
+
const PartialUser = Foo.Import('PartialUser') // TImport<{...}, 'PartialUser'>
|
|
1096
1094
|
|
|
1097
1095
|
type PartialUser = Static<typeof PartialUser> // type PartialUser = {
|
|
1098
1096
|
// id: string,
|
|
@@ -1102,45 +1100,11 @@ type PartialUser = Static<typeof PartialUser> // type PartialUser = {
|
|
|
1102
1100
|
// }
|
|
1103
1101
|
```
|
|
1104
1102
|
|
|
1105
|
-
<a name='syntax-context'></a>
|
|
1106
|
-
|
|
1107
|
-
### Context
|
|
1108
|
-
|
|
1109
|
-
The Parse function takes an optional leading Context object that contains external types. This Context allows the syntax to reference these external types using the property identifiers provided by the Context. The following passes the external type `T` to Parse.
|
|
1110
|
-
|
|
1111
|
-
```typescript
|
|
1112
|
-
const T = Type.Object({ // const T: TObject<{
|
|
1113
|
-
x: Type.Number(), // x: TNumber,
|
|
1114
|
-
y: Type.Number(), // y: TNumber,
|
|
1115
|
-
z: Type.Number() // z: TNumber
|
|
1116
|
-
}) // }>
|
|
1117
|
-
|
|
1118
|
-
const A = Parse({ T }, 'Partial<T>') // const A: TObject<{
|
|
1119
|
-
// x: TOptional<TNumber>,
|
|
1120
|
-
// y: TOptional<TNumber>,
|
|
1121
|
-
// z: TOptional<TNumber>
|
|
1122
|
-
// }>
|
|
1123
|
-
|
|
1124
|
-
const B = Parse({ T }, 'keyof T') // const B: TUnion<[
|
|
1125
|
-
// TLiteral<'x'>,
|
|
1126
|
-
// TLiteral<'y'>,
|
|
1127
|
-
// TLiteral<'z'>
|
|
1128
|
-
// ]>
|
|
1129
|
-
|
|
1130
|
-
const C = Parse({ T }, 'T & { w: number }') // const C: TIntersect<[TObject<{
|
|
1131
|
-
// x: TNumber;
|
|
1132
|
-
// y: TNumber;
|
|
1133
|
-
// z: TNumber;
|
|
1134
|
-
// }>, TObject<{
|
|
1135
|
-
// w: TNumber;
|
|
1136
|
-
// }>]>
|
|
1137
|
-
```
|
|
1138
|
-
|
|
1139
1103
|
<a name='syntax-static'></a>
|
|
1140
1104
|
|
|
1141
1105
|
### Static
|
|
1142
1106
|
|
|
1143
|
-
Syntax Types provide two Static types
|
|
1107
|
+
Syntax Types provide two Static types specific to inferring TypeBox and TypeScript types from strings.
|
|
1144
1108
|
|
|
1145
1109
|
```typescript
|
|
1146
1110
|
import { StaticParseAsSchema, StaticParseAsType } from '@sinclair/typebox/syntax'
|
|
@@ -1154,8 +1118,8 @@ type S = StaticParseAsSchema<{}, '{ x: number }'> // type S: TObject<{
|
|
|
1154
1118
|
// Will infer as a type
|
|
1155
1119
|
|
|
1156
1120
|
type T = StaticParseAsType<{}, '{ x: number }'> // type T = {
|
|
1157
|
-
//
|
|
1158
|
-
//
|
|
1121
|
+
// x: number
|
|
1122
|
+
// }
|
|
1159
1123
|
```
|
|
1160
1124
|
|
|
1161
1125
|
|
|
@@ -1361,26 +1325,28 @@ const B = Value.Encode(Type.String(), 42) // throw
|
|
|
1361
1325
|
|
|
1362
1326
|
### Parse
|
|
1363
1327
|
|
|
1364
|
-
Use the Parse function to parse a value
|
|
1328
|
+
Use the Parse function to parse a value. This function calls the `Clone` `Clean`, `Default`, `Convert`, `Assert` and `Decode` Value functions in this exact order to process a value.
|
|
1365
1329
|
|
|
1366
1330
|
```typescript
|
|
1367
|
-
const
|
|
1331
|
+
const R = Value.Parse(Type.String(), 'hello') // const R: string = "hello"
|
|
1368
1332
|
|
|
1369
|
-
//
|
|
1333
|
+
const E = Value.Parse(Type.String(), undefined) // throws AssertError
|
|
1334
|
+
```
|
|
1370
1335
|
|
|
1371
|
-
|
|
1336
|
+
You can override the order in which functions are are run, or omit functions entirely in the following way.
|
|
1372
1337
|
|
|
1373
|
-
|
|
1338
|
+
```typescript
|
|
1339
|
+
// Runs no functions.
|
|
1374
1340
|
|
|
1375
|
-
const
|
|
1341
|
+
const R = Value.Parse([], Type.String(), 12345)
|
|
1376
1342
|
|
|
1377
|
-
//
|
|
1343
|
+
// Runs the Assert() function.
|
|
1378
1344
|
|
|
1379
|
-
const
|
|
1345
|
+
const E = Value.Parse(['Assert'], Type.String(), 12345)
|
|
1380
1346
|
|
|
1381
|
-
// Assert
|
|
1347
|
+
// Runs the Convert() function followed by the Assert() function.
|
|
1382
1348
|
|
|
1383
|
-
const
|
|
1349
|
+
const S = Value.Parse(['Convert', 'Assert'], Type.String(), 12345)
|
|
1384
1350
|
```
|
|
1385
1351
|
|
|
1386
1352
|
<a name='values-equal'></a>
|