@sinclair/typebox 0.26.0-dev.4 → 0.26.0
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 +9 -4
- package/compiler/compiler.js +135 -108
- package/errors/errors.d.ts +5 -1
- package/errors/errors.js +173 -157
- package/package.json +1 -1
- package/readme.md +114 -101
- package/system/system.d.ts +4 -0
- package/system/system.js +15 -2
- package/typebox.d.ts +27 -36
- package/typebox.js +108 -173
- package/value/cast.d.ts +6 -2
- package/value/cast.js +130 -111
- package/value/check.d.ts +5 -1
- package/value/check.js +168 -154
- package/value/convert.d.ts +6 -6
- package/value/convert.js +83 -74
- package/value/create.d.ts +6 -2
- package/value/create.js +102 -77
- package/value/value.d.ts +10 -0
- package/value/value.js +15 -15
package/typebox.js
CHANGED
|
@@ -27,106 +27,70 @@ THE SOFTWARE.
|
|
|
27
27
|
|
|
28
28
|
---------------------------------------------------------------------------*/
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.Type = exports.StandardType = exports.ExtendedTypeBuilder = exports.StandardTypeBuilder = exports.TypeBuilder = exports.KeyResolver = exports.ObjectMap = exports.TypeClone = exports.TypeExtends = exports.TypeExtendsResult = exports.ExtendsUndefined = exports.TypeGuard = exports.TypeGuardUnknownTypeError = exports.FormatRegistry = exports.
|
|
30
|
+
exports.Type = exports.StandardType = exports.ExtendedTypeBuilder = exports.StandardTypeBuilder = exports.TypeBuilder = exports.KeyResolver = exports.ObjectMap = exports.TypeClone = exports.TypeExtends = exports.TypeExtendsResult = exports.ExtendsUndefined = exports.TypeGuard = exports.TypeGuardUnknownTypeError = exports.FormatRegistry = exports.TypeRegistry = exports.Kind = exports.Hint = exports.Modifier = void 0;
|
|
31
31
|
// --------------------------------------------------------------------------
|
|
32
32
|
// Compositing Symbols
|
|
33
33
|
// --------------------------------------------------------------------------
|
|
34
|
-
exports.Recursive = Symbol.for('TypeBox.Recursive');
|
|
35
34
|
exports.Modifier = Symbol.for('TypeBox.Modifier');
|
|
36
35
|
exports.Hint = Symbol.for('TypeBox.Hint');
|
|
37
36
|
exports.Kind = Symbol.for('TypeBox.Kind');
|
|
37
|
+
/** A registry for user defined types */
|
|
38
38
|
var TypeRegistry;
|
|
39
39
|
(function (TypeRegistry) {
|
|
40
|
-
const
|
|
40
|
+
const map = new Map();
|
|
41
|
+
/** Returns the entries in this registry */
|
|
42
|
+
function Entries() {
|
|
43
|
+
return new Map(map);
|
|
44
|
+
}
|
|
45
|
+
TypeRegistry.Entries = Entries;
|
|
41
46
|
/** Clears all user defined types */
|
|
42
47
|
function Clear() {
|
|
43
|
-
return
|
|
48
|
+
return map.clear();
|
|
44
49
|
}
|
|
45
50
|
TypeRegistry.Clear = Clear;
|
|
46
51
|
/** Returns true if this registry contains this kind */
|
|
47
52
|
function Has(kind) {
|
|
48
|
-
return
|
|
53
|
+
return map.has(kind);
|
|
49
54
|
}
|
|
50
55
|
TypeRegistry.Has = Has;
|
|
51
56
|
/** Sets a validation function for a user defined type */
|
|
52
57
|
function Set(kind, func) {
|
|
53
|
-
|
|
58
|
+
map.set(kind, func);
|
|
54
59
|
}
|
|
55
60
|
TypeRegistry.Set = Set;
|
|
56
61
|
/** Gets a custom validation function for a user defined type */
|
|
57
62
|
function Get(kind) {
|
|
58
|
-
return
|
|
63
|
+
return map.get(kind);
|
|
59
64
|
}
|
|
60
65
|
TypeRegistry.Get = Get;
|
|
61
66
|
})(TypeRegistry = exports.TypeRegistry || (exports.TypeRegistry = {}));
|
|
62
|
-
|
|
63
|
-
// ReferenceRegistry
|
|
64
|
-
// --------------------------------------------------------------------------
|
|
65
|
-
var ReferenceRegistry;
|
|
66
|
-
(function (ReferenceRegistry) {
|
|
67
|
-
const references = new Map();
|
|
68
|
-
/** Clears the reference registry */
|
|
69
|
-
function Clear() {
|
|
70
|
-
return references.clear();
|
|
71
|
-
}
|
|
72
|
-
ReferenceRegistry.Clear = Clear;
|
|
73
|
-
/** Returns true if this registry contains this schema */
|
|
74
|
-
function Has(schema) {
|
|
75
|
-
return references.has(schema.$id);
|
|
76
|
-
}
|
|
77
|
-
ReferenceRegistry.Has = Has;
|
|
78
|
-
/** Sets this schema on this registry if a $id exists */
|
|
79
|
-
function Set(schema) {
|
|
80
|
-
const $id = typeof schema === 'object' && schema !== null && typeof schema['$id'] === 'string' ? schema['$id'] : undefined;
|
|
81
|
-
if ($id !== undefined)
|
|
82
|
-
references.set($id, schema);
|
|
83
|
-
}
|
|
84
|
-
ReferenceRegistry.Set = Set;
|
|
85
|
-
/** Dereferences the schema one level deep */
|
|
86
|
-
function DerefOne(schema) {
|
|
87
|
-
if (TypeGuard.TRef(schema) || TypeGuard.TSelf(schema)) {
|
|
88
|
-
if (!references.has(schema.$ref))
|
|
89
|
-
throw Error(`ReferenceRegistry: Cannot deref schema with $id '${schema.$ref}'`);
|
|
90
|
-
return references.get(schema.$ref);
|
|
91
|
-
}
|
|
92
|
-
else {
|
|
93
|
-
return schema;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
ReferenceRegistry.DerefOne = DerefOne;
|
|
97
|
-
/** Dereferences the schema recursively */
|
|
98
|
-
function Deref(schema) {
|
|
99
|
-
if (TypeGuard.TRef(schema)) {
|
|
100
|
-
return Deref(DerefOne(schema));
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
103
|
-
return schema;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
ReferenceRegistry.Deref = Deref;
|
|
107
|
-
})(ReferenceRegistry = exports.ReferenceRegistry || (exports.ReferenceRegistry = {}));
|
|
108
|
-
/** Provides functions to create user defined string formats */
|
|
67
|
+
/** A registry for user defined string formats */
|
|
109
68
|
var FormatRegistry;
|
|
110
69
|
(function (FormatRegistry) {
|
|
111
|
-
const
|
|
70
|
+
const map = new Map();
|
|
71
|
+
/** Returns the entries in this registry */
|
|
72
|
+
function Entries() {
|
|
73
|
+
return new Map(map);
|
|
74
|
+
}
|
|
75
|
+
FormatRegistry.Entries = Entries;
|
|
112
76
|
/** Clears all user defined string formats */
|
|
113
77
|
function Clear() {
|
|
114
|
-
return
|
|
78
|
+
return map.clear();
|
|
115
79
|
}
|
|
116
80
|
FormatRegistry.Clear = Clear;
|
|
117
81
|
/** Returns true if the user defined string format exists */
|
|
118
82
|
function Has(format) {
|
|
119
|
-
return
|
|
83
|
+
return map.has(format);
|
|
120
84
|
}
|
|
121
85
|
FormatRegistry.Has = Has;
|
|
122
86
|
/** Sets a validation function for a user defined string format */
|
|
123
87
|
function Set(format, func) {
|
|
124
|
-
|
|
88
|
+
map.set(format, func);
|
|
125
89
|
}
|
|
126
90
|
FormatRegistry.Set = Set;
|
|
127
91
|
/** Gets a validation function for a user defined string format */
|
|
128
92
|
function Get(format) {
|
|
129
|
-
return
|
|
93
|
+
return map.get(format);
|
|
130
94
|
}
|
|
131
95
|
FormatRegistry.Get = Get;
|
|
132
96
|
})(FormatRegistry = exports.FormatRegistry || (exports.FormatRegistry = {}));
|
|
@@ -140,6 +104,7 @@ class TypeGuardUnknownTypeError extends Error {
|
|
|
140
104
|
}
|
|
141
105
|
}
|
|
142
106
|
exports.TypeGuardUnknownTypeError = TypeGuardUnknownTypeError;
|
|
107
|
+
/** Provides functions to test if JavaScript values are TypeBox types */
|
|
143
108
|
var TypeGuard;
|
|
144
109
|
(function (TypeGuard) {
|
|
145
110
|
function IsObject(value) {
|
|
@@ -346,18 +311,7 @@ var TypeGuard;
|
|
|
346
311
|
TypeGuard.TLiteral = TLiteral;
|
|
347
312
|
/** Returns true if the given schema is TNever */
|
|
348
313
|
function TNever(schema) {
|
|
349
|
-
return
|
|
350
|
-
// IsArray(schema.allOf) &&
|
|
351
|
-
// schema.allOf.length === 2 &&
|
|
352
|
-
// IsObject(schema.allOf[0]) &&
|
|
353
|
-
// IsString(schema.allOf[0].type) &&
|
|
354
|
-
// schema.allOf[0].type === 'boolean' &&
|
|
355
|
-
// schema.allOf[0].const === false &&
|
|
356
|
-
// IsObject(schema.allOf[1]) &&
|
|
357
|
-
// IsString(schema.allOf[1].type) &&
|
|
358
|
-
// schema.allOf[1].type === 'boolean' &&
|
|
359
|
-
// schema.allOf[1].const === true
|
|
360
|
-
);
|
|
314
|
+
return TKind(schema) && schema[exports.Kind] === 'Never' && IsObject(schema.not) && globalThis.Object.getOwnPropertyNames(schema.not).length === 0;
|
|
361
315
|
}
|
|
362
316
|
TypeGuard.TNever = TNever;
|
|
363
317
|
/** Returns true if the given schema is TNot */
|
|
@@ -621,9 +575,9 @@ var TypeGuard;
|
|
|
621
575
|
// --------------------------------------------------------------------------
|
|
622
576
|
// ExtendsUndefined
|
|
623
577
|
// --------------------------------------------------------------------------
|
|
578
|
+
/** Fast undefined check used for properties of type undefined */
|
|
624
579
|
var ExtendsUndefined;
|
|
625
580
|
(function (ExtendsUndefined) {
|
|
626
|
-
/** Fast undefined check for properties of type undefined */
|
|
627
581
|
function Check(schema) {
|
|
628
582
|
if (schema[exports.Kind] === 'Undefined')
|
|
629
583
|
return true;
|
|
@@ -1307,56 +1261,54 @@ var TypeExtends;
|
|
|
1307
1261
|
return TypeGuard.TVoid(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
1308
1262
|
}
|
|
1309
1263
|
function Visit(left, right) {
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
if (TypeGuard.
|
|
1313
|
-
return
|
|
1314
|
-
if (TypeGuard.
|
|
1315
|
-
return
|
|
1316
|
-
if (TypeGuard.
|
|
1317
|
-
return
|
|
1318
|
-
if (TypeGuard.
|
|
1319
|
-
return
|
|
1320
|
-
if (TypeGuard.
|
|
1321
|
-
return
|
|
1322
|
-
if (TypeGuard.
|
|
1323
|
-
return
|
|
1324
|
-
if (TypeGuard.
|
|
1325
|
-
return
|
|
1326
|
-
if (TypeGuard.
|
|
1327
|
-
return
|
|
1328
|
-
if (TypeGuard.
|
|
1329
|
-
return
|
|
1330
|
-
if (TypeGuard.
|
|
1331
|
-
return
|
|
1332
|
-
if (TypeGuard.
|
|
1333
|
-
return
|
|
1334
|
-
if (TypeGuard.
|
|
1335
|
-
return
|
|
1336
|
-
if (TypeGuard.
|
|
1337
|
-
return
|
|
1338
|
-
if (TypeGuard.
|
|
1339
|
-
return
|
|
1340
|
-
if (TypeGuard.
|
|
1341
|
-
return
|
|
1342
|
-
if (TypeGuard.
|
|
1343
|
-
return
|
|
1344
|
-
if (TypeGuard.
|
|
1345
|
-
return
|
|
1346
|
-
if (TypeGuard.
|
|
1347
|
-
return
|
|
1348
|
-
if (TypeGuard.
|
|
1349
|
-
return
|
|
1350
|
-
if (TypeGuard.
|
|
1351
|
-
return
|
|
1352
|
-
if (TypeGuard.
|
|
1353
|
-
return
|
|
1354
|
-
if (TypeGuard.
|
|
1355
|
-
return
|
|
1356
|
-
if (TypeGuard.
|
|
1357
|
-
return
|
|
1358
|
-
if (TypeGuard.TVoid(left_))
|
|
1359
|
-
return Void(left_, right_);
|
|
1264
|
+
if (TypeGuard.TAny(left))
|
|
1265
|
+
return Any(left, right);
|
|
1266
|
+
if (TypeGuard.TArray(left))
|
|
1267
|
+
return Array(left, right);
|
|
1268
|
+
if (TypeGuard.TBigInt(left))
|
|
1269
|
+
return BigInt(left, right);
|
|
1270
|
+
if (TypeGuard.TBoolean(left))
|
|
1271
|
+
return Boolean(left, right);
|
|
1272
|
+
if (TypeGuard.TConstructor(left))
|
|
1273
|
+
return Constructor(left, right);
|
|
1274
|
+
if (TypeGuard.TDate(left))
|
|
1275
|
+
return Date(left, right);
|
|
1276
|
+
if (TypeGuard.TFunction(left))
|
|
1277
|
+
return Function(left, right);
|
|
1278
|
+
if (TypeGuard.TInteger(left))
|
|
1279
|
+
return Integer(left, right);
|
|
1280
|
+
if (TypeGuard.TIntersect(left))
|
|
1281
|
+
return Intersect(left, right);
|
|
1282
|
+
if (TypeGuard.TLiteral(left))
|
|
1283
|
+
return Literal(left, right);
|
|
1284
|
+
if (TypeGuard.TNever(left))
|
|
1285
|
+
return Never(left, right);
|
|
1286
|
+
if (TypeGuard.TNull(left))
|
|
1287
|
+
return Null(left, right);
|
|
1288
|
+
if (TypeGuard.TNumber(left))
|
|
1289
|
+
return Number(left, right);
|
|
1290
|
+
if (TypeGuard.TRecord(left))
|
|
1291
|
+
return Record(left, right);
|
|
1292
|
+
if (TypeGuard.TString(left))
|
|
1293
|
+
return String(left, right);
|
|
1294
|
+
if (TypeGuard.TSymbol(left))
|
|
1295
|
+
return Symbol(left, right);
|
|
1296
|
+
if (TypeGuard.TObject(left))
|
|
1297
|
+
return Object(left, right);
|
|
1298
|
+
if (TypeGuard.TTuple(left))
|
|
1299
|
+
return Tuple(left, right);
|
|
1300
|
+
if (TypeGuard.TPromise(left))
|
|
1301
|
+
return Promise(left, right);
|
|
1302
|
+
if (TypeGuard.TUint8Array(left))
|
|
1303
|
+
return Uint8Array(left, right);
|
|
1304
|
+
if (TypeGuard.TUndefined(left))
|
|
1305
|
+
return Undefined(left, right);
|
|
1306
|
+
if (TypeGuard.TUnion(left))
|
|
1307
|
+
return Union(left, right);
|
|
1308
|
+
if (TypeGuard.TUnknown(left))
|
|
1309
|
+
return Unknown(left, right);
|
|
1310
|
+
if (TypeGuard.TVoid(left))
|
|
1311
|
+
return Void(left, right);
|
|
1360
1312
|
throw Error(`TypeExtends: Unknown left type operand '${left[exports.Kind]}'`);
|
|
1361
1313
|
}
|
|
1362
1314
|
function Extends(left, right) {
|
|
@@ -1367,14 +1319,11 @@ var TypeExtends;
|
|
|
1367
1319
|
// --------------------------------------------------------------------------
|
|
1368
1320
|
// TypeClone
|
|
1369
1321
|
// --------------------------------------------------------------------------
|
|
1370
|
-
/** Specialized Clone for Types
|
|
1322
|
+
/** Specialized Clone for Types */
|
|
1371
1323
|
var TypeClone;
|
|
1372
1324
|
(function (TypeClone) {
|
|
1373
|
-
function IsRecursive(value) {
|
|
1374
|
-
return typeof value[exports.Recursive] === 'string';
|
|
1375
|
-
}
|
|
1376
1325
|
function IsObject(value) {
|
|
1377
|
-
return typeof value === 'object' && value !== null
|
|
1326
|
+
return typeof value === 'object' && value !== null;
|
|
1378
1327
|
}
|
|
1379
1328
|
function IsArray(value) {
|
|
1380
1329
|
return globalThis.Array.isArray(value);
|
|
@@ -1383,22 +1332,24 @@ var TypeClone;
|
|
|
1383
1332
|
return value.map((value) => Visit(value));
|
|
1384
1333
|
}
|
|
1385
1334
|
function Object(value) {
|
|
1386
|
-
const
|
|
1387
|
-
|
|
1335
|
+
const clonedProperties = globalThis.Object.getOwnPropertyNames(value).reduce((acc, key) => {
|
|
1336
|
+
return { ...acc, [key]: Visit(value[key]) };
|
|
1337
|
+
}, {});
|
|
1338
|
+
const clonedSymbols = globalThis.Object.getOwnPropertySymbols(value).reduce((acc, key) => {
|
|
1339
|
+
return { ...acc, [key]: Visit(value[key]) };
|
|
1340
|
+
}, {});
|
|
1341
|
+
return { ...clonedProperties, ...clonedSymbols };
|
|
1388
1342
|
}
|
|
1389
1343
|
function Visit(value) {
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
if (IsArray(clone))
|
|
1396
|
-
return Array(clone);
|
|
1397
|
-
return clone;
|
|
1344
|
+
if (IsArray(value))
|
|
1345
|
+
return Array(value);
|
|
1346
|
+
if (IsObject(value))
|
|
1347
|
+
return Object(value);
|
|
1348
|
+
return value;
|
|
1398
1349
|
}
|
|
1399
1350
|
/** Clones a type. This function will omit non-self referential identifiers on the cloned type. */
|
|
1400
1351
|
function Clone(schema, options) {
|
|
1401
|
-
return { ...Visit(
|
|
1352
|
+
return { ...Visit(schema), ...options };
|
|
1402
1353
|
}
|
|
1403
1354
|
TypeClone.Clone = Clone;
|
|
1404
1355
|
})(TypeClone = exports.TypeClone || (exports.TypeClone = {}));
|
|
@@ -1408,10 +1359,10 @@ var TypeClone;
|
|
|
1408
1359
|
var ObjectMap;
|
|
1409
1360
|
(function (ObjectMap) {
|
|
1410
1361
|
function Intersect(schema, callback) {
|
|
1411
|
-
return exports.Type.Intersect(schema.allOf.map((inner) => Visit(
|
|
1362
|
+
return exports.Type.Intersect(schema.allOf.map((inner) => Visit(inner, callback)), { ...schema });
|
|
1412
1363
|
}
|
|
1413
1364
|
function Union(schema, callback) {
|
|
1414
|
-
return exports.Type.Union(schema.anyOf.map((inner) => Visit(
|
|
1365
|
+
return exports.Type.Union(schema.anyOf.map((inner) => Visit(inner, callback)), { ...schema });
|
|
1415
1366
|
}
|
|
1416
1367
|
function Object(schema, callback) {
|
|
1417
1368
|
return callback(schema);
|
|
@@ -1472,7 +1423,6 @@ let TypeOrdinal = 0;
|
|
|
1472
1423
|
class TypeBuilder {
|
|
1473
1424
|
/** `[Utility]` Creates a schema without `static` and `params` types */
|
|
1474
1425
|
Create(schema) {
|
|
1475
|
-
ReferenceRegistry.Set(schema);
|
|
1476
1426
|
return schema;
|
|
1477
1427
|
}
|
|
1478
1428
|
/** `[Standard]` Omits compositing symbols from this schema */
|
|
@@ -1515,28 +1465,16 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1515
1465
|
Boolean(options = {}) {
|
|
1516
1466
|
return this.Create({ ...options, [exports.Kind]: 'Boolean', type: 'boolean' });
|
|
1517
1467
|
}
|
|
1518
|
-
/** `[Standard]` Creates a Composite object type that will
|
|
1519
|
-
Composite(schemas, options
|
|
1520
|
-
const optional = new Set();
|
|
1521
|
-
for (const schema of schemas) {
|
|
1522
|
-
for (const [key, property] of globalThis.Object.entries(schema.properties)) {
|
|
1523
|
-
if (TypeGuard.TOptional(property) || TypeGuard.TReadonlyOptional(property))
|
|
1524
|
-
optional.add(key);
|
|
1525
|
-
}
|
|
1526
|
-
}
|
|
1468
|
+
/** `[Standard]` Creates a Composite object type that will union any overlapping properties of the given object array */
|
|
1469
|
+
Composite(schemas, options) {
|
|
1527
1470
|
const properties = {};
|
|
1528
1471
|
for (const object of schemas) {
|
|
1529
1472
|
for (const [key, property] of globalThis.Object.entries(object.properties)) {
|
|
1530
|
-
|
|
1531
|
-
properties[key] = optional.has(key) ? this.Optional(mapped) : mapped;
|
|
1473
|
+
properties[key] = key in properties ? this.Union([properties[key], property]) : TypeClone.Clone(property, {});
|
|
1532
1474
|
}
|
|
1533
1475
|
}
|
|
1534
1476
|
return this.Object(properties, options);
|
|
1535
1477
|
}
|
|
1536
|
-
/** `[Standard]` Dereferences the given TRef to its target type */
|
|
1537
|
-
Deref(schema) {
|
|
1538
|
-
return ReferenceRegistry.Deref(schema);
|
|
1539
|
-
}
|
|
1540
1478
|
/** `[Standard]` Creates a Enum type */
|
|
1541
1479
|
Enum(item, options = {}) {
|
|
1542
1480
|
// prettier-ignore
|
|
@@ -1546,7 +1484,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1546
1484
|
}
|
|
1547
1485
|
/** `[Standard]` A conditional type expression that will return the true type if the left type extends the right */
|
|
1548
1486
|
Extends(left, right, trueType, falseType, options = {}) {
|
|
1549
|
-
switch (TypeExtends.Extends(
|
|
1487
|
+
switch (TypeExtends.Extends(left, right)) {
|
|
1550
1488
|
case TypeExtendsResult.Union:
|
|
1551
1489
|
return this.Union([TypeClone.Clone(trueType, options), TypeClone.Clone(falseType, options)]);
|
|
1552
1490
|
case TypeExtendsResult.True:
|
|
@@ -1596,7 +1534,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1596
1534
|
}
|
|
1597
1535
|
/** `[Standard]` Creates a KeyOf type */
|
|
1598
1536
|
KeyOf(schema, options = {}) {
|
|
1599
|
-
const keys = KeyResolver.Resolve(
|
|
1537
|
+
const keys = KeyResolver.Resolve(schema);
|
|
1600
1538
|
// prettier-ignore
|
|
1601
1539
|
const keyof = keys.length === 0 ? this.Never(options) : this.Union(keys.map((key) => this.Literal(key)), options);
|
|
1602
1540
|
return keyof;
|
|
@@ -1623,15 +1561,13 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1623
1561
|
}
|
|
1624
1562
|
/** `[Standard]` Creates an Object type */
|
|
1625
1563
|
Object(properties, options = {}) {
|
|
1626
|
-
const
|
|
1627
|
-
const
|
|
1628
|
-
const
|
|
1564
|
+
const propertyKeys = globalThis.Object.getOwnPropertyNames(properties);
|
|
1565
|
+
const optionalKeys = propertyKeys.filter((key) => TypeGuard.TOptional(properties[key]) || TypeGuard.TReadonlyOptional(properties[key]));
|
|
1566
|
+
const requiredKeys = propertyKeys.filter((name) => !optionalKeys.includes(name));
|
|
1629
1567
|
const clonedAdditionalProperties = TypeGuard.TSchema(options.additionalProperties) ? { additionalProperties: TypeClone.Clone(options.additionalProperties, {}) } : {};
|
|
1630
|
-
const clonedProperties =
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
if (required.length > 0) {
|
|
1634
|
-
return this.Create({ ...options, ...clonedAdditionalProperties, [exports.Kind]: 'Object', type: 'object', properties: clonedProperties, required });
|
|
1568
|
+
const clonedProperties = propertyKeys.reduce((acc, key) => ({ ...acc, [key]: TypeClone.Clone(properties[key], {}) }), {});
|
|
1569
|
+
if (requiredKeys.length > 0) {
|
|
1570
|
+
return this.Create({ ...options, ...clonedAdditionalProperties, [exports.Kind]: 'Object', type: 'object', properties: clonedProperties, required: requiredKeys });
|
|
1635
1571
|
}
|
|
1636
1572
|
else {
|
|
1637
1573
|
return this.Create({ ...options, ...clonedAdditionalProperties, [exports.Kind]: 'Object', type: 'object', properties: clonedProperties });
|
|
@@ -1644,7 +1580,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1644
1580
|
TypeGuard.TNever(unresolved) ? [] :
|
|
1645
1581
|
unresolved;
|
|
1646
1582
|
// prettier-ignore
|
|
1647
|
-
return ObjectMap.Map(TypeClone.Clone(
|
|
1583
|
+
return ObjectMap.Map(TypeClone.Clone(schema, {}), (schema) => {
|
|
1648
1584
|
if (schema.required) {
|
|
1649
1585
|
schema.required = schema.required.filter((key) => !keys.includes(key));
|
|
1650
1586
|
if (schema.required.length === 0)
|
|
@@ -1677,7 +1613,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1677
1613
|
}
|
|
1678
1614
|
}
|
|
1679
1615
|
// prettier-ignore
|
|
1680
|
-
return ObjectMap.Map(TypeClone.Clone(
|
|
1616
|
+
return ObjectMap.Map(TypeClone.Clone(schema, {}), (schema) => {
|
|
1681
1617
|
delete schema.required;
|
|
1682
1618
|
globalThis.Object.keys(schema.properties).forEach(key => Apply(schema.properties[key]));
|
|
1683
1619
|
return schema;
|
|
@@ -1690,7 +1626,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1690
1626
|
TypeGuard.TNever(unresolved) ? [] :
|
|
1691
1627
|
unresolved;
|
|
1692
1628
|
// prettier-ignore
|
|
1693
|
-
return ObjectMap.Map(TypeClone.Clone(
|
|
1629
|
+
return ObjectMap.Map(TypeClone.Clone(schema, {}), (schema) => {
|
|
1694
1630
|
if (schema.required) {
|
|
1695
1631
|
schema.required = schema.required.filter((key) => keys.includes(key));
|
|
1696
1632
|
if (schema.required.length === 0)
|
|
@@ -1733,9 +1669,8 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1733
1669
|
if (options.$id === undefined)
|
|
1734
1670
|
options.$id = `T${TypeOrdinal++}`;
|
|
1735
1671
|
const self = callback({ [exports.Kind]: 'Self', $ref: `${options.$id}` });
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
return this.Create({ ...options, ...self, [exports.Recursive]: reassign.$id });
|
|
1672
|
+
self.$id = options.$id;
|
|
1673
|
+
return this.Create({ ...options, ...self });
|
|
1739
1674
|
}
|
|
1740
1675
|
/** `[Standard]` Creates a Ref type. The referenced type must contain a $id */
|
|
1741
1676
|
Ref(schema, options = {}) {
|
|
@@ -1763,7 +1698,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1763
1698
|
}
|
|
1764
1699
|
}
|
|
1765
1700
|
// prettier-ignore
|
|
1766
|
-
return ObjectMap.Map(TypeClone.Clone(
|
|
1701
|
+
return ObjectMap.Map(TypeClone.Clone(schema, {}), (schema) => {
|
|
1767
1702
|
schema.required = globalThis.Object.keys(schema.properties);
|
|
1768
1703
|
globalThis.Object.keys(schema.properties).forEach(key => Apply(schema.properties[key]));
|
|
1769
1704
|
return schema;
|
package/value/cast.d.ts
CHANGED
|
@@ -20,7 +20,11 @@ export declare class ValueCastUnknownTypeError extends Error {
|
|
|
20
20
|
readonly schema: Types.TSchema;
|
|
21
21
|
constructor(schema: Types.TSchema);
|
|
22
22
|
}
|
|
23
|
+
export declare class ValueCastDereferenceError extends Error {
|
|
24
|
+
readonly schema: Types.TRef | Types.TSelf;
|
|
25
|
+
constructor(schema: Types.TRef | Types.TSelf);
|
|
26
|
+
}
|
|
23
27
|
export declare namespace ValueCast {
|
|
24
|
-
function Visit(schema: Types.TSchema, value: any): any;
|
|
25
|
-
function Cast<T extends Types.TSchema>(schema: T, value: any): Types.Static<T>;
|
|
28
|
+
function Visit(schema: Types.TSchema, references: Types.TSchema[], value: any): any;
|
|
29
|
+
function Cast<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: any): Types.Static<T>;
|
|
26
30
|
}
|