@sinclair/typebox 0.26.0-dev.1 → 0.26.0-dev.3

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/value/cast.js CHANGED
@@ -1,348 +1,348 @@
1
- "use strict";
2
- /*--------------------------------------------------------------------------
3
-
4
- @sinclair/typebox/value
5
-
6
- The MIT License (MIT)
7
-
8
- Copyright (c) 2017-2023 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.ValueCast = exports.ValueCastUnknownTypeError = exports.ValueCastRecursiveTypeError = exports.ValueCastNeverTypeError = exports.ValueCastArrayUniqueItemsTypeError = exports.ValueCastReferenceTypeError = void 0;
31
- const Types = require("../typebox");
32
- const create_1 = require("./create");
33
- const check_1 = require("./check");
34
- const clone_1 = require("./clone");
35
- // ----------------------------------------------------------------------------------------------
36
- // Errors
37
- // ----------------------------------------------------------------------------------------------
38
- class ValueCastReferenceTypeError extends Error {
39
- constructor(schema) {
40
- super(`ValueCast: Cannot locate referenced schema with $id '${schema.$ref}'`);
41
- this.schema = schema;
42
- }
43
- }
44
- exports.ValueCastReferenceTypeError = ValueCastReferenceTypeError;
45
- class ValueCastArrayUniqueItemsTypeError extends Error {
46
- constructor(schema, value) {
47
- super('ValueCast: Array cast produced invalid data due to uniqueItems constraint');
48
- this.schema = schema;
49
- this.value = value;
50
- }
51
- }
52
- exports.ValueCastArrayUniqueItemsTypeError = ValueCastArrayUniqueItemsTypeError;
53
- class ValueCastNeverTypeError extends Error {
54
- constructor(schema) {
55
- super('ValueCast: Never types cannot be cast');
56
- this.schema = schema;
57
- }
58
- }
59
- exports.ValueCastNeverTypeError = ValueCastNeverTypeError;
60
- class ValueCastRecursiveTypeError extends Error {
61
- constructor(schema) {
62
- super('ValueCast.Recursive: Cannot cast recursive schemas');
63
- this.schema = schema;
64
- }
65
- }
66
- exports.ValueCastRecursiveTypeError = ValueCastRecursiveTypeError;
67
- class ValueCastUnknownTypeError extends Error {
68
- constructor(schema) {
69
- super('ValueCast: Unknown type');
70
- this.schema = schema;
71
- }
72
- }
73
- exports.ValueCastUnknownTypeError = ValueCastUnknownTypeError;
74
- // ----------------------------------------------------------------------------------------------
75
- // The following will score a schema against a value. For objects, the score is the tally of
76
- // points awarded for each property of the value. Property points are (1.0 / propertyCount)
77
- // to prevent large property counts biasing results. Properties that match literal values are
78
- // maximally awarded as literals are typically used as union discriminator fields.
79
- // ----------------------------------------------------------------------------------------------
80
- var UnionCastCreate;
81
- (function (UnionCastCreate) {
82
- function Score(schema, value) {
83
- if (schema[Types.Kind] === 'Object' && typeof value === 'object' && value !== null) {
84
- const object = schema;
85
- const keys = Object.keys(value);
86
- const entries = globalThis.Object.entries(object.properties);
87
- const [point, max] = [1 / entries.length, entries.length];
88
- return entries.reduce((acc, [key, schema]) => {
89
- const literal = schema[Types.Kind] === 'Literal' && schema.const === value[key] ? max : 0;
90
- const checks = check_1.ValueCheck.Check(schema, value[key]) ? point : 0;
91
- const exists = keys.includes(key) ? point : 0;
92
- return acc + (literal + checks + exists);
93
- }, 0);
94
- }
95
- else {
96
- return check_1.ValueCheck.Check(schema, value) ? 1 : 0;
97
- }
98
- }
99
- function Select(union, value) {
100
- let [select, best] = [union.anyOf[0], 0];
101
- for (const schema of union.anyOf) {
102
- const score = Score(schema, value);
103
- if (score > best) {
104
- select = schema;
105
- best = score;
106
- }
107
- }
108
- return select;
109
- }
110
- function Create(union, value) {
111
- if (union.default !== undefined) {
112
- return union.default;
113
- }
114
- else {
115
- const schema = Select(union, value);
116
- return ValueCast.Cast(schema, value);
117
- }
118
- }
119
- UnionCastCreate.Create = Create;
120
- })(UnionCastCreate || (UnionCastCreate = {}));
121
- var ValueCast;
122
- (function (ValueCast) {
123
- // ----------------------------------------------------------------------------------------------
124
- // Guards
125
- // ----------------------------------------------------------------------------------------------
126
- function IsObject(value) {
127
- return typeof value === 'object' && value !== null && !globalThis.Array.isArray(value);
128
- }
129
- function IsArray(value) {
130
- return typeof value === 'object' && globalThis.Array.isArray(value);
131
- }
132
- function IsNumber(value) {
133
- return typeof value === 'number' && !isNaN(value);
134
- }
135
- // ----------------------------------------------------------------------------------------------
136
- // Cast
137
- // ----------------------------------------------------------------------------------------------
138
- function Any(schema, value) {
139
- return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
140
- }
141
- function Array(schema, value) {
142
- if (check_1.ValueCheck.Check(schema, value))
143
- return clone_1.ValueClone.Clone(value);
144
- const created = IsArray(value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
145
- const minimum = IsNumber(schema.minItems) && created.length < schema.minItems ? [...created, ...globalThis.Array.from({ length: schema.minItems - created.length }, () => null)] : created;
146
- const maximum = IsNumber(schema.maxItems) && minimum.length > schema.maxItems ? minimum.slice(0, schema.maxItems) : minimum;
147
- const casted = maximum.map((value) => Visit(schema.items, value));
148
- if (schema.uniqueItems !== true)
149
- return casted;
150
- const unique = [...new Set(casted)];
151
- if (!check_1.ValueCheck.Check(schema, unique))
152
- throw new ValueCastArrayUniqueItemsTypeError(schema, unique);
153
- return unique;
154
- }
155
- function BigInt(schema, value) {
156
- return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
157
- }
158
- function Boolean(schema, value) {
159
- return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
160
- }
161
- function Constructor(schema, value) {
162
- if (check_1.ValueCheck.Check(schema, value))
163
- return create_1.ValueCreate.Create(schema);
164
- const required = new Set(schema.returns.required || []);
165
- const result = function () { };
166
- for (const [key, property] of globalThis.Object.entries(schema.returns.properties)) {
167
- if (!required.has(key) && value.prototype[key] === undefined)
168
- continue;
169
- result.prototype[key] = Visit(property, value.prototype[key]);
170
- }
171
- return result;
172
- }
173
- function Date(schema, value) {
174
- return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
175
- }
176
- function Function(schema, value) {
177
- return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
178
- }
179
- function Integer(schema, value) {
180
- return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
181
- }
182
- function Intersect(schema, value) {
183
- const created = create_1.ValueCreate.Create(schema);
184
- const mapped = IsObject(created) && IsObject(value) ? { ...created, ...value } : value;
185
- return check_1.ValueCheck.Check(schema, mapped) ? mapped : create_1.ValueCreate.Create(schema);
186
- }
187
- function Literal(schema, value) {
188
- return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
189
- }
190
- function Never(schema, value) {
191
- throw new ValueCastNeverTypeError(schema);
192
- }
193
- function Not(schema, value) {
194
- return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema.allOf[1]);
195
- }
196
- function Null(schema, value) {
197
- return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
198
- }
199
- function Number(schema, value) {
200
- return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
201
- }
202
- function Object(schema, value) {
203
- if (check_1.ValueCheck.Check(schema, value))
204
- return value;
205
- if (value === null || typeof value !== 'object')
206
- return create_1.ValueCreate.Create(schema);
207
- const required = new Set(schema.required || []);
208
- const result = {};
209
- for (const [key, property] of globalThis.Object.entries(schema.properties)) {
210
- if (!required.has(key) && value[key] === undefined)
211
- continue;
212
- result[key] = Visit(property, value[key]);
213
- }
214
- // additional schema properties
215
- if (typeof schema.additionalProperties === 'object') {
216
- const propertyNames = globalThis.Object.getOwnPropertyNames(schema.properties);
217
- for (const propertyName of globalThis.Object.getOwnPropertyNames(value)) {
218
- if (propertyNames.includes(propertyName))
219
- continue;
220
- result[propertyName] = Visit(schema.additionalProperties, value[propertyName]);
221
- }
222
- }
223
- return result;
224
- }
225
- function Promise(schema, value) {
226
- return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
227
- }
228
- function Record(schema, value) {
229
- if (check_1.ValueCheck.Check(schema, value))
230
- return clone_1.ValueClone.Clone(value);
231
- if (value === null || typeof value !== 'object' || globalThis.Array.isArray(value) || value instanceof globalThis.Date)
232
- return create_1.ValueCreate.Create(schema);
233
- const subschemaPropertyName = globalThis.Object.getOwnPropertyNames(schema.patternProperties)[0];
234
- const subschema = schema.patternProperties[subschemaPropertyName];
235
- const result = {};
236
- for (const [propKey, propValue] of globalThis.Object.entries(value)) {
237
- result[propKey] = Visit(subschema, propValue);
238
- }
239
- return result;
240
- }
241
- function Ref(schema, value) {
242
- return Visit(Types.ReferenceRegistry.DerefOne(schema), value);
243
- }
244
- function Self(schema, value) {
245
- return Visit(Types.ReferenceRegistry.DerefOne(schema), value);
246
- }
247
- function String(schema, value) {
248
- return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
249
- }
250
- function Symbol(schema, value) {
251
- return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
252
- }
253
- function Tuple(schema, value) {
254
- if (check_1.ValueCheck.Check(schema, value))
255
- return clone_1.ValueClone.Clone(value);
256
- if (!globalThis.Array.isArray(value))
257
- return create_1.ValueCreate.Create(schema);
258
- if (schema.items === undefined)
259
- return [];
260
- return schema.items.map((schema, index) => Visit(schema, value[index]));
261
- }
262
- function Undefined(schema, value) {
263
- return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
264
- }
265
- function Union(schema, value) {
266
- return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : UnionCastCreate.Create(schema, value);
267
- }
268
- function Uint8Array(schema, value) {
269
- return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
270
- }
271
- function Unknown(schema, value) {
272
- return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
273
- }
274
- function Void(schema, value) {
275
- return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
276
- }
277
- function UserDefined(schema, value) {
278
- return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
279
- }
280
- function Visit(schema, value) {
281
- const anySchema = schema;
282
- switch (schema[Types.Kind]) {
283
- case 'Any':
284
- return Any(anySchema, value);
285
- case 'Array':
286
- return Array(anySchema, value);
287
- case 'BigInt':
288
- return BigInt(anySchema, value);
289
- case 'Boolean':
290
- return Boolean(anySchema, value);
291
- case 'Constructor':
292
- return Constructor(anySchema, value);
293
- case 'Date':
294
- return Date(anySchema, value);
295
- case 'Function':
296
- return Function(anySchema, value);
297
- case 'Integer':
298
- return Integer(anySchema, value);
299
- case 'Intersect':
300
- return Intersect(anySchema, value);
301
- case 'Literal':
302
- return Literal(anySchema, value);
303
- case 'Never':
304
- return Never(anySchema, value);
305
- case 'Not':
306
- return Not(anySchema, value);
307
- case 'Null':
308
- return Null(anySchema, value);
309
- case 'Number':
310
- return Number(anySchema, value);
311
- case 'Object':
312
- return Object(anySchema, value);
313
- case 'Promise':
314
- return Promise(anySchema, value);
315
- case 'Record':
316
- return Record(anySchema, value);
317
- case 'Ref':
318
- return Ref(anySchema, value);
319
- case 'Self':
320
- return Self(anySchema, value);
321
- case 'String':
322
- return String(anySchema, value);
323
- case 'Symbol':
324
- return Symbol(anySchema, value);
325
- case 'Tuple':
326
- return Tuple(anySchema, value);
327
- case 'Undefined':
328
- return Undefined(anySchema, value);
329
- case 'Union':
330
- return Union(anySchema, value);
331
- case 'Uint8Array':
332
- return Uint8Array(anySchema, value);
333
- case 'Unknown':
334
- return Unknown(anySchema, value);
335
- case 'Void':
336
- return Void(anySchema, value);
337
- default:
338
- if (!Types.TypeRegistry.Has(anySchema[Types.Kind]))
339
- throw new ValueCastUnknownTypeError(anySchema);
340
- return UserDefined(anySchema, value);
341
- }
342
- }
343
- ValueCast.Visit = Visit;
344
- function Cast(schema, value) {
345
- return Visit(schema, clone_1.ValueClone.Clone(value));
346
- }
347
- ValueCast.Cast = Cast;
348
- })(ValueCast = exports.ValueCast || (exports.ValueCast = {}));
1
+ "use strict";
2
+ /*--------------------------------------------------------------------------
3
+
4
+ @sinclair/typebox/value
5
+
6
+ The MIT License (MIT)
7
+
8
+ Copyright (c) 2017-2023 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.ValueCast = exports.ValueCastUnknownTypeError = exports.ValueCastRecursiveTypeError = exports.ValueCastNeverTypeError = exports.ValueCastArrayUniqueItemsTypeError = exports.ValueCastReferenceTypeError = void 0;
31
+ const Types = require("../typebox");
32
+ const create_1 = require("./create");
33
+ const check_1 = require("./check");
34
+ const clone_1 = require("./clone");
35
+ // ----------------------------------------------------------------------------------------------
36
+ // Errors
37
+ // ----------------------------------------------------------------------------------------------
38
+ class ValueCastReferenceTypeError extends Error {
39
+ constructor(schema) {
40
+ super(`ValueCast: Cannot locate referenced schema with $id '${schema.$ref}'`);
41
+ this.schema = schema;
42
+ }
43
+ }
44
+ exports.ValueCastReferenceTypeError = ValueCastReferenceTypeError;
45
+ class ValueCastArrayUniqueItemsTypeError extends Error {
46
+ constructor(schema, value) {
47
+ super('ValueCast: Array cast produced invalid data due to uniqueItems constraint');
48
+ this.schema = schema;
49
+ this.value = value;
50
+ }
51
+ }
52
+ exports.ValueCastArrayUniqueItemsTypeError = ValueCastArrayUniqueItemsTypeError;
53
+ class ValueCastNeverTypeError extends Error {
54
+ constructor(schema) {
55
+ super('ValueCast: Never types cannot be cast');
56
+ this.schema = schema;
57
+ }
58
+ }
59
+ exports.ValueCastNeverTypeError = ValueCastNeverTypeError;
60
+ class ValueCastRecursiveTypeError extends Error {
61
+ constructor(schema) {
62
+ super('ValueCast.Recursive: Cannot cast recursive schemas');
63
+ this.schema = schema;
64
+ }
65
+ }
66
+ exports.ValueCastRecursiveTypeError = ValueCastRecursiveTypeError;
67
+ class ValueCastUnknownTypeError extends Error {
68
+ constructor(schema) {
69
+ super('ValueCast: Unknown type');
70
+ this.schema = schema;
71
+ }
72
+ }
73
+ exports.ValueCastUnknownTypeError = ValueCastUnknownTypeError;
74
+ // ----------------------------------------------------------------------------------------------
75
+ // The following will score a schema against a value. For objects, the score is the tally of
76
+ // points awarded for each property of the value. Property points are (1.0 / propertyCount)
77
+ // to prevent large property counts biasing results. Properties that match literal values are
78
+ // maximally awarded as literals are typically used as union discriminator fields.
79
+ // ----------------------------------------------------------------------------------------------
80
+ var UnionCastCreate;
81
+ (function (UnionCastCreate) {
82
+ function Score(schema, value) {
83
+ if (schema[Types.Kind] === 'Object' && typeof value === 'object' && value !== null) {
84
+ const object = schema;
85
+ const keys = Object.keys(value);
86
+ const entries = globalThis.Object.entries(object.properties);
87
+ const [point, max] = [1 / entries.length, entries.length];
88
+ return entries.reduce((acc, [key, schema]) => {
89
+ const literal = schema[Types.Kind] === 'Literal' && schema.const === value[key] ? max : 0;
90
+ const checks = check_1.ValueCheck.Check(schema, value[key]) ? point : 0;
91
+ const exists = keys.includes(key) ? point : 0;
92
+ return acc + (literal + checks + exists);
93
+ }, 0);
94
+ }
95
+ else {
96
+ return check_1.ValueCheck.Check(schema, value) ? 1 : 0;
97
+ }
98
+ }
99
+ function Select(union, value) {
100
+ let [select, best] = [union.anyOf[0], 0];
101
+ for (const schema of union.anyOf) {
102
+ const score = Score(schema, value);
103
+ if (score > best) {
104
+ select = schema;
105
+ best = score;
106
+ }
107
+ }
108
+ return select;
109
+ }
110
+ function Create(union, value) {
111
+ if (union.default !== undefined) {
112
+ return union.default;
113
+ }
114
+ else {
115
+ const schema = Select(union, value);
116
+ return ValueCast.Cast(schema, value);
117
+ }
118
+ }
119
+ UnionCastCreate.Create = Create;
120
+ })(UnionCastCreate || (UnionCastCreate = {}));
121
+ var ValueCast;
122
+ (function (ValueCast) {
123
+ // ----------------------------------------------------------------------------------------------
124
+ // Guards
125
+ // ----------------------------------------------------------------------------------------------
126
+ function IsObject(value) {
127
+ return typeof value === 'object' && value !== null && !globalThis.Array.isArray(value);
128
+ }
129
+ function IsArray(value) {
130
+ return typeof value === 'object' && globalThis.Array.isArray(value);
131
+ }
132
+ function IsNumber(value) {
133
+ return typeof value === 'number' && !isNaN(value);
134
+ }
135
+ // ----------------------------------------------------------------------------------------------
136
+ // Cast
137
+ // ----------------------------------------------------------------------------------------------
138
+ function Any(schema, value) {
139
+ return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
140
+ }
141
+ function Array(schema, value) {
142
+ if (check_1.ValueCheck.Check(schema, value))
143
+ return clone_1.ValueClone.Clone(value);
144
+ const created = IsArray(value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
145
+ const minimum = IsNumber(schema.minItems) && created.length < schema.minItems ? [...created, ...globalThis.Array.from({ length: schema.minItems - created.length }, () => null)] : created;
146
+ const maximum = IsNumber(schema.maxItems) && minimum.length > schema.maxItems ? minimum.slice(0, schema.maxItems) : minimum;
147
+ const casted = maximum.map((value) => Visit(schema.items, value));
148
+ if (schema.uniqueItems !== true)
149
+ return casted;
150
+ const unique = [...new Set(casted)];
151
+ if (!check_1.ValueCheck.Check(schema, unique))
152
+ throw new ValueCastArrayUniqueItemsTypeError(schema, unique);
153
+ return unique;
154
+ }
155
+ function BigInt(schema, value) {
156
+ return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
157
+ }
158
+ function Boolean(schema, value) {
159
+ return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
160
+ }
161
+ function Constructor(schema, value) {
162
+ if (check_1.ValueCheck.Check(schema, value))
163
+ return create_1.ValueCreate.Create(schema);
164
+ const required = new Set(schema.returns.required || []);
165
+ const result = function () { };
166
+ for (const [key, property] of globalThis.Object.entries(schema.returns.properties)) {
167
+ if (!required.has(key) && value.prototype[key] === undefined)
168
+ continue;
169
+ result.prototype[key] = Visit(property, value.prototype[key]);
170
+ }
171
+ return result;
172
+ }
173
+ function Date(schema, value) {
174
+ return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
175
+ }
176
+ function Function(schema, value) {
177
+ return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
178
+ }
179
+ function Integer(schema, value) {
180
+ return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
181
+ }
182
+ function Intersect(schema, value) {
183
+ const created = create_1.ValueCreate.Create(schema);
184
+ const mapped = IsObject(created) && IsObject(value) ? { ...created, ...value } : value;
185
+ return check_1.ValueCheck.Check(schema, mapped) ? mapped : create_1.ValueCreate.Create(schema);
186
+ }
187
+ function Literal(schema, value) {
188
+ return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
189
+ }
190
+ function Never(schema, value) {
191
+ throw new ValueCastNeverTypeError(schema);
192
+ }
193
+ function Not(schema, value) {
194
+ return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema.allOf[1]);
195
+ }
196
+ function Null(schema, value) {
197
+ return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
198
+ }
199
+ function Number(schema, value) {
200
+ return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
201
+ }
202
+ function Object(schema, value) {
203
+ if (check_1.ValueCheck.Check(schema, value))
204
+ return value;
205
+ if (value === null || typeof value !== 'object')
206
+ return create_1.ValueCreate.Create(schema);
207
+ const required = new Set(schema.required || []);
208
+ const result = {};
209
+ for (const [key, property] of globalThis.Object.entries(schema.properties)) {
210
+ if (!required.has(key) && value[key] === undefined)
211
+ continue;
212
+ result[key] = Visit(property, value[key]);
213
+ }
214
+ // additional schema properties
215
+ if (typeof schema.additionalProperties === 'object') {
216
+ const propertyNames = globalThis.Object.getOwnPropertyNames(schema.properties);
217
+ for (const propertyName of globalThis.Object.getOwnPropertyNames(value)) {
218
+ if (propertyNames.includes(propertyName))
219
+ continue;
220
+ result[propertyName] = Visit(schema.additionalProperties, value[propertyName]);
221
+ }
222
+ }
223
+ return result;
224
+ }
225
+ function Promise(schema, value) {
226
+ return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
227
+ }
228
+ function Record(schema, value) {
229
+ if (check_1.ValueCheck.Check(schema, value))
230
+ return clone_1.ValueClone.Clone(value);
231
+ if (value === null || typeof value !== 'object' || globalThis.Array.isArray(value) || value instanceof globalThis.Date)
232
+ return create_1.ValueCreate.Create(schema);
233
+ const subschemaPropertyName = globalThis.Object.getOwnPropertyNames(schema.patternProperties)[0];
234
+ const subschema = schema.patternProperties[subschemaPropertyName];
235
+ const result = {};
236
+ for (const [propKey, propValue] of globalThis.Object.entries(value)) {
237
+ result[propKey] = Visit(subschema, propValue);
238
+ }
239
+ return result;
240
+ }
241
+ function Ref(schema, value) {
242
+ return Visit(Types.ReferenceRegistry.DerefOne(schema), value);
243
+ }
244
+ function Self(schema, value) {
245
+ return Visit(Types.ReferenceRegistry.DerefOne(schema), value);
246
+ }
247
+ function String(schema, value) {
248
+ return check_1.ValueCheck.Check(schema, value) ? value : create_1.ValueCreate.Create(schema);
249
+ }
250
+ function Symbol(schema, value) {
251
+ return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
252
+ }
253
+ function Tuple(schema, value) {
254
+ if (check_1.ValueCheck.Check(schema, value))
255
+ return clone_1.ValueClone.Clone(value);
256
+ if (!globalThis.Array.isArray(value))
257
+ return create_1.ValueCreate.Create(schema);
258
+ if (schema.items === undefined)
259
+ return [];
260
+ return schema.items.map((schema, index) => Visit(schema, value[index]));
261
+ }
262
+ function Undefined(schema, value) {
263
+ return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
264
+ }
265
+ function Union(schema, value) {
266
+ return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : UnionCastCreate.Create(schema, value);
267
+ }
268
+ function Uint8Array(schema, value) {
269
+ return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
270
+ }
271
+ function Unknown(schema, value) {
272
+ return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
273
+ }
274
+ function Void(schema, value) {
275
+ return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
276
+ }
277
+ function UserDefined(schema, value) {
278
+ return check_1.ValueCheck.Check(schema, value) ? clone_1.ValueClone.Clone(value) : create_1.ValueCreate.Create(schema);
279
+ }
280
+ function Visit(schema, value) {
281
+ const anySchema = schema;
282
+ switch (schema[Types.Kind]) {
283
+ case 'Any':
284
+ return Any(anySchema, value);
285
+ case 'Array':
286
+ return Array(anySchema, value);
287
+ case 'BigInt':
288
+ return BigInt(anySchema, value);
289
+ case 'Boolean':
290
+ return Boolean(anySchema, value);
291
+ case 'Constructor':
292
+ return Constructor(anySchema, value);
293
+ case 'Date':
294
+ return Date(anySchema, value);
295
+ case 'Function':
296
+ return Function(anySchema, value);
297
+ case 'Integer':
298
+ return Integer(anySchema, value);
299
+ case 'Intersect':
300
+ return Intersect(anySchema, value);
301
+ case 'Literal':
302
+ return Literal(anySchema, value);
303
+ case 'Never':
304
+ return Never(anySchema, value);
305
+ case 'Not':
306
+ return Not(anySchema, value);
307
+ case 'Null':
308
+ return Null(anySchema, value);
309
+ case 'Number':
310
+ return Number(anySchema, value);
311
+ case 'Object':
312
+ return Object(anySchema, value);
313
+ case 'Promise':
314
+ return Promise(anySchema, value);
315
+ case 'Record':
316
+ return Record(anySchema, value);
317
+ case 'Ref':
318
+ return Ref(anySchema, value);
319
+ case 'Self':
320
+ return Self(anySchema, value);
321
+ case 'String':
322
+ return String(anySchema, value);
323
+ case 'Symbol':
324
+ return Symbol(anySchema, value);
325
+ case 'Tuple':
326
+ return Tuple(anySchema, value);
327
+ case 'Undefined':
328
+ return Undefined(anySchema, value);
329
+ case 'Union':
330
+ return Union(anySchema, value);
331
+ case 'Uint8Array':
332
+ return Uint8Array(anySchema, value);
333
+ case 'Unknown':
334
+ return Unknown(anySchema, value);
335
+ case 'Void':
336
+ return Void(anySchema, value);
337
+ default:
338
+ if (!Types.TypeRegistry.Has(anySchema[Types.Kind]))
339
+ throw new ValueCastUnknownTypeError(anySchema);
340
+ return UserDefined(anySchema, value);
341
+ }
342
+ }
343
+ ValueCast.Visit = Visit;
344
+ function Cast(schema, value) {
345
+ return Visit(schema, clone_1.ValueClone.Clone(value));
346
+ }
347
+ ValueCast.Cast = Cast;
348
+ })(ValueCast = exports.ValueCast || (exports.ValueCast = {}));