@sinclair/typebox 0.31.25 → 0.31.27
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 +2 -2
- package/package.json +1 -1
- package/typebox.d.ts +6 -4
- package/typebox.js +24 -6
- package/value/create.js +1 -1
- package/value/transform.d.ts +2 -2
- package/value/transform.js +7 -10
- package/value/value.d.ts +4 -4
- package/value/value.js +2 -2
package/compiler/compiler.js
CHANGED
|
@@ -62,11 +62,11 @@ class TypeCheck {
|
|
|
62
62
|
Decode(value) {
|
|
63
63
|
if (!this.checkFunc(value))
|
|
64
64
|
throw new transform_1.TransformDecodeCheckError(this.schema, value, this.Errors(value).First());
|
|
65
|
-
return this.hasTransform ? transform_1.DecodeTransform.Decode(this.schema, this.references, value
|
|
65
|
+
return this.hasTransform ? transform_1.DecodeTransform.Decode(this.schema, this.references, value) : value;
|
|
66
66
|
}
|
|
67
67
|
/** Encodes a value or throws if error */
|
|
68
68
|
Encode(value) {
|
|
69
|
-
const encoded = this.hasTransform ? transform_1.EncodeTransform.Encode(this.schema, this.references, value
|
|
69
|
+
const encoded = this.hasTransform ? transform_1.EncodeTransform.Encode(this.schema, this.references, value) : value;
|
|
70
70
|
if (!this.checkFunc(encoded))
|
|
71
71
|
throw new transform_1.TransformEncodeCheckError(this.schema, value, this.Errors(value).First());
|
|
72
72
|
return encoded;
|
package/package.json
CHANGED
package/typebox.d.ts
CHANGED
|
@@ -104,10 +104,9 @@ export interface TArray<T extends TSchema = TSchema> extends TSchema, ArrayOptio
|
|
|
104
104
|
type: 'array';
|
|
105
105
|
items: T;
|
|
106
106
|
}
|
|
107
|
-
export type TAsyncIteratorResolve<T extends TSchema, P extends unknown[]> = AsyncIterableIterator<Ensure<Static<T, P>>>;
|
|
108
107
|
export interface TAsyncIterator<T extends TSchema = TSchema> extends TSchema {
|
|
109
108
|
[Kind]: 'AsyncIterator';
|
|
110
|
-
static:
|
|
109
|
+
static: AsyncIterableIterator<Static<T, this['params']>>;
|
|
111
110
|
type: 'AsyncIterator';
|
|
112
111
|
items: T;
|
|
113
112
|
}
|
|
@@ -224,10 +223,9 @@ export interface TIntersect<T extends TSchema[] = TSchema[]> extends TSchema, In
|
|
|
224
223
|
type?: 'object';
|
|
225
224
|
allOf: [...T];
|
|
226
225
|
}
|
|
227
|
-
export type TIteratorResolve<T extends TSchema, P extends unknown[]> = IterableIterator<Ensure<Static<T, P>>>;
|
|
228
226
|
export interface TIterator<T extends TSchema = TSchema> extends TSchema {
|
|
229
227
|
[Kind]: 'Iterator';
|
|
230
|
-
static:
|
|
228
|
+
static: IterableIterator<Static<T, this['params']>>;
|
|
231
229
|
type: 'Iterator';
|
|
232
230
|
items: T;
|
|
233
231
|
}
|
|
@@ -562,6 +560,8 @@ export declare namespace ValueGuard {
|
|
|
562
560
|
function IsBigInt(value: unknown): value is bigint;
|
|
563
561
|
/** Returns true if this value is a boolean */
|
|
564
562
|
function IsBoolean(value: unknown): value is boolean;
|
|
563
|
+
/** Returns true if this value is a Date object */
|
|
564
|
+
function IsDate(value: unknown): value is Date;
|
|
565
565
|
/** Returns true if this value is null */
|
|
566
566
|
function IsNull(value: unknown): value is null;
|
|
567
567
|
/** Returns true if this value is number */
|
|
@@ -570,6 +570,8 @@ export declare namespace ValueGuard {
|
|
|
570
570
|
function IsObject(value: unknown): value is Record<PropertyKey, unknown>;
|
|
571
571
|
/** Returns true if this value is string */
|
|
572
572
|
function IsString(value: unknown): value is string;
|
|
573
|
+
/** Returns true if this value is a Uint8Array */
|
|
574
|
+
function IsUint8Array(value: unknown): value is Uint8Array;
|
|
573
575
|
/** Returns true if this value is undefined */
|
|
574
576
|
function IsUndefined(value: unknown): value is undefined;
|
|
575
577
|
}
|
package/typebox.js
CHANGED
|
@@ -145,6 +145,11 @@ var ValueGuard;
|
|
|
145
145
|
return typeof value === 'boolean';
|
|
146
146
|
}
|
|
147
147
|
ValueGuard.IsBoolean = IsBoolean;
|
|
148
|
+
/** Returns true if this value is a Date object */
|
|
149
|
+
function IsDate(value) {
|
|
150
|
+
return value instanceof globalThis.Date;
|
|
151
|
+
}
|
|
152
|
+
ValueGuard.IsDate = IsDate;
|
|
148
153
|
/** Returns true if this value is null */
|
|
149
154
|
function IsNull(value) {
|
|
150
155
|
return value === null;
|
|
@@ -165,6 +170,11 @@ var ValueGuard;
|
|
|
165
170
|
return typeof value === 'string';
|
|
166
171
|
}
|
|
167
172
|
ValueGuard.IsString = IsString;
|
|
173
|
+
/** Returns true if this value is a Uint8Array */
|
|
174
|
+
function IsUint8Array(value) {
|
|
175
|
+
return value instanceof globalThis.Uint8Array;
|
|
176
|
+
}
|
|
177
|
+
ValueGuard.IsUint8Array = IsUint8Array;
|
|
168
178
|
/** Returns true if this value is undefined */
|
|
169
179
|
function IsUndefined(value) {
|
|
170
180
|
return value === undefined;
|
|
@@ -1252,19 +1262,27 @@ var TypeExtends;
|
|
|
1252
1262
|
/** Specialized Clone for Types */
|
|
1253
1263
|
var TypeClone;
|
|
1254
1264
|
(function (TypeClone) {
|
|
1265
|
+
function ArrayType(value) {
|
|
1266
|
+
return value.map((value) => Visit(value));
|
|
1267
|
+
}
|
|
1268
|
+
function DateType(value) {
|
|
1269
|
+
return new Date(value.getTime());
|
|
1270
|
+
}
|
|
1271
|
+
function Uint8ArrayType(value) {
|
|
1272
|
+
return new Uint8Array(value);
|
|
1273
|
+
}
|
|
1255
1274
|
function ObjectType(value) {
|
|
1256
1275
|
const clonedProperties = Object.getOwnPropertyNames(value).reduce((acc, key) => ({ ...acc, [key]: Visit(value[key]) }), {});
|
|
1257
1276
|
const clonedSymbols = Object.getOwnPropertySymbols(value).reduce((acc, key) => ({ ...acc, [key]: Visit(value[key]) }), {});
|
|
1258
1277
|
return { ...clonedProperties, ...clonedSymbols };
|
|
1259
1278
|
}
|
|
1260
|
-
function ArrayType(value) {
|
|
1261
|
-
return value.map((value) => Visit(value));
|
|
1262
|
-
}
|
|
1263
1279
|
function Visit(value) {
|
|
1264
1280
|
// prettier-ignore
|
|
1265
|
-
return ValueGuard.IsArray(value) ? ArrayType(value) :
|
|
1266
|
-
ValueGuard.
|
|
1267
|
-
value
|
|
1281
|
+
return (ValueGuard.IsArray(value) ? ArrayType(value) :
|
|
1282
|
+
ValueGuard.IsDate(value) ? DateType(value) :
|
|
1283
|
+
ValueGuard.IsUint8Array(value) ? Uint8ArrayType(value) :
|
|
1284
|
+
ValueGuard.IsObject(value) ? ObjectType(value) :
|
|
1285
|
+
value);
|
|
1268
1286
|
}
|
|
1269
1287
|
/** Clones a Rest */
|
|
1270
1288
|
function Rest(schemas) {
|
package/value/create.js
CHANGED
package/value/transform.d.ts
CHANGED
|
@@ -34,9 +34,9 @@ export declare namespace HasTransform {
|
|
|
34
34
|
}
|
|
35
35
|
/** Decodes a value using transform decoders if available. Does not ensure correct results. */
|
|
36
36
|
export declare namespace DecodeTransform {
|
|
37
|
-
function Decode(schema: Types.TSchema, references: Types.TSchema[], value: unknown
|
|
37
|
+
function Decode(schema: Types.TSchema, references: Types.TSchema[], value: unknown): unknown;
|
|
38
38
|
}
|
|
39
39
|
/** Encodes a value using transform encoders if available. Does not ensure correct results. */
|
|
40
40
|
export declare namespace EncodeTransform {
|
|
41
|
-
function Encode(schema: Types.TSchema, references: Types.TSchema[], value: unknown
|
|
41
|
+
function Encode(schema: Types.TSchema, references: Types.TSchema[], value: unknown): unknown;
|
|
42
42
|
}
|
package/value/transform.js
CHANGED
|
@@ -30,6 +30,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
30
30
|
exports.EncodeTransform = exports.DecodeTransform = exports.HasTransform = exports.TransformEncodeError = exports.TransformDecodeError = exports.TransformEncodeCheckError = exports.TransformDecodeCheckError = exports.TransformUnknownTypeError = void 0;
|
|
31
31
|
const guard_1 = require("./guard");
|
|
32
32
|
const deref_1 = require("./deref");
|
|
33
|
+
const check_1 = require("./check");
|
|
33
34
|
const Types = require("../typebox");
|
|
34
35
|
// -------------------------------------------------------------------------
|
|
35
36
|
// Errors
|
|
@@ -125,7 +126,7 @@ var HasTransform;
|
|
|
125
126
|
return Visit((0, deref_1.Deref)(schema, references), references);
|
|
126
127
|
}
|
|
127
128
|
function TTuple(schema, references) {
|
|
128
|
-
return Types.TypeGuard.TTransform(schema) || (
|
|
129
|
+
return Types.TypeGuard.TTransform(schema) || (!(0, guard_1.IsUndefined)(schema.items) && schema.items.some((schema) => Visit(schema, references)));
|
|
129
130
|
}
|
|
130
131
|
function TUnion(schema, references) {
|
|
131
132
|
return Types.TypeGuard.TTransform(schema) || schema.anyOf.some((schema) => Visit(schema, references));
|
|
@@ -287,7 +288,7 @@ var DecodeTransform;
|
|
|
287
288
|
function TUnion(schema, references, value) {
|
|
288
289
|
const value1 = Default(schema, value);
|
|
289
290
|
for (const subschema of schema.anyOf) {
|
|
290
|
-
if (!
|
|
291
|
+
if (!(0, check_1.Check)(subschema, references, value1))
|
|
291
292
|
continue;
|
|
292
293
|
return Visit(subschema, references, value1);
|
|
293
294
|
}
|
|
@@ -350,9 +351,7 @@ var DecodeTransform;
|
|
|
350
351
|
return Default(schema_, value);
|
|
351
352
|
}
|
|
352
353
|
}
|
|
353
|
-
|
|
354
|
-
function Decode(schema, references, value, check) {
|
|
355
|
-
checkFunction = check;
|
|
354
|
+
function Decode(schema, references, value) {
|
|
356
355
|
return Visit(schema, references, value);
|
|
357
356
|
}
|
|
358
357
|
DecodeTransform.Decode = Decode;
|
|
@@ -441,7 +440,7 @@ var EncodeTransform;
|
|
|
441
440
|
function TUnion(schema, references, value) {
|
|
442
441
|
// test value against union variants
|
|
443
442
|
for (const subschema of schema.anyOf) {
|
|
444
|
-
if (!
|
|
443
|
+
if (!(0, check_1.Check)(subschema, references, value))
|
|
445
444
|
continue;
|
|
446
445
|
const value1 = Visit(subschema, references, value);
|
|
447
446
|
return Default(schema, value1);
|
|
@@ -449,7 +448,7 @@ var EncodeTransform;
|
|
|
449
448
|
// test transformed value against union variants
|
|
450
449
|
for (const subschema of schema.anyOf) {
|
|
451
450
|
const value1 = Visit(subschema, references, value);
|
|
452
|
-
if (!
|
|
451
|
+
if (!(0, check_1.Check)(schema, references, value1))
|
|
453
452
|
continue;
|
|
454
453
|
return Default(schema, value1);
|
|
455
454
|
}
|
|
@@ -511,9 +510,7 @@ var EncodeTransform;
|
|
|
511
510
|
return Default(schema_, value);
|
|
512
511
|
}
|
|
513
512
|
}
|
|
514
|
-
|
|
515
|
-
function Encode(schema, references, value, check) {
|
|
516
|
-
checkFunction = check;
|
|
513
|
+
function Encode(schema, references, value) {
|
|
517
514
|
return Visit(schema, references, value);
|
|
518
515
|
}
|
|
519
516
|
EncodeTransform.Encode = Encode;
|
package/value/value.d.ts
CHANGED
|
@@ -23,13 +23,13 @@ export declare namespace Value {
|
|
|
23
23
|
/** Returns a structural clone of the given value */
|
|
24
24
|
function Clone<T>(value: T): T;
|
|
25
25
|
/** Decodes a value or throws if error */
|
|
26
|
-
function Decode<T extends Types.TSchema,
|
|
26
|
+
function Decode<T extends Types.TSchema, R = Types.StaticDecode<T>>(schema: T, references: Types.TSchema[], value: unknown): R;
|
|
27
27
|
/** Decodes a value or throws if error */
|
|
28
|
-
function Decode<T extends Types.TSchema,
|
|
28
|
+
function Decode<T extends Types.TSchema, R = Types.StaticDecode<T>>(schema: T, value: unknown): R;
|
|
29
29
|
/** Encodes a value or throws if error */
|
|
30
|
-
function Encode<T extends Types.TSchema,
|
|
30
|
+
function Encode<T extends Types.TSchema, R = Types.StaticEncode<T>>(schema: T, references: Types.TSchema[], value: unknown): R;
|
|
31
31
|
/** Encodes a value or throws if error */
|
|
32
|
-
function Encode<T extends Types.TSchema,
|
|
32
|
+
function Encode<T extends Types.TSchema, R = Types.StaticEncode<T>>(schema: T, value: unknown): R;
|
|
33
33
|
/** Returns an iterator for each error in this value. */
|
|
34
34
|
function Errors<T extends Types.TSchema>(schema: T, references: Types.TSchema[], value: unknown): ValueErrors.ValueErrorIterator;
|
|
35
35
|
/** Returns an iterator for each error in this value. */
|
package/value/value.js
CHANGED
|
@@ -72,13 +72,13 @@ var Value;
|
|
|
72
72
|
const [schema, references, value] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], [], args[1]];
|
|
73
73
|
if (!Check(schema, references, value))
|
|
74
74
|
throw new ValueTransform.TransformDecodeCheckError(schema, value, Errors(schema, references, value).First());
|
|
75
|
-
return ValueTransform.DecodeTransform.Decode(schema, references, value
|
|
75
|
+
return ValueTransform.DecodeTransform.Decode(schema, references, value);
|
|
76
76
|
}
|
|
77
77
|
Value.Decode = Decode;
|
|
78
78
|
/** Encodes a value or throws if error */
|
|
79
79
|
function Encode(...args) {
|
|
80
80
|
const [schema, references, value] = args.length === 3 ? [args[0], args[1], args[2]] : [args[0], [], args[1]];
|
|
81
|
-
const encoded = ValueTransform.EncodeTransform.Encode(schema, references, value
|
|
81
|
+
const encoded = ValueTransform.EncodeTransform.Encode(schema, references, value);
|
|
82
82
|
if (!Check(schema, references, encoded))
|
|
83
83
|
throw new ValueTransform.TransformEncodeCheckError(schema, value, Errors(schema, references, value).First());
|
|
84
84
|
return encoded;
|