@sinclair/typebox 0.24.7 → 0.24.8
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/compiler/compiler.d.ts +5 -5
- package/compiler/compiler.js +98 -98
- package/compiler/index.d.ts +1 -1
- package/compiler/index.js +1 -1
- package/package.json +1 -1
- package/readme.md +30 -17
- package/typebox.d.ts +1 -3
- package/value/cast.d.ts +5 -0
- package/value/cast.js +251 -0
- package/value/check.d.ts +2 -3
- package/value/check.js +172 -118
- package/value/create.d.ts +3 -4
- package/value/create.js +79 -94
- package/value/errors.d.ts +10 -0
- package/{compiler → value}/errors.js +70 -91
- package/value/value.d.ts +17 -13
- package/value/value.js +20 -32
- package/compiler/errors.d.ts +0 -10
- package/value/clone.d.ts +0 -3
- package/value/clone.js +0 -94
- package/value/delta.d.ts +0 -13
- package/value/delta.js +0 -191
- package/value/pointer.d.ts +0 -12
- package/value/pointer.js +0 -110
- package/value/reflect.d.ts +0 -2
- package/value/reflect.js +0 -42
- package/value/upcast.d.ts +0 -4
- package/value/upcast.js +0 -247
package/value/delta.js
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*--------------------------------------------------------------------------
|
|
3
|
-
|
|
4
|
-
@sinclair/typebox/value
|
|
5
|
-
|
|
6
|
-
The MIT License (MIT)
|
|
7
|
-
|
|
8
|
-
Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
9
|
-
|
|
10
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
-
in the Software without restriction, including without limitation the rights
|
|
13
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
-
furnished to do so, subject to the following conditions:
|
|
16
|
-
|
|
17
|
-
The above copyright notice and this permission notice shall be included in
|
|
18
|
-
all copies or substantial portions of the Software.
|
|
19
|
-
|
|
20
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26
|
-
THE SOFTWARE.
|
|
27
|
-
|
|
28
|
-
---------------------------------------------------------------------------*/
|
|
29
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.DeltaValue = exports.EditType = void 0;
|
|
31
|
-
const reflect_1 = require("./reflect");
|
|
32
|
-
const pointer_1 = require("./pointer");
|
|
33
|
-
const clone_1 = require("./clone");
|
|
34
|
-
var FlatValue;
|
|
35
|
-
(function (FlatValue) {
|
|
36
|
-
function* Undefined(key, value) {
|
|
37
|
-
yield [key, { type: 'undefined', value }];
|
|
38
|
-
}
|
|
39
|
-
function* Null(key, value) {
|
|
40
|
-
yield [key, { type: 'null', value }];
|
|
41
|
-
}
|
|
42
|
-
function* Function(key, value) {
|
|
43
|
-
yield [key, { type: 'function', value }];
|
|
44
|
-
}
|
|
45
|
-
function* Object(key, value) {
|
|
46
|
-
yield [key, { type: 'object', value }];
|
|
47
|
-
for (const entry of globalThis.Object.entries(value)) {
|
|
48
|
-
yield* Visit(`${key}/${entry[0]}`, entry[1]);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
function* Array(key, value) {
|
|
52
|
-
yield [key, { type: 'array', value }];
|
|
53
|
-
for (let i = 0; i < value.length; i++) {
|
|
54
|
-
yield* Visit(`${key}/${i}`, value[i]);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
function* BigInt(key, value) {
|
|
58
|
-
yield [key, { type: 'bigint', value }];
|
|
59
|
-
}
|
|
60
|
-
function* Symbol(key, value) {
|
|
61
|
-
yield [key, { type: 'symbol', value }];
|
|
62
|
-
}
|
|
63
|
-
function* String(key, value) {
|
|
64
|
-
yield [key, { type: 'string', value }];
|
|
65
|
-
}
|
|
66
|
-
function* Boolean(key, value) {
|
|
67
|
-
yield [key, { type: 'boolean', value }];
|
|
68
|
-
}
|
|
69
|
-
function* Number(key, value) {
|
|
70
|
-
yield [key, { type: 'number', value }];
|
|
71
|
-
}
|
|
72
|
-
function* Visit(path, value) {
|
|
73
|
-
switch ((0, reflect_1.Reflect)(value)) {
|
|
74
|
-
case 'array':
|
|
75
|
-
return yield* Array(path, value);
|
|
76
|
-
case 'bigint':
|
|
77
|
-
return yield* BigInt(path, value);
|
|
78
|
-
case 'boolean':
|
|
79
|
-
return yield* Boolean(path, value);
|
|
80
|
-
case 'function':
|
|
81
|
-
return yield* Function(path, value);
|
|
82
|
-
case 'null':
|
|
83
|
-
return yield* Null(path, value);
|
|
84
|
-
case 'number':
|
|
85
|
-
return yield* Number(path, value);
|
|
86
|
-
case 'object':
|
|
87
|
-
return yield* Object(path, value);
|
|
88
|
-
case 'string':
|
|
89
|
-
return yield* String(path, value);
|
|
90
|
-
case 'symbol':
|
|
91
|
-
return yield* Symbol(path, value);
|
|
92
|
-
case 'undefined':
|
|
93
|
-
return yield* Undefined(path, value);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
function* Create(value) {
|
|
97
|
-
return yield* Visit('', value);
|
|
98
|
-
}
|
|
99
|
-
FlatValue.Create = Create;
|
|
100
|
-
})(FlatValue || (FlatValue = {}));
|
|
101
|
-
var EditType;
|
|
102
|
-
(function (EditType) {
|
|
103
|
-
EditType[EditType["Delete"] = 0] = "Delete";
|
|
104
|
-
EditType[EditType["Update"] = 1] = "Update";
|
|
105
|
-
EditType[EditType["Insert"] = 2] = "Insert";
|
|
106
|
-
})(EditType = exports.EditType || (exports.EditType = {}));
|
|
107
|
-
var DeltaValue;
|
|
108
|
-
(function (DeltaValue) {
|
|
109
|
-
function IsRootUpdate(edits) {
|
|
110
|
-
return edits.length > 0 && edits[0][0] === EditType.Update && edits[0][1] === '';
|
|
111
|
-
}
|
|
112
|
-
function IsNullUpdate(edits) {
|
|
113
|
-
return edits.length === 0;
|
|
114
|
-
}
|
|
115
|
-
function* Updates(mapA, mapB) {
|
|
116
|
-
for (const [keyB, entryB] of mapB) {
|
|
117
|
-
if (!mapA.has(keyB))
|
|
118
|
-
continue;
|
|
119
|
-
const entryA = mapA.get(keyB);
|
|
120
|
-
if (entryA.value === entryB.value)
|
|
121
|
-
continue;
|
|
122
|
-
if (entryA.type === 'object' && entryB.type === 'object')
|
|
123
|
-
continue;
|
|
124
|
-
if (entryA.type === 'array' && entryB.type === 'array')
|
|
125
|
-
continue;
|
|
126
|
-
yield [EditType.Update, keyB, entryB.value];
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
function* Inserts(mapA, mapB, updates) {
|
|
130
|
-
const discards = [];
|
|
131
|
-
for (const [keyB, entryB] of mapB) {
|
|
132
|
-
if (discards.some((ignore) => keyB.indexOf(ignore) === 0))
|
|
133
|
-
continue;
|
|
134
|
-
if (updates.some((update) => keyB.indexOf(update[1]) === 0))
|
|
135
|
-
continue;
|
|
136
|
-
if (mapA.has(keyB))
|
|
137
|
-
continue;
|
|
138
|
-
if (entryB.type === 'object' || entryB.type === 'array')
|
|
139
|
-
discards.push(keyB);
|
|
140
|
-
yield [EditType.Insert, keyB, entryB.value];
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
function* Deletes(mapA, mapB) {
|
|
144
|
-
const discards = [];
|
|
145
|
-
for (const [keyA, entryA] of mapA) {
|
|
146
|
-
if (discards.some((discard) => keyA.indexOf(discard) === 0))
|
|
147
|
-
continue;
|
|
148
|
-
if (mapB.has(keyA))
|
|
149
|
-
continue;
|
|
150
|
-
if (entryA.type === 'object' || entryA.type === 'array')
|
|
151
|
-
discards.push(keyA);
|
|
152
|
-
yield [EditType.Delete, keyA];
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
function Diff(valueA, valueB) {
|
|
156
|
-
const mapA = new Map(FlatValue.Create(valueA));
|
|
157
|
-
const mapB = new Map(FlatValue.Create(valueB));
|
|
158
|
-
const updates = [...Updates(mapA, mapB)];
|
|
159
|
-
const inserts = [...Inserts(mapA, mapB, updates)];
|
|
160
|
-
const deletes = [...Deletes(mapA, mapB)].reverse();
|
|
161
|
-
return [...updates, ...inserts, ...deletes];
|
|
162
|
-
}
|
|
163
|
-
DeltaValue.Diff = Diff;
|
|
164
|
-
function Edit(valueA, operations) {
|
|
165
|
-
if (IsRootUpdate(operations)) {
|
|
166
|
-
return clone_1.CloneValue.Create(operations[0][2]);
|
|
167
|
-
}
|
|
168
|
-
if (IsNullUpdate(operations)) {
|
|
169
|
-
return clone_1.CloneValue.Create(valueA);
|
|
170
|
-
}
|
|
171
|
-
const clone = clone_1.CloneValue.Create(valueA);
|
|
172
|
-
for (const operation of operations) {
|
|
173
|
-
switch (operation[0]) {
|
|
174
|
-
case EditType.Insert: {
|
|
175
|
-
pointer_1.Pointer.Set(clone, operation[1], operation[2]);
|
|
176
|
-
break;
|
|
177
|
-
}
|
|
178
|
-
case EditType.Update: {
|
|
179
|
-
pointer_1.Pointer.Set(clone, operation[1], operation[2]);
|
|
180
|
-
break;
|
|
181
|
-
}
|
|
182
|
-
case EditType.Delete: {
|
|
183
|
-
pointer_1.Pointer.Delete(clone, operation[1]);
|
|
184
|
-
break;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
return clone;
|
|
189
|
-
}
|
|
190
|
-
DeltaValue.Edit = Edit;
|
|
191
|
-
})(DeltaValue = exports.DeltaValue || (exports.DeltaValue = {}));
|
package/value/pointer.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export declare namespace Pointer {
|
|
2
|
-
/** Formats the RFC6901 JsonPointer as an array of paths. */
|
|
3
|
-
function Format(pointer: string): string[];
|
|
4
|
-
/** Sets the value at the given pointer. If the pointer does not exist it is created. */
|
|
5
|
-
function Set(value: any, pointer: string, update: any): void;
|
|
6
|
-
/** Deletes a value at the given pointer. */
|
|
7
|
-
function Delete(value: any, pointer: string): any[] | undefined;
|
|
8
|
-
/** True if a value exists at the given pointer */
|
|
9
|
-
function Has(value: any, pointer: string): boolean;
|
|
10
|
-
/** Gets the value at the given pointer */
|
|
11
|
-
function Get(value: any, pointer: string): any;
|
|
12
|
-
}
|
package/value/pointer.js
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*--------------------------------------------------------------------------
|
|
3
|
-
|
|
4
|
-
@sinclair/typebox/value
|
|
5
|
-
|
|
6
|
-
The MIT License (MIT)
|
|
7
|
-
|
|
8
|
-
Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
9
|
-
|
|
10
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
-
in the Software without restriction, including without limitation the rights
|
|
13
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
-
furnished to do so, subject to the following conditions:
|
|
16
|
-
|
|
17
|
-
The above copyright notice and this permission notice shall be included in
|
|
18
|
-
all copies or substantial portions of the Software.
|
|
19
|
-
|
|
20
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26
|
-
THE SOFTWARE.
|
|
27
|
-
|
|
28
|
-
---------------------------------------------------------------------------*/
|
|
29
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.Pointer = void 0;
|
|
31
|
-
var Pointer;
|
|
32
|
-
(function (Pointer) {
|
|
33
|
-
/** Formats the RFC6901 JsonPointer as an array of paths. */
|
|
34
|
-
function Format(pointer) {
|
|
35
|
-
if (pointer === '/')
|
|
36
|
-
return [''];
|
|
37
|
-
return pointer
|
|
38
|
-
.split(`/`)
|
|
39
|
-
.filter((part) => part.length > 0)
|
|
40
|
-
.map((part) => part.replace(/~0/g, `~`).replace(/~1/g, `/`));
|
|
41
|
-
}
|
|
42
|
-
Pointer.Format = Format;
|
|
43
|
-
/** Sets the value at the given pointer. If the pointer does not exist it is created. */
|
|
44
|
-
function Set(value, pointer, update) {
|
|
45
|
-
if (pointer === '')
|
|
46
|
-
throw Error('Cannot set root value');
|
|
47
|
-
const path = Format(pointer);
|
|
48
|
-
let current = value;
|
|
49
|
-
while (path.length > 1) {
|
|
50
|
-
const next = path.shift();
|
|
51
|
-
if (current[next] === undefined)
|
|
52
|
-
current[next] = {};
|
|
53
|
-
current = current[next];
|
|
54
|
-
}
|
|
55
|
-
current[path.shift()] = update;
|
|
56
|
-
}
|
|
57
|
-
Pointer.Set = Set;
|
|
58
|
-
/** Deletes a value at the given pointer. */
|
|
59
|
-
function Delete(value, pointer) {
|
|
60
|
-
if (pointer === '')
|
|
61
|
-
throw Error('Cannot delete root value');
|
|
62
|
-
let current = value;
|
|
63
|
-
const path = Format(pointer);
|
|
64
|
-
while (path.length > 1) {
|
|
65
|
-
const next = path.shift();
|
|
66
|
-
if (current[next] === undefined)
|
|
67
|
-
return;
|
|
68
|
-
current = current[next];
|
|
69
|
-
}
|
|
70
|
-
if (Array.isArray(current)) {
|
|
71
|
-
const index = parseInt(path.shift());
|
|
72
|
-
return current.splice(index, 1);
|
|
73
|
-
}
|
|
74
|
-
else {
|
|
75
|
-
const key = path.shift();
|
|
76
|
-
delete current[key];
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
Pointer.Delete = Delete;
|
|
80
|
-
/** True if a value exists at the given pointer */
|
|
81
|
-
function Has(value, pointer) {
|
|
82
|
-
if (pointer === '')
|
|
83
|
-
return true;
|
|
84
|
-
let current = value;
|
|
85
|
-
const path = Format(pointer);
|
|
86
|
-
while (path.length > 1) {
|
|
87
|
-
const next = path.shift();
|
|
88
|
-
if (current[next] === undefined)
|
|
89
|
-
return false;
|
|
90
|
-
current = current[next];
|
|
91
|
-
}
|
|
92
|
-
return current[path.shift()] !== undefined;
|
|
93
|
-
}
|
|
94
|
-
Pointer.Has = Has;
|
|
95
|
-
/** Gets the value at the given pointer */
|
|
96
|
-
function Get(value, pointer) {
|
|
97
|
-
if (pointer === '')
|
|
98
|
-
return value;
|
|
99
|
-
let current = value;
|
|
100
|
-
const path = Format(pointer);
|
|
101
|
-
while (path.length > 1) {
|
|
102
|
-
const next = path.shift();
|
|
103
|
-
if (current[next] === undefined)
|
|
104
|
-
return undefined;
|
|
105
|
-
current = current[next];
|
|
106
|
-
}
|
|
107
|
-
return current[path.shift()];
|
|
108
|
-
}
|
|
109
|
-
Pointer.Get = Get;
|
|
110
|
-
})(Pointer = exports.Pointer || (exports.Pointer = {}));
|
package/value/reflect.d.ts
DELETED
package/value/reflect.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*--------------------------------------------------------------------------
|
|
3
|
-
|
|
4
|
-
@sinclair/typebox/value
|
|
5
|
-
|
|
6
|
-
The MIT License (MIT)
|
|
7
|
-
|
|
8
|
-
Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
9
|
-
|
|
10
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
-
in the Software without restriction, including without limitation the rights
|
|
13
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
-
furnished to do so, subject to the following conditions:
|
|
16
|
-
|
|
17
|
-
The above copyright notice and this permission notice shall be included in
|
|
18
|
-
all copies or substantial portions of the Software.
|
|
19
|
-
|
|
20
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26
|
-
THE SOFTWARE.
|
|
27
|
-
|
|
28
|
-
---------------------------------------------------------------------------*/
|
|
29
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.Reflect = void 0;
|
|
31
|
-
function Reflect(value) {
|
|
32
|
-
if (value === undefined)
|
|
33
|
-
return 'undefined';
|
|
34
|
-
if (value === null)
|
|
35
|
-
return 'null';
|
|
36
|
-
if (typeof value === 'object' && !globalThis.Array.isArray(value) && value !== null)
|
|
37
|
-
return 'object';
|
|
38
|
-
if (typeof value === 'object' && globalThis.Array.isArray(value))
|
|
39
|
-
return 'array';
|
|
40
|
-
return typeof value;
|
|
41
|
-
}
|
|
42
|
-
exports.Reflect = Reflect;
|
package/value/upcast.d.ts
DELETED
package/value/upcast.js
DELETED
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*--------------------------------------------------------------------------
|
|
3
|
-
|
|
4
|
-
@sinclair/typebox/value
|
|
5
|
-
|
|
6
|
-
The MIT License (MIT)
|
|
7
|
-
|
|
8
|
-
Copyright (c) 2022 Haydn Paterson (sinclair) <haydn.developer@gmail.com>
|
|
9
|
-
|
|
10
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
-
in the Software without restriction, including without limitation the rights
|
|
13
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
-
furnished to do so, subject to the following conditions:
|
|
16
|
-
|
|
17
|
-
The above copyright notice and this permission notice shall be included in
|
|
18
|
-
all copies or substantial portions of the Software.
|
|
19
|
-
|
|
20
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
26
|
-
THE SOFTWARE.
|
|
27
|
-
|
|
28
|
-
---------------------------------------------------------------------------*/
|
|
29
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.UpcastValue = void 0;
|
|
31
|
-
const Types = require("../typebox");
|
|
32
|
-
const create_1 = require("./create");
|
|
33
|
-
const check_1 = require("./check");
|
|
34
|
-
// --------------------------------------------------------------------------
|
|
35
|
-
// Specialized Union Patch. Because a value can be one of many different
|
|
36
|
-
// unions with properties potentially overlapping, we need a strategy
|
|
37
|
-
// in which to resolve the appropriate schema to patch from.
|
|
38
|
-
//
|
|
39
|
-
// The following will score each union type found within the types anyOf
|
|
40
|
-
// array. Typically this is executed for objects only, so the score is a
|
|
41
|
-
// essentially a tally of how many properties are valid. The reasoning
|
|
42
|
-
// here is the discriminator field would tip the scales in favor of that
|
|
43
|
-
// union if other properties overlap and match.
|
|
44
|
-
// --------------------------------------------------------------------------
|
|
45
|
-
var UpcastUnionValue;
|
|
46
|
-
(function (UpcastUnionValue) {
|
|
47
|
-
function Score(schema, value) {
|
|
48
|
-
let score = 0;
|
|
49
|
-
if (schema[Types.Kind] === 'Object' && typeof value === 'object' && value !== null) {
|
|
50
|
-
const objectSchema = schema;
|
|
51
|
-
const entries = globalThis.Object.entries(objectSchema.properties);
|
|
52
|
-
score += entries.reduce((acc, [key, schema]) => acc + (check_1.CheckValue.Visit(schema, value[key]) ? 1 : 0), 0);
|
|
53
|
-
}
|
|
54
|
-
return score;
|
|
55
|
-
}
|
|
56
|
-
function Select(schema, value) {
|
|
57
|
-
let select = schema.anyOf[0];
|
|
58
|
-
let best = 0;
|
|
59
|
-
for (const subschema of schema.anyOf) {
|
|
60
|
-
const score = Score(subschema, value);
|
|
61
|
-
if (score > best) {
|
|
62
|
-
select = subschema;
|
|
63
|
-
best = score;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return select;
|
|
67
|
-
}
|
|
68
|
-
function Create(schema, value) {
|
|
69
|
-
return check_1.CheckValue.Visit(schema, value) ? value : UpcastValue.Create(Select(schema, value), value);
|
|
70
|
-
}
|
|
71
|
-
UpcastUnionValue.Create = Create;
|
|
72
|
-
})(UpcastUnionValue || (UpcastUnionValue = {}));
|
|
73
|
-
var UpcastValue;
|
|
74
|
-
(function (UpcastValue) {
|
|
75
|
-
const ids = new Map();
|
|
76
|
-
function Any(schema, value) {
|
|
77
|
-
return check_1.CheckValue.Visit(schema, value) ? value : create_1.CreateValue.Create(schema);
|
|
78
|
-
}
|
|
79
|
-
function Array(schema, value) {
|
|
80
|
-
if (check_1.CheckValue.Visit(schema, value))
|
|
81
|
-
return value;
|
|
82
|
-
if (!globalThis.Array.isArray(value))
|
|
83
|
-
return create_1.CreateValue.Create(schema);
|
|
84
|
-
return value.map((element) => Create(schema.items, element));
|
|
85
|
-
}
|
|
86
|
-
function Boolean(schema, value) {
|
|
87
|
-
return check_1.CheckValue.Visit(schema, value) ? value : create_1.CreateValue.Create(schema);
|
|
88
|
-
}
|
|
89
|
-
function Constructor(schema, value) {
|
|
90
|
-
if (check_1.CheckValue.Visit(schema, value))
|
|
91
|
-
return create_1.CreateValue.Create(schema);
|
|
92
|
-
const required = new Set(schema.returns.required || []);
|
|
93
|
-
const result = function () { };
|
|
94
|
-
for (const [key, property] of globalThis.Object.entries(schema.returns.properties)) {
|
|
95
|
-
if (!required.has(key) && value.prototype[key] === undefined)
|
|
96
|
-
continue;
|
|
97
|
-
result.prototype[key] = Create(property, value.prototype[key]);
|
|
98
|
-
}
|
|
99
|
-
return result;
|
|
100
|
-
}
|
|
101
|
-
function Enum(schema, value) {
|
|
102
|
-
return check_1.CheckValue.Visit(schema, value) ? value : create_1.CreateValue.Create(schema);
|
|
103
|
-
}
|
|
104
|
-
function Function(schema, value) {
|
|
105
|
-
return check_1.CheckValue.Visit(schema, value) ? value : create_1.CreateValue.Create(schema);
|
|
106
|
-
}
|
|
107
|
-
function Integer(schema, value) {
|
|
108
|
-
return check_1.CheckValue.Visit(schema, value) ? value : create_1.CreateValue.Create(schema);
|
|
109
|
-
}
|
|
110
|
-
function Intersect(schema, value) {
|
|
111
|
-
return Object(schema, value);
|
|
112
|
-
}
|
|
113
|
-
function Literal(schema, value) {
|
|
114
|
-
return check_1.CheckValue.Visit(schema, value) ? value : create_1.CreateValue.Create(schema);
|
|
115
|
-
}
|
|
116
|
-
function Null(schema, value) {
|
|
117
|
-
return check_1.CheckValue.Visit(schema, value) ? value : create_1.CreateValue.Create(schema);
|
|
118
|
-
}
|
|
119
|
-
function Number(schema, value) {
|
|
120
|
-
return check_1.CheckValue.Visit(schema, value) ? value : create_1.CreateValue.Create(schema);
|
|
121
|
-
}
|
|
122
|
-
function Object(schema, value) {
|
|
123
|
-
if (check_1.CheckValue.Visit(schema, value))
|
|
124
|
-
return value;
|
|
125
|
-
if (value === null || typeof value !== 'object')
|
|
126
|
-
return create_1.CreateValue.Create(schema);
|
|
127
|
-
ids.set(schema.$id, schema);
|
|
128
|
-
const required = new Set(schema.required || []);
|
|
129
|
-
const result = {};
|
|
130
|
-
for (const [key, property] of globalThis.Object.entries(schema.properties)) {
|
|
131
|
-
if (!required.has(key) && value[key] === undefined)
|
|
132
|
-
continue;
|
|
133
|
-
result[key] = Create(property, value[key]);
|
|
134
|
-
}
|
|
135
|
-
return result;
|
|
136
|
-
}
|
|
137
|
-
function Promise(schema, value) {
|
|
138
|
-
return check_1.CheckValue.Visit(schema, value) ? value : create_1.CreateValue.Create(schema);
|
|
139
|
-
}
|
|
140
|
-
function Record(schema, value) {
|
|
141
|
-
if (check_1.CheckValue.Visit(schema, value))
|
|
142
|
-
return value;
|
|
143
|
-
if (value === null || typeof value !== 'object' || globalThis.Array.isArray(value))
|
|
144
|
-
return create_1.CreateValue.Create(schema);
|
|
145
|
-
const subschemaKey = globalThis.Object.keys(schema.patternProperties)[0];
|
|
146
|
-
const subschema = schema.patternProperties[subschemaKey];
|
|
147
|
-
const result = {};
|
|
148
|
-
for (const [key, property] of globalThis.Object.entries(value)) {
|
|
149
|
-
result[key] = Create(subschema, property);
|
|
150
|
-
}
|
|
151
|
-
return result;
|
|
152
|
-
}
|
|
153
|
-
function Recursive(schema, value) {
|
|
154
|
-
throw Error('Cannot patch recursive schemas');
|
|
155
|
-
}
|
|
156
|
-
function Ref(schema, value) {
|
|
157
|
-
throw Error('Cannot patch referenced schemas');
|
|
158
|
-
}
|
|
159
|
-
function Self(schema, value) {
|
|
160
|
-
if (!ids.has(schema.$ref))
|
|
161
|
-
throw new Error(`Upcast: Cannot locate schema with $id '${schema.$id}' for referenced type`);
|
|
162
|
-
return Object(ids.get(schema.$ref), value);
|
|
163
|
-
}
|
|
164
|
-
function String(schema, value) {
|
|
165
|
-
return check_1.CheckValue.Visit(schema, value) ? value : create_1.CreateValue.Create(schema);
|
|
166
|
-
}
|
|
167
|
-
function Tuple(schema, value) {
|
|
168
|
-
if (check_1.CheckValue.Visit(schema, value))
|
|
169
|
-
return value;
|
|
170
|
-
if (!globalThis.Array.isArray(value))
|
|
171
|
-
return create_1.CreateValue.Create(schema);
|
|
172
|
-
if (schema.items === undefined)
|
|
173
|
-
return [];
|
|
174
|
-
return schema.items.map((schema, index) => Create(schema, value[index]));
|
|
175
|
-
}
|
|
176
|
-
function Undefined(schema, value) {
|
|
177
|
-
return check_1.CheckValue.Visit(schema, value) ? value : create_1.CreateValue.Create(schema);
|
|
178
|
-
}
|
|
179
|
-
function Union(schema, value) {
|
|
180
|
-
return UpcastUnionValue.Create(schema, value);
|
|
181
|
-
}
|
|
182
|
-
function Uint8Array(schema, value) {
|
|
183
|
-
return check_1.CheckValue.Visit(schema, value) ? value : create_1.CreateValue.Create(schema);
|
|
184
|
-
}
|
|
185
|
-
function Unknown(schema, value) {
|
|
186
|
-
return check_1.CheckValue.Visit(schema, value) ? value : create_1.CreateValue.Create(schema);
|
|
187
|
-
}
|
|
188
|
-
function Void(schema, value) {
|
|
189
|
-
return check_1.CheckValue.Visit(schema, value) ? value : create_1.CreateValue.Create(schema);
|
|
190
|
-
}
|
|
191
|
-
function Create(schema, value) {
|
|
192
|
-
const anySchema = schema;
|
|
193
|
-
switch (schema[Types.Kind]) {
|
|
194
|
-
case 'Any':
|
|
195
|
-
return Any(anySchema, value);
|
|
196
|
-
case 'Array':
|
|
197
|
-
return Array(anySchema, value);
|
|
198
|
-
case 'Boolean':
|
|
199
|
-
return Boolean(anySchema, value);
|
|
200
|
-
case 'Constructor':
|
|
201
|
-
return Constructor(anySchema, value);
|
|
202
|
-
case 'Enum':
|
|
203
|
-
return Enum(anySchema, value);
|
|
204
|
-
case 'Function':
|
|
205
|
-
return Function(anySchema, value);
|
|
206
|
-
case 'Integer':
|
|
207
|
-
return Integer(anySchema, value);
|
|
208
|
-
case 'Intersect':
|
|
209
|
-
return Intersect(anySchema, value);
|
|
210
|
-
case 'Literal':
|
|
211
|
-
return Literal(anySchema, value);
|
|
212
|
-
case 'Null':
|
|
213
|
-
return Null(anySchema, value);
|
|
214
|
-
case 'Number':
|
|
215
|
-
return Number(anySchema, value);
|
|
216
|
-
case 'Object':
|
|
217
|
-
return Object(anySchema, value);
|
|
218
|
-
case 'Promise':
|
|
219
|
-
return Promise(anySchema, value);
|
|
220
|
-
case 'Record':
|
|
221
|
-
return Record(anySchema, value);
|
|
222
|
-
case 'Rec':
|
|
223
|
-
return Recursive(anySchema, value);
|
|
224
|
-
case 'Ref':
|
|
225
|
-
return Ref(anySchema, value);
|
|
226
|
-
case 'Self':
|
|
227
|
-
return Self(anySchema, value);
|
|
228
|
-
case 'String':
|
|
229
|
-
return String(anySchema, value);
|
|
230
|
-
case 'Tuple':
|
|
231
|
-
return Tuple(anySchema, value);
|
|
232
|
-
case 'Undefined':
|
|
233
|
-
return Undefined(anySchema, value);
|
|
234
|
-
case 'Union':
|
|
235
|
-
return Union(anySchema, value);
|
|
236
|
-
case 'Uint8Array':
|
|
237
|
-
return Uint8Array(anySchema, value);
|
|
238
|
-
case 'Unknown':
|
|
239
|
-
return Unknown(anySchema, value);
|
|
240
|
-
case 'Void':
|
|
241
|
-
return Void(anySchema, value);
|
|
242
|
-
default:
|
|
243
|
-
throw Error(`Unknown schema kind '${schema[Types.Kind]}'`);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
UpcastValue.Create = Create;
|
|
247
|
-
})(UpcastValue = exports.UpcastValue || (exports.UpcastValue = {}));
|