@sinclair/typebox 0.34.11 → 0.34.13
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/ref/ref.d.ts +19 -6
- package/build/cjs/type/type/json.d.ts +19 -6
- package/build/cjs/value/convert/convert.js +1 -1
- package/build/cjs/value/mutate/mutate.js +10 -4
- package/build/esm/type/ref/ref.d.mts +19 -6
- package/build/esm/type/type/json.d.mts +19 -6
- package/build/esm/value/convert/convert.mjs +1 -1
- package/build/esm/value/mutate/mutate.mjs +10 -4
- package/package.json +1 -1
- package/readme.md +39 -13
|
@@ -11,18 +11,31 @@ export type TRefUnsafe<Type extends TSchema> = TUnsafe<Static<Type>>;
|
|
|
11
11
|
/** `[Json]` Creates a Ref type.*/
|
|
12
12
|
export declare function Ref<Ref extends string>($ref: Ref, options?: SchemaOptions): TRef<Ref>;
|
|
13
13
|
/**
|
|
14
|
-
* @deprecated `[Json]` Creates a Ref type.
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
14
|
+
* @deprecated `[Json]` Creates a Ref type. This signature was deprecated in 0.34.0 where Ref requires callers to pass
|
|
15
|
+
* a `string` value for the reference (and not a schema).
|
|
16
|
+
*
|
|
17
|
+
* To adhere to the 0.34.0 signature, Ref implementations should be updated to the following.
|
|
18
18
|
*
|
|
19
19
|
* ```typescript
|
|
20
|
+
* // pre-0.34.0
|
|
21
|
+
*
|
|
22
|
+
* const T = Type.String({ $id: 'T' })
|
|
23
|
+
*
|
|
20
24
|
* const R = Type.Ref(T)
|
|
21
25
|
* ```
|
|
22
|
-
* to
|
|
26
|
+
* should be changed to the following
|
|
27
|
+
*
|
|
28
|
+
* ```typescript
|
|
29
|
+
* // post-0.34.0
|
|
30
|
+
*
|
|
31
|
+
* const T = Type.String({ $id: 'T' })
|
|
32
|
+
*
|
|
33
|
+
* const R = Type.Unsafe<Static<typeof T>>(Type.Ref('T'))
|
|
34
|
+
* ```
|
|
35
|
+
* You can also create a generic function to replicate the pre-0.34.0 signature if required
|
|
23
36
|
*
|
|
24
37
|
* ```typescript
|
|
25
|
-
* const
|
|
38
|
+
* const LegacyRef = <T extends TSchema>(schema: T) => Type.Unsafe<Static<T>>(Type.Ref(schema.$id!))
|
|
26
39
|
* ```
|
|
27
40
|
*/
|
|
28
41
|
export declare function Ref<Type extends TSchema>(type: Type, options?: SchemaOptions): TRefUnsafe<Type>;
|
|
@@ -151,18 +151,31 @@ export declare class JsonTypeBuilder {
|
|
|
151
151
|
/** `[Json]` Creates a Ref type.*/
|
|
152
152
|
Ref<Ref extends string>($ref: Ref, options?: SchemaOptions): TRef<Ref>;
|
|
153
153
|
/**
|
|
154
|
-
* @deprecated `[Json]` Creates a Ref type.
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
154
|
+
* @deprecated `[Json]` Creates a Ref type. This signature was deprecated in 0.34.0 where Ref requires callers to pass
|
|
155
|
+
* a `string` value for the reference (and not a schema).
|
|
156
|
+
*
|
|
157
|
+
* To adhere to the 0.34.0 signature, Ref implementations should be updated to the following.
|
|
158
158
|
*
|
|
159
159
|
* ```typescript
|
|
160
|
+
* // pre-0.34.0
|
|
161
|
+
*
|
|
162
|
+
* const T = Type.String({ $id: 'T' })
|
|
163
|
+
*
|
|
160
164
|
* const R = Type.Ref(T)
|
|
161
165
|
* ```
|
|
162
|
-
* to
|
|
166
|
+
* should be changed to the following
|
|
167
|
+
*
|
|
168
|
+
* ```typescript
|
|
169
|
+
* // post-0.34.0
|
|
170
|
+
*
|
|
171
|
+
* const T = Type.String({ $id: 'T' })
|
|
172
|
+
*
|
|
173
|
+
* const R = Type.Unsafe<Static<typeof T>>(Type.Ref('T'))
|
|
174
|
+
* ```
|
|
175
|
+
* You can also create a generic function to replicate the pre-0.34.0 signature if required
|
|
163
176
|
*
|
|
164
177
|
* ```typescript
|
|
165
|
-
* const
|
|
178
|
+
* const LegacyRef = <T extends TSchema>(schema: T) => Type.Unsafe<Static<T>>(Type.Ref(schema.$id!))
|
|
166
179
|
* ```
|
|
167
180
|
*/
|
|
168
181
|
Ref<Type extends TSchema>(type: Type, options?: SchemaOptions): TRefUnsafe<Type>;
|
|
@@ -70,7 +70,7 @@ function TryConvertBigInt(value) {
|
|
|
70
70
|
return IsStringNumeric(value) ? BigInt(truncateInteger(value)) : (0, index_5.IsNumber)(value) ? BigInt(Math.trunc(value)) : IsValueFalse(value) ? BigInt(0) : IsValueTrue(value) ? BigInt(1) : value;
|
|
71
71
|
}
|
|
72
72
|
function TryConvertString(value) {
|
|
73
|
-
return
|
|
73
|
+
return (0, index_5.IsSymbol)(value) && value.description !== undefined ? value.description.toString() : IsValueToString(value) ? value.toString() : value;
|
|
74
74
|
}
|
|
75
75
|
function TryConvertNumber(value) {
|
|
76
76
|
return IsStringNumeric(value) ? parseFloat(value) : IsValueTrue(value) ? 1 : IsValueFalse(value) ? 0 : value;
|
|
@@ -8,6 +8,12 @@ const index_2 = require("../pointer/index");
|
|
|
8
8
|
const index_3 = require("../clone/index");
|
|
9
9
|
const index_4 = require("../../type/error/index");
|
|
10
10
|
// ------------------------------------------------------------------
|
|
11
|
+
// IsStandardObject
|
|
12
|
+
// ------------------------------------------------------------------
|
|
13
|
+
function IsStandardObject(value) {
|
|
14
|
+
return (0, index_1.IsObject)(value) && !(0, index_1.IsArray)(value);
|
|
15
|
+
}
|
|
16
|
+
// ------------------------------------------------------------------
|
|
11
17
|
// Errors
|
|
12
18
|
// ------------------------------------------------------------------
|
|
13
19
|
class ValueMutateError extends index_4.TypeBoxError {
|
|
@@ -17,7 +23,7 @@ class ValueMutateError extends index_4.TypeBoxError {
|
|
|
17
23
|
}
|
|
18
24
|
exports.ValueMutateError = ValueMutateError;
|
|
19
25
|
function ObjectType(root, path, current, next) {
|
|
20
|
-
if (!(
|
|
26
|
+
if (!IsStandardObject(current)) {
|
|
21
27
|
index_2.ValuePointer.Set(root, path, (0, index_3.Clone)(next));
|
|
22
28
|
}
|
|
23
29
|
else {
|
|
@@ -69,7 +75,7 @@ function Visit(root, path, current, next) {
|
|
|
69
75
|
return ArrayType(root, path, current, next);
|
|
70
76
|
if ((0, index_1.IsTypedArray)(next))
|
|
71
77
|
return TypedArrayType(root, path, current, next);
|
|
72
|
-
if ((
|
|
78
|
+
if (IsStandardObject(next))
|
|
73
79
|
return ObjectType(root, path, current, next);
|
|
74
80
|
if ((0, index_1.IsValueType)(next))
|
|
75
81
|
return ValueType(root, path, current, next);
|
|
@@ -82,8 +88,8 @@ function IsNonMutableValue(value) {
|
|
|
82
88
|
}
|
|
83
89
|
function IsMismatchedValue(current, next) {
|
|
84
90
|
// prettier-ignore
|
|
85
|
-
return (((
|
|
86
|
-
((0, index_1.IsArray)(current) && (
|
|
91
|
+
return ((IsStandardObject(current) && (0, index_1.IsArray)(next)) ||
|
|
92
|
+
((0, index_1.IsArray)(current) && IsStandardObject(next)));
|
|
87
93
|
}
|
|
88
94
|
// ------------------------------------------------------------------
|
|
89
95
|
// Mutate
|
|
@@ -11,18 +11,31 @@ export type TRefUnsafe<Type extends TSchema> = TUnsafe<Static<Type>>;
|
|
|
11
11
|
/** `[Json]` Creates a Ref type.*/
|
|
12
12
|
export declare function Ref<Ref extends string>($ref: Ref, options?: SchemaOptions): TRef<Ref>;
|
|
13
13
|
/**
|
|
14
|
-
* @deprecated `[Json]` Creates a Ref type.
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
14
|
+
* @deprecated `[Json]` Creates a Ref type. This signature was deprecated in 0.34.0 where Ref requires callers to pass
|
|
15
|
+
* a `string` value for the reference (and not a schema).
|
|
16
|
+
*
|
|
17
|
+
* To adhere to the 0.34.0 signature, Ref implementations should be updated to the following.
|
|
18
18
|
*
|
|
19
19
|
* ```typescript
|
|
20
|
+
* // pre-0.34.0
|
|
21
|
+
*
|
|
22
|
+
* const T = Type.String({ $id: 'T' })
|
|
23
|
+
*
|
|
20
24
|
* const R = Type.Ref(T)
|
|
21
25
|
* ```
|
|
22
|
-
* to
|
|
26
|
+
* should be changed to the following
|
|
27
|
+
*
|
|
28
|
+
* ```typescript
|
|
29
|
+
* // post-0.34.0
|
|
30
|
+
*
|
|
31
|
+
* const T = Type.String({ $id: 'T' })
|
|
32
|
+
*
|
|
33
|
+
* const R = Type.Unsafe<Static<typeof T>>(Type.Ref('T'))
|
|
34
|
+
* ```
|
|
35
|
+
* You can also create a generic function to replicate the pre-0.34.0 signature if required
|
|
23
36
|
*
|
|
24
37
|
* ```typescript
|
|
25
|
-
* const
|
|
38
|
+
* const LegacyRef = <T extends TSchema>(schema: T) => Type.Unsafe<Static<T>>(Type.Ref(schema.$id!))
|
|
26
39
|
* ```
|
|
27
40
|
*/
|
|
28
41
|
export declare function Ref<Type extends TSchema>(type: Type, options?: SchemaOptions): TRefUnsafe<Type>;
|
|
@@ -151,18 +151,31 @@ export declare class JsonTypeBuilder {
|
|
|
151
151
|
/** `[Json]` Creates a Ref type.*/
|
|
152
152
|
Ref<Ref extends string>($ref: Ref, options?: SchemaOptions): TRef<Ref>;
|
|
153
153
|
/**
|
|
154
|
-
* @deprecated `[Json]` Creates a Ref type.
|
|
155
|
-
*
|
|
156
|
-
*
|
|
157
|
-
*
|
|
154
|
+
* @deprecated `[Json]` Creates a Ref type. This signature was deprecated in 0.34.0 where Ref requires callers to pass
|
|
155
|
+
* a `string` value for the reference (and not a schema).
|
|
156
|
+
*
|
|
157
|
+
* To adhere to the 0.34.0 signature, Ref implementations should be updated to the following.
|
|
158
158
|
*
|
|
159
159
|
* ```typescript
|
|
160
|
+
* // pre-0.34.0
|
|
161
|
+
*
|
|
162
|
+
* const T = Type.String({ $id: 'T' })
|
|
163
|
+
*
|
|
160
164
|
* const R = Type.Ref(T)
|
|
161
165
|
* ```
|
|
162
|
-
* to
|
|
166
|
+
* should be changed to the following
|
|
167
|
+
*
|
|
168
|
+
* ```typescript
|
|
169
|
+
* // post-0.34.0
|
|
170
|
+
*
|
|
171
|
+
* const T = Type.String({ $id: 'T' })
|
|
172
|
+
*
|
|
173
|
+
* const R = Type.Unsafe<Static<typeof T>>(Type.Ref('T'))
|
|
174
|
+
* ```
|
|
175
|
+
* You can also create a generic function to replicate the pre-0.34.0 signature if required
|
|
163
176
|
*
|
|
164
177
|
* ```typescript
|
|
165
|
-
* const
|
|
178
|
+
* const LegacyRef = <T extends TSchema>(schema: T) => Type.Unsafe<Static<T>>(Type.Ref(schema.$id!))
|
|
166
179
|
* ```
|
|
167
180
|
*/
|
|
168
181
|
Ref<Type extends TSchema>(type: Type, options?: SchemaOptions): TRefUnsafe<Type>;
|
|
@@ -66,7 +66,7 @@ function TryConvertBigInt(value) {
|
|
|
66
66
|
return IsStringNumeric(value) ? BigInt(truncateInteger(value)) : IsNumber(value) ? BigInt(Math.trunc(value)) : IsValueFalse(value) ? BigInt(0) : IsValueTrue(value) ? BigInt(1) : value;
|
|
67
67
|
}
|
|
68
68
|
function TryConvertString(value) {
|
|
69
|
-
return
|
|
69
|
+
return IsSymbol(value) && value.description !== undefined ? value.description.toString() : IsValueToString(value) ? value.toString() : value;
|
|
70
70
|
}
|
|
71
71
|
function TryConvertNumber(value) {
|
|
72
72
|
return IsStringNumeric(value) ? parseFloat(value) : IsValueTrue(value) ? 1 : IsValueFalse(value) ? 0 : value;
|
|
@@ -3,6 +3,12 @@ import { ValuePointer } from '../pointer/index.mjs';
|
|
|
3
3
|
import { Clone } from '../clone/index.mjs';
|
|
4
4
|
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
5
5
|
// ------------------------------------------------------------------
|
|
6
|
+
// IsStandardObject
|
|
7
|
+
// ------------------------------------------------------------------
|
|
8
|
+
function IsStandardObject(value) {
|
|
9
|
+
return IsObject(value) && !IsArray(value);
|
|
10
|
+
}
|
|
11
|
+
// ------------------------------------------------------------------
|
|
6
12
|
// Errors
|
|
7
13
|
// ------------------------------------------------------------------
|
|
8
14
|
export class ValueMutateError extends TypeBoxError {
|
|
@@ -11,7 +17,7 @@ export class ValueMutateError extends TypeBoxError {
|
|
|
11
17
|
}
|
|
12
18
|
}
|
|
13
19
|
function ObjectType(root, path, current, next) {
|
|
14
|
-
if (!
|
|
20
|
+
if (!IsStandardObject(current)) {
|
|
15
21
|
ValuePointer.Set(root, path, Clone(next));
|
|
16
22
|
}
|
|
17
23
|
else {
|
|
@@ -63,7 +69,7 @@ function Visit(root, path, current, next) {
|
|
|
63
69
|
return ArrayType(root, path, current, next);
|
|
64
70
|
if (IsTypedArray(next))
|
|
65
71
|
return TypedArrayType(root, path, current, next);
|
|
66
|
-
if (
|
|
72
|
+
if (IsStandardObject(next))
|
|
67
73
|
return ObjectType(root, path, current, next);
|
|
68
74
|
if (IsValueType(next))
|
|
69
75
|
return ValueType(root, path, current, next);
|
|
@@ -76,8 +82,8 @@ function IsNonMutableValue(value) {
|
|
|
76
82
|
}
|
|
77
83
|
function IsMismatchedValue(current, next) {
|
|
78
84
|
// prettier-ignore
|
|
79
|
-
return ((
|
|
80
|
-
(IsArray(current) &&
|
|
85
|
+
return ((IsStandardObject(current) && IsArray(next)) ||
|
|
86
|
+
(IsArray(current) && IsStandardObject(next)));
|
|
81
87
|
}
|
|
82
88
|
// ------------------------------------------------------------------
|
|
83
89
|
// Mutate
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -68,6 +68,7 @@ License MIT
|
|
|
68
68
|
- [Options](#types-options)
|
|
69
69
|
- [Properties](#types-properties)
|
|
70
70
|
- [Generics](#types-generics)
|
|
71
|
+
- [Recursive](#types-recursive)
|
|
71
72
|
- [Modules](#types-modules)
|
|
72
73
|
- [Template Literal](#types-template-literal)
|
|
73
74
|
- [Indexed](#types-indexed)
|
|
@@ -759,21 +760,51 @@ const T = Nullable(Type.String()) // const T = {
|
|
|
759
760
|
type T = Static<typeof T> // type T = string | null
|
|
760
761
|
```
|
|
761
762
|
|
|
762
|
-
<a name='types-
|
|
763
|
+
<a name='types-recursive'></a>
|
|
763
764
|
|
|
764
|
-
###
|
|
765
|
+
### Recursive Types
|
|
765
766
|
|
|
766
|
-
|
|
767
|
+
Use the Recursive function to create a singular recursive type.
|
|
767
768
|
|
|
768
|
-
|
|
769
|
+
```typescript
|
|
770
|
+
const Node = Type.Recursive(Self => Type.Object({ // const Node = {
|
|
771
|
+
id: Type.String(), // $id: 'Node',
|
|
772
|
+
nodes: Type.Array(Self) // type: 'object',
|
|
773
|
+
}), { $id: 'Node' }) // properties: {
|
|
774
|
+
// id: {
|
|
775
|
+
// type: 'string'
|
|
776
|
+
// },
|
|
777
|
+
// nodes: {
|
|
778
|
+
// type: 'array',
|
|
779
|
+
// items: {
|
|
780
|
+
// $ref: 'Node'
|
|
781
|
+
// }
|
|
782
|
+
// }
|
|
783
|
+
// },
|
|
784
|
+
// required: [
|
|
785
|
+
// 'id',
|
|
786
|
+
// 'nodes'
|
|
787
|
+
// ]
|
|
788
|
+
// }
|
|
769
789
|
|
|
770
|
-
|
|
790
|
+
type Node = Static<typeof Node> // type Node = {
|
|
791
|
+
// id: string
|
|
792
|
+
// nodes: Node[]
|
|
793
|
+
// }
|
|
771
794
|
|
|
772
|
-
|
|
773
|
-
|
|
795
|
+
function test(node: Node) {
|
|
796
|
+
const id = node.nodes[0].nodes[0].id // id is string
|
|
797
|
+
}
|
|
798
|
+
```
|
|
774
799
|
|
|
775
|
-
|
|
800
|
+
<a name='types-modules'></a>
|
|
801
|
+
|
|
802
|
+
### Module Types
|
|
776
803
|
|
|
804
|
+
Module types are containers for a set of referential types. Modules act as namespaces, enabling types to reference one another via string identifiers. Modules support both singular and mutually recursive references, as well as deferred dereferencing for computed types such as Partial. Types imported from a module are expressed using the Json Schema `$defs` keyword.
|
|
805
|
+
|
|
806
|
+
```typescript
|
|
807
|
+
const Module = Type.Module({
|
|
777
808
|
PartialUser: Type.Partial(Type.Ref('User')), // TComputed<'Partial', [TRef<'User'>]>
|
|
778
809
|
|
|
779
810
|
User: Type.Object({ // TObject<{
|
|
@@ -781,11 +812,7 @@ const Module = Type.Module({
|
|
|
781
812
|
name: Type.String(), // name: TString,
|
|
782
813
|
email: Type.String() // email: TString
|
|
783
814
|
}), // }>
|
|
784
|
-
|
|
785
815
|
})
|
|
786
|
-
|
|
787
|
-
// Types must be imported before use.
|
|
788
|
-
|
|
789
816
|
const User = Module.Import('User') // const User: TImport<{...}, 'User'>
|
|
790
817
|
|
|
791
818
|
type User = Static<typeof User> // type User = {
|
|
@@ -803,7 +830,6 @@ type PartialUser = Static<typeof PartialUser> // type PartialUser = {
|
|
|
803
830
|
// }
|
|
804
831
|
```
|
|
805
832
|
|
|
806
|
-
|
|
807
833
|
<a name='types-template-literal'></a>
|
|
808
834
|
|
|
809
835
|
### Template Literal Types
|