@sinclair/typebox 0.32.15 → 0.32.16
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/import/system/system.d.mts +1 -1
- package/build/import/type/indexed/indexed.d.mts +1 -1
- package/build/import/type/indexed/indexed.mjs +3 -1
- package/build/import/type/template-literal/parse.mjs +24 -5
- package/build/import/type/template-literal/syntax.d.mts +1 -1
- package/build/import/value/convert/convert.mjs +2 -15
- package/build/import/value/delta/delta.d.mts +22 -22
- package/build/import/value/transform/decode.d.mts +3 -1
- package/build/import/value/transform/decode.mjs +58 -52
- package/build/import/value/transform/encode.d.mts +3 -1
- package/build/import/value/transform/encode.mjs +56 -50
- package/build/import/value/value/value.mjs +1 -1
- package/build/require/system/system.d.ts +1 -1
- package/build/require/type/indexed/indexed.d.ts +1 -1
- package/build/require/type/indexed/indexed.js +3 -1
- package/build/require/type/template-literal/parse.js +24 -5
- package/build/require/type/template-literal/syntax.d.ts +1 -1
- package/build/require/value/convert/convert.js +33 -46
- package/build/require/value/delta/delta.d.ts +22 -22
- package/build/require/value/transform/decode.d.ts +3 -1
- package/build/require/value/transform/decode.js +56 -52
- package/build/require/value/transform/encode.d.ts +3 -1
- package/build/require/value/transform/encode.js +54 -50
- package/build/require/value/value/value.js +1 -1
- package/package.json +1 -1
- package/readme.md +1 -0
|
@@ -8,7 +8,7 @@ export declare class TypeSystemDuplicateFormat extends TypeBoxError {
|
|
|
8
8
|
/** Creates user defined types and formats and provides overrides for value checking behaviours */
|
|
9
9
|
export declare namespace TypeSystem {
|
|
10
10
|
/** Creates a new type */
|
|
11
|
-
function Type<Type, Options = Record<PropertyKey, unknown>>(kind: string, check: (options: Options, value: unknown) => boolean): (options?: Partial<Options>) => import("
|
|
11
|
+
function Type<Type, Options = Record<PropertyKey, unknown>>(kind: string, check: (options: Options, value: unknown) => boolean): (options?: Partial<Options>) => import("src/type/unsafe/unsafe.mjs").TUnsafe<Type>;
|
|
12
12
|
/** Creates a new string format */
|
|
13
13
|
function Format<F extends string>(format: F, check: (value: string) => boolean): F;
|
|
14
14
|
}
|
|
@@ -16,7 +16,7 @@ import { type TIndexFromMappedResult } from './indexed-from-mapped-result.mjs';
|
|
|
16
16
|
type TFromRest<T extends TSchema[], K extends PropertyKey, Acc extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromRest<R, K, [...Acc, Assert<TIndexFromPropertyKey<L, K>, TSchema>]> : Acc);
|
|
17
17
|
type TFromIntersectRest<T extends TSchema[], Acc extends TSchema[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? L extends TNever ? TFromIntersectRest<R, [...Acc]> : TFromIntersectRest<R, [...Acc, L]> : Acc);
|
|
18
18
|
type TFromIntersect<T extends TSchema[], K extends PropertyKey> = (TIntersectEvaluated<TFromIntersectRest<TFromRest<T, K>>>);
|
|
19
|
-
type TFromUnionRest<T extends TSchema[]> = T;
|
|
19
|
+
type TFromUnionRest<T extends TSchema[], Acc extends TSchema[] = []> = T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? L extends TNever ? [] : TFromUnionRest<R, [L, ...Acc]> : Acc;
|
|
20
20
|
type TFromUnion<T extends TSchema[], K extends PropertyKey> = (TUnionEvaluated<TFromUnionRest<TFromRest<T, K>>>);
|
|
21
21
|
type TFromTuple<T extends TSchema[], K extends PropertyKey> = (K extends keyof T ? T[K] : K extends '[number]' ? TUnionEvaluated<T> : TNever);
|
|
22
22
|
type TFromArray<T extends TSchema, K extends PropertyKey> = (K extends '[number]' ? T : TNever);
|
|
@@ -4,23 +4,42 @@ import { TypeBoxError } from '../error/index.mjs';
|
|
|
4
4
|
// ------------------------------------------------------------------
|
|
5
5
|
export class TemplateLiteralParserError extends TypeBoxError {
|
|
6
6
|
}
|
|
7
|
+
// -------------------------------------------------------------------
|
|
8
|
+
// Unescape
|
|
9
|
+
//
|
|
10
|
+
// Unescape for these control characters specifically. Note that this
|
|
11
|
+
// function is only called on non union group content, and where we
|
|
12
|
+
// still want to allow the user to embed control characters in that
|
|
13
|
+
// content. For review.
|
|
14
|
+
// -------------------------------------------------------------------
|
|
7
15
|
// prettier-ignore
|
|
16
|
+
function Unescape(pattern) {
|
|
17
|
+
return pattern
|
|
18
|
+
.replace(/\\\$/g, '$')
|
|
19
|
+
.replace(/\\\*/g, '*')
|
|
20
|
+
.replace(/\\\^/g, '^')
|
|
21
|
+
.replace(/\\\|/g, '|')
|
|
22
|
+
.replace(/\\\(/g, '(')
|
|
23
|
+
.replace(/\\\)/g, ')');
|
|
24
|
+
}
|
|
25
|
+
// -------------------------------------------------------------------
|
|
26
|
+
// Control Characters
|
|
27
|
+
// -------------------------------------------------------------------
|
|
8
28
|
function IsNonEscaped(pattern, index, char) {
|
|
9
29
|
return pattern[index] === char && pattern.charCodeAt(index - 1) !== 92;
|
|
10
30
|
}
|
|
11
|
-
// prettier-ignore
|
|
12
31
|
function IsOpenParen(pattern, index) {
|
|
13
32
|
return IsNonEscaped(pattern, index, '(');
|
|
14
33
|
}
|
|
15
|
-
// prettier-ignore
|
|
16
34
|
function IsCloseParen(pattern, index) {
|
|
17
35
|
return IsNonEscaped(pattern, index, ')');
|
|
18
36
|
}
|
|
19
|
-
// prettier-ignore
|
|
20
37
|
function IsSeparator(pattern, index) {
|
|
21
38
|
return IsNonEscaped(pattern, index, '|');
|
|
22
39
|
}
|
|
23
|
-
//
|
|
40
|
+
// -------------------------------------------------------------------
|
|
41
|
+
// Control Groups
|
|
42
|
+
// -------------------------------------------------------------------
|
|
24
43
|
function IsGroup(pattern) {
|
|
25
44
|
if (!(IsOpenParen(pattern, 0) && IsCloseParen(pattern, pattern.length - 1)))
|
|
26
45
|
return false;
|
|
@@ -137,7 +156,7 @@ export function TemplateLiteralParse(pattern) {
|
|
|
137
156
|
return (IsGroup(pattern) ? TemplateLiteralParse(InGroup(pattern)) :
|
|
138
157
|
IsPrecedenceOr(pattern) ? Or(pattern) :
|
|
139
158
|
IsPrecedenceAnd(pattern) ? And(pattern) :
|
|
140
|
-
{ type: 'const', const: pattern });
|
|
159
|
+
{ type: 'const', const: Unescape(pattern) });
|
|
141
160
|
}
|
|
142
161
|
// ------------------------------------------------------------------
|
|
143
162
|
// TemplateLiteralParseExact
|
|
@@ -12,7 +12,7 @@ type FromUnionLiteral<T extends string> = T extends `${infer L}|${infer R}` ? [T
|
|
|
12
12
|
];
|
|
13
13
|
type FromUnion<T extends string> = TUnionEvaluated<FromUnionLiteral<T>>;
|
|
14
14
|
type FromTerminal<T extends string> = T extends 'boolean' ? TBoolean : T extends 'bigint' ? TBigInt : T extends 'number' ? TNumber : T extends 'string' ? TString : FromUnion<T>;
|
|
15
|
-
type FromString<T extends string> = T extends `{${infer L}}${infer R}` ? [FromTerminal<L>, ...FromString<R>] : T extends `${infer L}
|
|
15
|
+
type FromString<T extends string> = T extends `{${infer L}}${infer R}` ? [FromTerminal<L>, ...FromString<R>] : T extends `${infer L}$\{${infer R1}\}${infer R2}` ? [TLiteral<L>, ...FromString<`{${R1}}`>, ...FromString<R2>] : T extends `${infer L}$\{${infer R1}\}` ? [TLiteral<L>, ...FromString<`{${R1}}`>] : T extends `${infer L}` ? [TLiteral<L>] : [
|
|
16
16
|
];
|
|
17
17
|
export type TTemplateLiteralSyntax<T extends string> = (TTemplateLiteral<Assert<FromString<T>, TTemplateLiteralKind[]>>);
|
|
18
18
|
/** Parses TemplateLiteralSyntax and returns a tuple of TemplateLiteralKinds */
|
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import { Clone } from '../clone/index.mjs';
|
|
2
|
-
import { Check } from '../check/index.mjs';
|
|
3
2
|
import { Deref } from '../deref/index.mjs';
|
|
4
|
-
import { IsObject as IsObjectType } from '../../type/guard/type.mjs';
|
|
5
3
|
import { Kind } from '../../type/symbols/index.mjs';
|
|
6
|
-
import { Composite } from '../../type/composite/index.mjs';
|
|
7
4
|
// ------------------------------------------------------------------
|
|
8
5
|
// ValueGuard
|
|
9
6
|
// ------------------------------------------------------------------
|
|
@@ -129,12 +126,8 @@ function FromDate(schema, references, value) {
|
|
|
129
126
|
function FromInteger(schema, references, value) {
|
|
130
127
|
return TryConvertInteger(value);
|
|
131
128
|
}
|
|
132
|
-
// prettier-ignore
|
|
133
129
|
function FromIntersect(schema, references, value) {
|
|
134
|
-
|
|
135
|
-
if (allObjects)
|
|
136
|
-
return Visit(Composite(schema.allOf), references, value);
|
|
137
|
-
return Visit(schema.allOf[0], references, value); // todo: fix this
|
|
130
|
+
return schema.allOf.reduce((value, schema) => Visit(schema, references, value), value);
|
|
138
131
|
}
|
|
139
132
|
function FromLiteral(schema, references, value) {
|
|
140
133
|
return TryConvertLiteral(schema, value);
|
|
@@ -192,13 +185,7 @@ function FromUndefined(schema, references, value) {
|
|
|
192
185
|
return TryConvertUndefined(value);
|
|
193
186
|
}
|
|
194
187
|
function FromUnion(schema, references, value) {
|
|
195
|
-
|
|
196
|
-
const converted = Visit(subschema, references, value);
|
|
197
|
-
if (Check(subschema, references, converted)) {
|
|
198
|
-
return converted;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
return value;
|
|
188
|
+
return schema.anyOf.reduce((value, schema) => Visit(schema, references, value), value);
|
|
202
189
|
}
|
|
203
190
|
function Visit(schema, references, value) {
|
|
204
191
|
const references_ = IsString(schema.$id) ? [...references, schema] : references;
|
|
@@ -1,34 +1,34 @@
|
|
|
1
1
|
import type { Static } from '../../type/static/index.mjs';
|
|
2
2
|
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
3
3
|
export type Insert = Static<typeof Insert>;
|
|
4
|
-
export declare const Insert: import("
|
|
5
|
-
type: import("
|
|
6
|
-
path: import("
|
|
7
|
-
value: import("
|
|
4
|
+
export declare const Insert: import("src/type/object/object.mjs").TObject<{
|
|
5
|
+
type: import("src/type/literal/literal.mjs").TLiteral<"insert">;
|
|
6
|
+
path: import("src/type/string/string.mjs").TString;
|
|
7
|
+
value: import("src/type/unknown/unknown.mjs").TUnknown;
|
|
8
8
|
}>;
|
|
9
9
|
export type Update = Static<typeof Update>;
|
|
10
|
-
export declare const Update: import("
|
|
11
|
-
type: import("
|
|
12
|
-
path: import("
|
|
13
|
-
value: import("
|
|
10
|
+
export declare const Update: import("src/type/object/object.mjs").TObject<{
|
|
11
|
+
type: import("src/type/literal/literal.mjs").TLiteral<"update">;
|
|
12
|
+
path: import("src/type/string/string.mjs").TString;
|
|
13
|
+
value: import("src/type/unknown/unknown.mjs").TUnknown;
|
|
14
14
|
}>;
|
|
15
15
|
export type Delete = Static<typeof Delete>;
|
|
16
|
-
export declare const Delete: import("
|
|
17
|
-
type: import("
|
|
18
|
-
path: import("
|
|
16
|
+
export declare const Delete: import("src/type/object/object.mjs").TObject<{
|
|
17
|
+
type: import("src/type/literal/literal.mjs").TLiteral<"delete">;
|
|
18
|
+
path: import("src/type/string/string.mjs").TString;
|
|
19
19
|
}>;
|
|
20
20
|
export type Edit = Static<typeof Edit>;
|
|
21
|
-
export declare const Edit: import("
|
|
22
|
-
type: import("
|
|
23
|
-
path: import("
|
|
24
|
-
value: import("
|
|
25
|
-
}>, import("
|
|
26
|
-
type: import("
|
|
27
|
-
path: import("
|
|
28
|
-
value: import("
|
|
29
|
-
}>, import("
|
|
30
|
-
type: import("
|
|
31
|
-
path: import("
|
|
21
|
+
export declare const Edit: import("src/type/union/union-type.mjs").TUnion<[import("src/type/object/object.mjs").TObject<{
|
|
22
|
+
type: import("src/type/literal/literal.mjs").TLiteral<"insert">;
|
|
23
|
+
path: import("src/type/string/string.mjs").TString;
|
|
24
|
+
value: import("src/type/unknown/unknown.mjs").TUnknown;
|
|
25
|
+
}>, import("src/type/object/object.mjs").TObject<{
|
|
26
|
+
type: import("src/type/literal/literal.mjs").TLiteral<"update">;
|
|
27
|
+
path: import("src/type/string/string.mjs").TString;
|
|
28
|
+
value: import("src/type/unknown/unknown.mjs").TUnknown;
|
|
29
|
+
}>, import("src/type/object/object.mjs").TObject<{
|
|
30
|
+
type: import("src/type/literal/literal.mjs").TLiteral<"delete">;
|
|
31
|
+
path: import("src/type/string/string.mjs").TString;
|
|
32
32
|
}>]>;
|
|
33
33
|
export declare class ValueDeltaError extends TypeBoxError {
|
|
34
34
|
readonly value: unknown;
|
|
@@ -9,8 +9,10 @@ export declare class TransformDecodeCheckError extends TypeBoxError {
|
|
|
9
9
|
}
|
|
10
10
|
export declare class TransformDecodeError extends TypeBoxError {
|
|
11
11
|
readonly schema: TSchema;
|
|
12
|
+
readonly path: string;
|
|
12
13
|
readonly value: unknown;
|
|
13
|
-
|
|
14
|
+
readonly error: Error;
|
|
15
|
+
constructor(schema: TSchema, path: string, value: unknown, error: Error);
|
|
14
16
|
}
|
|
15
17
|
/**
|
|
16
18
|
* `[Internal]` Decodes the value and returns the result. This function requires that
|
|
@@ -16,168 +16,174 @@ import { IsTransform, IsSchema } from '../../type/guard/type.mjs';
|
|
|
16
16
|
// Errors
|
|
17
17
|
// ------------------------------------------------------------------
|
|
18
18
|
// thrown externally
|
|
19
|
+
// prettier-ignore
|
|
19
20
|
export class TransformDecodeCheckError extends TypeBoxError {
|
|
20
21
|
schema;
|
|
21
22
|
value;
|
|
22
23
|
error;
|
|
23
24
|
constructor(schema, value, error) {
|
|
24
|
-
super(`Unable to decode
|
|
25
|
+
super(`Unable to decode value as it does not match the expected schema`);
|
|
25
26
|
this.schema = schema;
|
|
26
27
|
this.value = value;
|
|
27
28
|
this.error = error;
|
|
28
29
|
}
|
|
29
30
|
}
|
|
31
|
+
// prettier-ignore
|
|
30
32
|
export class TransformDecodeError extends TypeBoxError {
|
|
31
33
|
schema;
|
|
34
|
+
path;
|
|
32
35
|
value;
|
|
33
|
-
|
|
34
|
-
|
|
36
|
+
error;
|
|
37
|
+
constructor(schema, path, value, error) {
|
|
38
|
+
super(error instanceof Error ? error.message : 'Unknown error');
|
|
35
39
|
this.schema = schema;
|
|
40
|
+
this.path = path;
|
|
36
41
|
this.value = value;
|
|
42
|
+
this.error = error;
|
|
37
43
|
}
|
|
38
44
|
}
|
|
39
45
|
// ------------------------------------------------------------------
|
|
40
46
|
// Decode
|
|
41
47
|
// ------------------------------------------------------------------
|
|
42
48
|
// prettier-ignore
|
|
43
|
-
function Default(schema, value) {
|
|
49
|
+
function Default(schema, path, value) {
|
|
44
50
|
try {
|
|
45
51
|
return IsTransform(schema) ? schema[TransformKind].Decode(value) : value;
|
|
46
52
|
}
|
|
47
53
|
catch (error) {
|
|
48
|
-
throw new TransformDecodeError(schema, value, error);
|
|
54
|
+
throw new TransformDecodeError(schema, path, value, error);
|
|
49
55
|
}
|
|
50
56
|
}
|
|
51
57
|
// prettier-ignore
|
|
52
|
-
function FromArray(schema, references, value) {
|
|
58
|
+
function FromArray(schema, references, path, value) {
|
|
53
59
|
return (IsArray(value))
|
|
54
|
-
? Default(schema, value.map((value) => Visit(schema.items, references, value)))
|
|
55
|
-
: Default(schema, value);
|
|
60
|
+
? Default(schema, path, value.map((value, index) => Visit(schema.items, references, `${path}/${index}`, value)))
|
|
61
|
+
: Default(schema, path, value);
|
|
56
62
|
}
|
|
57
63
|
// prettier-ignore
|
|
58
|
-
function FromIntersect(schema, references, value) {
|
|
64
|
+
function FromIntersect(schema, references, path, value) {
|
|
59
65
|
if (!IsStandardObject(value) || IsValueType(value))
|
|
60
|
-
return Default(schema, value);
|
|
66
|
+
return Default(schema, path, value);
|
|
61
67
|
const knownKeys = KeyOfPropertyKeys(schema);
|
|
62
68
|
const knownProperties = knownKeys.reduce((value, key) => {
|
|
63
69
|
return (key in value)
|
|
64
|
-
? { ...value, [key]: Visit(Index(schema, [key]), references, value[key]) }
|
|
70
|
+
? { ...value, [key]: Visit(Index(schema, [key]), references, `${path}/${key}`, value[key]) }
|
|
65
71
|
: value;
|
|
66
72
|
}, value);
|
|
67
73
|
if (!IsTransform(schema.unevaluatedProperties)) {
|
|
68
|
-
return Default(schema, knownProperties);
|
|
74
|
+
return Default(schema, path, knownProperties);
|
|
69
75
|
}
|
|
70
76
|
const unknownKeys = Object.getOwnPropertyNames(knownProperties);
|
|
71
77
|
const unevaluatedProperties = schema.unevaluatedProperties;
|
|
72
78
|
const unknownProperties = unknownKeys.reduce((value, key) => {
|
|
73
79
|
return !knownKeys.includes(key)
|
|
74
|
-
? { ...value, [key]: Default(unevaluatedProperties, value[key]) }
|
|
80
|
+
? { ...value, [key]: Default(unevaluatedProperties, `${path}/${key}`, value[key]) }
|
|
75
81
|
: value;
|
|
76
82
|
}, knownProperties);
|
|
77
|
-
return Default(schema, unknownProperties);
|
|
83
|
+
return Default(schema, path, unknownProperties);
|
|
78
84
|
}
|
|
79
|
-
function FromNot(schema, references, value) {
|
|
80
|
-
return Default(schema, Visit(schema.not, references, value));
|
|
85
|
+
function FromNot(schema, references, path, value) {
|
|
86
|
+
return Default(schema, path, Visit(schema.not, references, path, value));
|
|
81
87
|
}
|
|
82
88
|
// prettier-ignore
|
|
83
|
-
function FromObject(schema, references, value) {
|
|
89
|
+
function FromObject(schema, references, path, value) {
|
|
84
90
|
if (!IsStandardObject(value))
|
|
85
|
-
return Default(schema, value);
|
|
91
|
+
return Default(schema, path, value);
|
|
86
92
|
const knownKeys = KeyOfPropertyKeys(schema);
|
|
87
93
|
const knownProperties = knownKeys.reduce((value, key) => {
|
|
88
94
|
return (key in value)
|
|
89
|
-
? { ...value, [key]: Visit(schema.properties[key], references, value[key]) }
|
|
95
|
+
? { ...value, [key]: Visit(schema.properties[key], references, `${path}/${key}`, value[key]) }
|
|
90
96
|
: value;
|
|
91
97
|
}, value);
|
|
92
98
|
if (!IsSchema(schema.additionalProperties)) {
|
|
93
|
-
return Default(schema, knownProperties);
|
|
99
|
+
return Default(schema, path, knownProperties);
|
|
94
100
|
}
|
|
95
101
|
const unknownKeys = Object.getOwnPropertyNames(knownProperties);
|
|
96
102
|
const additionalProperties = schema.additionalProperties;
|
|
97
103
|
const unknownProperties = unknownKeys.reduce((value, key) => {
|
|
98
104
|
return !knownKeys.includes(key)
|
|
99
|
-
? { ...value, [key]: Default(additionalProperties, value[key]) }
|
|
105
|
+
? { ...value, [key]: Default(additionalProperties, `${path}/${key}`, value[key]) }
|
|
100
106
|
: value;
|
|
101
107
|
}, knownProperties);
|
|
102
|
-
return Default(schema, unknownProperties);
|
|
108
|
+
return Default(schema, path, unknownProperties);
|
|
103
109
|
}
|
|
104
110
|
// prettier-ignore
|
|
105
|
-
function FromRecord(schema, references, value) {
|
|
111
|
+
function FromRecord(schema, references, path, value) {
|
|
106
112
|
if (!IsStandardObject(value))
|
|
107
|
-
return Default(schema, value);
|
|
113
|
+
return Default(schema, path, value);
|
|
108
114
|
const pattern = Object.getOwnPropertyNames(schema.patternProperties)[0];
|
|
109
115
|
const knownKeys = new RegExp(pattern);
|
|
110
116
|
const knownProperties = Object.getOwnPropertyNames(value).reduce((value, key) => {
|
|
111
117
|
return knownKeys.test(key)
|
|
112
|
-
? { ...value, [key]: Visit(schema.patternProperties[pattern], references, value[key]) }
|
|
118
|
+
? { ...value, [key]: Visit(schema.patternProperties[pattern], references, `${path}/${key}`, value[key]) }
|
|
113
119
|
: value;
|
|
114
120
|
}, value);
|
|
115
121
|
if (!IsSchema(schema.additionalProperties)) {
|
|
116
|
-
return Default(schema, knownProperties);
|
|
122
|
+
return Default(schema, path, knownProperties);
|
|
117
123
|
}
|
|
118
124
|
const unknownKeys = Object.getOwnPropertyNames(knownProperties);
|
|
119
125
|
const additionalProperties = schema.additionalProperties;
|
|
120
126
|
const unknownProperties = unknownKeys.reduce((value, key) => {
|
|
121
127
|
return !knownKeys.test(key)
|
|
122
|
-
? { ...value, [key]: Default(additionalProperties, value[key]) }
|
|
128
|
+
? { ...value, [key]: Default(additionalProperties, `${path}/${key}`, value[key]) }
|
|
123
129
|
: value;
|
|
124
130
|
}, knownProperties);
|
|
125
|
-
return Default(schema, unknownProperties);
|
|
131
|
+
return Default(schema, path, unknownProperties);
|
|
126
132
|
}
|
|
127
133
|
// prettier-ignore
|
|
128
|
-
function FromRef(schema, references, value) {
|
|
134
|
+
function FromRef(schema, references, path, value) {
|
|
129
135
|
const target = Deref(schema, references);
|
|
130
|
-
return Default(schema, Visit(target, references, value));
|
|
136
|
+
return Default(schema, path, Visit(target, references, path, value));
|
|
131
137
|
}
|
|
132
138
|
// prettier-ignore
|
|
133
|
-
function FromThis(schema, references, value) {
|
|
139
|
+
function FromThis(schema, references, path, value) {
|
|
134
140
|
const target = Deref(schema, references);
|
|
135
|
-
return Default(schema, Visit(target, references, value));
|
|
141
|
+
return Default(schema, path, Visit(target, references, path, value));
|
|
136
142
|
}
|
|
137
143
|
// prettier-ignore
|
|
138
|
-
function FromTuple(schema, references, value) {
|
|
144
|
+
function FromTuple(schema, references, path, value) {
|
|
139
145
|
return (IsArray(value) && IsArray(schema.items))
|
|
140
|
-
? Default(schema, schema.items.map((schema, index) => Visit(schema, references, value[index])))
|
|
141
|
-
: Default(schema, value);
|
|
146
|
+
? Default(schema, path, schema.items.map((schema, index) => Visit(schema, references, `${path}/${index}`, value[index])))
|
|
147
|
+
: Default(schema, path, value);
|
|
142
148
|
}
|
|
143
149
|
// prettier-ignore
|
|
144
|
-
function FromUnion(schema, references, value) {
|
|
150
|
+
function FromUnion(schema, references, path, value) {
|
|
145
151
|
for (const subschema of schema.anyOf) {
|
|
146
152
|
if (!Check(subschema, references, value))
|
|
147
153
|
continue;
|
|
148
154
|
// note: ensure interior is decoded first
|
|
149
|
-
const decoded = Visit(subschema, references, value);
|
|
150
|
-
return Default(schema, decoded);
|
|
155
|
+
const decoded = Visit(subschema, references, path, value);
|
|
156
|
+
return Default(schema, path, decoded);
|
|
151
157
|
}
|
|
152
|
-
return Default(schema, value);
|
|
158
|
+
return Default(schema, path, value);
|
|
153
159
|
}
|
|
154
160
|
// prettier-ignore
|
|
155
|
-
function Visit(schema, references, value) {
|
|
161
|
+
function Visit(schema, references, path, value) {
|
|
156
162
|
const references_ = typeof schema.$id === 'string' ? [...references, schema] : references;
|
|
157
163
|
const schema_ = schema;
|
|
158
164
|
switch (schema[Kind]) {
|
|
159
165
|
case 'Array':
|
|
160
|
-
return FromArray(schema_, references_, value);
|
|
166
|
+
return FromArray(schema_, references_, path, value);
|
|
161
167
|
case 'Intersect':
|
|
162
|
-
return FromIntersect(schema_, references_, value);
|
|
168
|
+
return FromIntersect(schema_, references_, path, value);
|
|
163
169
|
case 'Not':
|
|
164
|
-
return FromNot(schema_, references_, value);
|
|
170
|
+
return FromNot(schema_, references_, path, value);
|
|
165
171
|
case 'Object':
|
|
166
|
-
return FromObject(schema_, references_, value);
|
|
172
|
+
return FromObject(schema_, references_, path, value);
|
|
167
173
|
case 'Record':
|
|
168
|
-
return FromRecord(schema_, references_, value);
|
|
174
|
+
return FromRecord(schema_, references_, path, value);
|
|
169
175
|
case 'Ref':
|
|
170
|
-
return FromRef(schema_, references_, value);
|
|
176
|
+
return FromRef(schema_, references_, path, value);
|
|
171
177
|
case 'Symbol':
|
|
172
|
-
return Default(schema_, value);
|
|
178
|
+
return Default(schema_, path, value);
|
|
173
179
|
case 'This':
|
|
174
|
-
return FromThis(schema_, references_, value);
|
|
180
|
+
return FromThis(schema_, references_, path, value);
|
|
175
181
|
case 'Tuple':
|
|
176
|
-
return FromTuple(schema_, references_, value);
|
|
182
|
+
return FromTuple(schema_, references_, path, value);
|
|
177
183
|
case 'Union':
|
|
178
|
-
return FromUnion(schema_, references_, value);
|
|
184
|
+
return FromUnion(schema_, references_, path, value);
|
|
179
185
|
default:
|
|
180
|
-
return Default(schema_, value);
|
|
186
|
+
return Default(schema_, path, value);
|
|
181
187
|
}
|
|
182
188
|
}
|
|
183
189
|
/**
|
|
@@ -186,5 +192,5 @@ function Visit(schema, references, value) {
|
|
|
186
192
|
* undefined behavior. Refer to the `Value.Decode()` for implementation details.
|
|
187
193
|
*/
|
|
188
194
|
export function TransformDecode(schema, references, value) {
|
|
189
|
-
return Visit(schema, references, value);
|
|
195
|
+
return Visit(schema, references, '', value);
|
|
190
196
|
}
|
|
@@ -9,8 +9,10 @@ export declare class TransformEncodeCheckError extends TypeBoxError {
|
|
|
9
9
|
}
|
|
10
10
|
export declare class TransformEncodeError extends TypeBoxError {
|
|
11
11
|
readonly schema: TSchema;
|
|
12
|
+
readonly path: string;
|
|
12
13
|
readonly value: unknown;
|
|
13
|
-
|
|
14
|
+
readonly error: Error;
|
|
15
|
+
constructor(schema: TSchema, path: string, value: unknown, error: Error);
|
|
14
16
|
}
|
|
15
17
|
/**
|
|
16
18
|
* `[Internal]` Encodes the value and returns the result. This function expects the
|