@sinclair/typebox 0.34.11 → 0.34.12
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/value/mutate/mutate.js +10 -4
- package/build/esm/value/mutate/mutate.mjs +10 -4
- package/package.json +1 -1
- package/readme.md +39 -13
|
@@ -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
|
|
@@ -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
|