@sinclair/typebox 0.27.8 → 0.28.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.js +17 -21
- package/errors/errors.js +23 -20
- package/package.json +1 -1
- package/readme.md +94 -67
- package/system/system.d.ts +0 -4
- package/system/system.js +0 -13
- package/typebox.d.ts +136 -76
- package/typebox.js +148 -60
- package/value/check.js +22 -20
- package/value/delta.d.ts +6 -6
package/typebox.js
CHANGED
|
@@ -27,7 +27,7 @@ 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.TemplateLiteralGenerator = exports.TemplateLiteralFinite = exports.TemplateLiteralParser = exports.TemplateLiteralParserError = exports.TemplateLiteralResolver = exports.TemplateLiteralPattern = exports.KeyResolver = exports.ObjectMap = exports.TypeClone = exports.TypeExtends = exports.TypeExtendsResult = exports.ExtendsUndefined = exports.TypeGuard = exports.TypeGuardUnknownTypeError = exports.FormatRegistry = exports.TypeRegistry = exports.PatternStringExact = exports.PatternNumberExact = exports.PatternBooleanExact = exports.PatternString = exports.PatternNumber = exports.PatternBoolean = exports.Kind = exports.Hint = exports.Modifier = void 0;
|
|
30
|
+
exports.Type = exports.StandardType = exports.ExtendedTypeBuilder = exports.StandardTypeBuilder = exports.TypeBuilder = exports.TemplateLiteralGenerator = exports.TemplateLiteralFinite = exports.TemplateLiteralParser = exports.TemplateLiteralParserError = exports.TemplateLiteralResolver = exports.TemplateLiteralPattern = exports.KeyArrayResolver = exports.KeyResolver = exports.ObjectMap = exports.IndexedAccessor = exports.TypeClone = exports.TypeExtends = exports.TypeExtendsResult = exports.ExtendsUndefined = exports.TypeGuard = exports.TypeGuardUnknownTypeError = exports.FormatRegistry = exports.TypeRegistry = exports.PatternStringExact = exports.PatternNumberExact = exports.PatternBooleanExact = exports.PatternString = exports.PatternNumber = exports.PatternBoolean = exports.Kind = exports.Hint = exports.Modifier = void 0;
|
|
31
31
|
// --------------------------------------------------------------------------
|
|
32
32
|
// Symbols
|
|
33
33
|
// --------------------------------------------------------------------------
|
|
@@ -142,6 +142,9 @@ var TypeGuard;
|
|
|
142
142
|
}
|
|
143
143
|
return true;
|
|
144
144
|
}
|
|
145
|
+
function IsAdditionalProperties(value) {
|
|
146
|
+
return IsOptionalBoolean(value) || TSchema(value);
|
|
147
|
+
}
|
|
145
148
|
function IsBigInt(value) {
|
|
146
149
|
return typeof value === 'bigint';
|
|
147
150
|
}
|
|
@@ -306,16 +309,29 @@ var TypeGuard;
|
|
|
306
309
|
return IsObject(schema) && exports.Kind in schema && typeof schema[exports.Kind] === 'string'; // TS 4.1.5: any required for symbol indexer
|
|
307
310
|
}
|
|
308
311
|
TypeGuard.TKind = TKind;
|
|
312
|
+
/** Returns true if the given schema is TLiteral<string> */
|
|
313
|
+
function TLiteralString(schema) {
|
|
314
|
+
return TKind(schema) && schema[exports.Kind] === 'Literal' && IsOptionalString(schema.$id) && typeof schema.const === 'string';
|
|
315
|
+
}
|
|
316
|
+
TypeGuard.TLiteralString = TLiteralString;
|
|
317
|
+
/** Returns true if the given schema is TLiteral<number> */
|
|
318
|
+
function TLiteralNumber(schema) {
|
|
319
|
+
return TKind(schema) && schema[exports.Kind] === 'Literal' && IsOptionalString(schema.$id) && typeof schema.const === 'number';
|
|
320
|
+
}
|
|
321
|
+
TypeGuard.TLiteralNumber = TLiteralNumber;
|
|
322
|
+
/** Returns true if the given schema is TLiteral<boolean> */
|
|
323
|
+
function TLiteralBoolean(schema) {
|
|
324
|
+
return TKind(schema) && schema[exports.Kind] === 'Literal' && IsOptionalString(schema.$id) && typeof schema.const === 'boolean';
|
|
325
|
+
}
|
|
326
|
+
TypeGuard.TLiteralBoolean = TLiteralBoolean;
|
|
327
|
+
/** Returns true if the given schema is TUnion<Literal<string>[]> */
|
|
328
|
+
function TLiteralUnion(schema) {
|
|
329
|
+
return TUnion(schema) && schema.anyOf.every((schema) => TLiteral(schema));
|
|
330
|
+
}
|
|
331
|
+
TypeGuard.TLiteralUnion = TLiteralUnion;
|
|
309
332
|
/** Returns true if the given schema is TLiteral */
|
|
310
333
|
function TLiteral(schema) {
|
|
311
|
-
|
|
312
|
-
return (TKind(schema) &&
|
|
313
|
-
schema[exports.Kind] === 'Literal' &&
|
|
314
|
-
IsOptionalString(schema.$id) &&
|
|
315
|
-
(IsString(schema.const) ||
|
|
316
|
-
IsNumber(schema.const) ||
|
|
317
|
-
IsBoolean(schema.const) ||
|
|
318
|
-
IsBigInt(schema.const)));
|
|
334
|
+
return TLiteralString(schema) || TLiteralNumber(schema) || TLiteralBoolean(schema);
|
|
319
335
|
}
|
|
320
336
|
TypeGuard.TLiteral = TLiteral;
|
|
321
337
|
/** Returns true if the given schema is TNever */
|
|
@@ -364,7 +380,7 @@ var TypeGuard;
|
|
|
364
380
|
schema.type === 'object' &&
|
|
365
381
|
IsOptionalString(schema.$id) &&
|
|
366
382
|
IsObject(schema.properties) &&
|
|
367
|
-
(
|
|
383
|
+
IsAdditionalProperties(schema.additionalProperties) &&
|
|
368
384
|
IsOptionalNumber(schema.minProperties) &&
|
|
369
385
|
IsOptionalNumber(schema.maxProperties))) {
|
|
370
386
|
return false;
|
|
@@ -396,7 +412,7 @@ var TypeGuard;
|
|
|
396
412
|
schema[exports.Kind] === 'Record' &&
|
|
397
413
|
schema.type === 'object' &&
|
|
398
414
|
IsOptionalString(schema.$id) &&
|
|
399
|
-
schema.additionalProperties
|
|
415
|
+
IsAdditionalProperties(schema.additionalProperties) &&
|
|
400
416
|
IsObject(schema.patternProperties))) {
|
|
401
417
|
return false;
|
|
402
418
|
}
|
|
@@ -515,11 +531,6 @@ var TypeGuard;
|
|
|
515
531
|
return true;
|
|
516
532
|
}
|
|
517
533
|
TypeGuard.TUnion = TUnion;
|
|
518
|
-
/** Returns true if the given schema is TUnion<Literal<string>[]> */
|
|
519
|
-
function TUnionLiteral(schema) {
|
|
520
|
-
return TUnion(schema) && schema.anyOf.every((schema) => TLiteral(schema) && typeof schema.const === 'string');
|
|
521
|
-
}
|
|
522
|
-
TypeGuard.TUnionLiteral = TUnionLiteral;
|
|
523
534
|
/** Returns true if the given schema is TUint8Array */
|
|
524
535
|
function TUint8Array(schema) {
|
|
525
536
|
return TKind(schema) && schema[exports.Kind] === 'Uint8Array' && schema.type === 'object' && IsOptionalString(schema.$id) && schema.instanceOf === 'Uint8Array' && IsOptionalNumber(schema.minByteLength) && IsOptionalNumber(schema.maxByteLength);
|
|
@@ -1290,12 +1301,12 @@ var TypeExtends;
|
|
|
1290
1301
|
return TypeGuard.TVoid(right) ? TypeExtendsResult.True : TypeExtendsResult.False;
|
|
1291
1302
|
}
|
|
1292
1303
|
function Visit(left, right) {
|
|
1293
|
-
//
|
|
1304
|
+
// Template Literal Union Unwrap
|
|
1294
1305
|
if (TypeGuard.TTemplateLiteral(left))
|
|
1295
1306
|
return Visit(TemplateLiteralResolver.Resolve(left), right);
|
|
1296
1307
|
if (TypeGuard.TTemplateLiteral(right))
|
|
1297
1308
|
return Visit(left, TemplateLiteralResolver.Resolve(right));
|
|
1298
|
-
//
|
|
1309
|
+
// Standard Extends
|
|
1299
1310
|
if (TypeGuard.TAny(left))
|
|
1300
1311
|
return Any(left, right);
|
|
1301
1312
|
if (TypeGuard.TArray(left))
|
|
@@ -1389,6 +1400,41 @@ var TypeClone;
|
|
|
1389
1400
|
TypeClone.Clone = Clone;
|
|
1390
1401
|
})(TypeClone = exports.TypeClone || (exports.TypeClone = {}));
|
|
1391
1402
|
// --------------------------------------------------------------------------
|
|
1403
|
+
// IndexedAccessor
|
|
1404
|
+
// --------------------------------------------------------------------------
|
|
1405
|
+
var IndexedAccessor;
|
|
1406
|
+
(function (IndexedAccessor) {
|
|
1407
|
+
function Intersect(schema, key) {
|
|
1408
|
+
return schema.allOf.reduce((acc, schema) => [...acc, ...Visit(schema, key)], []);
|
|
1409
|
+
}
|
|
1410
|
+
function Union(schema, key) {
|
|
1411
|
+
return schema.anyOf.reduce((acc, schema) => [...acc, ...Visit(schema, key)], []);
|
|
1412
|
+
}
|
|
1413
|
+
function Object(schema, key) {
|
|
1414
|
+
const keys = globalThis.Object.getOwnPropertyNames(schema.properties).filter((k) => k === key);
|
|
1415
|
+
return keys.map((key) => schema.properties[key]);
|
|
1416
|
+
}
|
|
1417
|
+
function Tuple(schema, key) {
|
|
1418
|
+
const items = schema.items === undefined ? [] : schema.items;
|
|
1419
|
+
return items.filter((_, index) => index.toString() === key.toString());
|
|
1420
|
+
}
|
|
1421
|
+
function Visit(schema, key) {
|
|
1422
|
+
if (schema[exports.Kind] === 'Intersect')
|
|
1423
|
+
return Intersect(schema, key);
|
|
1424
|
+
if (schema[exports.Kind] === 'Union')
|
|
1425
|
+
return Union(schema, key);
|
|
1426
|
+
if (schema[exports.Kind] === 'Object')
|
|
1427
|
+
return Object(schema, key);
|
|
1428
|
+
if (schema[exports.Kind] === 'Tuple')
|
|
1429
|
+
return Tuple(schema, key);
|
|
1430
|
+
return [];
|
|
1431
|
+
}
|
|
1432
|
+
function Resolve(schema, keys) {
|
|
1433
|
+
return keys.reduce((acc, key) => [...acc, ...Visit(schema, key)], []);
|
|
1434
|
+
}
|
|
1435
|
+
IndexedAccessor.Resolve = Resolve;
|
|
1436
|
+
})(IndexedAccessor = exports.IndexedAccessor || (exports.IndexedAccessor = {}));
|
|
1437
|
+
// --------------------------------------------------------------------------
|
|
1392
1438
|
// ObjectMap
|
|
1393
1439
|
// --------------------------------------------------------------------------
|
|
1394
1440
|
var ObjectMap;
|
|
@@ -1422,39 +1468,72 @@ var ObjectMap;
|
|
|
1422
1468
|
}
|
|
1423
1469
|
ObjectMap.Map = Map;
|
|
1424
1470
|
})(ObjectMap = exports.ObjectMap || (exports.ObjectMap = {}));
|
|
1425
|
-
// --------------------------------------------------------------------------
|
|
1426
|
-
// KeyResolver
|
|
1427
|
-
// --------------------------------------------------------------------------
|
|
1428
1471
|
var KeyResolver;
|
|
1429
1472
|
(function (KeyResolver) {
|
|
1430
|
-
function
|
|
1431
|
-
return
|
|
1473
|
+
function UnwrapPattern(key) {
|
|
1474
|
+
return key[0] === '^' && key[key.length - 1] === '$' ? key.slice(1, key.length - 1) : key;
|
|
1432
1475
|
}
|
|
1433
|
-
function Intersect(schema) {
|
|
1434
|
-
return
|
|
1476
|
+
function Intersect(schema, options) {
|
|
1477
|
+
return schema.allOf.reduce((acc, schema) => [...acc, ...Visit(schema, options)], []);
|
|
1435
1478
|
}
|
|
1436
|
-
function Union(schema) {
|
|
1437
|
-
const sets = schema.anyOf.
|
|
1479
|
+
function Union(schema, options) {
|
|
1480
|
+
const sets = schema.anyOf.map((inner) => Visit(inner, options));
|
|
1438
1481
|
return [...sets.reduce((set, outer) => outer.map((key) => (sets.every((inner) => inner.includes(key)) ? set.add(key) : set))[0], new Set())];
|
|
1439
1482
|
}
|
|
1440
|
-
function Object(schema) {
|
|
1483
|
+
function Object(schema, options) {
|
|
1441
1484
|
return globalThis.Object.keys(schema.properties);
|
|
1442
1485
|
}
|
|
1443
|
-
function
|
|
1486
|
+
function Record(schema, options) {
|
|
1487
|
+
return options.includePatterns ? globalThis.Object.keys(schema.patternProperties) : [];
|
|
1488
|
+
}
|
|
1489
|
+
function Visit(schema, options) {
|
|
1444
1490
|
if (TypeGuard.TIntersect(schema))
|
|
1445
|
-
return Intersect(schema);
|
|
1491
|
+
return Intersect(schema, options);
|
|
1446
1492
|
if (TypeGuard.TUnion(schema))
|
|
1447
|
-
return Union(schema);
|
|
1493
|
+
return Union(schema, options);
|
|
1448
1494
|
if (TypeGuard.TObject(schema))
|
|
1449
|
-
return Object(schema);
|
|
1495
|
+
return Object(schema, options);
|
|
1496
|
+
if (TypeGuard.TRecord(schema))
|
|
1497
|
+
return Record(schema, options);
|
|
1450
1498
|
return [];
|
|
1451
1499
|
}
|
|
1452
|
-
|
|
1453
|
-
|
|
1500
|
+
/** Resolves an array of keys in this schema */
|
|
1501
|
+
function ResolveKeys(schema, options) {
|
|
1502
|
+
return [...new Set(Visit(schema, options))];
|
|
1503
|
+
}
|
|
1504
|
+
KeyResolver.ResolveKeys = ResolveKeys;
|
|
1505
|
+
/** Resolves a regular expression pattern matching all keys in this schema */
|
|
1506
|
+
function ResolvePattern(schema) {
|
|
1507
|
+
const keys = ResolveKeys(schema, { includePatterns: true });
|
|
1508
|
+
const pattern = keys.map((key) => `(${UnwrapPattern(key)})`);
|
|
1509
|
+
return `^(${pattern.join('|')})$`;
|
|
1454
1510
|
}
|
|
1455
|
-
KeyResolver.
|
|
1511
|
+
KeyResolver.ResolvePattern = ResolvePattern;
|
|
1456
1512
|
})(KeyResolver = exports.KeyResolver || (exports.KeyResolver = {}));
|
|
1457
1513
|
// --------------------------------------------------------------------------
|
|
1514
|
+
// KeyArrayResolver
|
|
1515
|
+
// --------------------------------------------------------------------------
|
|
1516
|
+
var KeyArrayResolver;
|
|
1517
|
+
(function (KeyArrayResolver) {
|
|
1518
|
+
/** Resolves an array of string[] keys from the given schema or array type. */
|
|
1519
|
+
function Resolve(schema) {
|
|
1520
|
+
if (globalThis.Array.isArray(schema))
|
|
1521
|
+
return schema;
|
|
1522
|
+
if (TypeGuard.TLiteralUnion(schema))
|
|
1523
|
+
return schema.anyOf.map((schema) => schema.const);
|
|
1524
|
+
if (TypeGuard.TLiteral(schema))
|
|
1525
|
+
return [schema.const];
|
|
1526
|
+
if (TypeGuard.TTemplateLiteral(schema)) {
|
|
1527
|
+
const expression = TemplateLiteralParser.ParseExact(schema.pattern);
|
|
1528
|
+
if (!TemplateLiteralFinite.Check(expression))
|
|
1529
|
+
throw Error('KeyArrayResolver: Cannot resolve keys from infinite template expression');
|
|
1530
|
+
return [...TemplateLiteralGenerator.Generate(expression)];
|
|
1531
|
+
}
|
|
1532
|
+
return [];
|
|
1533
|
+
}
|
|
1534
|
+
KeyArrayResolver.Resolve = Resolve;
|
|
1535
|
+
})(KeyArrayResolver = exports.KeyArrayResolver || (exports.KeyArrayResolver = {}));
|
|
1536
|
+
// --------------------------------------------------------------------------
|
|
1458
1537
|
// TemplateLiteralPattern
|
|
1459
1538
|
// --------------------------------------------------------------------------
|
|
1460
1539
|
var TemplateLiteralPattern;
|
|
@@ -1506,6 +1585,7 @@ var TemplateLiteralPattern;
|
|
|
1506
1585
|
// --------------------------------------------------------------------------------------
|
|
1507
1586
|
var TemplateLiteralResolver;
|
|
1508
1587
|
(function (TemplateLiteralResolver) {
|
|
1588
|
+
/** Resolves a template literal as a TUnion */
|
|
1509
1589
|
function Resolve(template) {
|
|
1510
1590
|
const expression = TemplateLiteralParser.ParseExact(template.pattern);
|
|
1511
1591
|
if (!TemplateLiteralFinite.Check(expression))
|
|
@@ -1811,23 +1891,21 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1811
1891
|
if (!optional.has(key))
|
|
1812
1892
|
delete property[exports.Modifier];
|
|
1813
1893
|
if (key in properties) {
|
|
1814
|
-
|
|
1815
|
-
const right = TypeExtends.Extends(property, properties[key]) !== TypeExtendsResult.False;
|
|
1816
|
-
if (!left && !right)
|
|
1817
|
-
properties[key] = exports.Type.Never();
|
|
1818
|
-
if (!left && right)
|
|
1819
|
-
properties[key] = property;
|
|
1894
|
+
properties[key] = TypeGuard.TIntersect(properties[key]) ? this.Intersect([...properties[key].allOf, property]) : this.Intersect([properties[key], property]);
|
|
1820
1895
|
}
|
|
1821
1896
|
else {
|
|
1822
1897
|
properties[key] = property;
|
|
1823
1898
|
}
|
|
1824
1899
|
}
|
|
1825
1900
|
}
|
|
1901
|
+
for (const key of globalThis.Object.getOwnPropertyNames(properties)) {
|
|
1902
|
+
properties[key] = optional.has(key) ? this.Optional(properties[key]) : properties[key];
|
|
1903
|
+
}
|
|
1826
1904
|
if (required.size > 0) {
|
|
1827
|
-
return this.Create({ ...options, [exports.Kind]: 'Object',
|
|
1905
|
+
return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties, required: [...required] });
|
|
1828
1906
|
}
|
|
1829
1907
|
else {
|
|
1830
|
-
return this.Create({ ...options, [exports.Kind]: 'Object',
|
|
1908
|
+
return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties });
|
|
1831
1909
|
}
|
|
1832
1910
|
}
|
|
1833
1911
|
/** `[Standard]` Creates a Enum type */
|
|
@@ -1876,6 +1954,16 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1876
1954
|
return (TypeExtends.Extends(left, right) !== TypeExtendsResult.False ? TypeClone.Clone(left, options) : this.Never(options));
|
|
1877
1955
|
}
|
|
1878
1956
|
}
|
|
1957
|
+
/** `[Standard]` Returns indexed property types for the given keys */
|
|
1958
|
+
Index(schema, unresolved, options = {}) {
|
|
1959
|
+
const keys = KeyArrayResolver.Resolve(unresolved);
|
|
1960
|
+
if (TypeGuard.TArray(schema) && TypeGuard.TNumber(unresolved)) {
|
|
1961
|
+
return TypeClone.Clone(schema.items, options);
|
|
1962
|
+
}
|
|
1963
|
+
const resolved = IndexedAccessor.Resolve(schema, keys);
|
|
1964
|
+
const cloned = resolved.map((schema) => TypeClone.Clone(schema, {}));
|
|
1965
|
+
return this.Union(cloned, options);
|
|
1966
|
+
}
|
|
1879
1967
|
/** `[Standard]` Creates an Integer type */
|
|
1880
1968
|
Integer(options = {}) {
|
|
1881
1969
|
return this.Create({ ...options, [exports.Kind]: 'Integer', type: 'integer' });
|
|
@@ -1905,11 +1993,19 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1905
1993
|
return this.String(options);
|
|
1906
1994
|
throw Error('StandardTypeBuilder: Unable to resolve key type from Record key pattern');
|
|
1907
1995
|
}
|
|
1996
|
+
else if (TypeGuard.TTuple(schema)) {
|
|
1997
|
+
const items = schema.items === undefined ? [] : schema.items;
|
|
1998
|
+
const literals = items.map((_, index) => exports.Type.Literal(index));
|
|
1999
|
+
return this.Union(literals, options);
|
|
2000
|
+
}
|
|
2001
|
+
else if (TypeGuard.TArray(schema)) {
|
|
2002
|
+
return this.Number(options);
|
|
2003
|
+
}
|
|
1908
2004
|
else {
|
|
1909
|
-
const
|
|
1910
|
-
if (
|
|
2005
|
+
const keys = KeyResolver.ResolveKeys(schema, { includePatterns: false });
|
|
2006
|
+
if (keys.length === 0)
|
|
1911
2007
|
return this.Never(options);
|
|
1912
|
-
const literals =
|
|
2008
|
+
const literals = keys.map((key) => this.Literal(key));
|
|
1913
2009
|
return this.Union(literals, options);
|
|
1914
2010
|
}
|
|
1915
2011
|
}
|
|
@@ -1948,11 +2044,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1948
2044
|
}
|
|
1949
2045
|
}
|
|
1950
2046
|
Omit(schema, unresolved, options = {}) {
|
|
1951
|
-
|
|
1952
|
-
const keys = TypeGuard.TUnionLiteral(unresolved) ? unresolved.anyOf.map((schema) => schema.const) :
|
|
1953
|
-
TypeGuard.TLiteral(unresolved) ? [unresolved.const] :
|
|
1954
|
-
TypeGuard.TNever(unresolved) ? [] :
|
|
1955
|
-
unresolved;
|
|
2047
|
+
const keys = KeyArrayResolver.Resolve(unresolved);
|
|
1956
2048
|
// prettier-ignore
|
|
1957
2049
|
return ObjectMap.Map(TypeClone.Clone(schema, {}), (schema) => {
|
|
1958
2050
|
if (schema.required) {
|
|
@@ -1994,11 +2086,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1994
2086
|
}, options);
|
|
1995
2087
|
}
|
|
1996
2088
|
Pick(schema, unresolved, options = {}) {
|
|
1997
|
-
|
|
1998
|
-
const keys = TypeGuard.TUnionLiteral(unresolved) ? unresolved.anyOf.map((schema) => schema.const) :
|
|
1999
|
-
TypeGuard.TLiteral(unresolved) ? [unresolved.const] :
|
|
2000
|
-
TypeGuard.TNever(unresolved) ? [] :
|
|
2001
|
-
unresolved;
|
|
2089
|
+
const keys = KeyArrayResolver.Resolve(unresolved);
|
|
2002
2090
|
// prettier-ignore
|
|
2003
2091
|
return ObjectMap.Map(TypeClone.Clone(schema, {}), (schema) => {
|
|
2004
2092
|
if (schema.required) {
|
|
@@ -2020,9 +2108,9 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2020
2108
|
// prettier-ignore
|
|
2021
2109
|
return TemplateLiteralFinite.Check(expression)
|
|
2022
2110
|
? (this.Object([...TemplateLiteralGenerator.Generate(expression)].reduce((acc, key) => ({ ...acc, [key]: TypeClone.Clone(schema, {}) }), {}), options))
|
|
2023
|
-
: this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [key.pattern]: TypeClone.Clone(schema, {}) }
|
|
2111
|
+
: this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [key.pattern]: TypeClone.Clone(schema, {}) } });
|
|
2024
2112
|
}
|
|
2025
|
-
else if (TypeGuard.
|
|
2113
|
+
else if (TypeGuard.TLiteralUnion(key)) {
|
|
2026
2114
|
if (key.anyOf.every((schema) => TypeGuard.TLiteral(schema) && (typeof schema.const === 'string' || typeof schema.const === 'number'))) {
|
|
2027
2115
|
const properties = key.anyOf.reduce((acc, literal) => ({ ...acc, [literal.const]: TypeClone.Clone(schema, {}) }), {});
|
|
2028
2116
|
return this.Object(properties, { ...options, [exports.Hint]: 'Record' });
|
|
@@ -2039,11 +2127,11 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
2039
2127
|
}
|
|
2040
2128
|
else if (TypeGuard.TInteger(key) || TypeGuard.TNumber(key)) {
|
|
2041
2129
|
const pattern = exports.PatternNumberExact;
|
|
2042
|
-
return this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [pattern]: TypeClone.Clone(schema, {}) }
|
|
2130
|
+
return this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [pattern]: TypeClone.Clone(schema, {}) } });
|
|
2043
2131
|
}
|
|
2044
2132
|
else if (TypeGuard.TString(key)) {
|
|
2045
2133
|
const pattern = key.pattern === undefined ? exports.PatternStringExact : key.pattern;
|
|
2046
|
-
return this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [pattern]: TypeClone.Clone(schema, {}) }
|
|
2134
|
+
return this.Create({ ...options, [exports.Kind]: 'Record', type: 'object', patternProperties: { [pattern]: TypeClone.Clone(schema, {}) } });
|
|
2047
2135
|
}
|
|
2048
2136
|
else {
|
|
2049
2137
|
throw Error(`StandardTypeBuilder: Invalid Record Key`);
|
package/value/check.js
CHANGED
|
@@ -189,21 +189,19 @@ var ValueCheck;
|
|
|
189
189
|
return true;
|
|
190
190
|
}
|
|
191
191
|
function Intersect(schema, references, value) {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
const valueKeys = globalThis.Object.getOwnPropertyNames(value);
|
|
198
|
-
return valueKeys.every((key) => schemaKeys.includes(key));
|
|
192
|
+
const check1 = schema.allOf.every((schema) => Visit(schema, references, value));
|
|
193
|
+
if (schema.unevaluatedProperties === false) {
|
|
194
|
+
const keyCheck = new RegExp(Types.KeyResolver.ResolvePattern(schema));
|
|
195
|
+
const check2 = globalThis.Object.getOwnPropertyNames(value).every((key) => keyCheck.test(key));
|
|
196
|
+
return check1 && check2;
|
|
199
197
|
}
|
|
200
198
|
else if (Types.TypeGuard.TSchema(schema.unevaluatedProperties)) {
|
|
201
|
-
const
|
|
202
|
-
const
|
|
203
|
-
return
|
|
199
|
+
const keyCheck = new RegExp(Types.KeyResolver.ResolvePattern(schema));
|
|
200
|
+
const check2 = globalThis.Object.getOwnPropertyNames(value).every((key) => keyCheck.test(key) || Visit(schema.unevaluatedProperties, references, value[key]));
|
|
201
|
+
return check1 && check2;
|
|
204
202
|
}
|
|
205
203
|
else {
|
|
206
|
-
return
|
|
204
|
+
return check1;
|
|
207
205
|
}
|
|
208
206
|
}
|
|
209
207
|
function Literal(schema, references, value) {
|
|
@@ -297,16 +295,20 @@ var ValueCheck;
|
|
|
297
295
|
if (IsDefined(schema.maxProperties) && !(globalThis.Object.getOwnPropertyNames(value).length <= schema.maxProperties)) {
|
|
298
296
|
return false;
|
|
299
297
|
}
|
|
300
|
-
const [
|
|
301
|
-
const regex = new RegExp(
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
if (
|
|
298
|
+
const [patternKey, patternSchema] = globalThis.Object.entries(schema.patternProperties)[0];
|
|
299
|
+
const regex = new RegExp(patternKey);
|
|
300
|
+
return globalThis.Object.entries(value).every(([key, value]) => {
|
|
301
|
+
if (regex.test(key)) {
|
|
302
|
+
return Visit(patternSchema, references, value);
|
|
303
|
+
}
|
|
304
|
+
if (typeof schema.additionalProperties === 'object') {
|
|
305
|
+
return Visit(schema.additionalProperties, references, value);
|
|
306
|
+
}
|
|
307
|
+
if (schema.additionalProperties === false) {
|
|
307
308
|
return false;
|
|
308
|
-
|
|
309
|
-
|
|
309
|
+
}
|
|
310
|
+
return true;
|
|
311
|
+
});
|
|
310
312
|
}
|
|
311
313
|
function Ref(schema, references, value) {
|
|
312
314
|
const index = references.findIndex((foreign) => foreign.$id === schema.$ref);
|
package/value/delta.d.ts
CHANGED
|
@@ -2,32 +2,32 @@ import { Static } from '../typebox';
|
|
|
2
2
|
export type Insert = Static<typeof Insert>;
|
|
3
3
|
export declare const Insert: import("../typebox").TObject<{
|
|
4
4
|
type: import("../typebox").TLiteral<"insert">;
|
|
5
|
-
path: import("../typebox").TString
|
|
5
|
+
path: import("../typebox").TString;
|
|
6
6
|
value: import("../typebox").TUnknown;
|
|
7
7
|
}>;
|
|
8
8
|
export type Update = Static<typeof Update>;
|
|
9
9
|
export declare const Update: import("../typebox").TObject<{
|
|
10
10
|
type: import("../typebox").TLiteral<"update">;
|
|
11
|
-
path: import("../typebox").TString
|
|
11
|
+
path: import("../typebox").TString;
|
|
12
12
|
value: import("../typebox").TUnknown;
|
|
13
13
|
}>;
|
|
14
14
|
export type Delete = Static<typeof Delete>;
|
|
15
15
|
export declare const Delete: import("../typebox").TObject<{
|
|
16
16
|
type: import("../typebox").TLiteral<"delete">;
|
|
17
|
-
path: import("../typebox").TString
|
|
17
|
+
path: import("../typebox").TString;
|
|
18
18
|
}>;
|
|
19
19
|
export type Edit = Static<typeof Edit>;
|
|
20
20
|
export declare const Edit: import("../typebox").TUnion<[import("../typebox").TObject<{
|
|
21
21
|
type: import("../typebox").TLiteral<"insert">;
|
|
22
|
-
path: import("../typebox").TString
|
|
22
|
+
path: import("../typebox").TString;
|
|
23
23
|
value: import("../typebox").TUnknown;
|
|
24
24
|
}>, import("../typebox").TObject<{
|
|
25
25
|
type: import("../typebox").TLiteral<"update">;
|
|
26
|
-
path: import("../typebox").TString
|
|
26
|
+
path: import("../typebox").TString;
|
|
27
27
|
value: import("../typebox").TUnknown;
|
|
28
28
|
}>, import("../typebox").TObject<{
|
|
29
29
|
type: import("../typebox").TLiteral<"delete">;
|
|
30
|
-
path: import("../typebox").TString
|
|
30
|
+
path: import("../typebox").TString;
|
|
31
31
|
}>]>;
|
|
32
32
|
export declare class ValueDeltaObjectWithSymbolKeyError extends Error {
|
|
33
33
|
readonly key: unknown;
|