@sinclair/typebox 0.33.3 → 0.33.5
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 +2 -2
- package/build/cjs/compiler/compiler.js +1 -1
- package/build/cjs/system/policy.d.ts +5 -5
- package/build/cjs/system/policy.js +5 -5
- package/build/cjs/value/assert/assert.d.ts +15 -0
- package/build/cjs/value/assert/assert.js +55 -0
- package/build/cjs/value/assert/index.d.ts +1 -0
- package/build/cjs/value/assert/index.js +18 -0
- package/build/cjs/value/cast/cast.js +1 -1
- package/build/cjs/value/clean/clean.js +6 -6
- package/build/cjs/value/clone/clone.js +21 -11
- package/build/cjs/value/convert/convert.d.ts +2 -2
- package/build/cjs/value/convert/convert.js +17 -18
- package/build/cjs/value/create/create.js +5 -1
- package/build/cjs/value/default/default.js +14 -16
- package/build/cjs/value/equal/equal.js +3 -3
- package/build/cjs/value/hash/hash.js +1 -1
- package/build/cjs/value/index.d.ts +2 -0
- package/build/cjs/value/index.js +2 -0
- package/build/cjs/value/mutate/mutate.js +4 -4
- package/build/cjs/value/parse/index.d.ts +1 -0
- package/build/cjs/value/parse/index.js +18 -0
- package/build/cjs/value/parse/parse.d.ts +6 -0
- package/build/cjs/value/parse/parse.js +29 -0
- package/build/cjs/value/transform/decode.js +21 -8
- package/build/cjs/value/transform/encode.js +22 -9
- package/build/cjs/value/transform/has.js +5 -1
- package/build/cjs/value/value/value.d.ts +10 -2
- package/build/cjs/value/value/value.js +38 -26
- package/build/esm/compiler/compiler.d.mts +2 -2
- package/build/esm/compiler/compiler.mjs +1 -1
- package/build/esm/system/policy.d.mts +5 -5
- package/build/esm/system/policy.mjs +5 -5
- package/build/esm/value/assert/assert.d.mts +15 -0
- package/build/esm/value/assert/assert.mjs +49 -0
- package/build/esm/value/assert/index.d.mts +1 -0
- package/build/esm/value/assert/index.mjs +1 -0
- package/build/esm/value/cast/cast.mjs +2 -2
- package/build/esm/value/clean/clean.mjs +7 -7
- package/build/esm/value/clone/clone.mjs +22 -12
- package/build/esm/value/convert/convert.d.mts +2 -2
- package/build/esm/value/convert/convert.mjs +17 -18
- package/build/esm/value/create/create.mjs +5 -1
- package/build/esm/value/default/default.mjs +14 -16
- package/build/esm/value/equal/equal.mjs +4 -4
- package/build/esm/value/hash/hash.mjs +2 -2
- package/build/esm/value/index.d.mts +2 -0
- package/build/esm/value/index.mjs +2 -0
- package/build/esm/value/mutate/mutate.mjs +5 -5
- package/build/esm/value/parse/index.d.mts +1 -0
- package/build/esm/value/parse/index.mjs +1 -0
- package/build/esm/value/parse/parse.d.mts +6 -0
- package/build/esm/value/parse/parse.mjs +25 -0
- package/build/esm/value/transform/decode.mjs +23 -10
- package/build/esm/value/transform/encode.mjs +24 -11
- package/build/esm/value/transform/has.mjs +5 -1
- package/build/esm/value/value/value.d.mts +10 -2
- package/build/esm/value/value/value.mjs +11 -1
- package/package.json +1 -1
- package/readme.md +115 -130
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.TransformDecodeError = exports.TransformDecodeCheckError = void 0;
|
|
5
5
|
exports.TransformDecode = TransformDecode;
|
|
6
|
+
const policy_1 = require("../../system/policy");
|
|
6
7
|
const index_1 = require("../../type/symbols/index");
|
|
7
8
|
const index_2 = require("../../type/error/index");
|
|
8
9
|
const index_3 = require("../../type/keyof/index");
|
|
@@ -61,7 +62,7 @@ function FromArray(schema, references, path, value) {
|
|
|
61
62
|
}
|
|
62
63
|
// prettier-ignore
|
|
63
64
|
function FromIntersect(schema, references, path, value) {
|
|
64
|
-
if (!(0, index_6.
|
|
65
|
+
if (!(0, index_6.IsObject)(value) || (0, index_6.IsValueType)(value))
|
|
65
66
|
return Default(schema, path, value);
|
|
66
67
|
const knownEntries = (0, index_3.KeyOfPropertyEntries)(schema);
|
|
67
68
|
const knownKeys = knownEntries.map(entry => entry[0]);
|
|
@@ -87,14 +88,22 @@ function FromNot(schema, references, path, value) {
|
|
|
87
88
|
}
|
|
88
89
|
// prettier-ignore
|
|
89
90
|
function FromObject(schema, references, path, value) {
|
|
90
|
-
if (!(0, index_6.
|
|
91
|
+
if (!(0, index_6.IsObject)(value))
|
|
91
92
|
return Default(schema, path, value);
|
|
92
93
|
const knownKeys = (0, index_3.KeyOfPropertyKeys)(schema);
|
|
93
94
|
const knownProperties = { ...value };
|
|
94
|
-
for (const key of knownKeys)
|
|
95
|
-
if (
|
|
96
|
-
|
|
97
|
-
|
|
95
|
+
for (const key of knownKeys) {
|
|
96
|
+
if (!(0, index_6.HasPropertyKey)(knownProperties, key))
|
|
97
|
+
continue;
|
|
98
|
+
// if the property value is undefined, but the target is not, nor does it satisfy exact optional
|
|
99
|
+
// property policy, then we need to continue. This is a special case for optional property handling
|
|
100
|
+
// where a transforms wrapped in a optional modifiers should not run.
|
|
101
|
+
if ((0, index_6.IsUndefined)(knownProperties[key]) && (!(0, type_1.IsUndefined)(schema.properties[key]) ||
|
|
102
|
+
policy_1.TypeSystemPolicy.IsExactOptionalProperty(knownProperties, key)))
|
|
103
|
+
continue;
|
|
104
|
+
// decode property
|
|
105
|
+
knownProperties[key] = Visit(schema.properties[key], references, `${path}/${key}`, knownProperties[key]);
|
|
106
|
+
}
|
|
98
107
|
if (!(0, type_1.IsSchema)(schema.additionalProperties)) {
|
|
99
108
|
return Default(schema, path, knownProperties);
|
|
100
109
|
}
|
|
@@ -109,7 +118,7 @@ function FromObject(schema, references, path, value) {
|
|
|
109
118
|
}
|
|
110
119
|
// prettier-ignore
|
|
111
120
|
function FromRecord(schema, references, path, value) {
|
|
112
|
-
if (!(0, index_6.
|
|
121
|
+
if (!(0, index_6.IsObject)(value))
|
|
113
122
|
return Default(schema, path, value);
|
|
114
123
|
const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0];
|
|
115
124
|
const knownKeys = new RegExp(pattern);
|
|
@@ -157,9 +166,13 @@ function FromUnion(schema, references, path, value) {
|
|
|
157
166
|
}
|
|
158
167
|
return Default(schema, path, value);
|
|
159
168
|
}
|
|
169
|
+
function AddReference(references, schema) {
|
|
170
|
+
references.push(schema);
|
|
171
|
+
return references;
|
|
172
|
+
}
|
|
160
173
|
// prettier-ignore
|
|
161
174
|
function Visit(schema, references, path, value) {
|
|
162
|
-
const references_ = typeof schema.$id === 'string' ?
|
|
175
|
+
const references_ = typeof schema.$id === 'string' ? AddReference(references, schema) : references;
|
|
163
176
|
const schema_ = schema;
|
|
164
177
|
switch (schema[index_1.Kind]) {
|
|
165
178
|
case 'Array':
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.TransformEncodeError = exports.TransformEncodeCheckError = void 0;
|
|
5
5
|
exports.TransformEncode = TransformEncode;
|
|
6
|
+
const policy_1 = require("../../system/policy");
|
|
6
7
|
const index_1 = require("../../type/symbols/index");
|
|
7
8
|
const index_2 = require("../../type/error/index");
|
|
8
9
|
const index_3 = require("../../type/keyof/index");
|
|
@@ -62,7 +63,7 @@ function FromArray(schema, references, path, value) {
|
|
|
62
63
|
// prettier-ignore
|
|
63
64
|
function FromIntersect(schema, references, path, value) {
|
|
64
65
|
const defaulted = Default(schema, path, value);
|
|
65
|
-
if (!(0, index_6.
|
|
66
|
+
if (!(0, index_6.IsObject)(value) || (0, index_6.IsValueType)(value))
|
|
66
67
|
return defaulted;
|
|
67
68
|
const knownEntries = (0, index_3.KeyOfPropertyEntries)(schema);
|
|
68
69
|
const knownKeys = knownEntries.map(entry => entry[0]);
|
|
@@ -90,14 +91,22 @@ function FromNot(schema, references, path, value) {
|
|
|
90
91
|
// prettier-ignore
|
|
91
92
|
function FromObject(schema, references, path, value) {
|
|
92
93
|
const defaulted = Default(schema, path, value);
|
|
93
|
-
if (!(0, index_6.
|
|
94
|
+
if (!(0, index_6.IsObject)(defaulted))
|
|
94
95
|
return defaulted;
|
|
95
96
|
const knownKeys = (0, index_3.KeyOfPropertyKeys)(schema);
|
|
96
97
|
const knownProperties = { ...defaulted };
|
|
97
|
-
for (const key of knownKeys)
|
|
98
|
-
if (
|
|
99
|
-
|
|
100
|
-
|
|
98
|
+
for (const key of knownKeys) {
|
|
99
|
+
if (!(0, index_6.HasPropertyKey)(knownProperties, key))
|
|
100
|
+
continue;
|
|
101
|
+
// if the property value is undefined, but the target is not, nor does it satisfy exact optional
|
|
102
|
+
// property policy, then we need to continue. This is a special case for optional property handling
|
|
103
|
+
// where a transforms wrapped in a optional modifiers should not run.
|
|
104
|
+
if ((0, index_6.IsUndefined)(knownProperties[key]) && (!(0, type_1.IsUndefined)(schema.properties[key]) ||
|
|
105
|
+
policy_1.TypeSystemPolicy.IsExactOptionalProperty(knownProperties, key)))
|
|
106
|
+
continue;
|
|
107
|
+
// encode property
|
|
108
|
+
knownProperties[key] = Visit(schema.properties[key], references, `${path}/${key}`, knownProperties[key]);
|
|
109
|
+
}
|
|
101
110
|
if (!(0, type_1.IsSchema)(schema.additionalProperties)) {
|
|
102
111
|
return knownProperties;
|
|
103
112
|
}
|
|
@@ -113,7 +122,7 @@ function FromObject(schema, references, path, value) {
|
|
|
113
122
|
// prettier-ignore
|
|
114
123
|
function FromRecord(schema, references, path, value) {
|
|
115
124
|
const defaulted = Default(schema, path, value);
|
|
116
|
-
if (!(0, index_6.
|
|
125
|
+
if (!(0, index_6.IsObject)(value))
|
|
117
126
|
return defaulted;
|
|
118
127
|
const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0];
|
|
119
128
|
const knownKeys = new RegExp(pattern);
|
|
@@ -123,7 +132,7 @@ function FromRecord(schema, references, path, value) {
|
|
|
123
132
|
knownProperties[key] = Visit(schema.patternProperties[pattern], references, `${path}/${key}`, knownProperties[key]);
|
|
124
133
|
}
|
|
125
134
|
if (!(0, type_1.IsSchema)(schema.additionalProperties)) {
|
|
126
|
-
return
|
|
135
|
+
return knownProperties;
|
|
127
136
|
}
|
|
128
137
|
const unknownKeys = Object.getOwnPropertyNames(knownProperties);
|
|
129
138
|
const additionalProperties = schema.additionalProperties;
|
|
@@ -169,9 +178,13 @@ function FromUnion(schema, references, path, value) {
|
|
|
169
178
|
}
|
|
170
179
|
return Default(schema, path, value);
|
|
171
180
|
}
|
|
181
|
+
function AddReference(references, schema) {
|
|
182
|
+
references.push(schema);
|
|
183
|
+
return references;
|
|
184
|
+
}
|
|
172
185
|
// prettier-ignore
|
|
173
186
|
function Visit(schema, references, path, value) {
|
|
174
|
-
const references_ = typeof schema.$id === 'string' ?
|
|
187
|
+
const references_ = typeof schema.$id === 'string' ? AddReference(references, schema) : references;
|
|
175
188
|
const schema_ = schema;
|
|
176
189
|
switch (schema[index_1.Kind]) {
|
|
177
190
|
case 'Array':
|
|
@@ -76,9 +76,13 @@ function FromTuple(schema, references) {
|
|
|
76
76
|
function FromUnion(schema, references) {
|
|
77
77
|
return (0, type_1.IsTransform)(schema) || schema.anyOf.some((schema) => Visit(schema, references));
|
|
78
78
|
}
|
|
79
|
+
function AddReference(references, schema) {
|
|
80
|
+
references.push(schema);
|
|
81
|
+
return references;
|
|
82
|
+
}
|
|
79
83
|
// prettier-ignore
|
|
80
84
|
function Visit(schema, references) {
|
|
81
|
-
const references_ = (0, index_3.IsString)(schema.$id) ?
|
|
85
|
+
const references_ = (0, index_3.IsString)(schema.$id) ? AddReference(references, schema) : references;
|
|
82
86
|
const schema_ = schema;
|
|
83
87
|
if (schema.$id && visited.has(schema.$id))
|
|
84
88
|
return false;
|
|
@@ -3,6 +3,10 @@ import { Edit } from '../delta/index';
|
|
|
3
3
|
import { ValueErrorIterator } from '../../errors/index';
|
|
4
4
|
import type { TSchema } from '../../type/schema/index';
|
|
5
5
|
import type { Static, StaticDecode, StaticEncode } from '../../type/static/index';
|
|
6
|
+
/** Asserts a value matches the given type or throws an `AssertError` if invalid. */
|
|
7
|
+
export declare function Assert<T extends TSchema, R = Static<T>>(schema: T, references: TSchema[], value: unknown): asserts value is R;
|
|
8
|
+
/** Asserts a value matches the given type or throws an `AssertError` if invalid. */
|
|
9
|
+
export declare function Assert<T extends TSchema, R = Static<T>>(schema: T, value: unknown): asserts value is R;
|
|
6
10
|
/** Casts a value into a given type. The return value will retain as much information of the original value as possible. */
|
|
7
11
|
export declare function Cast<T extends TSchema>(schema: T, references: TSchema[], value: unknown): Static<T>;
|
|
8
12
|
/** Casts a value into a given type. The return value will retain as much information of the original value as possible. */
|
|
@@ -19,9 +23,9 @@ export declare function Check<T extends TSchema>(schema: T, value: unknown): val
|
|
|
19
23
|
export declare function Clean(schema: TSchema, references: TSchema[], value: unknown): unknown;
|
|
20
24
|
/** `[Mutable]` Removes excess properties from a value and returns the result. This function does not check the value and returns an unknown type. You should Check the result before use. Clean is a mutable operation. To avoid mutation, Clone the value first. */
|
|
21
25
|
export declare function Clean(schema: TSchema, value: unknown): unknown;
|
|
22
|
-
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
26
|
+
/** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
23
27
|
export declare function Convert(schema: TSchema, references: TSchema[], value: unknown): unknown;
|
|
24
|
-
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
28
|
+
/** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
25
29
|
export declare function Convert(schema: TSchema, value: unknown): unknown;
|
|
26
30
|
/** Returns a structural clone of the given value */
|
|
27
31
|
export declare function Clone<T>(value: T): T;
|
|
@@ -37,6 +41,10 @@ export declare function Default(schema: TSchema, value: unknown): unknown;
|
|
|
37
41
|
export declare function Encode<T extends TSchema, R = StaticEncode<T>>(schema: T, references: TSchema[], value: unknown): R;
|
|
38
42
|
/** Encodes a value or throws if error */
|
|
39
43
|
export declare function Encode<T extends TSchema, R = StaticEncode<T>>(schema: T, value: unknown): R;
|
|
44
|
+
/** Parses a value or throws an `AssertError` if invalid. */
|
|
45
|
+
export declare function Parse<T extends TSchema, R = StaticDecode<T>>(schema: T, references: TSchema[], value: unknown): R;
|
|
46
|
+
/** Parses a value or throws an `AssertError` if invalid. */
|
|
47
|
+
export declare function Parse<T extends TSchema, R = StaticDecode<T>>(schema: T, value: unknown): R;
|
|
40
48
|
/** Returns an iterator for each error in this value. */
|
|
41
49
|
export declare function Errors<T extends TSchema>(schema: T, references: TSchema[], value: unknown): ValueErrorIterator;
|
|
42
50
|
/** Returns an iterator for each error in this value. */
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Assert = Assert;
|
|
4
5
|
exports.Cast = Cast;
|
|
5
6
|
exports.Create = Create;
|
|
6
7
|
exports.Check = Check;
|
|
@@ -10,6 +11,7 @@ exports.Clone = Clone;
|
|
|
10
11
|
exports.Decode = Decode;
|
|
11
12
|
exports.Default = Default;
|
|
12
13
|
exports.Encode = Encode;
|
|
14
|
+
exports.Parse = Parse;
|
|
13
15
|
exports.Errors = Errors;
|
|
14
16
|
exports.Equal = Equal;
|
|
15
17
|
exports.Diff = Diff;
|
|
@@ -17,41 +19,47 @@ exports.Hash = Hash;
|
|
|
17
19
|
exports.Patch = Patch;
|
|
18
20
|
exports.Mutate = Mutate;
|
|
19
21
|
const index_1 = require("../transform/index");
|
|
20
|
-
const index_2 = require("../
|
|
21
|
-
const index_3 = require("../
|
|
22
|
-
const index_4 = require("../
|
|
23
|
-
const index_5 = require("../
|
|
24
|
-
const index_6 = require("../
|
|
25
|
-
const index_7 = require("../
|
|
26
|
-
const index_8 = require("../
|
|
27
|
-
const index_9 = require("../
|
|
28
|
-
const index_10 = require("../
|
|
29
|
-
const index_11 = require("../
|
|
30
|
-
const index_12 = require("../
|
|
31
|
-
const index_13 = require("
|
|
22
|
+
const index_2 = require("../assert/index");
|
|
23
|
+
const index_3 = require("../mutate/index");
|
|
24
|
+
const index_4 = require("../hash/index");
|
|
25
|
+
const index_5 = require("../equal/index");
|
|
26
|
+
const index_6 = require("../cast/index");
|
|
27
|
+
const index_7 = require("../clone/index");
|
|
28
|
+
const index_8 = require("../convert/index");
|
|
29
|
+
const index_9 = require("../create/index");
|
|
30
|
+
const index_10 = require("../clean/index");
|
|
31
|
+
const index_11 = require("../check/index");
|
|
32
|
+
const index_12 = require("../parse/index");
|
|
33
|
+
const index_13 = require("../default/index");
|
|
34
|
+
const index_14 = require("../delta/index");
|
|
35
|
+
const index_15 = require("../../errors/index");
|
|
36
|
+
/** Asserts a value matches the given type or throws an `AssertError` if invalid. */
|
|
37
|
+
function Assert(...args) {
|
|
38
|
+
return index_2.Assert.apply(index_2.Assert, args);
|
|
39
|
+
}
|
|
32
40
|
/** Casts a value into a given type. The return value will retain as much information of the original value as possible. */
|
|
33
41
|
function Cast(...args) {
|
|
34
|
-
return
|
|
42
|
+
return index_6.Cast.apply(index_6.Cast, args);
|
|
35
43
|
}
|
|
36
44
|
/** Creates a value from the given type */
|
|
37
45
|
function Create(...args) {
|
|
38
|
-
return
|
|
46
|
+
return index_9.Create.apply(index_9.Create, args);
|
|
39
47
|
}
|
|
40
48
|
/** Returns true if the value matches the given type */
|
|
41
49
|
function Check(...args) {
|
|
42
|
-
return
|
|
50
|
+
return index_11.Check.apply(index_11.Check, args);
|
|
43
51
|
}
|
|
44
52
|
/** `[Mutable]` Removes excess properties from a value and returns the result. This function does not check the value and returns an unknown type. You should Check the result before use. Clean is a mutable operation. To avoid mutation, Clone the value first. */
|
|
45
53
|
function Clean(...args) {
|
|
46
|
-
return
|
|
54
|
+
return index_10.Clean.apply(index_10.Clean, args);
|
|
47
55
|
}
|
|
48
|
-
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
56
|
+
/** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
49
57
|
function Convert(...args) {
|
|
50
|
-
return
|
|
58
|
+
return index_8.Convert.apply(index_8.Convert, args);
|
|
51
59
|
}
|
|
52
60
|
/** Returns a structural clone of the given value */
|
|
53
61
|
function Clone(value) {
|
|
54
|
-
return (0,
|
|
62
|
+
return (0, index_7.Clone)(value);
|
|
55
63
|
}
|
|
56
64
|
/** Decodes a value or throws if error */
|
|
57
65
|
function Decode(...args) {
|
|
@@ -62,7 +70,7 @@ function Decode(...args) {
|
|
|
62
70
|
}
|
|
63
71
|
/** `[Mutable]` Generates missing properties on a value using default schema annotations if available. This function does not check the value and returns an unknown type. You should Check the result before use. Default is a mutable operation. To avoid mutation, Clone the value first. */
|
|
64
72
|
function Default(...args) {
|
|
65
|
-
return
|
|
73
|
+
return index_13.Default.apply(index_13.Default, args);
|
|
66
74
|
}
|
|
67
75
|
/** Encodes a value or throws if error */
|
|
68
76
|
function Encode(...args) {
|
|
@@ -72,27 +80,31 @@ function Encode(...args) {
|
|
|
72
80
|
throw new index_1.TransformEncodeCheckError(schema, encoded, Errors(schema, references, encoded).First());
|
|
73
81
|
return encoded;
|
|
74
82
|
}
|
|
83
|
+
/** Parses a value or throws an `AssertError` if invalid. */
|
|
84
|
+
function Parse(...args) {
|
|
85
|
+
return index_12.Parse.apply(index_12.Parse, args);
|
|
86
|
+
}
|
|
75
87
|
/** Returns an iterator for each error in this value. */
|
|
76
88
|
function Errors(...args) {
|
|
77
|
-
return
|
|
89
|
+
return index_15.Errors.apply(index_15.Errors, args);
|
|
78
90
|
}
|
|
79
91
|
/** Returns true if left and right values are structurally equal */
|
|
80
92
|
function Equal(left, right) {
|
|
81
|
-
return (0,
|
|
93
|
+
return (0, index_5.Equal)(left, right);
|
|
82
94
|
}
|
|
83
95
|
/** Returns edits to transform the current value into the next value */
|
|
84
96
|
function Diff(current, next) {
|
|
85
|
-
return (0,
|
|
97
|
+
return (0, index_14.Diff)(current, next);
|
|
86
98
|
}
|
|
87
99
|
/** Returns a FNV1A-64 non cryptographic hash of the given value */
|
|
88
100
|
function Hash(value) {
|
|
89
|
-
return (0,
|
|
101
|
+
return (0, index_4.Hash)(value);
|
|
90
102
|
}
|
|
91
103
|
/** Returns a new value with edits applied to the given value */
|
|
92
104
|
function Patch(current, edits) {
|
|
93
|
-
return (0,
|
|
105
|
+
return (0, index_14.Patch)(current, edits);
|
|
94
106
|
}
|
|
95
107
|
/** `[Mutable]` Performs a deep mutable value assignment while retaining internal references. */
|
|
96
108
|
function Mutate(current, next) {
|
|
97
|
-
(0,
|
|
109
|
+
(0, index_3.Mutate)(current, next);
|
|
98
110
|
}
|
|
@@ -17,9 +17,9 @@ export declare class TypeCheck<T extends TSchema> {
|
|
|
17
17
|
/** Returns true if the value matches the compiled type. */
|
|
18
18
|
Check(value: unknown): value is Static<T>;
|
|
19
19
|
/** Decodes a value or throws if error */
|
|
20
|
-
Decode(value: unknown):
|
|
20
|
+
Decode<R = StaticDecode<T>>(value: unknown): R;
|
|
21
21
|
/** Encodes a value or throws if error */
|
|
22
|
-
Encode(value: unknown):
|
|
22
|
+
Encode<R = StaticEncode<T>>(value: unknown): R;
|
|
23
23
|
}
|
|
24
24
|
export declare class TypeCompilerUnknownTypeError extends TypeBoxError {
|
|
25
25
|
readonly schema: TSchema;
|
|
@@ -44,7 +44,7 @@ export class TypeCheck {
|
|
|
44
44
|
Decode(value) {
|
|
45
45
|
if (!this.checkFunc(value))
|
|
46
46
|
throw new TransformDecodeCheckError(this.schema, value, this.Errors(value).First());
|
|
47
|
-
return this.hasTransform ? TransformDecode(this.schema, this.references, value) : value;
|
|
47
|
+
return (this.hasTransform ? TransformDecode(this.schema, this.references, value) : value);
|
|
48
48
|
}
|
|
49
49
|
/** Encodes a value or throws if error */
|
|
50
50
|
Encode(value) {
|
|
@@ -16,14 +16,14 @@ export declare namespace TypeSystemPolicy {
|
|
|
16
16
|
let AllowNaN: boolean;
|
|
17
17
|
/** Sets whether `null` should validate for void types. The default is `false` */
|
|
18
18
|
let AllowNullVoid: boolean;
|
|
19
|
-
/**
|
|
19
|
+
/** Checks this value using the ExactOptionalPropertyTypes policy */
|
|
20
20
|
function IsExactOptionalProperty(value: Record<keyof any, unknown>, key: string): boolean;
|
|
21
|
-
/**
|
|
21
|
+
/** Checks this value using the AllowArrayObjects policy */
|
|
22
22
|
function IsObjectLike(value: unknown): value is Record<keyof any, unknown>;
|
|
23
|
-
/**
|
|
23
|
+
/** Checks this value as a record using the AllowArrayObjects policy */
|
|
24
24
|
function IsRecordLike(value: unknown): value is Record<keyof any, unknown>;
|
|
25
|
-
/**
|
|
25
|
+
/** Checks this value using the AllowNaN policy */
|
|
26
26
|
function IsNumberLike(value: unknown): value is number;
|
|
27
|
-
/**
|
|
27
|
+
/** Checks this value using the AllowVoidNull policy */
|
|
28
28
|
function IsVoidLike(value: unknown): value is void;
|
|
29
29
|
}
|
|
@@ -24,28 +24,28 @@ export var TypeSystemPolicy;
|
|
|
24
24
|
TypeSystemPolicy.AllowNaN = false;
|
|
25
25
|
/** Sets whether `null` should validate for void types. The default is `false` */
|
|
26
26
|
TypeSystemPolicy.AllowNullVoid = false;
|
|
27
|
-
/**
|
|
27
|
+
/** Checks this value using the ExactOptionalPropertyTypes policy */
|
|
28
28
|
function IsExactOptionalProperty(value, key) {
|
|
29
29
|
return TypeSystemPolicy.ExactOptionalPropertyTypes ? key in value : value[key] !== undefined;
|
|
30
30
|
}
|
|
31
31
|
TypeSystemPolicy.IsExactOptionalProperty = IsExactOptionalProperty;
|
|
32
|
-
/**
|
|
32
|
+
/** Checks this value using the AllowArrayObjects policy */
|
|
33
33
|
function IsObjectLike(value) {
|
|
34
34
|
const isObject = IsObject(value);
|
|
35
35
|
return TypeSystemPolicy.AllowArrayObject ? isObject : isObject && !IsArray(value);
|
|
36
36
|
}
|
|
37
37
|
TypeSystemPolicy.IsObjectLike = IsObjectLike;
|
|
38
|
-
/**
|
|
38
|
+
/** Checks this value as a record using the AllowArrayObjects policy */
|
|
39
39
|
function IsRecordLike(value) {
|
|
40
40
|
return IsObjectLike(value) && !(value instanceof Date) && !(value instanceof Uint8Array);
|
|
41
41
|
}
|
|
42
42
|
TypeSystemPolicy.IsRecordLike = IsRecordLike;
|
|
43
|
-
/**
|
|
43
|
+
/** Checks this value using the AllowNaN policy */
|
|
44
44
|
function IsNumberLike(value) {
|
|
45
45
|
return TypeSystemPolicy.AllowNaN ? IsNumber(value) : Number.isFinite(value);
|
|
46
46
|
}
|
|
47
47
|
TypeSystemPolicy.IsNumberLike = IsNumberLike;
|
|
48
|
-
/**
|
|
48
|
+
/** Checks this value using the AllowVoidNull policy */
|
|
49
49
|
function IsVoidLike(value) {
|
|
50
50
|
const isUndefined = IsUndefined(value);
|
|
51
51
|
return TypeSystemPolicy.AllowNullVoid ? isUndefined || value === null : isUndefined;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ValueErrorIterator, ValueError } from '../../errors/index.mjs';
|
|
2
|
+
import { TypeBoxError } from '../../type/error/error.mjs';
|
|
3
|
+
import { TSchema } from '../../type/schema/index.mjs';
|
|
4
|
+
import { Static } from '../../type/static/index.mjs';
|
|
5
|
+
export declare class AssertError extends TypeBoxError {
|
|
6
|
+
#private;
|
|
7
|
+
error: ValueError | undefined;
|
|
8
|
+
constructor(iterator: ValueErrorIterator);
|
|
9
|
+
/** Returns an iterator for each error in this value. */
|
|
10
|
+
Errors(): ValueErrorIterator;
|
|
11
|
+
}
|
|
12
|
+
/** Asserts a value matches the given type or throws an `AssertError` if invalid */
|
|
13
|
+
export declare function Assert<T extends TSchema>(schema: T, references: TSchema[], value: unknown): asserts value is Static<T>;
|
|
14
|
+
/** Asserts a value matches the given type or throws an `AssertError` if invalid */
|
|
15
|
+
export declare function Assert<T extends TSchema>(schema: T, value: unknown): asserts value is Static<T>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _AssertError_instances, _AssertError_iterator, _AssertError_Iterator;
|
|
13
|
+
import { Errors, ValueErrorIterator } from '../../errors/index.mjs';
|
|
14
|
+
import { TypeBoxError } from '../../type/error/error.mjs';
|
|
15
|
+
import { Check } from '../check/check.mjs';
|
|
16
|
+
// ------------------------------------------------------------------
|
|
17
|
+
// AssertError
|
|
18
|
+
// ------------------------------------------------------------------
|
|
19
|
+
export class AssertError extends TypeBoxError {
|
|
20
|
+
constructor(iterator) {
|
|
21
|
+
const error = iterator.First();
|
|
22
|
+
super(error === undefined ? 'Invalid Value' : error.message);
|
|
23
|
+
_AssertError_instances.add(this);
|
|
24
|
+
_AssertError_iterator.set(this, void 0);
|
|
25
|
+
__classPrivateFieldSet(this, _AssertError_iterator, iterator, "f");
|
|
26
|
+
this.error = error;
|
|
27
|
+
}
|
|
28
|
+
/** Returns an iterator for each error in this value. */
|
|
29
|
+
Errors() {
|
|
30
|
+
return new ValueErrorIterator(__classPrivateFieldGet(this, _AssertError_instances, "m", _AssertError_Iterator).call(this));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
_AssertError_iterator = new WeakMap(), _AssertError_instances = new WeakSet(), _AssertError_Iterator = function* _AssertError_Iterator() {
|
|
34
|
+
if (this.error)
|
|
35
|
+
yield this.error;
|
|
36
|
+
yield* __classPrivateFieldGet(this, _AssertError_iterator, "f");
|
|
37
|
+
};
|
|
38
|
+
// ------------------------------------------------------------------
|
|
39
|
+
// AssertValue
|
|
40
|
+
// ------------------------------------------------------------------
|
|
41
|
+
function AssertValue(schema, references, value) {
|
|
42
|
+
if (Check(schema, references, value))
|
|
43
|
+
return;
|
|
44
|
+
throw new AssertError(Errors(schema, references, value));
|
|
45
|
+
}
|
|
46
|
+
/** Asserts a value matches the given type or throws an `AssertError` if invalid */
|
|
47
|
+
export function Assert(...args) {
|
|
48
|
+
return args.length === 3 ? AssertValue(args[0], args[1], args[2]) : AssertValue(args[0], [], args[1]);
|
|
49
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './assert.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './assert.mjs';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IsObject, IsArray, IsString, IsNumber, IsNull } from '../guard/index.mjs';
|
|
2
2
|
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
3
3
|
import { Kind } from '../../type/symbols/index.mjs';
|
|
4
4
|
import { Create } from '../create/index.mjs';
|
|
@@ -100,7 +100,7 @@ function FromConstructor(schema, references, value) {
|
|
|
100
100
|
}
|
|
101
101
|
function FromIntersect(schema, references, value) {
|
|
102
102
|
const created = Create(schema, references);
|
|
103
|
-
const mapped =
|
|
103
|
+
const mapped = IsObject(created) && IsObject(value) ? { ...created, ...value } : value;
|
|
104
104
|
return Check(schema, references, mapped) ? mapped : Create(schema, references);
|
|
105
105
|
}
|
|
106
106
|
function FromNever(schema, references, value) {
|
|
@@ -7,17 +7,17 @@ import { Kind } from '../../type/symbols/index.mjs';
|
|
|
7
7
|
// ValueGuard
|
|
8
8
|
// ------------------------------------------------------------------
|
|
9
9
|
// prettier-ignore
|
|
10
|
-
import { IsString, IsObject, IsArray, IsUndefined } from '../guard/index.mjs';
|
|
10
|
+
import { HasPropertyKey, IsString, IsObject, IsArray, IsUndefined } from '../guard/index.mjs';
|
|
11
11
|
// ------------------------------------------------------------------
|
|
12
12
|
// TypeGuard
|
|
13
13
|
// ------------------------------------------------------------------
|
|
14
14
|
// prettier-ignore
|
|
15
|
-
import {
|
|
15
|
+
import { IsKind } from '../../type/guard/kind.mjs';
|
|
16
16
|
// ------------------------------------------------------------------
|
|
17
17
|
// IsCheckable
|
|
18
18
|
// ------------------------------------------------------------------
|
|
19
19
|
function IsCheckable(schema) {
|
|
20
|
-
return
|
|
20
|
+
return IsKind(schema) && schema[Kind] !== 'Unsafe';
|
|
21
21
|
}
|
|
22
22
|
// ------------------------------------------------------------------
|
|
23
23
|
// Types
|
|
@@ -31,7 +31,7 @@ function FromIntersect(schema, references, value) {
|
|
|
31
31
|
const unevaluatedProperties = schema.unevaluatedProperties;
|
|
32
32
|
const intersections = schema.allOf.map((schema) => Visit(schema, references, Clone(value)));
|
|
33
33
|
const composite = intersections.reduce((acc, value) => (IsObject(value) ? { ...acc, ...value } : value), {});
|
|
34
|
-
if (!IsObject(value) || !IsObject(composite) || !
|
|
34
|
+
if (!IsObject(value) || !IsObject(composite) || !IsKind(unevaluatedProperties))
|
|
35
35
|
return composite;
|
|
36
36
|
const knownkeys = KeyOfPropertyKeys(schema);
|
|
37
37
|
for (const key of Object.getOwnPropertyNames(value)) {
|
|
@@ -48,11 +48,11 @@ function FromObject(schema, references, value) {
|
|
|
48
48
|
return value; // Check IsArray for AllowArrayObject configuration
|
|
49
49
|
const additionalProperties = schema.additionalProperties;
|
|
50
50
|
for (const key of Object.getOwnPropertyNames(value)) {
|
|
51
|
-
if (
|
|
51
|
+
if (HasPropertyKey(schema.properties, key)) {
|
|
52
52
|
value[key] = Visit(schema.properties[key], references, value[key]);
|
|
53
53
|
continue;
|
|
54
54
|
}
|
|
55
|
-
if (
|
|
55
|
+
if (IsKind(additionalProperties) && Check(additionalProperties, references, value[key])) {
|
|
56
56
|
value[key] = Visit(additionalProperties, references, value[key]);
|
|
57
57
|
continue;
|
|
58
58
|
}
|
|
@@ -72,7 +72,7 @@ function FromRecord(schema, references, value) {
|
|
|
72
72
|
value[key] = Visit(propertySchema, references, value[key]);
|
|
73
73
|
continue;
|
|
74
74
|
}
|
|
75
|
-
if (
|
|
75
|
+
if (IsKind(additionalProperties) && Check(additionalProperties, references, value[key])) {
|
|
76
76
|
value[key] = Visit(additionalProperties, references, value[key]);
|
|
77
77
|
continue;
|
|
78
78
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// ------------------------------------------------------------------
|
|
2
2
|
// ValueGuard
|
|
3
3
|
// ------------------------------------------------------------------
|
|
4
|
-
import { IsArray, IsDate,
|
|
4
|
+
import { IsArray, IsDate, IsMap, IsSet, IsObject, IsTypedArray, IsValueType } from '../guard/index.mjs';
|
|
5
5
|
// ------------------------------------------------------------------
|
|
6
6
|
// Clonable
|
|
7
7
|
// ------------------------------------------------------------------
|
|
8
|
-
function
|
|
8
|
+
function FromObject(value) {
|
|
9
9
|
const Acc = {};
|
|
10
10
|
for (const key of Object.getOwnPropertyNames(value)) {
|
|
11
11
|
Acc[key] = Clone(value[key]);
|
|
@@ -15,16 +15,22 @@ function ObjectType(value) {
|
|
|
15
15
|
}
|
|
16
16
|
return Acc;
|
|
17
17
|
}
|
|
18
|
-
function
|
|
18
|
+
function FromArray(value) {
|
|
19
19
|
return value.map((element) => Clone(element));
|
|
20
20
|
}
|
|
21
|
-
function
|
|
21
|
+
function FromTypedArray(value) {
|
|
22
22
|
return value.slice();
|
|
23
23
|
}
|
|
24
|
-
function
|
|
24
|
+
function FromMap(value) {
|
|
25
|
+
return new Map(Clone([...value.entries()]));
|
|
26
|
+
}
|
|
27
|
+
function FromSet(value) {
|
|
28
|
+
return new Set(Clone([...value.entries()]));
|
|
29
|
+
}
|
|
30
|
+
function FromDate(value) {
|
|
25
31
|
return new Date(value.toISOString());
|
|
26
32
|
}
|
|
27
|
-
function
|
|
33
|
+
function FromValue(value) {
|
|
28
34
|
return value;
|
|
29
35
|
}
|
|
30
36
|
// ------------------------------------------------------------------
|
|
@@ -33,14 +39,18 @@ function ValueType(value) {
|
|
|
33
39
|
/** Returns a clone of the given value */
|
|
34
40
|
export function Clone(value) {
|
|
35
41
|
if (IsArray(value))
|
|
36
|
-
return
|
|
42
|
+
return FromArray(value);
|
|
37
43
|
if (IsDate(value))
|
|
38
|
-
return
|
|
39
|
-
if (IsStandardObject(value))
|
|
40
|
-
return ObjectType(value);
|
|
44
|
+
return FromDate(value);
|
|
41
45
|
if (IsTypedArray(value))
|
|
42
|
-
return
|
|
46
|
+
return FromTypedArray(value);
|
|
47
|
+
if (IsMap(value))
|
|
48
|
+
return FromMap(value);
|
|
49
|
+
if (IsSet(value))
|
|
50
|
+
return FromSet(value);
|
|
51
|
+
if (IsObject(value))
|
|
52
|
+
return FromObject(value);
|
|
43
53
|
if (IsValueType(value))
|
|
44
|
-
return
|
|
54
|
+
return FromValue(value);
|
|
45
55
|
throw new Error('ValueClone: Unable to clone value');
|
|
46
56
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { TSchema } from '../../type/schema/index.mjs';
|
|
2
|
-
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
2
|
+
/** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
3
3
|
export declare function Convert(schema: TSchema, references: TSchema[], value: unknown): unknown;
|
|
4
|
-
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
4
|
+
/** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
5
5
|
export declare function Convert(schema: TSchema, value: unknown): unknown;
|