@sinclair/typebox 0.34.6 → 0.34.8
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/type/module/compute.d.ts +3 -1
- package/build/cjs/type/module/compute.js +59 -52
- package/build/cjs/type/ref/ref.d.ts +20 -1
- package/build/cjs/type/ref/ref.js +7 -3
- package/build/cjs/type/type/json.d.ts +18 -2
- package/build/cjs/type/type/json.js +3 -3
- package/build/esm/type/module/compute.d.mts +3 -1
- package/build/esm/type/module/compute.mjs +21 -14
- package/build/esm/type/ref/ref.d.mts +20 -1
- package/build/esm/type/ref/ref.mjs +5 -1
- package/build/esm/type/type/json.d.mts +18 -2
- package/build/esm/type/type/json.mjs +3 -3
- package/package.json +1 -1
- package/readme.md +2 -10
|
@@ -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,9 +1,28 @@
|
|
|
1
1
|
import type { TSchema, SchemaOptions } from '../schema/index';
|
|
2
2
|
import { Kind } from '../symbols/index';
|
|
3
|
+
import { TUnsafe } from '../unsafe/index';
|
|
4
|
+
import { Static } from '../static/index';
|
|
3
5
|
export interface TRef<Ref extends string = string> extends TSchema {
|
|
4
6
|
[Kind]: 'Ref';
|
|
5
7
|
static: unknown;
|
|
6
8
|
$ref: Ref;
|
|
7
9
|
}
|
|
8
|
-
|
|
10
|
+
export type TRefUnsafe<Type extends TSchema> = TUnsafe<Static<Type>>;
|
|
11
|
+
/** `[Json]` Creates a Ref type.*/
|
|
9
12
|
export declare function Ref<Ref extends string>($ref: Ref, options?: SchemaOptions): TRef<Ref>;
|
|
13
|
+
/**
|
|
14
|
+
* @deprecated `[Json]` Creates a Ref type. The referenced type MUST contain a $id. The Ref(TSchema) signature was
|
|
15
|
+
* deprecated on 0.34.0 in support of a new type referencing model (Module). Existing implementations using Ref(TSchema)
|
|
16
|
+
* can migrate using the following. The Ref(TSchema) validation behavior of Ref will be preserved how the construction
|
|
17
|
+
* of legacy Ref(TSchema) will require wrapping in TUnsafe (where TUnsafe is used for inference only)
|
|
18
|
+
*
|
|
19
|
+
* ```typescript
|
|
20
|
+
* const R = Type.Ref(T)
|
|
21
|
+
* ```
|
|
22
|
+
* to
|
|
23
|
+
*
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const R = Type.Unsafe<Static<T>>(T.$id)
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function Ref<Type extends TSchema>(type: Type, options?: SchemaOptions): TRefUnsafe<Type>;
|
|
@@ -2,9 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.Ref = Ref;
|
|
5
|
+
const index_1 = require("../error/index");
|
|
5
6
|
const type_1 = require("../create/type");
|
|
6
|
-
const
|
|
7
|
+
const index_2 = require("../symbols/index");
|
|
7
8
|
/** `[Json]` Creates a Ref type. The referenced type must contain a $id */
|
|
8
|
-
function Ref(
|
|
9
|
-
|
|
9
|
+
function Ref(...args) {
|
|
10
|
+
const [$ref, options] = typeof args[0] === 'string' ? [args[0], args[1]] : [args[0].$id, args[1]];
|
|
11
|
+
if (typeof $ref !== 'string')
|
|
12
|
+
throw new index_1.TypeBoxError('Ref: $ref must be a string');
|
|
13
|
+
return (0, type_1.CreateType)({ [index_2.Kind]: 'Ref', $ref }, options);
|
|
10
14
|
}
|
|
@@ -29,7 +29,7 @@ import { type TReadonlyWithFlag, type TReadonlyFromMappedResult } from '../reado
|
|
|
29
29
|
import { type TReadonlyOptional } from '../readonly-optional/index';
|
|
30
30
|
import { type TRecordOrObject } from '../record/index';
|
|
31
31
|
import { type TRecursive, type TThis } from '../recursive/index';
|
|
32
|
-
import { type TRef } from '../ref/index';
|
|
32
|
+
import { type TRef, type TRefUnsafe } from '../ref/index';
|
|
33
33
|
import { type TRequired, type TRequiredFromMappedResult } from '../required/index';
|
|
34
34
|
import { type TRest } from '../rest/index';
|
|
35
35
|
import { type TSchema, type SchemaOptions } from '../schema/index';
|
|
@@ -142,8 +142,24 @@ export declare class JsonTypeBuilder {
|
|
|
142
142
|
Record<Key extends TSchema, Value extends TSchema>(key: Key, value: Value, options?: ObjectOptions): TRecordOrObject<Key, Value>;
|
|
143
143
|
/** `[Json]` Creates a Recursive type */
|
|
144
144
|
Recursive<T extends TSchema>(callback: (thisType: TThis) => T, options?: SchemaOptions): TRecursive<T>;
|
|
145
|
-
/** `[Json]` Creates a Ref type
|
|
145
|
+
/** `[Json]` Creates a Ref type.*/
|
|
146
146
|
Ref<Ref extends string>($ref: Ref, options?: SchemaOptions): TRef<Ref>;
|
|
147
|
+
/**
|
|
148
|
+
* @deprecated `[Json]` Creates a Ref type. The referenced type MUST contain a $id. The Ref(TSchema) signature was
|
|
149
|
+
* deprecated on 0.34.0 in support of a new type referencing model (Module). Existing implementations using Ref(TSchema)
|
|
150
|
+
* can migrate using the following. The Ref(TSchema) validation behavior of Ref will be preserved how the construction
|
|
151
|
+
* of legacy Ref(TSchema) will require wrapping in TUnsafe (where TUnsafe is used for inference only)
|
|
152
|
+
*
|
|
153
|
+
* ```typescript
|
|
154
|
+
* const R = Type.Ref(T)
|
|
155
|
+
* ```
|
|
156
|
+
* to
|
|
157
|
+
*
|
|
158
|
+
* ```typescript
|
|
159
|
+
* const R = Type.Unsafe<Static<T>>(T.$id)
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
Ref<Type extends TSchema>(type: Type, options?: SchemaOptions): TRefUnsafe<Type>;
|
|
147
163
|
/** `[Json]` Constructs a type where all properties are required */
|
|
148
164
|
Required<MappedResult extends TMappedResult>(type: MappedResult, options?: SchemaOptions): TRequiredFromMappedResult<MappedResult>;
|
|
149
165
|
/** `[Json]` Constructs a type where all properties are required */
|
|
@@ -174,9 +174,9 @@ class JsonTypeBuilder {
|
|
|
174
174
|
Recursive(callback, options) {
|
|
175
175
|
return (0, index_30.Recursive)(callback, options);
|
|
176
176
|
}
|
|
177
|
-
/** `[Json]` Creates a Ref type. */
|
|
178
|
-
Ref(
|
|
179
|
-
return (0, index_31.Ref)(
|
|
177
|
+
/** `[Json]` Creates a Ref type. The referenced type must contain a $id */
|
|
178
|
+
Ref(...args) {
|
|
179
|
+
return (0, index_31.Ref)(args[0], args[1]);
|
|
180
180
|
}
|
|
181
181
|
/** `[Json]` Constructs a type where all properties are required */
|
|
182
182
|
Required(type, options) {
|
|
@@ -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,9 +1,28 @@
|
|
|
1
1
|
import type { TSchema, SchemaOptions } from '../schema/index.mjs';
|
|
2
2
|
import { Kind } from '../symbols/index.mjs';
|
|
3
|
+
import { TUnsafe } from '../unsafe/index.mjs';
|
|
4
|
+
import { Static } from '../static/index.mjs';
|
|
3
5
|
export interface TRef<Ref extends string = string> extends TSchema {
|
|
4
6
|
[Kind]: 'Ref';
|
|
5
7
|
static: unknown;
|
|
6
8
|
$ref: Ref;
|
|
7
9
|
}
|
|
8
|
-
|
|
10
|
+
export type TRefUnsafe<Type extends TSchema> = TUnsafe<Static<Type>>;
|
|
11
|
+
/** `[Json]` Creates a Ref type.*/
|
|
9
12
|
export declare function Ref<Ref extends string>($ref: Ref, options?: SchemaOptions): TRef<Ref>;
|
|
13
|
+
/**
|
|
14
|
+
* @deprecated `[Json]` Creates a Ref type. The referenced type MUST contain a $id. The Ref(TSchema) signature was
|
|
15
|
+
* deprecated on 0.34.0 in support of a new type referencing model (Module). Existing implementations using Ref(TSchema)
|
|
16
|
+
* can migrate using the following. The Ref(TSchema) validation behavior of Ref will be preserved how the construction
|
|
17
|
+
* of legacy Ref(TSchema) will require wrapping in TUnsafe (where TUnsafe is used for inference only)
|
|
18
|
+
*
|
|
19
|
+
* ```typescript
|
|
20
|
+
* const R = Type.Ref(T)
|
|
21
|
+
* ```
|
|
22
|
+
* to
|
|
23
|
+
*
|
|
24
|
+
* ```typescript
|
|
25
|
+
* const R = Type.Unsafe<Static<T>>(T.$id)
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function Ref<Type extends TSchema>(type: Type, options?: SchemaOptions): TRefUnsafe<Type>;
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
+
import { TypeBoxError } from '../error/index.mjs';
|
|
1
2
|
import { CreateType } from '../create/type.mjs';
|
|
2
3
|
import { Kind } from '../symbols/index.mjs';
|
|
3
4
|
/** `[Json]` Creates a Ref type. The referenced type must contain a $id */
|
|
4
|
-
export function Ref(
|
|
5
|
+
export function Ref(...args) {
|
|
6
|
+
const [$ref, options] = typeof args[0] === 'string' ? [args[0], args[1]] : [args[0].$id, args[1]];
|
|
7
|
+
if (typeof $ref !== 'string')
|
|
8
|
+
throw new TypeBoxError('Ref: $ref must be a string');
|
|
5
9
|
return CreateType({ [Kind]: 'Ref', $ref }, options);
|
|
6
10
|
}
|
|
@@ -29,7 +29,7 @@ import { type TReadonlyWithFlag, type TReadonlyFromMappedResult } from '../reado
|
|
|
29
29
|
import { type TReadonlyOptional } from '../readonly-optional/index.mjs';
|
|
30
30
|
import { type TRecordOrObject } from '../record/index.mjs';
|
|
31
31
|
import { type TRecursive, type TThis } from '../recursive/index.mjs';
|
|
32
|
-
import { type TRef } from '../ref/index.mjs';
|
|
32
|
+
import { type TRef, type TRefUnsafe } from '../ref/index.mjs';
|
|
33
33
|
import { type TRequired, type TRequiredFromMappedResult } from '../required/index.mjs';
|
|
34
34
|
import { type TRest } from '../rest/index.mjs';
|
|
35
35
|
import { type TSchema, type SchemaOptions } from '../schema/index.mjs';
|
|
@@ -142,8 +142,24 @@ export declare class JsonTypeBuilder {
|
|
|
142
142
|
Record<Key extends TSchema, Value extends TSchema>(key: Key, value: Value, options?: ObjectOptions): TRecordOrObject<Key, Value>;
|
|
143
143
|
/** `[Json]` Creates a Recursive type */
|
|
144
144
|
Recursive<T extends TSchema>(callback: (thisType: TThis) => T, options?: SchemaOptions): TRecursive<T>;
|
|
145
|
-
/** `[Json]` Creates a Ref type
|
|
145
|
+
/** `[Json]` Creates a Ref type.*/
|
|
146
146
|
Ref<Ref extends string>($ref: Ref, options?: SchemaOptions): TRef<Ref>;
|
|
147
|
+
/**
|
|
148
|
+
* @deprecated `[Json]` Creates a Ref type. The referenced type MUST contain a $id. The Ref(TSchema) signature was
|
|
149
|
+
* deprecated on 0.34.0 in support of a new type referencing model (Module). Existing implementations using Ref(TSchema)
|
|
150
|
+
* can migrate using the following. The Ref(TSchema) validation behavior of Ref will be preserved how the construction
|
|
151
|
+
* of legacy Ref(TSchema) will require wrapping in TUnsafe (where TUnsafe is used for inference only)
|
|
152
|
+
*
|
|
153
|
+
* ```typescript
|
|
154
|
+
* const R = Type.Ref(T)
|
|
155
|
+
* ```
|
|
156
|
+
* to
|
|
157
|
+
*
|
|
158
|
+
* ```typescript
|
|
159
|
+
* const R = Type.Unsafe<Static<T>>(T.$id)
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
Ref<Type extends TSchema>(type: Type, options?: SchemaOptions): TRefUnsafe<Type>;
|
|
147
163
|
/** `[Json]` Constructs a type where all properties are required */
|
|
148
164
|
Required<MappedResult extends TMappedResult>(type: MappedResult, options?: SchemaOptions): TRequiredFromMappedResult<MappedResult>;
|
|
149
165
|
/** `[Json]` Constructs a type where all properties are required */
|
|
@@ -170,9 +170,9 @@ export class JsonTypeBuilder {
|
|
|
170
170
|
Recursive(callback, options) {
|
|
171
171
|
return Recursive(callback, options);
|
|
172
172
|
}
|
|
173
|
-
/** `[Json]` Creates a Ref type. */
|
|
174
|
-
Ref(
|
|
175
|
-
return Ref(
|
|
173
|
+
/** `[Json]` Creates a Ref type. The referenced type must contain a $id */
|
|
174
|
+
Ref(...args) {
|
|
175
|
+
return Ref(args[0], args[1]);
|
|
176
176
|
}
|
|
177
177
|
/** `[Json]` Constructs a type where all properties are required */
|
|
178
178
|
Required(type, options) {
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -337,7 +337,7 @@ The following table lists the supported Json types. These types are fully compat
|
|
|
337
337
|
│ }), │ y: number │ required: ['x'], │
|
|
338
338
|
│ Type.Object({ │ } │ properties: { │
|
|
339
339
|
│ y: Type.Number() │ │ x: { │
|
|
340
|
-
│
|
|
340
|
+
│ }) │ │ type: 'number' │
|
|
341
341
|
│ ]) │ │ } │
|
|
342
342
|
│ │ │ } │
|
|
343
343
|
│ │ │ }, { │
|
|
@@ -534,15 +534,7 @@ The following table lists the supported Json types. These types are fully compat
|
|
|
534
534
|
│ │ │ } │
|
|
535
535
|
│ │ │ │
|
|
536
536
|
├────────────────────────────────┼─────────────────────────────┼────────────────────────────────┤
|
|
537
|
-
│ const
|
|
538
|
-
│ x: Type.Number(), │ x: number, │ $ref: 'T' │
|
|
539
|
-
│ y: Type.Number() │ y: number │ } │
|
|
540
|
-
│ }, { $id: 'T' }) | } │ │
|
|
541
|
-
│ │ │ │
|
|
542
|
-
│ const R = Type.Ref(T) │ type R = T │ │
|
|
543
|
-
│ │ │ │
|
|
544
|
-
│ │ │ │
|
|
545
|
-
│ │ │ │
|
|
537
|
+
│ const R = Type.Ref('T') │ type R = unknown │ const R = { $ref: 'T' } │
|
|
546
538
|
│ │ │ │
|
|
547
539
|
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
|
|
548
540
|
```
|