@sinclair/typebox 0.33.3 → 0.33.4
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/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 +15 -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 +8 -4
- package/build/cjs/value/transform/encode.js +9 -5
- 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/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 +16 -19
- 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 +9 -5
- package/build/esm/value/transform/encode.mjs +10 -6
- 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 +114 -130
|
@@ -9,7 +9,7 @@ import { IsString, IsObject, IsArray, IsUndefined } from '../guard/index.mjs';
|
|
|
9
9
|
// ------------------------------------------------------------------
|
|
10
10
|
// TypeGuard
|
|
11
11
|
// ------------------------------------------------------------------
|
|
12
|
-
import {
|
|
12
|
+
import { IsKind } from '../../type/guard/kind.mjs';
|
|
13
13
|
// ------------------------------------------------------------------
|
|
14
14
|
// ValueOrDefault
|
|
15
15
|
// ------------------------------------------------------------------
|
|
@@ -17,16 +17,10 @@ function ValueOrDefault(schema, value) {
|
|
|
17
17
|
return value === undefined && 'default' in schema ? Clone(schema.default) : value;
|
|
18
18
|
}
|
|
19
19
|
// ------------------------------------------------------------------
|
|
20
|
-
//
|
|
20
|
+
// HasDefaultProperty
|
|
21
21
|
// ------------------------------------------------------------------
|
|
22
|
-
function
|
|
23
|
-
return
|
|
24
|
-
}
|
|
25
|
-
// ------------------------------------------------------------------
|
|
26
|
-
// IsDefaultSchema
|
|
27
|
-
// ------------------------------------------------------------------
|
|
28
|
-
function IsDefaultSchema(value) {
|
|
29
|
-
return IsSchema(value) && 'default' in value;
|
|
22
|
+
function HasDefaultProperty(schema) {
|
|
23
|
+
return IsKind(schema) && 'default' in schema;
|
|
30
24
|
}
|
|
31
25
|
// ------------------------------------------------------------------
|
|
32
26
|
// Types
|
|
@@ -55,12 +49,12 @@ function FromObject(schema, references, value) {
|
|
|
55
49
|
const knownPropertyKeys = Object.getOwnPropertyNames(schema.properties);
|
|
56
50
|
// properties
|
|
57
51
|
for (const key of knownPropertyKeys) {
|
|
58
|
-
if (!
|
|
52
|
+
if (!HasDefaultProperty(schema.properties[key]))
|
|
59
53
|
continue;
|
|
60
54
|
defaulted[key] = Visit(schema.properties[key], references, defaulted[key]);
|
|
61
55
|
}
|
|
62
56
|
// return if not additional properties
|
|
63
|
-
if (!
|
|
57
|
+
if (!HasDefaultProperty(additionalPropertiesSchema))
|
|
64
58
|
return defaulted;
|
|
65
59
|
// additional properties
|
|
66
60
|
for (const key of Object.getOwnPropertyNames(defaulted)) {
|
|
@@ -79,12 +73,12 @@ function FromRecord(schema, references, value) {
|
|
|
79
73
|
const knownPropertyKey = new RegExp(propertyKeyPattern);
|
|
80
74
|
// properties
|
|
81
75
|
for (const key of Object.getOwnPropertyNames(defaulted)) {
|
|
82
|
-
if (!(knownPropertyKey.test(key) &&
|
|
76
|
+
if (!(knownPropertyKey.test(key) && HasDefaultProperty(propertySchema)))
|
|
83
77
|
continue;
|
|
84
78
|
defaulted[key] = Visit(propertySchema, references, defaulted[key]);
|
|
85
79
|
}
|
|
86
80
|
// return if not additional properties
|
|
87
|
-
if (!
|
|
81
|
+
if (!HasDefaultProperty(additionalPropertiesSchema))
|
|
88
82
|
return defaulted;
|
|
89
83
|
// additional properties
|
|
90
84
|
for (const key of Object.getOwnPropertyNames(defaulted)) {
|
|
@@ -115,14 +109,18 @@ function FromUnion(schema, references, value) {
|
|
|
115
109
|
const defaulted = ValueOrDefault(schema, value);
|
|
116
110
|
for (const inner of schema.anyOf) {
|
|
117
111
|
const result = Visit(inner, references, defaulted);
|
|
118
|
-
if (
|
|
112
|
+
if (Check(inner, result)) {
|
|
119
113
|
return result;
|
|
120
114
|
}
|
|
121
115
|
}
|
|
122
116
|
return defaulted;
|
|
123
117
|
}
|
|
118
|
+
function AddReference(references, schema) {
|
|
119
|
+
references.push(schema);
|
|
120
|
+
return references;
|
|
121
|
+
}
|
|
124
122
|
function Visit(schema, references, value) {
|
|
125
|
-
const references_ = IsString(schema.$id) ?
|
|
123
|
+
const references_ = IsString(schema.$id) ? AddReference(references, schema) : references;
|
|
126
124
|
const schema_ = schema;
|
|
127
125
|
switch (schema_[Kind]) {
|
|
128
126
|
case 'Array':
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IsObject, IsDate, IsArray, IsTypedArray, IsValueType } from '../guard/index.mjs';
|
|
2
2
|
// ------------------------------------------------------------------
|
|
3
3
|
// Equality Checks
|
|
4
4
|
// ------------------------------------------------------------------
|
|
5
5
|
function ObjectType(left, right) {
|
|
6
|
-
if (!
|
|
6
|
+
if (!IsObject(right))
|
|
7
7
|
return false;
|
|
8
8
|
const leftKeys = [...Object.keys(left), ...Object.getOwnPropertySymbols(left)];
|
|
9
9
|
const rightKeys = [...Object.keys(right), ...Object.getOwnPropertySymbols(right)];
|
|
@@ -32,14 +32,14 @@ function ValueType(left, right) {
|
|
|
32
32
|
// ------------------------------------------------------------------
|
|
33
33
|
/** Returns true if the left value deep-equals the right */
|
|
34
34
|
export function Equal(left, right) {
|
|
35
|
-
if (IsStandardObject(left))
|
|
36
|
-
return ObjectType(left, right);
|
|
37
35
|
if (IsDate(left))
|
|
38
36
|
return DateType(left, right);
|
|
39
37
|
if (IsTypedArray(left))
|
|
40
38
|
return TypedArrayType(left, right);
|
|
41
39
|
if (IsArray(left))
|
|
42
40
|
return ArrayType(left, right);
|
|
41
|
+
if (IsObject(left))
|
|
42
|
+
return ObjectType(left, right);
|
|
43
43
|
if (IsValueType(left))
|
|
44
44
|
return ValueType(left, right);
|
|
45
45
|
throw new Error('ValueEquals: Unable to compare value');
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IsArray, IsBoolean, IsBigInt, IsDate, IsNull, IsNumber,
|
|
1
|
+
import { IsArray, IsBoolean, IsBigInt, IsDate, IsNull, IsNumber, IsObject, IsString, IsSymbol, IsUint8Array, IsUndefined } from '../guard/index.mjs';
|
|
2
2
|
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
3
3
|
// ------------------------------------------------------------------
|
|
4
4
|
// Errors
|
|
@@ -119,7 +119,7 @@ function Visit(value) {
|
|
|
119
119
|
return NullType(value);
|
|
120
120
|
if (IsNumber(value))
|
|
121
121
|
return NumberType(value);
|
|
122
|
-
if (
|
|
122
|
+
if (IsObject(value))
|
|
123
123
|
return ObjectType(value);
|
|
124
124
|
if (IsString(value))
|
|
125
125
|
return StringType(value);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { ValueError, ValueErrorType, ValueErrorIterator } from '../errors/index.mjs';
|
|
2
2
|
export * from './guard/index.mjs';
|
|
3
|
+
export * from './assert/index.mjs';
|
|
3
4
|
export * from './cast/index.mjs';
|
|
4
5
|
export * from './check/index.mjs';
|
|
5
6
|
export * from './clean/index.mjs';
|
|
@@ -11,6 +12,7 @@ export * from './delta/index.mjs';
|
|
|
11
12
|
export * from './equal/index.mjs';
|
|
12
13
|
export * from './hash/index.mjs';
|
|
13
14
|
export * from './mutate/index.mjs';
|
|
15
|
+
export * from './parse/index.mjs';
|
|
14
16
|
export * from './pointer/index.mjs';
|
|
15
17
|
export * from './transform/index.mjs';
|
|
16
18
|
export { Value } from './value/index.mjs';
|
|
@@ -9,6 +9,7 @@ export * from './guard/index.mjs';
|
|
|
9
9
|
// ------------------------------------------------------------------
|
|
10
10
|
// Operators
|
|
11
11
|
// ------------------------------------------------------------------
|
|
12
|
+
export * from './assert/index.mjs';
|
|
12
13
|
export * from './cast/index.mjs';
|
|
13
14
|
export * from './check/index.mjs';
|
|
14
15
|
export * from './clean/index.mjs';
|
|
@@ -20,6 +21,7 @@ export * from './delta/index.mjs';
|
|
|
20
21
|
export * from './equal/index.mjs';
|
|
21
22
|
export * from './hash/index.mjs';
|
|
22
23
|
export * from './mutate/index.mjs';
|
|
24
|
+
export * from './parse/index.mjs';
|
|
23
25
|
export * from './pointer/index.mjs';
|
|
24
26
|
export * from './transform/index.mjs';
|
|
25
27
|
// ------------------------------------------------------------------
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IsObject, IsArray, IsTypedArray, IsValueType } from '../guard/index.mjs';
|
|
2
2
|
import { ValuePointer } from '../pointer/index.mjs';
|
|
3
3
|
import { Clone } from '../clone/index.mjs';
|
|
4
4
|
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
@@ -11,7 +11,7 @@ export class ValueMutateError extends TypeBoxError {
|
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
13
|
function ObjectType(root, path, current, next) {
|
|
14
|
-
if (!
|
|
14
|
+
if (!IsObject(current)) {
|
|
15
15
|
ValuePointer.Set(root, path, Clone(next));
|
|
16
16
|
}
|
|
17
17
|
else {
|
|
@@ -63,7 +63,7 @@ function Visit(root, path, current, next) {
|
|
|
63
63
|
return ArrayType(root, path, current, next);
|
|
64
64
|
if (IsTypedArray(next))
|
|
65
65
|
return TypedArrayType(root, path, current, next);
|
|
66
|
-
if (
|
|
66
|
+
if (IsObject(next))
|
|
67
67
|
return ObjectType(root, path, current, next);
|
|
68
68
|
if (IsValueType(next))
|
|
69
69
|
return ValueType(root, path, current, next);
|
|
@@ -76,8 +76,8 @@ function IsNonMutableValue(value) {
|
|
|
76
76
|
}
|
|
77
77
|
function IsMismatchedValue(current, next) {
|
|
78
78
|
// prettier-ignore
|
|
79
|
-
return ((
|
|
80
|
-
(IsArray(current) &&
|
|
79
|
+
return ((IsObject(current) && IsArray(next)) ||
|
|
80
|
+
(IsArray(current) && IsObject(next)));
|
|
81
81
|
}
|
|
82
82
|
// ------------------------------------------------------------------
|
|
83
83
|
// Mutate
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './parse.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './parse.mjs';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { TSchema } from '../../type/schema/index.mjs';
|
|
2
|
+
import { StaticDecode } from '../../type/static/index.mjs';
|
|
3
|
+
/** Parses a value or throws an `AssertError` if invalid. */
|
|
4
|
+
export declare function Parse<T extends TSchema, R = StaticDecode<T>>(schema: T, references: TSchema[], value: unknown): R;
|
|
5
|
+
/** Parses a value or throws an `AssertError` if invalid. */
|
|
6
|
+
export declare function Parse<T extends TSchema, R = StaticDecode<T>>(schema: T, value: unknown): R;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { TransformDecode, HasTransform } from '../transform/index.mjs';
|
|
2
|
+
import { Assert } from '../assert/assert.mjs';
|
|
3
|
+
import { Default } from '../default/default.mjs';
|
|
4
|
+
import { Convert } from '../convert/convert.mjs';
|
|
5
|
+
import { Clean } from '../clean/clean.mjs';
|
|
6
|
+
import { Clone } from '../clone/index.mjs';
|
|
7
|
+
// prettier-ignore
|
|
8
|
+
const ParseReducer = [
|
|
9
|
+
(_schema, _references, value) => Clone(value),
|
|
10
|
+
(schema, references, value) => Default(schema, references, value),
|
|
11
|
+
(schema, references, value) => Clean(schema, references, value),
|
|
12
|
+
(schema, references, value) => Convert(schema, references, value),
|
|
13
|
+
(schema, references, value) => { Assert(schema, references, value); return value; },
|
|
14
|
+
(schema, references, value) => (HasTransform(schema, references) ? TransformDecode(schema, references, value) : value),
|
|
15
|
+
];
|
|
16
|
+
// ------------------------------------------------------------------
|
|
17
|
+
// ParseValue
|
|
18
|
+
// ------------------------------------------------------------------
|
|
19
|
+
function ParseValue(schema, references, value) {
|
|
20
|
+
return ParseReducer.reduce((value, reducer) => reducer(schema, references, value), value);
|
|
21
|
+
}
|
|
22
|
+
/** Parses a value or throws an `AssertError` if invalid. */
|
|
23
|
+
export function Parse(...args) {
|
|
24
|
+
return args.length === 3 ? ParseValue(args[0], args[1], args[2]) : ParseValue(args[0], [], args[1]);
|
|
25
|
+
}
|
|
@@ -6,7 +6,7 @@ import { Check } from '../check/index.mjs';
|
|
|
6
6
|
// ------------------------------------------------------------------
|
|
7
7
|
// ValueGuard
|
|
8
8
|
// ------------------------------------------------------------------
|
|
9
|
-
import {
|
|
9
|
+
import { IsObject, IsArray, IsValueType } from '../guard/index.mjs';
|
|
10
10
|
// ------------------------------------------------------------------
|
|
11
11
|
// TypeGuard
|
|
12
12
|
// ------------------------------------------------------------------
|
|
@@ -54,7 +54,7 @@ function FromArray(schema, references, path, value) {
|
|
|
54
54
|
}
|
|
55
55
|
// prettier-ignore
|
|
56
56
|
function FromIntersect(schema, references, path, value) {
|
|
57
|
-
if (!
|
|
57
|
+
if (!IsObject(value) || IsValueType(value))
|
|
58
58
|
return Default(schema, path, value);
|
|
59
59
|
const knownEntries = KeyOfPropertyEntries(schema);
|
|
60
60
|
const knownKeys = knownEntries.map(entry => entry[0]);
|
|
@@ -80,7 +80,7 @@ function FromNot(schema, references, path, value) {
|
|
|
80
80
|
}
|
|
81
81
|
// prettier-ignore
|
|
82
82
|
function FromObject(schema, references, path, value) {
|
|
83
|
-
if (!
|
|
83
|
+
if (!IsObject(value))
|
|
84
84
|
return Default(schema, path, value);
|
|
85
85
|
const knownKeys = KeyOfPropertyKeys(schema);
|
|
86
86
|
const knownProperties = { ...value };
|
|
@@ -102,7 +102,7 @@ function FromObject(schema, references, path, value) {
|
|
|
102
102
|
}
|
|
103
103
|
// prettier-ignore
|
|
104
104
|
function FromRecord(schema, references, path, value) {
|
|
105
|
-
if (!
|
|
105
|
+
if (!IsObject(value))
|
|
106
106
|
return Default(schema, path, value);
|
|
107
107
|
const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0];
|
|
108
108
|
const knownKeys = new RegExp(pattern);
|
|
@@ -150,9 +150,13 @@ function FromUnion(schema, references, path, value) {
|
|
|
150
150
|
}
|
|
151
151
|
return Default(schema, path, value);
|
|
152
152
|
}
|
|
153
|
+
function AddReference(references, schema) {
|
|
154
|
+
references.push(schema);
|
|
155
|
+
return references;
|
|
156
|
+
}
|
|
153
157
|
// prettier-ignore
|
|
154
158
|
function Visit(schema, references, path, value) {
|
|
155
|
-
const references_ = typeof schema.$id === 'string' ?
|
|
159
|
+
const references_ = typeof schema.$id === 'string' ? AddReference(references, schema) : references;
|
|
156
160
|
const schema_ = schema;
|
|
157
161
|
switch (schema[Kind]) {
|
|
158
162
|
case 'Array':
|
|
@@ -6,7 +6,7 @@ import { Check } from '../check/index.mjs';
|
|
|
6
6
|
// ------------------------------------------------------------------
|
|
7
7
|
// ValueGuard
|
|
8
8
|
// ------------------------------------------------------------------
|
|
9
|
-
import {
|
|
9
|
+
import { IsObject, IsArray, IsValueType } from '../guard/index.mjs';
|
|
10
10
|
// ------------------------------------------------------------------
|
|
11
11
|
// TypeGuard
|
|
12
12
|
// ------------------------------------------------------------------
|
|
@@ -55,7 +55,7 @@ function FromArray(schema, references, path, value) {
|
|
|
55
55
|
// prettier-ignore
|
|
56
56
|
function FromIntersect(schema, references, path, value) {
|
|
57
57
|
const defaulted = Default(schema, path, value);
|
|
58
|
-
if (!
|
|
58
|
+
if (!IsObject(value) || IsValueType(value))
|
|
59
59
|
return defaulted;
|
|
60
60
|
const knownEntries = KeyOfPropertyEntries(schema);
|
|
61
61
|
const knownKeys = knownEntries.map(entry => entry[0]);
|
|
@@ -83,7 +83,7 @@ function FromNot(schema, references, path, value) {
|
|
|
83
83
|
// prettier-ignore
|
|
84
84
|
function FromObject(schema, references, path, value) {
|
|
85
85
|
const defaulted = Default(schema, path, value);
|
|
86
|
-
if (!
|
|
86
|
+
if (!IsObject(defaulted))
|
|
87
87
|
return defaulted;
|
|
88
88
|
const knownKeys = KeyOfPropertyKeys(schema);
|
|
89
89
|
const knownProperties = { ...defaulted };
|
|
@@ -106,7 +106,7 @@ function FromObject(schema, references, path, value) {
|
|
|
106
106
|
// prettier-ignore
|
|
107
107
|
function FromRecord(schema, references, path, value) {
|
|
108
108
|
const defaulted = Default(schema, path, value);
|
|
109
|
-
if (!
|
|
109
|
+
if (!IsObject(value))
|
|
110
110
|
return defaulted;
|
|
111
111
|
const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0];
|
|
112
112
|
const knownKeys = new RegExp(pattern);
|
|
@@ -116,7 +116,7 @@ function FromRecord(schema, references, path, value) {
|
|
|
116
116
|
knownProperties[key] = Visit(schema.patternProperties[pattern], references, `${path}/${key}`, knownProperties[key]);
|
|
117
117
|
}
|
|
118
118
|
if (!IsSchema(schema.additionalProperties)) {
|
|
119
|
-
return
|
|
119
|
+
return knownProperties;
|
|
120
120
|
}
|
|
121
121
|
const unknownKeys = Object.getOwnPropertyNames(knownProperties);
|
|
122
122
|
const additionalProperties = schema.additionalProperties;
|
|
@@ -162,9 +162,13 @@ function FromUnion(schema, references, path, value) {
|
|
|
162
162
|
}
|
|
163
163
|
return Default(schema, path, value);
|
|
164
164
|
}
|
|
165
|
+
function AddReference(references, schema) {
|
|
166
|
+
references.push(schema);
|
|
167
|
+
return references;
|
|
168
|
+
}
|
|
165
169
|
// prettier-ignore
|
|
166
170
|
function Visit(schema, references, path, value) {
|
|
167
|
-
const references_ = typeof schema.$id === 'string' ?
|
|
171
|
+
const references_ = typeof schema.$id === 'string' ? AddReference(references, schema) : references;
|
|
168
172
|
const schema_ = schema;
|
|
169
173
|
switch (schema[Kind]) {
|
|
170
174
|
case 'Array':
|
|
@@ -72,9 +72,13 @@ function FromTuple(schema, references) {
|
|
|
72
72
|
function FromUnion(schema, references) {
|
|
73
73
|
return IsTransform(schema) || schema.anyOf.some((schema) => Visit(schema, references));
|
|
74
74
|
}
|
|
75
|
+
function AddReference(references, schema) {
|
|
76
|
+
references.push(schema);
|
|
77
|
+
return references;
|
|
78
|
+
}
|
|
75
79
|
// prettier-ignore
|
|
76
80
|
function Visit(schema, references) {
|
|
77
|
-
const references_ = IsString(schema.$id) ?
|
|
81
|
+
const references_ = IsString(schema.$id) ? AddReference(references, schema) : references;
|
|
78
82
|
const schema_ = schema;
|
|
79
83
|
if (schema.$id && visited.has(schema.$id))
|
|
80
84
|
return false;
|
|
@@ -3,6 +3,10 @@ import { Edit } from '../delta/index.mjs';
|
|
|
3
3
|
import { ValueErrorIterator } from '../../errors/index.mjs';
|
|
4
4
|
import type { TSchema } from '../../type/schema/index.mjs';
|
|
5
5
|
import type { Static, StaticDecode, StaticEncode } from '../../type/static/index.mjs';
|
|
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,4 +1,5 @@
|
|
|
1
1
|
import { HasTransform, TransformDecode, TransformEncode, TransformDecodeCheckError, TransformEncodeCheckError } from '../transform/index.mjs';
|
|
2
|
+
import { Assert as AssertValue } from '../assert/index.mjs';
|
|
2
3
|
import { Mutate as MutateValue } from '../mutate/index.mjs';
|
|
3
4
|
import { Hash as HashValue } from '../hash/index.mjs';
|
|
4
5
|
import { Equal as EqualValue } from '../equal/index.mjs';
|
|
@@ -8,9 +9,14 @@ import { Convert as ConvertValue } from '../convert/index.mjs';
|
|
|
8
9
|
import { Create as CreateValue } from '../create/index.mjs';
|
|
9
10
|
import { Clean as CleanValue } from '../clean/index.mjs';
|
|
10
11
|
import { Check as CheckValue } from '../check/index.mjs';
|
|
12
|
+
import { Parse as ParseValue } from '../parse/index.mjs';
|
|
11
13
|
import { Default as DefaultValue } from '../default/index.mjs';
|
|
12
14
|
import { Diff as DiffValue, Patch as PatchValue } from '../delta/index.mjs';
|
|
13
15
|
import { Errors as ValueErrors } from '../../errors/index.mjs';
|
|
16
|
+
/** Asserts a value matches the given type or throws an `AssertError` if invalid. */
|
|
17
|
+
export function Assert(...args) {
|
|
18
|
+
return AssertValue.apply(AssertValue, args);
|
|
19
|
+
}
|
|
14
20
|
/** Casts a value into a given type. The return value will retain as much information of the original value as possible. */
|
|
15
21
|
export function Cast(...args) {
|
|
16
22
|
return CastValue.apply(CastValue, args);
|
|
@@ -27,7 +33,7 @@ export function Check(...args) {
|
|
|
27
33
|
export function Clean(...args) {
|
|
28
34
|
return CleanValue.apply(CleanValue, args);
|
|
29
35
|
}
|
|
30
|
-
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
36
|
+
/** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
31
37
|
export function Convert(...args) {
|
|
32
38
|
return ConvertValue.apply(ConvertValue, args);
|
|
33
39
|
}
|
|
@@ -54,6 +60,10 @@ export function Encode(...args) {
|
|
|
54
60
|
throw new TransformEncodeCheckError(schema, encoded, Errors(schema, references, encoded).First());
|
|
55
61
|
return encoded;
|
|
56
62
|
}
|
|
63
|
+
/** Parses a value or throws an `AssertError` if invalid. */
|
|
64
|
+
export function Parse(...args) {
|
|
65
|
+
return ParseValue.apply(ParseValue, args);
|
|
66
|
+
}
|
|
57
67
|
/** Returns an iterator for each error in this value. */
|
|
58
68
|
export function Errors(...args) {
|
|
59
69
|
return ValueErrors.apply(ValueErrors, args);
|