@sinclair/typebox 0.33.2 → 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/delta/delta.d.ts +1 -5
- package/build/cjs/value/delta/delta.js +37 -33
- 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/delta/delta.d.mts +1 -5
- package/build/esm/value/delta/delta.mjs +36 -31
- 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
|
@@ -5,7 +5,7 @@ import { Kind } from '../../type/symbols/index.mjs';
|
|
|
5
5
|
// ------------------------------------------------------------------
|
|
6
6
|
// ValueGuard
|
|
7
7
|
// ------------------------------------------------------------------
|
|
8
|
-
import { IsArray, IsObject, IsDate, IsUndefined, IsString, IsNumber, IsBoolean, IsBigInt, IsSymbol
|
|
8
|
+
import { IsArray, IsObject, IsDate, IsUndefined, IsString, IsNumber, IsBoolean, IsBigInt, IsSymbol } from '../guard/index.mjs';
|
|
9
9
|
// ------------------------------------------------------------------
|
|
10
10
|
// Conversions
|
|
11
11
|
// ------------------------------------------------------------------
|
|
@@ -56,7 +56,7 @@ function TryConvertLiteral(schema, value) {
|
|
|
56
56
|
return (IsString(schema.const) ? TryConvertLiteralString(value, schema.const) :
|
|
57
57
|
IsNumber(schema.const) ? TryConvertLiteralNumber(value, schema.const) :
|
|
58
58
|
IsBoolean(schema.const) ? TryConvertLiteralBoolean(value, schema.const) :
|
|
59
|
-
|
|
59
|
+
value);
|
|
60
60
|
}
|
|
61
61
|
function TryConvertBoolean(value) {
|
|
62
62
|
return IsValueTrue(value) ? true : IsValueFalse(value) ? false : value;
|
|
@@ -139,16 +139,12 @@ function FromNumber(schema, references, value) {
|
|
|
139
139
|
}
|
|
140
140
|
// prettier-ignore
|
|
141
141
|
function FromObject(schema, references, value) {
|
|
142
|
-
|
|
143
|
-
if (!isConvertable)
|
|
142
|
+
if (!IsObject(value))
|
|
144
143
|
return value;
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
result[key] = HasPropertyKey(schema.properties, key)
|
|
148
|
-
? Visit(schema.properties[key], references, value[key])
|
|
149
|
-
: value[key];
|
|
144
|
+
for (const key of Object.getOwnPropertyNames(schema.properties)) {
|
|
145
|
+
value[key] = Visit(schema.properties[key], references, value[key]);
|
|
150
146
|
}
|
|
151
|
-
return
|
|
147
|
+
return value;
|
|
152
148
|
}
|
|
153
149
|
function FromRecord(schema, references, value) {
|
|
154
150
|
const isConvertable = IsObject(value);
|
|
@@ -156,11 +152,10 @@ function FromRecord(schema, references, value) {
|
|
|
156
152
|
return value;
|
|
157
153
|
const propertyKey = Object.getOwnPropertyNames(schema.patternProperties)[0];
|
|
158
154
|
const property = schema.patternProperties[propertyKey];
|
|
159
|
-
const result = {};
|
|
160
155
|
for (const [propKey, propValue] of Object.entries(value)) {
|
|
161
|
-
|
|
156
|
+
value[propKey] = Visit(property, references, propValue);
|
|
162
157
|
}
|
|
163
|
-
return
|
|
158
|
+
return value;
|
|
164
159
|
}
|
|
165
160
|
function FromRef(schema, references, value) {
|
|
166
161
|
return Visit(Deref(schema, references), references, value);
|
|
@@ -190,15 +185,19 @@ function FromUndefined(schema, references, value) {
|
|
|
190
185
|
}
|
|
191
186
|
function FromUnion(schema, references, value) {
|
|
192
187
|
for (const subschema of schema.anyOf) {
|
|
193
|
-
const converted = Visit(subschema, references, value);
|
|
188
|
+
const converted = Visit(subschema, references, Clone(value));
|
|
194
189
|
if (!Check(subschema, references, converted))
|
|
195
190
|
continue;
|
|
196
191
|
return converted;
|
|
197
192
|
}
|
|
198
193
|
return value;
|
|
199
194
|
}
|
|
195
|
+
function AddReference(references, schema) {
|
|
196
|
+
references.push(schema);
|
|
197
|
+
return references;
|
|
198
|
+
}
|
|
200
199
|
function Visit(schema, references, value) {
|
|
201
|
-
const references_ = IsString(schema.$id) ?
|
|
200
|
+
const references_ = IsString(schema.$id) ? AddReference(references, schema) : references;
|
|
202
201
|
const schema_ = schema;
|
|
203
202
|
switch (schema[Kind]) {
|
|
204
203
|
case 'Array':
|
|
@@ -241,10 +240,8 @@ function Visit(schema, references, value) {
|
|
|
241
240
|
return Default(value);
|
|
242
241
|
}
|
|
243
242
|
}
|
|
244
|
-
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
243
|
+
/** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
245
244
|
// prettier-ignore
|
|
246
245
|
export function Convert(...args) {
|
|
247
|
-
return args.length === 3
|
|
248
|
-
? Visit(args[0], args[1], args[2])
|
|
249
|
-
: Visit(args[0], [], args[1]);
|
|
246
|
+
return args.length === 3 ? Visit(args[0], args[1], args[2]) : Visit(args[0], [], args[1]);
|
|
250
247
|
}
|
|
@@ -380,8 +380,12 @@ function FromKind(schema, references) {
|
|
|
380
380
|
throw new Error('User defined types must specify a default value');
|
|
381
381
|
}
|
|
382
382
|
}
|
|
383
|
+
function AddReference(references, schema) {
|
|
384
|
+
references.push(schema);
|
|
385
|
+
return references;
|
|
386
|
+
}
|
|
383
387
|
function Visit(schema, references) {
|
|
384
|
-
const references_ = IsString(schema.$id) ?
|
|
388
|
+
const references_ = IsString(schema.$id) ? AddReference(references, schema) : references;
|
|
385
389
|
const schema_ = schema;
|
|
386
390
|
switch (schema_[Kind]) {
|
|
387
391
|
case 'Any':
|
|
@@ -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':
|
|
@@ -24,13 +24,9 @@ export declare const Delete: TObject<{
|
|
|
24
24
|
}>;
|
|
25
25
|
export type Edit = Static<typeof Edit>;
|
|
26
26
|
export declare const Edit: TUnion<[typeof Insert, typeof Update, typeof Delete]>;
|
|
27
|
-
export declare class
|
|
27
|
+
export declare class ValueDiffError extends TypeBoxError {
|
|
28
28
|
readonly value: unknown;
|
|
29
29
|
constructor(value: unknown, message: string);
|
|
30
30
|
}
|
|
31
|
-
export declare class ValueDeltaSymbolError extends ValueDeltaError {
|
|
32
|
-
readonly value: unknown;
|
|
33
|
-
constructor(value: unknown);
|
|
34
|
-
}
|
|
35
31
|
export declare function Diff(current: unknown, next: unknown): Edit[];
|
|
36
32
|
export declare function Patch<T = any>(current: unknown, edits: Edit[]): T;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { IsStandardObject, IsArray, IsTypedArray, IsValueType
|
|
1
|
+
import { HasPropertyKey, IsStandardObject, IsArray, IsTypedArray, IsValueType } from '../guard/index.mjs';
|
|
2
2
|
import { ValuePointer } from '../pointer/index.mjs';
|
|
3
3
|
import { Clone } from '../clone/index.mjs';
|
|
4
|
+
import { Equal } from '../equal/equal.mjs';
|
|
4
5
|
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
5
6
|
import { Literal } from '../../type/literal/index.mjs';
|
|
6
7
|
import { Object } from '../../type/object/index.mjs';
|
|
@@ -25,18 +26,12 @@ export const Edit = Union([Insert, Update, Delete]);
|
|
|
25
26
|
// ------------------------------------------------------------------
|
|
26
27
|
// Errors
|
|
27
28
|
// ------------------------------------------------------------------
|
|
28
|
-
export class
|
|
29
|
+
export class ValueDiffError extends TypeBoxError {
|
|
29
30
|
constructor(value, message) {
|
|
30
31
|
super(message);
|
|
31
32
|
this.value = value;
|
|
32
33
|
}
|
|
33
34
|
}
|
|
34
|
-
export class ValueDeltaSymbolError extends ValueDeltaError {
|
|
35
|
-
constructor(value) {
|
|
36
|
-
super(value, 'Cannot diff objects with symbol keys');
|
|
37
|
-
this.value = value;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
35
|
// ------------------------------------------------------------------
|
|
41
36
|
// Command Factory
|
|
42
37
|
// ------------------------------------------------------------------
|
|
@@ -50,37 +45,47 @@ function CreateDelete(path) {
|
|
|
50
45
|
return { type: 'delete', path };
|
|
51
46
|
}
|
|
52
47
|
// ------------------------------------------------------------------
|
|
48
|
+
// AssertDiffable
|
|
49
|
+
// ------------------------------------------------------------------
|
|
50
|
+
function AssertDiffable(value) {
|
|
51
|
+
if (globalThis.Object.getOwnPropertySymbols(value).length > 0)
|
|
52
|
+
throw new ValueDiffError(value, 'Cannot diff objects with symbols');
|
|
53
|
+
}
|
|
54
|
+
// ------------------------------------------------------------------
|
|
53
55
|
// Diffing Generators
|
|
54
56
|
// ------------------------------------------------------------------
|
|
55
57
|
function* ObjectType(path, current, next) {
|
|
58
|
+
AssertDiffable(current);
|
|
59
|
+
AssertDiffable(next);
|
|
56
60
|
if (!IsStandardObject(next))
|
|
57
61
|
return yield CreateUpdate(path, next);
|
|
58
|
-
const currentKeys =
|
|
59
|
-
const nextKeys =
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (IsUndefined(next[key]) && nextKeys.includes(key))
|
|
64
|
-
yield CreateUpdate(`${path}/${globalThis.String(key)}`, undefined);
|
|
65
|
-
}
|
|
62
|
+
const currentKeys = globalThis.Object.getOwnPropertyNames(current);
|
|
63
|
+
const nextKeys = globalThis.Object.getOwnPropertyNames(next);
|
|
64
|
+
// ----------------------------------------------------------------
|
|
65
|
+
// inserts
|
|
66
|
+
// ----------------------------------------------------------------
|
|
66
67
|
for (const key of nextKeys) {
|
|
67
|
-
if (
|
|
68
|
+
if (HasPropertyKey(current, key))
|
|
68
69
|
continue;
|
|
69
|
-
|
|
70
|
-
throw new ValueDeltaSymbolError(key);
|
|
71
|
-
yield* Visit(`${path}/${globalThis.String(key)}`, current[key], next[key]);
|
|
70
|
+
yield CreateInsert(`${path}/${key}`, next[key]);
|
|
72
71
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
72
|
+
// ----------------------------------------------------------------
|
|
73
|
+
// updates
|
|
74
|
+
// ----------------------------------------------------------------
|
|
75
|
+
for (const key of currentKeys) {
|
|
76
|
+
if (!HasPropertyKey(next, key))
|
|
77
|
+
continue;
|
|
78
|
+
if (Equal(current, next))
|
|
79
|
+
continue;
|
|
80
|
+
yield* Visit(`${path}/${key}`, current[key], next[key]);
|
|
78
81
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
82
|
+
// ----------------------------------------------------------------
|
|
83
|
+
// deletes
|
|
84
|
+
// ----------------------------------------------------------------
|
|
85
|
+
for (const key of currentKeys) {
|
|
86
|
+
if (HasPropertyKey(next, key))
|
|
87
|
+
continue;
|
|
88
|
+
yield CreateDelete(`${path}/${key}`);
|
|
84
89
|
}
|
|
85
90
|
}
|
|
86
91
|
function* ArrayType(path, current, next) {
|
|
@@ -121,7 +126,7 @@ function* Visit(path, current, next) {
|
|
|
121
126
|
return yield* TypedArrayType(path, current, next);
|
|
122
127
|
if (IsValueType(current))
|
|
123
128
|
return yield* ValueType(path, current, next);
|
|
124
|
-
throw new
|
|
129
|
+
throw new ValueDiffError(current, 'Unable to diff value');
|
|
125
130
|
}
|
|
126
131
|
// ------------------------------------------------------------------
|
|
127
132
|
// Diff
|
|
@@ -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. */
|