@sinclair/typebox 0.26.0-dev.1 → 0.26.0-dev.2
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 +2 -2
- package/errors/errors.d.ts +8 -1
- package/errors/errors.js +21 -4
- package/package.json +1 -1
- package/readme.md +3 -3
- package/typebox.d.ts +11 -10
- package/typebox.js +67 -59
- package/value/index.d.ts +1 -1
- package/value/index.js +2 -1
- package/value/value.d.ts +2 -2
package/compiler/compiler.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as Types from '../typebox';
|
|
2
|
-
import {
|
|
2
|
+
import { ValueErrorIterator } from '../errors/index';
|
|
3
3
|
export type CheckFunction = (value: unknown) => boolean;
|
|
4
4
|
export declare class TypeCheck<T extends Types.TSchema> {
|
|
5
5
|
private readonly schema;
|
|
@@ -9,7 +9,7 @@ export declare class TypeCheck<T extends Types.TSchema> {
|
|
|
9
9
|
/** Returns the generated assertion code used to validate this type. */
|
|
10
10
|
Code(): string;
|
|
11
11
|
/** Returns an iterator for each error in this value. */
|
|
12
|
-
Errors(value: unknown):
|
|
12
|
+
Errors(value: unknown): ValueErrorIterator;
|
|
13
13
|
/** Returns true if the value matches the compiled type. */
|
|
14
14
|
Check(value: unknown): value is Types.Static<T>;
|
|
15
15
|
}
|
package/errors/errors.d.ts
CHANGED
|
@@ -67,11 +67,18 @@ export interface ValueError {
|
|
|
67
67
|
value: unknown;
|
|
68
68
|
message: string;
|
|
69
69
|
}
|
|
70
|
+
export declare class ValueErrorIterator {
|
|
71
|
+
private readonly iterator;
|
|
72
|
+
constructor(iterator: IterableIterator<ValueError>);
|
|
73
|
+
[Symbol.iterator](): IterableIterator<ValueError>;
|
|
74
|
+
/** Returns the first value error or undefined if no errors */
|
|
75
|
+
First(): ValueError | undefined;
|
|
76
|
+
}
|
|
70
77
|
export declare class ValueErrorsUnknownTypeError extends Error {
|
|
71
78
|
readonly schema: Types.TSchema;
|
|
72
79
|
constructor(schema: Types.TSchema);
|
|
73
80
|
}
|
|
74
81
|
/** Provides functionality to generate a sequence of errors against a TypeBox type. */
|
|
75
82
|
export declare namespace ValueErrors {
|
|
76
|
-
function Errors<T extends Types.TSchema>(schema: T, value: any):
|
|
83
|
+
function Errors<T extends Types.TSchema>(schema: T, value: any): ValueErrorIterator;
|
|
77
84
|
}
|
package/errors/errors.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ValueErrors = exports.ValueErrorsUnknownTypeError = exports.ValueErrorIterator = exports.ValueErrorType = void 0;
|
|
2
4
|
/*--------------------------------------------------------------------------
|
|
3
5
|
|
|
4
6
|
@sinclair/typebox/errors
|
|
@@ -26,8 +28,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
26
28
|
THE SOFTWARE.
|
|
27
29
|
|
|
28
30
|
---------------------------------------------------------------------------*/
|
|
29
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
-
exports.ValueErrors = exports.ValueErrorsUnknownTypeError = exports.ValueErrorType = void 0;
|
|
31
31
|
const Types = require("../typebox");
|
|
32
32
|
const index_1 = require("../system/index");
|
|
33
33
|
const hash_1 = require("../value/hash");
|
|
@@ -97,6 +97,23 @@ var ValueErrorType;
|
|
|
97
97
|
ValueErrorType[ValueErrorType["Custom"] = 58] = "Custom";
|
|
98
98
|
})(ValueErrorType = exports.ValueErrorType || (exports.ValueErrorType = {}));
|
|
99
99
|
// -------------------------------------------------------------------
|
|
100
|
+
// ValueErrorIterator
|
|
101
|
+
// -------------------------------------------------------------------
|
|
102
|
+
class ValueErrorIterator {
|
|
103
|
+
constructor(iterator) {
|
|
104
|
+
this.iterator = iterator;
|
|
105
|
+
}
|
|
106
|
+
[Symbol.iterator]() {
|
|
107
|
+
return this.iterator;
|
|
108
|
+
}
|
|
109
|
+
/** Returns the first value error or undefined if no errors */
|
|
110
|
+
First() {
|
|
111
|
+
const next = this.iterator.next();
|
|
112
|
+
return next.done ? undefined : next.value;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.ValueErrorIterator = ValueErrorIterator;
|
|
116
|
+
// -------------------------------------------------------------------
|
|
100
117
|
// ValueErrors
|
|
101
118
|
// -------------------------------------------------------------------
|
|
102
119
|
class ValueErrorsUnknownTypeError extends Error {
|
|
@@ -546,8 +563,8 @@ var ValueErrors;
|
|
|
546
563
|
return yield* UserDefined(anySchema, path, value);
|
|
547
564
|
}
|
|
548
565
|
}
|
|
549
|
-
function
|
|
550
|
-
|
|
566
|
+
function Errors(schema, value) {
|
|
567
|
+
return new ValueErrorIterator(Visit(schema, '', value));
|
|
551
568
|
}
|
|
552
569
|
ValueErrors.Errors = Errors;
|
|
553
570
|
})(ValueErrors = exports.ValueErrors || (exports.ValueErrors = {}));
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -586,9 +586,9 @@ TypeBox provides property modifier types that allow properties to be mapped with
|
|
|
586
586
|
│ const T = Type.Object({ │ type T = { │ const T = { │
|
|
587
587
|
│ name: Type.Optional( │ name?: string │ type: 'object', │
|
|
588
588
|
│ Type.String() │ } │ properties: { │
|
|
589
|
-
│ ) │ │
|
|
590
|
-
│ }) │ │
|
|
591
|
-
│ │ │
|
|
589
|
+
│ ) │ │ name: { │
|
|
590
|
+
│ }) │ │ type: 'string' │
|
|
591
|
+
│ │ │ } │
|
|
592
592
|
│ │ │ } │
|
|
593
593
|
│ │ │ } │
|
|
594
594
|
│ │ │ │
|
package/typebox.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export declare const Kind: unique symbol;
|
|
2
2
|
export declare const Hint: unique symbol;
|
|
3
3
|
export declare const Modifier: unique symbol;
|
|
4
|
+
export declare const Recursive: unique symbol;
|
|
4
5
|
export type TupleToIntersect<T extends any[]> = T extends [infer I] ? I : T extends [infer I, ...infer R] ? I & TupleToIntersect<R> : never;
|
|
5
6
|
export type TupleToUnion<T extends any[]> = {
|
|
6
7
|
[K in keyof T]: T[K];
|
|
@@ -41,8 +42,9 @@ export interface TKind {
|
|
|
41
42
|
[Kind]: string;
|
|
42
43
|
}
|
|
43
44
|
export interface TSchema extends SchemaOptions, TKind {
|
|
44
|
-
[Hint]?: string;
|
|
45
45
|
[Modifier]?: string;
|
|
46
|
+
[Recursive]?: string;
|
|
47
|
+
[Hint]?: string;
|
|
46
48
|
params: unknown[];
|
|
47
49
|
static: unknown;
|
|
48
50
|
}
|
|
@@ -170,7 +172,7 @@ export type TKeyOfTuple<T extends TSchema> = {
|
|
|
170
172
|
} extends infer U ? UnionToTuple<Exclude<{
|
|
171
173
|
[K in keyof U]: U[K];
|
|
172
174
|
}[keyof U], undefined>> : never;
|
|
173
|
-
export type TKeyOf<T extends TSchema = TSchema
|
|
175
|
+
export type TKeyOf<T extends TSchema = TSchema, D = TDeref<T>> = (D extends TIntersect ? TKeyOfTuple<T> : D extends TUnion ? TKeyOfTuple<T> : D extends TObject ? TKeyOfTuple<T> : [
|
|
174
176
|
]) extends infer R ? R extends [] ? TNever : TUnion<Assert<R, TSchema[]>> : never;
|
|
175
177
|
export type TLiteralValue = string | number | boolean | bigint;
|
|
176
178
|
export interface TLiteral<T extends TLiteralValue = TLiteralValue> extends TSchema {
|
|
@@ -242,7 +244,7 @@ export type TOmitArray<T extends TSchema[], K extends keyof any> = Assert<{
|
|
|
242
244
|
[K2 in keyof T]: TOmit<Assert<T[K2], TSchema>, K>;
|
|
243
245
|
}, TSchema[]>;
|
|
244
246
|
export type TOmitProperties<T extends TProperties, K extends keyof any> = Evaluate<Assert<Omit<T, K>, TProperties>>;
|
|
245
|
-
export type TOmit<T extends TSchema, K extends keyof any
|
|
247
|
+
export type TOmit<T extends TSchema, K extends keyof any, D = TDeref<T>> = D extends TIntersect<infer S> ? TIntersect<TOmitArray<S, K>> : D extends TUnion<infer S> ? TUnion<TOmitArray<S, K>> : D extends TObject<infer S> ? TObject<TOmitProperties<S, K>> : D;
|
|
246
248
|
export type TParameters<T extends TFunction> = TTuple<T['parameters']>;
|
|
247
249
|
export type TPartialArray<T extends TSchema[]> = Assert<{
|
|
248
250
|
[K in keyof T]: TPartial<Assert<T[K], TSchema>>;
|
|
@@ -250,14 +252,14 @@ export type TPartialArray<T extends TSchema[]> = Assert<{
|
|
|
250
252
|
export type TPartialProperties<T extends TProperties> = Evaluate<Assert<{
|
|
251
253
|
[K in keyof T]: T[K] extends TReadonlyOptional<infer U> ? TReadonlyOptional<U> : T[K] extends TReadonly<infer U> ? TReadonlyOptional<U> : T[K] extends TOptional<infer U> ? TOptional<U> : TOptional<T[K]>;
|
|
252
254
|
}, TProperties>>;
|
|
253
|
-
export type TPartial<T extends TSchema
|
|
255
|
+
export type TPartial<T extends TSchema, D = TDeref<T>> = D extends TIntersect<infer S> ? TIntersect<TPartialArray<S>> : D extends TUnion<infer S> ? TUnion<TPartialArray<S>> : D extends TObject<infer S> ? TObject<TPartialProperties<S>> : D;
|
|
254
256
|
export type TPickArray<T extends TSchema[], K extends keyof any> = {
|
|
255
257
|
[K2 in keyof T]: TPick<Assert<T[K2], TSchema>, K>;
|
|
256
258
|
};
|
|
257
259
|
export type TPickProperties<T extends TProperties, K extends keyof any> = Pick<T, Assert<Extract<K, keyof T>, keyof T>> extends infer R ? ({
|
|
258
260
|
[K in keyof R]: Assert<R[K], TSchema> extends TSchema ? R[K] : never;
|
|
259
261
|
}) : never;
|
|
260
|
-
export type TPick<T extends TSchema, K extends keyof any
|
|
262
|
+
export type TPick<T extends TSchema, K extends keyof any, D = TDeref<T>> = D extends TIntersect<infer S> ? TIntersect<TPickArray<S, K>> : D extends TUnion<infer S> ? TUnion<TPickArray<S, K>> : D extends TObject<infer S> ? TObject<TPickProperties<S, K>> : D;
|
|
261
263
|
export type TPromiseStatic<T extends TSchema, P extends unknown[]> = Promise<Static<T, P>>;
|
|
262
264
|
export interface TPromise<T extends TSchema = TSchema> extends TSchema {
|
|
263
265
|
[Kind]: 'Promise';
|
|
@@ -305,7 +307,7 @@ export type TRequiredArray<T extends TSchema[]> = Assert<{
|
|
|
305
307
|
export type TRequiredProperties<T extends TProperties> = Evaluate<Assert<{
|
|
306
308
|
[K in keyof T]: T[K] extends TReadonlyOptional<infer U> ? TReadonly<U> : T[K] extends TReadonly<infer U> ? TReadonly<U> : T[K] extends TOptional<infer U> ? U : T[K];
|
|
307
309
|
}, TProperties>>;
|
|
308
|
-
export type TRequired<T extends TSchema
|
|
310
|
+
export type TRequired<T extends TSchema, D = TDeref<T>> = D extends TIntersect<infer S> ? TIntersect<TRequiredArray<S>> : D extends TUnion<infer S> ? TUnion<TRequiredArray<S>> : D extends TObject<infer S> ? TObject<TRequiredProperties<S>> : D;
|
|
309
311
|
export type StringFormatOption = 'date-time' | 'time' | 'date' | 'email' | 'idn-email' | 'hostname' | 'idn-hostname' | 'ipv4' | 'ipv6' | 'uri' | 'uri-reference' | 'iri' | 'uuid' | 'iri-reference' | 'uri-template' | 'json-pointer' | 'relative-json-pointer' | 'regex';
|
|
310
312
|
export interface StringOptions<Format extends string> extends SchemaOptions {
|
|
311
313
|
minLength?: number;
|
|
@@ -507,9 +509,10 @@ export declare enum TypeExtendsResult {
|
|
|
507
509
|
export declare namespace TypeExtends {
|
|
508
510
|
function Extends(left: TSchema, right: TSchema): TypeExtendsResult;
|
|
509
511
|
}
|
|
510
|
-
/** Specialized Clone for Types. */
|
|
512
|
+
/** Specialized Clone for Types. Clones and removes non self-referential identifiers */
|
|
511
513
|
export declare namespace TypeClone {
|
|
512
|
-
function
|
|
514
|
+
/** Clones a type. This function will omit non-self referential identifiers on the cloned type. */
|
|
515
|
+
function Clone<T extends TSchema>(schema: T, options: SchemaOptions): T;
|
|
513
516
|
}
|
|
514
517
|
export declare namespace ObjectMap {
|
|
515
518
|
function Map<T = TSchema>(schema: TSchema, callback: (object: TObject) => TObject, options: SchemaOptions): T;
|
|
@@ -520,8 +523,6 @@ export declare namespace KeyResolver {
|
|
|
520
523
|
export declare class TypeBuilder {
|
|
521
524
|
/** `[Utility]` Creates a schema without `static` and `params` types */
|
|
522
525
|
protected Create<T>(schema: Omit<T, 'static' | 'params'>): T;
|
|
523
|
-
/** `[Utility]` Clones a schema or properties object */
|
|
524
|
-
protected Clone<T extends TSchema | TProperties>(schema: T, options?: SchemaOptions): T;
|
|
525
526
|
/** `[Standard]` Omits compositing symbols from this schema */
|
|
526
527
|
Strict<T extends TSchema>(schema: T): T;
|
|
527
528
|
}
|
package/typebox.js
CHANGED
|
@@ -27,13 +27,14 @@ 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.ReferenceRegistry = exports.TypeRegistry = exports.Modifier = exports.Hint = exports.Kind = void 0;
|
|
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.ReferenceRegistry = exports.TypeRegistry = exports.Recursive = exports.Modifier = exports.Hint = exports.Kind = void 0;
|
|
31
31
|
// -------------------------------------------------------------------------------------
|
|
32
|
-
// Symbols
|
|
32
|
+
// Compositing Symbols
|
|
33
33
|
// -------------------------------------------------------------------------------------
|
|
34
34
|
exports.Kind = Symbol.for('TypeBox.Kind');
|
|
35
35
|
exports.Hint = Symbol.for('TypeBox.Hint');
|
|
36
36
|
exports.Modifier = Symbol.for('TypeBox.Modifier');
|
|
37
|
+
exports.Recursive = Symbol.for('TypeBox.Recursive');
|
|
37
38
|
var TypeRegistry;
|
|
38
39
|
(function (TypeRegistry) {
|
|
39
40
|
const types = new Map();
|
|
@@ -1356,7 +1357,7 @@ var TypeExtends;
|
|
|
1356
1357
|
return Unknown(left_, right_);
|
|
1357
1358
|
if (TypeGuard.TVoid(left_))
|
|
1358
1359
|
return Void(left_, right_);
|
|
1359
|
-
throw Error(`TypeExtends: Unknown left operand '${left[exports.Kind]}'`);
|
|
1360
|
+
throw Error(`TypeExtends: Unknown left type operand '${left[exports.Kind]}'`);
|
|
1360
1361
|
}
|
|
1361
1362
|
function Extends(left, right) {
|
|
1362
1363
|
return Visit(left, right);
|
|
@@ -1366,9 +1367,12 @@ var TypeExtends;
|
|
|
1366
1367
|
// -------------------------------------------------------------------------------------
|
|
1367
1368
|
// TypeClone
|
|
1368
1369
|
// -------------------------------------------------------------------------------------
|
|
1369
|
-
/** Specialized Clone for Types. */
|
|
1370
|
+
/** Specialized Clone for Types. Clones and removes non self-referential identifiers */
|
|
1370
1371
|
var TypeClone;
|
|
1371
1372
|
(function (TypeClone) {
|
|
1373
|
+
function IsRecursive(value) {
|
|
1374
|
+
return typeof value[exports.Recursive] === 'string';
|
|
1375
|
+
}
|
|
1372
1376
|
function IsObject(value) {
|
|
1373
1377
|
return typeof value === 'object' && value !== null && !globalThis.Array.isArray(value);
|
|
1374
1378
|
}
|
|
@@ -1383,14 +1387,16 @@ var TypeClone;
|
|
|
1383
1387
|
return clone;
|
|
1384
1388
|
}
|
|
1385
1389
|
function Visit(value) {
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1390
|
+
const clone = IsObject(value) ? { ...value } : IsArray(value) ? [...value] : value;
|
|
1391
|
+
if (IsObject(clone) && !IsRecursive(clone))
|
|
1392
|
+
delete clone['$id'];
|
|
1393
|
+
if (IsObject(clone))
|
|
1394
|
+
return Object(clone);
|
|
1395
|
+
if (IsArray(clone))
|
|
1396
|
+
return Array(clone);
|
|
1397
|
+
return clone;
|
|
1393
1398
|
}
|
|
1399
|
+
/** Clones a type. This function will omit non-self referential identifiers on the cloned type. */
|
|
1394
1400
|
function Clone(schema, options) {
|
|
1395
1401
|
return { ...Visit({ ...schema }), ...options };
|
|
1396
1402
|
}
|
|
@@ -1469,12 +1475,6 @@ class TypeBuilder {
|
|
|
1469
1475
|
ReferenceRegistry.Set(schema);
|
|
1470
1476
|
return schema;
|
|
1471
1477
|
}
|
|
1472
|
-
/** `[Utility]` Clones a schema or properties object */
|
|
1473
|
-
Clone(schema, options = {}) {
|
|
1474
|
-
const clone = TypeClone.Clone(schema, options);
|
|
1475
|
-
ReferenceRegistry.Set(clone);
|
|
1476
|
-
return clone;
|
|
1477
|
-
}
|
|
1478
1478
|
/** `[Standard]` Omits compositing symbols from this schema */
|
|
1479
1479
|
Strict(schema) {
|
|
1480
1480
|
return JSON.parse(JSON.stringify(schema));
|
|
@@ -1490,11 +1490,11 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1490
1490
|
// ----------------------------------------------------------------------
|
|
1491
1491
|
/** `[Modifier]` Creates a Optional property */
|
|
1492
1492
|
Optional(schema) {
|
|
1493
|
-
return { [exports.Modifier]: 'Optional', ...
|
|
1493
|
+
return { [exports.Modifier]: 'Optional', ...TypeClone.Clone(schema, {}) };
|
|
1494
1494
|
}
|
|
1495
1495
|
/** `[Modifier]` Creates a ReadonlyOptional property */
|
|
1496
1496
|
ReadonlyOptional(schema) {
|
|
1497
|
-
return { [exports.Modifier]: 'ReadonlyOptional', ...
|
|
1497
|
+
return { [exports.Modifier]: 'ReadonlyOptional', ...TypeClone.Clone(schema, {}) };
|
|
1498
1498
|
}
|
|
1499
1499
|
/** `[Modifier]` Creates a Readonly object or property */
|
|
1500
1500
|
Readonly(schema) {
|
|
@@ -1509,7 +1509,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1509
1509
|
}
|
|
1510
1510
|
/** `[Standard]` Creates an Array type */
|
|
1511
1511
|
Array(items, options = {}) {
|
|
1512
|
-
return this.Create({ ...options, [exports.Kind]: 'Array', type: 'array', items });
|
|
1512
|
+
return this.Create({ ...options, [exports.Kind]: 'Array', type: 'array', items: TypeClone.Clone(items, {}) });
|
|
1513
1513
|
}
|
|
1514
1514
|
/** `[Standard]` Creates a Boolean type */
|
|
1515
1515
|
Boolean(options = {}) {
|
|
@@ -1527,7 +1527,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1527
1527
|
const properties = {};
|
|
1528
1528
|
for (const object of schemas) {
|
|
1529
1529
|
for (const [key, property] of globalThis.Object.entries(object.properties)) {
|
|
1530
|
-
const mapped = key in properties ? this.Union([properties[key], property]) : property;
|
|
1530
|
+
const mapped = key in properties ? this.Union([properties[key], property]) : TypeClone.Clone(property, {});
|
|
1531
1531
|
properties[key] = optional.has(key) ? this.Optional(mapped) : mapped;
|
|
1532
1532
|
}
|
|
1533
1533
|
}
|
|
@@ -1548,31 +1548,31 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1548
1548
|
Extends(left, right, trueType, falseType, options = {}) {
|
|
1549
1549
|
switch (TypeExtends.Extends(ReferenceRegistry.Deref(left), ReferenceRegistry.Deref(right))) {
|
|
1550
1550
|
case TypeExtendsResult.Union:
|
|
1551
|
-
return this.Union([
|
|
1551
|
+
return this.Union([TypeClone.Clone(trueType, options), TypeClone.Clone(falseType, options)]);
|
|
1552
1552
|
case TypeExtendsResult.True:
|
|
1553
|
-
return
|
|
1553
|
+
return TypeClone.Clone(trueType, options);
|
|
1554
1554
|
case TypeExtendsResult.False:
|
|
1555
|
-
return
|
|
1555
|
+
return TypeClone.Clone(falseType, options);
|
|
1556
1556
|
}
|
|
1557
1557
|
}
|
|
1558
1558
|
/** `[Standard]` Excludes from the left type any type that is not assignable to the right */
|
|
1559
1559
|
Exclude(left, right, options = {}) {
|
|
1560
1560
|
if (TypeGuard.TUnion(left)) {
|
|
1561
1561
|
const narrowed = left.anyOf.filter((inner) => TypeExtends.Extends(inner, right) === TypeExtendsResult.False);
|
|
1562
|
-
return (narrowed.length === 1 ?
|
|
1562
|
+
return (narrowed.length === 1 ? TypeClone.Clone(narrowed[0], options) : this.Union(narrowed, options));
|
|
1563
1563
|
}
|
|
1564
1564
|
else {
|
|
1565
|
-
return (TypeExtends.Extends(left, right) !== TypeExtendsResult.False ? this.Never(options) :
|
|
1565
|
+
return (TypeExtends.Extends(left, right) !== TypeExtendsResult.False ? this.Never(options) : TypeClone.Clone(left, options));
|
|
1566
1566
|
}
|
|
1567
1567
|
}
|
|
1568
1568
|
/** `[Standard]` Extracts from left left any type that is assignable to the right */
|
|
1569
1569
|
Extract(left, right, options = {}) {
|
|
1570
1570
|
if (TypeGuard.TUnion(left)) {
|
|
1571
1571
|
const narrowed = left.anyOf.filter((inner) => TypeExtends.Extends(inner, right) !== TypeExtendsResult.False);
|
|
1572
|
-
return (narrowed.length === 1 ?
|
|
1572
|
+
return (narrowed.length === 1 ? TypeClone.Clone(narrowed[0], options) : this.Union(narrowed, options));
|
|
1573
1573
|
}
|
|
1574
1574
|
else {
|
|
1575
|
-
return (TypeExtends.Extends(left, right) !== TypeExtendsResult.False ?
|
|
1575
|
+
return (TypeExtends.Extends(left, right) !== TypeExtendsResult.False ? TypeClone.Clone(left, options) : this.Never(options));
|
|
1576
1576
|
}
|
|
1577
1577
|
}
|
|
1578
1578
|
/** `[Standard]` Creates an Integer type */
|
|
@@ -1583,9 +1583,9 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1583
1583
|
if (allOf.length === 0)
|
|
1584
1584
|
return exports.Type.Never();
|
|
1585
1585
|
if (allOf.length === 1)
|
|
1586
|
-
return
|
|
1586
|
+
return TypeClone.Clone(allOf[0], options);
|
|
1587
1587
|
const objects = allOf.every((schema) => TypeGuard.TObject(schema));
|
|
1588
|
-
const cloned = allOf.map((schema) =>
|
|
1588
|
+
const cloned = allOf.map((schema) => TypeClone.Clone(schema, {}));
|
|
1589
1589
|
if (options.unevaluatedProperties === false || TypeGuard.TSchema(options.unevaluatedProperties) || objects) {
|
|
1590
1590
|
return this.Create({ ...options, [exports.Kind]: 'Intersect', type: 'object', allOf: cloned });
|
|
1591
1591
|
}
|
|
@@ -1614,7 +1614,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1614
1614
|
}
|
|
1615
1615
|
/** `[Standard]` Creates a Not type. The first argument is the disallowed type, the second is the allowed. */
|
|
1616
1616
|
Not(not, schema, options) {
|
|
1617
|
-
return this.Create({ ...options, [exports.Kind]: 'Not', allOf: [{ not }, schema] });
|
|
1617
|
+
return this.Create({ ...options, [exports.Kind]: 'Not', allOf: [{ not: TypeClone.Clone(not, {}) }, TypeClone.Clone(schema, {})] });
|
|
1618
1618
|
}
|
|
1619
1619
|
/** `[Standard]` Creates a Null type */
|
|
1620
1620
|
Null(options = {}) {
|
|
@@ -1629,17 +1629,20 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1629
1629
|
const keys = globalThis.Object.keys(properties);
|
|
1630
1630
|
const optional = keys.filter((key) => TypeGuard.TOptional(properties[key]) || TypeGuard.TReadonlyOptional(properties[key]));
|
|
1631
1631
|
const required = keys.filter((name) => !optional.includes(name));
|
|
1632
|
+
const clonedProperties = globalThis.Object.getOwnPropertyNames(properties).reduce((acc, key) => {
|
|
1633
|
+
return { ...acc, [key]: TypeClone.Clone(properties[key], {}) };
|
|
1634
|
+
}, {});
|
|
1632
1635
|
if (required.length > 0) {
|
|
1633
|
-
return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties:
|
|
1636
|
+
return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties: clonedProperties, required });
|
|
1634
1637
|
}
|
|
1635
1638
|
else {
|
|
1636
|
-
return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties:
|
|
1639
|
+
return this.Create({ ...options, [exports.Kind]: 'Object', type: 'object', properties: clonedProperties });
|
|
1637
1640
|
}
|
|
1638
1641
|
}
|
|
1639
1642
|
Omit(schema, unresolved, options = {}) {
|
|
1640
1643
|
const keys = TypeGuard.TUnionLiteral(unresolved) ? unresolved.anyOf.map((schema) => schema.const) : unresolved;
|
|
1641
1644
|
// prettier-ignore
|
|
1642
|
-
return ObjectMap.Map(
|
|
1645
|
+
return ObjectMap.Map(TypeClone.Clone(ReferenceRegistry.Deref(schema), {}), (schema) => {
|
|
1643
1646
|
if (schema.required) {
|
|
1644
1647
|
schema.required = schema.required.filter((key) => !keys.includes(key));
|
|
1645
1648
|
if (schema.required.length === 0)
|
|
@@ -1672,7 +1675,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1672
1675
|
}
|
|
1673
1676
|
}
|
|
1674
1677
|
// prettier-ignore
|
|
1675
|
-
return ObjectMap.Map(
|
|
1678
|
+
return ObjectMap.Map(TypeClone.Clone(ReferenceRegistry.Deref(schema), {}), (schema) => {
|
|
1676
1679
|
delete schema.required;
|
|
1677
1680
|
globalThis.Object.keys(schema.properties).forEach(key => Apply(schema.properties[key]));
|
|
1678
1681
|
return schema;
|
|
@@ -1681,7 +1684,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1681
1684
|
Pick(schema, unresolved, options = {}) {
|
|
1682
1685
|
const keys = TypeGuard.TUnionLiteral(unresolved) ? unresolved.anyOf.map((schema) => schema.const) : unresolved;
|
|
1683
1686
|
// prettier-ignore
|
|
1684
|
-
return ObjectMap.Map(
|
|
1687
|
+
return ObjectMap.Map(TypeClone.Clone(ReferenceRegistry.Deref(schema), {}), (schema) => {
|
|
1685
1688
|
if (schema.required) {
|
|
1686
1689
|
schema.required = schema.required.filter((key) => keys.includes(key));
|
|
1687
1690
|
if (schema.required.length === 0)
|
|
@@ -1697,14 +1700,14 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1697
1700
|
Record(key, schema, options = {}) {
|
|
1698
1701
|
if (TypeGuard.TLiteral(key)) {
|
|
1699
1702
|
if (typeof key.const === 'string' || typeof key.const === 'number') {
|
|
1700
|
-
return this.Object({ [key.const]: schema }, options);
|
|
1703
|
+
return this.Object({ [key.const]: TypeClone.Clone(schema, {}) }, options);
|
|
1701
1704
|
}
|
|
1702
1705
|
else
|
|
1703
1706
|
throw Error('TypeBuilder: Record key can only be derived from literals of number or string');
|
|
1704
1707
|
}
|
|
1705
1708
|
if (TypeGuard.TUnion(key)) {
|
|
1706
1709
|
if (key.anyOf.every((schema) => TypeGuard.TLiteral(schema) && (typeof schema.const === 'string' || typeof schema.const === 'number'))) {
|
|
1707
|
-
const properties = key.anyOf.reduce((acc, literal) => ({ ...acc, [literal.const]:
|
|
1710
|
+
const properties = key.anyOf.reduce((acc, literal) => ({ ...acc, [literal.const]: TypeClone.Clone(schema, {}) }), {});
|
|
1708
1711
|
return this.Object(properties, { ...options, [exports.Hint]: 'Record' });
|
|
1709
1712
|
}
|
|
1710
1713
|
else
|
|
@@ -1715,7 +1718,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1715
1718
|
...options,
|
|
1716
1719
|
[exports.Kind]: 'Record',
|
|
1717
1720
|
type: 'object',
|
|
1718
|
-
patternProperties: { [pattern]: schema },
|
|
1721
|
+
patternProperties: { [pattern]: TypeClone.Clone(schema, {}) },
|
|
1719
1722
|
additionalProperties: false,
|
|
1720
1723
|
});
|
|
1721
1724
|
}
|
|
@@ -1726,12 +1729,12 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1726
1729
|
const self = callback({ [exports.Kind]: 'Self', $ref: `${options.$id}` });
|
|
1727
1730
|
const reassign = self;
|
|
1728
1731
|
reassign.$id = options.$id; // required as $id is readonly
|
|
1729
|
-
return this.Create({ ...options, ...self });
|
|
1732
|
+
return this.Create({ ...options, ...self, [exports.Recursive]: reassign.$id });
|
|
1730
1733
|
}
|
|
1731
1734
|
/** `[Standard]` Creates a Ref type. The referenced type must contain a $id */
|
|
1732
1735
|
Ref(schema, options = {}) {
|
|
1733
1736
|
if (schema.$id === undefined)
|
|
1734
|
-
throw Error('
|
|
1737
|
+
throw Error('StandardTypeBuilder.Ref: Target type must specify an $id');
|
|
1735
1738
|
return this.Create({ ...options, [exports.Kind]: 'Ref', $ref: schema.$id });
|
|
1736
1739
|
}
|
|
1737
1740
|
/** `[Standard]` Creates a mapped type where all properties are Required */
|
|
@@ -1754,7 +1757,7 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1754
1757
|
}
|
|
1755
1758
|
}
|
|
1756
1759
|
// prettier-ignore
|
|
1757
|
-
return ObjectMap.Map(
|
|
1760
|
+
return ObjectMap.Map(TypeClone.Clone(ReferenceRegistry.Deref(schema), {}), (schema) => {
|
|
1758
1761
|
schema.required = globalThis.Object.keys(schema.properties);
|
|
1759
1762
|
globalThis.Object.keys(schema.properties).forEach(key => Apply(schema.properties[key]));
|
|
1760
1763
|
return schema;
|
|
@@ -1767,9 +1770,10 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1767
1770
|
/** `[Standard]` Creates a Tuple type */
|
|
1768
1771
|
Tuple(items, options = {}) {
|
|
1769
1772
|
const [additionalItems, minItems, maxItems] = [false, items.length, items.length];
|
|
1773
|
+
const clonedItems = items.map((item) => TypeClone.Clone(item, {}));
|
|
1770
1774
|
// prettier-ignore
|
|
1771
1775
|
const schema = (items.length > 0 ?
|
|
1772
|
-
{ ...options, [exports.Kind]: 'Tuple', type: 'array', items, additionalItems, minItems, maxItems } :
|
|
1776
|
+
{ ...options, [exports.Kind]: 'Tuple', type: 'array', items: clonedItems, additionalItems, minItems, maxItems } :
|
|
1773
1777
|
{ ...options, [exports.Kind]: 'Tuple', type: 'array', minItems, maxItems });
|
|
1774
1778
|
return this.Create(schema);
|
|
1775
1779
|
}
|
|
@@ -1777,9 +1781,9 @@ class StandardTypeBuilder extends TypeBuilder {
|
|
|
1777
1781
|
if (anyOf.length === 0)
|
|
1778
1782
|
return this.Never(options);
|
|
1779
1783
|
if (anyOf.length === 1)
|
|
1780
|
-
return
|
|
1781
|
-
const
|
|
1782
|
-
return this.Create({ ...options, [exports.Kind]: 'Union', anyOf:
|
|
1784
|
+
return TypeClone.Clone(anyOf[0], options);
|
|
1785
|
+
const clonedAnyOf = anyOf.map((schema) => TypeClone.Clone(schema, {}));
|
|
1786
|
+
return this.Create({ ...options, [exports.Kind]: 'Union', anyOf: clonedAnyOf });
|
|
1783
1787
|
}
|
|
1784
1788
|
/** `[Standard]` Creates an Unknown type */
|
|
1785
1789
|
Unknown(options = {}) {
|
|
@@ -1804,15 +1808,17 @@ class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
|
1804
1808
|
return this.Tuple([...schema.parameters], { ...options });
|
|
1805
1809
|
}
|
|
1806
1810
|
Constructor(parameters, returns, options = {}) {
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1811
|
+
const clonedReturns = TypeClone.Clone(returns, {});
|
|
1812
|
+
if (TypeGuard.TTuple(parameters)) {
|
|
1813
|
+
const clonedParameters = parameters.items === undefined ? [] : parameters.items.map((parameter) => TypeClone.Clone(parameter, {}));
|
|
1814
|
+
return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'object', instanceOf: 'Constructor', parameters: clonedParameters, returns: clonedReturns });
|
|
1810
1815
|
}
|
|
1811
1816
|
else if (globalThis.Array.isArray(parameters)) {
|
|
1812
|
-
|
|
1817
|
+
const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter, {}));
|
|
1818
|
+
return this.Create({ ...options, [exports.Kind]: 'Constructor', type: 'object', instanceOf: 'Constructor', parameters: clonedParameters, returns: clonedReturns });
|
|
1813
1819
|
}
|
|
1814
1820
|
else {
|
|
1815
|
-
throw new Error('
|
|
1821
|
+
throw new Error('ExtendedTypeBuilder.Constructor: Invalid parameters');
|
|
1816
1822
|
}
|
|
1817
1823
|
}
|
|
1818
1824
|
/** `[Extended]` Creates a Date type */
|
|
@@ -1820,20 +1826,22 @@ class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
|
1820
1826
|
return this.Create({ ...options, [exports.Kind]: 'Date', type: 'object', instanceOf: 'Date' });
|
|
1821
1827
|
}
|
|
1822
1828
|
Function(parameters, returns, options = {}) {
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1829
|
+
const clonedReturns = TypeClone.Clone(returns, {});
|
|
1830
|
+
if (TypeGuard.TTuple(parameters)) {
|
|
1831
|
+
const clonedParameters = parameters.items === undefined ? [] : parameters.items.map((parameter) => TypeClone.Clone(parameter, {}));
|
|
1832
|
+
return this.Create({ ...options, [exports.Kind]: 'Function', type: 'object', instanceOf: 'Function', parameters: clonedParameters, returns: clonedReturns });
|
|
1826
1833
|
}
|
|
1827
1834
|
else if (globalThis.Array.isArray(parameters)) {
|
|
1828
|
-
|
|
1835
|
+
const clonedParameters = parameters.map((parameter) => TypeClone.Clone(parameter, {}));
|
|
1836
|
+
return this.Create({ ...options, [exports.Kind]: 'Function', type: 'object', instanceOf: 'Function', parameters: clonedParameters, returns: clonedReturns });
|
|
1829
1837
|
}
|
|
1830
1838
|
else {
|
|
1831
|
-
throw new Error('
|
|
1839
|
+
throw new Error('ExtendedTypeBuilder.Function: Invalid parameters');
|
|
1832
1840
|
}
|
|
1833
1841
|
}
|
|
1834
1842
|
/** `[Extended]` Extracts the InstanceType from the given Constructor */
|
|
1835
1843
|
InstanceType(schema, options = {}) {
|
|
1836
|
-
return
|
|
1844
|
+
return TypeClone.Clone(schema.returns, options);
|
|
1837
1845
|
}
|
|
1838
1846
|
/** `[Extended]` Extracts the Parameters from the given Function type */
|
|
1839
1847
|
Parameters(schema, options = {}) {
|
|
@@ -1841,7 +1849,7 @@ class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
|
1841
1849
|
}
|
|
1842
1850
|
/** `[Extended]` Creates a Promise type */
|
|
1843
1851
|
Promise(item, options = {}) {
|
|
1844
|
-
return this.Create({ ...options, [exports.Kind]: 'Promise', type: 'object', instanceOf: 'Promise', item });
|
|
1852
|
+
return this.Create({ ...options, [exports.Kind]: 'Promise', type: 'object', instanceOf: 'Promise', item: TypeClone.Clone(item, {}) });
|
|
1845
1853
|
}
|
|
1846
1854
|
/** `[Extended]` Creates a regular expression type */
|
|
1847
1855
|
RegEx(regex, options = {}) {
|
|
@@ -1849,7 +1857,7 @@ class ExtendedTypeBuilder extends StandardTypeBuilder {
|
|
|
1849
1857
|
}
|
|
1850
1858
|
/** `[Extended]` Extracts the ReturnType from the given Function */
|
|
1851
1859
|
ReturnType(schema, options = {}) {
|
|
1852
|
-
return
|
|
1860
|
+
return TypeClone.Clone(schema.returns, options);
|
|
1853
1861
|
}
|
|
1854
1862
|
/** `[Extended]` Creates a Symbol type */
|
|
1855
1863
|
Symbol(options) {
|
package/value/index.d.ts
CHANGED
package/value/index.js
CHANGED
|
@@ -41,8 +41,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
41
41
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
42
42
|
};
|
|
43
43
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
-
exports.Delete = exports.Update = exports.Insert = exports.Edit = exports.ValueHash = exports.ValueErrorType = void 0;
|
|
44
|
+
exports.Delete = exports.Update = exports.Insert = exports.Edit = exports.ValueHash = exports.ValueErrorType = exports.ValueErrorIterator = void 0;
|
|
45
45
|
var index_1 = require("../errors/index");
|
|
46
|
+
Object.defineProperty(exports, "ValueErrorIterator", { enumerable: true, get: function () { return index_1.ValueErrorIterator; } });
|
|
46
47
|
Object.defineProperty(exports, "ValueErrorType", { enumerable: true, get: function () { return index_1.ValueErrorType; } });
|
|
47
48
|
var hash_1 = require("./hash");
|
|
48
49
|
Object.defineProperty(exports, "ValueHash", { enumerable: true, get: function () { return hash_1.ValueHash; } });
|
package/value/value.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as Types from '../typebox';
|
|
2
|
-
import {
|
|
2
|
+
import { ValueErrorIterator } from '../errors/index';
|
|
3
3
|
import { Edit } from './delta';
|
|
4
4
|
/** Provides functions to perform structural updates to JavaScript values */
|
|
5
5
|
export declare namespace Value {
|
|
@@ -14,7 +14,7 @@ export declare namespace Value {
|
|
|
14
14
|
/** Returns a structural clone of the given value */
|
|
15
15
|
function Clone<T>(value: T): T;
|
|
16
16
|
/** Returns an iterator for each error in this value. */
|
|
17
|
-
function Errors<T extends Types.TSchema>(schema: T, value: unknown):
|
|
17
|
+
function Errors<T extends Types.TSchema>(schema: T, value: unknown): ValueErrorIterator;
|
|
18
18
|
/** Returns true if left and right values are structurally equal */
|
|
19
19
|
function Equal<T>(left: T, right: unknown): right is T;
|
|
20
20
|
/** Returns edits to transform the current value into the next value */
|