@sinclair/typebox 0.32.0-dev-22 → 0.32.0-dev-24
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/build/import/compiler/compiler.d.mts +3 -2
- package/build/import/compiler/compiler.mjs +3 -2
- package/build/import/errors/errors.d.mts +2 -1
- package/build/import/errors/errors.mjs +2 -1
- package/build/import/index.d.mts +1 -0
- package/build/import/index.mjs +1 -0
- package/build/import/system/system.d.mts +3 -2
- package/build/import/system/system.mjs +3 -2
- package/build/import/type/error/error.d.mts +4 -0
- package/build/import/type/error/error.mjs +6 -0
- package/build/import/type/error/index.d.mts +1 -0
- package/build/import/type/error/index.mjs +1 -0
- package/build/import/type/extends/extends-check.d.mts +2 -1
- package/build/import/type/extends/extends-check.mjs +2 -1
- package/build/import/type/guard/type.d.mts +2 -1
- package/build/import/type/guard/type.mjs +2 -1
- package/build/import/type/index.d.mts +1 -0
- package/build/import/type/index.mjs +1 -0
- package/build/import/type/record/record.d.mts +1 -1
- package/build/import/type/template-literal/finite.d.mts +2 -1
- package/build/import/type/template-literal/finite.mjs +2 -1
- package/build/import/type/template-literal/generate.d.mts +3 -2
- package/build/import/type/template-literal/generate.mjs +2 -1
- package/build/import/type/template-literal/parse.d.mts +2 -1
- package/build/import/type/template-literal/parse.mjs +2 -1
- package/build/import/type/template-literal/pattern.d.mts +2 -1
- package/build/import/type/template-literal/pattern.mjs +2 -1
- package/build/import/value/cast/cast.d.mts +5 -4
- package/build/import/value/cast/cast.mjs +5 -4
- package/build/import/value/check/check.d.mts +2 -1
- package/build/import/value/check/check.mjs +2 -1
- package/build/import/value/convert/convert.d.mts +2 -1
- package/build/import/value/convert/convert.mjs +2 -1
- package/build/import/value/create/create.d.mts +7 -6
- package/build/import/value/create/create.mjs +7 -6
- package/build/import/value/delta/delta.d.mts +3 -2
- package/build/import/value/delta/delta.mjs +3 -2
- package/build/import/value/deref/deref.d.mts +2 -1
- package/build/import/value/deref/deref.mjs +2 -1
- package/build/import/value/hash/hash.d.mts +2 -1
- package/build/import/value/hash/hash.mjs +2 -1
- package/build/import/value/mutate/mutate.d.mts +3 -2
- package/build/import/value/mutate/mutate.mjs +3 -2
- package/build/import/value/pointer/pointer.d.mts +3 -2
- package/build/import/value/pointer/pointer.mjs +3 -2
- package/build/import/value/transform/decode.d.mts +3 -2
- package/build/import/value/transform/decode.mjs +3 -2
- package/build/import/value/transform/encode.d.mts +3 -2
- package/build/import/value/transform/encode.mjs +3 -2
- package/build/require/compiler/compiler.d.ts +3 -2
- package/build/require/compiler/compiler.js +61 -60
- package/build/require/errors/errors.d.ts +2 -1
- package/build/require/errors/errors.js +34 -33
- package/build/require/index.d.ts +1 -0
- package/build/require/index.js +154 -152
- package/build/require/system/system.d.ts +3 -2
- package/build/require/system/system.js +3 -2
- package/build/require/type/error/error.d.ts +4 -0
- package/build/require/type/error/error.js +11 -0
- package/build/require/type/error/index.d.ts +1 -0
- package/build/require/type/error/index.js +18 -0
- package/build/require/type/extends/extends-check.d.ts +2 -1
- package/build/require/type/extends/extends-check.js +172 -171
- package/build/require/type/guard/type.d.ts +2 -1
- package/build/require/type/guard/type.js +2 -1
- package/build/require/type/index.d.ts +1 -0
- package/build/require/type/index.js +1 -0
- package/build/require/type/record/record.d.ts +1 -1
- package/build/require/type/template-literal/finite.d.ts +2 -1
- package/build/require/type/template-literal/finite.js +2 -1
- package/build/require/type/template-literal/generate.d.ts +3 -2
- package/build/require/type/template-literal/generate.js +2 -1
- package/build/require/type/template-literal/parse.d.ts +2 -1
- package/build/require/type/template-literal/parse.js +3 -2
- package/build/require/type/template-literal/pattern.d.ts +2 -1
- package/build/require/type/template-literal/pattern.js +2 -1
- package/build/require/value/cast/cast.d.ts +5 -4
- package/build/require/value/cast/cast.js +36 -35
- package/build/require/value/check/check.d.ts +2 -1
- package/build/require/value/check/check.js +24 -23
- package/build/require/value/convert/convert.d.ts +2 -1
- package/build/require/value/convert/convert.js +32 -31
- package/build/require/value/create/create.d.ts +7 -6
- package/build/require/value/create/create.js +7 -6
- package/build/require/value/delta/delta.d.ts +3 -2
- package/build/require/value/delta/delta.js +20 -19
- package/build/require/value/deref/deref.d.ts +2 -1
- package/build/require/value/deref/deref.js +2 -1
- package/build/require/value/hash/hash.d.ts +2 -1
- package/build/require/value/hash/hash.js +2 -1
- package/build/require/value/mutate/mutate.d.ts +3 -2
- package/build/require/value/mutate/mutate.js +3 -2
- package/build/require/value/pointer/pointer.d.ts +3 -2
- package/build/require/value/pointer/pointer.js +3 -2
- package/build/require/value/transform/decode.d.ts +3 -2
- package/build/require/value/transform/decode.js +19 -18
- package/build/require/value/transform/encode.d.ts +3 -2
- package/build/require/value/transform/encode.js +20 -19
- package/package.json +1 -1
- package/readme.md +117 -137
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Kind, Hint, TransformKind } from '../symbols/index';
|
|
2
|
+
import { TypeBoxError } from '../error/index';
|
|
2
3
|
import { TransformOptions } from '../transform/index';
|
|
3
4
|
import { TTemplateLiteral, TTemplateLiteralKind } from '../template-literal/index';
|
|
4
5
|
import { TArray } from '../array/index';
|
|
@@ -35,7 +36,7 @@ import type { TUnsafe } from '../unsafe/index';
|
|
|
35
36
|
import type { TVoid } from '../void/index';
|
|
36
37
|
import type { TDate } from '../date/index';
|
|
37
38
|
import type { TThis } from '../recursive/index';
|
|
38
|
-
export declare class TypeGuardUnknownTypeError extends
|
|
39
|
+
export declare class TypeGuardUnknownTypeError extends TypeBoxError {
|
|
39
40
|
}
|
|
40
41
|
/** Returns true if this value has a Readonly symbol */
|
|
41
42
|
export declare function IsReadonly<T extends TSchema>(value: T): value is TReadonly<T>;
|
|
@@ -4,7 +4,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
exports.IsSchema = exports.IsKind = exports.IsVoid = exports.IsUnsafe = exports.IsUnknown = exports.IsUint8Array = exports.IsUnion = exports.IsUnionLiteral = exports.IsUndefined = exports.IsTuple = exports.IsTransform = exports.IsThis = exports.IsTemplateLiteral = exports.IsSymbol = exports.IsString = exports.IsRef = exports.IsRecursive = exports.IsRecord = exports.IsPromise = exports.IsObject = exports.IsNumber = exports.IsNull = exports.IsNot = exports.IsNever = exports.IsMappedResult = exports.IsMappedKey = exports.IsLiteralValue = exports.IsLiteral = exports.IsLiteralBoolean = exports.IsLiteralNumber = exports.IsLiteralString = exports.IsKindOf = exports.IsIterator = exports.IsIntersect = exports.IsProperties = exports.IsInteger = exports.IsFunction = exports.IsDate = exports.IsConstructor = exports.IsBoolean = exports.IsBigInt = exports.IsAsyncIterator = exports.IsArray = exports.IsAny = exports.IsOptional = exports.IsReadonly = exports.TypeGuardUnknownTypeError = void 0;
|
|
5
5
|
const ValueGuard = require("./value");
|
|
6
6
|
const index_1 = require("../symbols/index");
|
|
7
|
-
|
|
7
|
+
const index_2 = require("../error/index");
|
|
8
|
+
class TypeGuardUnknownTypeError extends index_2.TypeBoxError {
|
|
8
9
|
}
|
|
9
10
|
exports.TypeGuardUnknownTypeError = TypeGuardUnknownTypeError;
|
|
10
11
|
const KnownTypes = [
|
|
@@ -13,6 +13,7 @@ export * from './date/index';
|
|
|
13
13
|
export * from './deref/index';
|
|
14
14
|
export * from './discard/index';
|
|
15
15
|
export * from './enum/index';
|
|
16
|
+
export * from './error/index';
|
|
16
17
|
export * from './exclude/index';
|
|
17
18
|
export * from './extends/index';
|
|
18
19
|
export * from './extract/index';
|
|
@@ -30,6 +30,7 @@ __exportStar(require("./date/index"), exports);
|
|
|
30
30
|
__exportStar(require("./deref/index"), exports);
|
|
31
31
|
__exportStar(require("./discard/index"), exports);
|
|
32
32
|
__exportStar(require("./enum/index"), exports);
|
|
33
|
+
__exportStar(require("./error/index"), exports);
|
|
33
34
|
__exportStar(require("./exclude/index"), exports);
|
|
34
35
|
__exportStar(require("./extends/index"), exports);
|
|
35
36
|
__exportStar(require("./extract/index"), exports);
|
|
@@ -26,7 +26,7 @@ type TFromUnionKeyLiteralNumber<K extends TLiteral<number>, T extends TSchema> =
|
|
|
26
26
|
[_ in K['const']]: T;
|
|
27
27
|
};
|
|
28
28
|
type TFromUnionKeyRest<K extends TSchema[], T extends TSchema> = K extends [infer L extends TSchema, ...infer R extends TSchema[]] ? (L extends TUnion<infer S> ? TFromUnionKeyRest<S, T> & TFromUnionKeyRest<R, T> : L extends TLiteral<string> ? TFromUnionKeyLiteralString<L, T> & TFromUnionKeyRest<R, T> : L extends TLiteral<number> ? TFromUnionKeyLiteralNumber<L, T> & TFromUnionKeyRest<R, T> : {}) : {};
|
|
29
|
-
type TFromUnionKey<K extends TSchema[], T extends TSchema, P extends TProperties = TFromUnionKeyRest<K, T>> = (Ensure<TObject<P
|
|
29
|
+
type TFromUnionKey<K extends TSchema[], T extends TSchema, P extends TProperties = TFromUnionKeyRest<K, T>> = (Ensure<TObject<Evaluate<P>>>);
|
|
30
30
|
type TFromLiteralKey<K extends TLiteralValue, T extends TSchema> = (Ensure<TObject<{
|
|
31
31
|
[_ in Assert<K, PropertyKey>]: T;
|
|
32
32
|
}>>);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { TypeBoxError } from '../error/index';
|
|
1
2
|
import type { TTemplateLiteral, TTemplateLiteralKind } from './index';
|
|
2
3
|
import type { TUnion } from '../union/index';
|
|
3
4
|
import type { TString } from '../string/index';
|
|
@@ -7,7 +8,7 @@ import type { TInteger } from '../integer/index';
|
|
|
7
8
|
import type { TBigInt } from '../bigint/index';
|
|
8
9
|
import type { TLiteral } from '../literal/index';
|
|
9
10
|
import type { Expression } from './parse';
|
|
10
|
-
export declare class TemplateLiteralFiniteError extends
|
|
11
|
+
export declare class TemplateLiteralFiniteError extends TypeBoxError {
|
|
11
12
|
}
|
|
12
13
|
type TFromTemplateLiteralKind<T> = T extends TTemplateLiteral<infer U extends TTemplateLiteralKind[]> ? TFromTemplateLiteralKinds<U> : T extends TUnion<infer U extends TTemplateLiteralKind[]> ? TFromTemplateLiteralKinds<U> : T extends TString ? false : T extends TNumber ? false : T extends TInteger ? false : T extends TBigInt ? false : T extends TBoolean ? true : T extends TLiteral ? true : false;
|
|
13
14
|
type TFromTemplateLiteralKinds<T extends TTemplateLiteralKind[]> = T extends [infer L extends TTemplateLiteralKind, ...infer R extends TTemplateLiteralKind[]] ? TFromTemplateLiteralKind<L> extends false ? false : TFromTemplateLiteralKinds<R> : true;
|
|
@@ -3,10 +3,11 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.IsTemplateLiteralFinite = exports.IsTemplateLiteralExpressionFinite = exports.TemplateLiteralFiniteError = void 0;
|
|
5
5
|
const parse_1 = require("./parse");
|
|
6
|
+
const index_1 = require("../error/index");
|
|
6
7
|
// ------------------------------------------------------------------
|
|
7
8
|
// TemplateLiteralFiniteError
|
|
8
9
|
// ------------------------------------------------------------------
|
|
9
|
-
class TemplateLiteralFiniteError extends
|
|
10
|
+
class TemplateLiteralFiniteError extends index_1.TypeBoxError {
|
|
10
11
|
}
|
|
11
12
|
exports.TemplateLiteralFiniteError = TemplateLiteralFiniteError;
|
|
12
13
|
// ------------------------------------------------------------------
|
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import { TIsTemplateLiteralFinite } from './finite';
|
|
2
|
+
import { TypeBoxError } from '../error/index';
|
|
2
3
|
import type { Assert } from '../helpers/index';
|
|
3
4
|
import type { TBoolean } from '../boolean/index';
|
|
4
5
|
import type { TTemplateLiteral, TTemplateLiteralKind } from './index';
|
|
5
6
|
import type { TLiteral, TLiteralValue } from '../literal/index';
|
|
6
7
|
import type { Expression } from './parse';
|
|
7
8
|
import type { TUnion } from '../union/index';
|
|
8
|
-
export declare class TemplateLiteralGenerateError extends
|
|
9
|
+
export declare class TemplateLiteralGenerateError extends TypeBoxError {
|
|
9
10
|
}
|
|
10
11
|
type TStringReduceUnary<L extends string, R extends string[], Acc extends string[] = []> = R extends [infer A extends string, ...infer B extends string[]] ? TStringReduceUnary<L, B, [...Acc, `${L}${A}`]> : Acc;
|
|
11
12
|
type TStringReduceBinary<L extends string[], R extends string[], Acc extends string[] = []> = L extends [infer A extends string, ...infer B extends string[]] ? TStringReduceBinary<B, R, [...Acc, ...TStringReduceUnary<A, R>]> : Acc;
|
|
12
13
|
type TStringReduceMany<T extends string[][]> = T extends [infer L extends string[], infer R extends string[], ...infer Rest extends string[][]] ? TStringReduceMany<[TStringReduceBinary<L, R>, ...Rest]> : T;
|
|
13
14
|
type TStringReduce<T extends string[][], O = TStringReduceMany<T>> = 0 extends keyof O ? Assert<O[0], string[]> : [];
|
|
14
|
-
type TFromTemplateLiteralUnionKinds<T extends TTemplateLiteralKind[]> = T extends [infer L extends TLiteral, ...infer R extends TLiteral[]] ? [L['const']
|
|
15
|
+
type TFromTemplateLiteralUnionKinds<T extends TTemplateLiteralKind[]> = T extends [infer L extends TLiteral, ...infer R extends TLiteral[]] ? [`${L['const']}`, ...TFromTemplateLiteralUnionKinds<R>] : [];
|
|
15
16
|
type TFromTemplateLiteralKinds<T extends TTemplateLiteralKind[], Acc extends TLiteralValue[][] = []> = T extends [infer L extends TTemplateLiteralKind, ...infer R extends TTemplateLiteralKind[]] ? (L extends TLiteral<infer S extends TLiteralValue> ? TFromTemplateLiteralKinds<R, [...Acc, [S]]> : L extends TUnion<infer S extends TTemplateLiteralKind[]> ? TFromTemplateLiteralKinds<R, [...Acc, TFromTemplateLiteralUnionKinds<S>]> : L extends TBoolean ? TFromTemplateLiteralKinds<R, [...Acc, ['true', 'false']]> : Acc) : Acc;
|
|
16
17
|
export declare function TemplateLiteralExpressionGenerate(expression: Expression): IterableIterator<string>;
|
|
17
18
|
export type TTemplateLiteralGenerate<T extends TTemplateLiteral, F = TIsTemplateLiteralFinite<T>> = F extends true ? (T extends TTemplateLiteral<infer S extends TTemplateLiteralKind[]> ? TFromTemplateLiteralKinds<S> extends infer R extends string[][] ? TStringReduce<R> : [] : []) : [];
|
|
@@ -4,10 +4,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
exports.TemplateLiteralGenerate = exports.TemplateLiteralExpressionGenerate = exports.TemplateLiteralGenerateError = void 0;
|
|
5
5
|
const finite_1 = require("./finite");
|
|
6
6
|
const parse_1 = require("./parse");
|
|
7
|
+
const index_1 = require("../error/index");
|
|
7
8
|
// ------------------------------------------------------------------
|
|
8
9
|
// TemplateLiteralGenerateError
|
|
9
10
|
// ------------------------------------------------------------------
|
|
10
|
-
class TemplateLiteralGenerateError extends
|
|
11
|
+
class TemplateLiteralGenerateError extends index_1.TypeBoxError {
|
|
11
12
|
}
|
|
12
13
|
exports.TemplateLiteralGenerateError = TemplateLiteralGenerateError;
|
|
13
14
|
// ------------------------------------------------------------------
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import { TypeBoxError } from '../error/index';
|
|
2
|
+
export declare class TemplateLiteralParserError extends TypeBoxError {
|
|
2
3
|
}
|
|
3
4
|
export type Expression = ExpressionAnd | ExpressionOr | ExpressionConst;
|
|
4
5
|
export type ExpressionConst = {
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
3
|
exports.TemplateLiteralParseExact = exports.TemplateLiteralParse = exports.TemplateLiteralParserError = void 0;
|
|
4
|
+
|
|
5
|
+
const index_1 = require("../error/index");
|
|
5
6
|
// ------------------------------------------------------------------
|
|
6
7
|
// TemplateLiteralParserError
|
|
7
8
|
// ------------------------------------------------------------------
|
|
8
|
-
class TemplateLiteralParserError extends
|
|
9
|
+
class TemplateLiteralParserError extends index_1.TypeBoxError {
|
|
9
10
|
}
|
|
10
11
|
exports.TemplateLiteralParserError = TemplateLiteralParserError;
|
|
11
12
|
// prettier-ignore
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { TTemplateLiteralKind } from './index';
|
|
2
|
-
|
|
2
|
+
import { TypeBoxError } from '../error/index';
|
|
3
|
+
export declare class TemplateLiteralPatternError extends TypeBoxError {
|
|
3
4
|
}
|
|
4
5
|
export declare function TemplateLiteralPattern(kinds: TTemplateLiteralKind[]): string;
|
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
exports.TemplateLiteralPattern = exports.TemplateLiteralPatternError = void 0;
|
|
5
5
|
const index_1 = require("../patterns/index");
|
|
6
6
|
const index_2 = require("../symbols/index");
|
|
7
|
+
const index_3 = require("../error/index");
|
|
7
8
|
// ------------------------------------------------------------------
|
|
8
9
|
// TypeGuard
|
|
9
10
|
// ------------------------------------------------------------------
|
|
@@ -12,7 +13,7 @@ const type_1 = require("../guard/type");
|
|
|
12
13
|
// ------------------------------------------------------------------
|
|
13
14
|
// TemplateLiteralPatternError
|
|
14
15
|
// ------------------------------------------------------------------
|
|
15
|
-
class TemplateLiteralPatternError extends
|
|
16
|
+
class TemplateLiteralPatternError extends index_3.TypeBoxError {
|
|
16
17
|
}
|
|
17
18
|
exports.TemplateLiteralPatternError = TemplateLiteralPatternError;
|
|
18
19
|
// ------------------------------------------------------------------
|
|
@@ -1,19 +1,20 @@
|
|
|
1
|
+
import { TypeBoxError } from '../../type/error/index';
|
|
1
2
|
import type { TSchema } from '../../type/schema/index';
|
|
2
3
|
import type { Static } from '../../type/static/index';
|
|
3
|
-
export declare class ValueCastArrayUniqueItemsTypeError extends
|
|
4
|
+
export declare class ValueCastArrayUniqueItemsTypeError extends TypeBoxError {
|
|
4
5
|
readonly schema: TSchema;
|
|
5
6
|
readonly value: unknown;
|
|
6
7
|
constructor(schema: TSchema, value: unknown);
|
|
7
8
|
}
|
|
8
|
-
export declare class ValueCastNeverTypeError extends
|
|
9
|
+
export declare class ValueCastNeverTypeError extends TypeBoxError {
|
|
9
10
|
readonly schema: TSchema;
|
|
10
11
|
constructor(schema: TSchema);
|
|
11
12
|
}
|
|
12
|
-
export declare class ValueCastRecursiveTypeError extends
|
|
13
|
+
export declare class ValueCastRecursiveTypeError extends TypeBoxError {
|
|
13
14
|
readonly schema: TSchema;
|
|
14
15
|
constructor(schema: TSchema);
|
|
15
16
|
}
|
|
16
|
-
export declare class ValueCastUnknownTypeError extends
|
|
17
|
+
export declare class ValueCastUnknownTypeError extends TypeBoxError {
|
|
17
18
|
readonly schema: TSchema;
|
|
18
19
|
constructor(schema: TSchema);
|
|
19
20
|
}
|
|
@@ -3,15 +3,16 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.Cast = exports.ValueCastUnknownTypeError = exports.ValueCastRecursiveTypeError = exports.ValueCastNeverTypeError = exports.ValueCastArrayUniqueItemsTypeError = void 0;
|
|
5
5
|
const index_1 = require("../guard/index");
|
|
6
|
-
const index_2 = require("../../type/
|
|
7
|
-
const index_3 = require("
|
|
8
|
-
const index_4 = require("../
|
|
9
|
-
const index_5 = require("../
|
|
10
|
-
const index_6 = require("../
|
|
6
|
+
const index_2 = require("../../type/error/index");
|
|
7
|
+
const index_3 = require("../../type/symbols/index");
|
|
8
|
+
const index_4 = require("../create/index");
|
|
9
|
+
const index_5 = require("../check/index");
|
|
10
|
+
const index_6 = require("../clone/index");
|
|
11
|
+
const index_7 = require("../deref/index");
|
|
11
12
|
// ------------------------------------------------------------------
|
|
12
13
|
// Errors
|
|
13
14
|
// ------------------------------------------------------------------
|
|
14
|
-
class ValueCastArrayUniqueItemsTypeError extends
|
|
15
|
+
class ValueCastArrayUniqueItemsTypeError extends index_2.TypeBoxError {
|
|
15
16
|
constructor(schema, value) {
|
|
16
17
|
super('Array cast produced invalid data due to uniqueItems constraint');
|
|
17
18
|
this.schema = schema;
|
|
@@ -19,21 +20,21 @@ class ValueCastArrayUniqueItemsTypeError extends Error {
|
|
|
19
20
|
}
|
|
20
21
|
}
|
|
21
22
|
exports.ValueCastArrayUniqueItemsTypeError = ValueCastArrayUniqueItemsTypeError;
|
|
22
|
-
class ValueCastNeverTypeError extends
|
|
23
|
+
class ValueCastNeverTypeError extends index_2.TypeBoxError {
|
|
23
24
|
constructor(schema) {
|
|
24
25
|
super('Never types cannot be cast');
|
|
25
26
|
this.schema = schema;
|
|
26
27
|
}
|
|
27
28
|
}
|
|
28
29
|
exports.ValueCastNeverTypeError = ValueCastNeverTypeError;
|
|
29
|
-
class ValueCastRecursiveTypeError extends
|
|
30
|
+
class ValueCastRecursiveTypeError extends index_2.TypeBoxError {
|
|
30
31
|
constructor(schema) {
|
|
31
32
|
super('Cannot cast recursive schemas');
|
|
32
33
|
this.schema = schema;
|
|
33
34
|
}
|
|
34
35
|
}
|
|
35
36
|
exports.ValueCastRecursiveTypeError = ValueCastRecursiveTypeError;
|
|
36
|
-
class ValueCastUnknownTypeError extends
|
|
37
|
+
class ValueCastUnknownTypeError extends index_2.TypeBoxError {
|
|
37
38
|
constructor(schema) {
|
|
38
39
|
super('Unknown type');
|
|
39
40
|
this.schema = schema;
|
|
@@ -49,20 +50,20 @@ exports.ValueCastUnknownTypeError = ValueCastUnknownTypeError;
|
|
|
49
50
|
// used as union discriminator fields.
|
|
50
51
|
// ------------------------------------------------------------------
|
|
51
52
|
function ScoreUnion(schema, references, value) {
|
|
52
|
-
if (schema[
|
|
53
|
+
if (schema[index_3.Kind] === 'Object' && typeof value === 'object' && !(0, index_1.IsNull)(value)) {
|
|
53
54
|
const object = schema;
|
|
54
55
|
const keys = Object.getOwnPropertyNames(value);
|
|
55
56
|
const entries = Object.entries(object.properties);
|
|
56
57
|
const [point, max] = [1 / entries.length, entries.length];
|
|
57
58
|
return entries.reduce((acc, [key, schema]) => {
|
|
58
|
-
const literal = schema[
|
|
59
|
-
const checks = (0,
|
|
59
|
+
const literal = schema[index_3.Kind] === 'Literal' && schema.const === value[key] ? max : 0;
|
|
60
|
+
const checks = (0, index_5.Check)(schema, references, value[key]) ? point : 0;
|
|
60
61
|
const exists = keys.includes(key) ? point : 0;
|
|
61
62
|
return acc + (literal + checks + exists);
|
|
62
63
|
}, 0);
|
|
63
64
|
}
|
|
64
65
|
else {
|
|
65
|
-
return (0,
|
|
66
|
+
return (0, index_5.Check)(schema, references, value) ? 1 : 0;
|
|
66
67
|
}
|
|
67
68
|
}
|
|
68
69
|
function SelectUnion(union, references, value) {
|
|
@@ -89,31 +90,31 @@ function CastUnion(union, references, value) {
|
|
|
89
90
|
// Default
|
|
90
91
|
// ------------------------------------------------------------------
|
|
91
92
|
function DefaultClone(schema, references, value) {
|
|
92
|
-
return (0,
|
|
93
|
+
return (0, index_5.Check)(schema, references, value) ? (0, index_6.Clone)(value) : (0, index_4.Create)(schema, references);
|
|
93
94
|
}
|
|
94
95
|
function Default(schema, references, value) {
|
|
95
|
-
return (0,
|
|
96
|
+
return (0, index_5.Check)(schema, references, value) ? value : (0, index_4.Create)(schema, references);
|
|
96
97
|
}
|
|
97
98
|
// ------------------------------------------------------------------
|
|
98
99
|
// Cast
|
|
99
100
|
// ------------------------------------------------------------------
|
|
100
101
|
function FromArray(schema, references, value) {
|
|
101
|
-
if ((0,
|
|
102
|
-
return (0,
|
|
103
|
-
const created = (0, index_1.IsArray)(value) ? (0,
|
|
102
|
+
if ((0, index_5.Check)(schema, references, value))
|
|
103
|
+
return (0, index_6.Clone)(value);
|
|
104
|
+
const created = (0, index_1.IsArray)(value) ? (0, index_6.Clone)(value) : (0, index_4.Create)(schema, references);
|
|
104
105
|
const minimum = (0, index_1.IsNumber)(schema.minItems) && created.length < schema.minItems ? [...created, ...Array.from({ length: schema.minItems - created.length }, () => null)] : created;
|
|
105
106
|
const maximum = (0, index_1.IsNumber)(schema.maxItems) && minimum.length > schema.maxItems ? minimum.slice(0, schema.maxItems) : minimum;
|
|
106
107
|
const casted = maximum.map((value) => Visit(schema.items, references, value));
|
|
107
108
|
if (schema.uniqueItems !== true)
|
|
108
109
|
return casted;
|
|
109
110
|
const unique = [...new Set(casted)];
|
|
110
|
-
if (!(0,
|
|
111
|
+
if (!(0, index_5.Check)(schema, references, unique))
|
|
111
112
|
throw new ValueCastArrayUniqueItemsTypeError(schema, unique);
|
|
112
113
|
return unique;
|
|
113
114
|
}
|
|
114
115
|
function FromConstructor(schema, references, value) {
|
|
115
|
-
if ((0,
|
|
116
|
-
return (0,
|
|
116
|
+
if ((0, index_5.Check)(schema, references, value))
|
|
117
|
+
return (0, index_4.Create)(schema, references);
|
|
117
118
|
const required = new Set(schema.returns.required || []);
|
|
118
119
|
const result = function () { };
|
|
119
120
|
for (const [key, property] of Object.entries(schema.returns.properties)) {
|
|
@@ -124,18 +125,18 @@ function FromConstructor(schema, references, value) {
|
|
|
124
125
|
return result;
|
|
125
126
|
}
|
|
126
127
|
function FromIntersect(schema, references, value) {
|
|
127
|
-
const created = (0,
|
|
128
|
+
const created = (0, index_4.Create)(schema, references);
|
|
128
129
|
const mapped = (0, index_1.IsPlainObject)(created) && (0, index_1.IsPlainObject)(value) ? { ...created, ...value } : value;
|
|
129
|
-
return (0,
|
|
130
|
+
return (0, index_5.Check)(schema, references, mapped) ? mapped : (0, index_4.Create)(schema, references);
|
|
130
131
|
}
|
|
131
132
|
function FromNever(schema, references, value) {
|
|
132
133
|
throw new ValueCastNeverTypeError(schema);
|
|
133
134
|
}
|
|
134
135
|
function FromObject(schema, references, value) {
|
|
135
|
-
if ((0,
|
|
136
|
+
if ((0, index_5.Check)(schema, references, value))
|
|
136
137
|
return value;
|
|
137
138
|
if (value === null || typeof value !== 'object')
|
|
138
|
-
return (0,
|
|
139
|
+
return (0, index_4.Create)(schema, references);
|
|
139
140
|
const required = new Set(schema.required || []);
|
|
140
141
|
const result = {};
|
|
141
142
|
for (const [key, property] of Object.entries(schema.properties)) {
|
|
@@ -155,10 +156,10 @@ function FromObject(schema, references, value) {
|
|
|
155
156
|
return result;
|
|
156
157
|
}
|
|
157
158
|
function FromRecord(schema, references, value) {
|
|
158
|
-
if ((0,
|
|
159
|
-
return (0,
|
|
159
|
+
if ((0, index_5.Check)(schema, references, value))
|
|
160
|
+
return (0, index_6.Clone)(value);
|
|
160
161
|
if (value === null || typeof value !== 'object' || Array.isArray(value) || value instanceof Date)
|
|
161
|
-
return (0,
|
|
162
|
+
return (0, index_4.Create)(schema, references);
|
|
162
163
|
const subschemaPropertyName = Object.getOwnPropertyNames(schema.patternProperties)[0];
|
|
163
164
|
const subschema = schema.patternProperties[subschemaPropertyName];
|
|
164
165
|
const result = {};
|
|
@@ -168,27 +169,27 @@ function FromRecord(schema, references, value) {
|
|
|
168
169
|
return result;
|
|
169
170
|
}
|
|
170
171
|
function FromRef(schema, references, value) {
|
|
171
|
-
return Visit((0,
|
|
172
|
+
return Visit((0, index_7.Deref)(schema, references), references, value);
|
|
172
173
|
}
|
|
173
174
|
function FromThis(schema, references, value) {
|
|
174
|
-
return Visit((0,
|
|
175
|
+
return Visit((0, index_7.Deref)(schema, references), references, value);
|
|
175
176
|
}
|
|
176
177
|
function FromTuple(schema, references, value) {
|
|
177
|
-
if ((0,
|
|
178
|
-
return (0,
|
|
178
|
+
if ((0, index_5.Check)(schema, references, value))
|
|
179
|
+
return (0, index_6.Clone)(value);
|
|
179
180
|
if (!(0, index_1.IsArray)(value))
|
|
180
|
-
return (0,
|
|
181
|
+
return (0, index_4.Create)(schema, references);
|
|
181
182
|
if (schema.items === undefined)
|
|
182
183
|
return [];
|
|
183
184
|
return schema.items.map((schema, index) => Visit(schema, references, value[index]));
|
|
184
185
|
}
|
|
185
186
|
function FromUnion(schema, references, value) {
|
|
186
|
-
return (0,
|
|
187
|
+
return (0, index_5.Check)(schema, references, value) ? (0, index_6.Clone)(value) : CastUnion(schema, references, value);
|
|
187
188
|
}
|
|
188
189
|
function Visit(schema, references, value) {
|
|
189
190
|
const references_ = (0, index_1.IsString)(schema.$id) ? [...references, schema] : references;
|
|
190
191
|
const schema_ = schema;
|
|
191
|
-
switch (schema[
|
|
192
|
+
switch (schema[index_3.Kind]) {
|
|
192
193
|
// --------------------------------------------------------------
|
|
193
194
|
// Structural
|
|
194
195
|
// --------------------------------------------------------------
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { TypeBoxError } from '../../type/error/index';
|
|
1
2
|
import type { TSchema } from '../../type/schema/index';
|
|
2
3
|
import type { Static } from '../../type/static/index';
|
|
3
|
-
export declare class ValueCheckUnknownTypeError extends
|
|
4
|
+
export declare class ValueCheckUnknownTypeError extends TypeBoxError {
|
|
4
5
|
readonly schema: TSchema;
|
|
5
6
|
constructor(schema: TSchema);
|
|
6
7
|
}
|
|
@@ -9,12 +9,13 @@ const index_4 = require("../../type/symbols/index");
|
|
|
9
9
|
const index_5 = require("../../type/keyof/index");
|
|
10
10
|
const index_6 = require("../../type/extends/index");
|
|
11
11
|
const index_7 = require("../../type/registry/index");
|
|
12
|
-
const index_8 = require("../../type/
|
|
12
|
+
const index_8 = require("../../type/error/index");
|
|
13
|
+
const index_9 = require("../../type/never/index");
|
|
13
14
|
// ------------------------------------------------------------------
|
|
14
15
|
// ValueGuard
|
|
15
16
|
// ------------------------------------------------------------------
|
|
16
17
|
// prettier-ignore
|
|
17
|
-
const
|
|
18
|
+
const index_10 = require("../guard/index");
|
|
18
19
|
// ------------------------------------------------------------------
|
|
19
20
|
// TypeGuard
|
|
20
21
|
// ------------------------------------------------------------------
|
|
@@ -22,7 +23,7 @@ const type_1 = require("../../type/guard/type");
|
|
|
22
23
|
// ------------------------------------------------------------------
|
|
23
24
|
// Errors
|
|
24
25
|
// ------------------------------------------------------------------
|
|
25
|
-
class ValueCheckUnknownTypeError extends
|
|
26
|
+
class ValueCheckUnknownTypeError extends index_8.TypeBoxError {
|
|
26
27
|
constructor(schema) {
|
|
27
28
|
super(`Unknown type`);
|
|
28
29
|
this.schema = schema;
|
|
@@ -48,7 +49,7 @@ function FromAny(schema, references, value) {
|
|
|
48
49
|
return true;
|
|
49
50
|
}
|
|
50
51
|
function FromArray(schema, references, value) {
|
|
51
|
-
if (!(0,
|
|
52
|
+
if (!(0, index_10.IsArray)(value))
|
|
52
53
|
return false;
|
|
53
54
|
if (IsDefined(schema.minItems) && !(value.length >= schema.minItems)) {
|
|
54
55
|
return false;
|
|
@@ -72,27 +73,27 @@ function FromArray(schema, references, value) {
|
|
|
72
73
|
return false;
|
|
73
74
|
}
|
|
74
75
|
// contains
|
|
75
|
-
if (!(IsDefined(schema.contains) || (0,
|
|
76
|
+
if (!(IsDefined(schema.contains) || (0, index_10.IsNumber)(schema.minContains) || (0, index_10.IsNumber)(schema.maxContains))) {
|
|
76
77
|
return true; // exit
|
|
77
78
|
}
|
|
78
|
-
const containsSchema = IsDefined(schema.contains) ? schema.contains : (0,
|
|
79
|
+
const containsSchema = IsDefined(schema.contains) ? schema.contains : (0, index_9.Never)();
|
|
79
80
|
const containsCount = value.reduce((acc, value) => (Visit(containsSchema, references, value) ? acc + 1 : acc), 0);
|
|
80
81
|
if (containsCount === 0) {
|
|
81
82
|
return false;
|
|
82
83
|
}
|
|
83
|
-
if ((0,
|
|
84
|
+
if ((0, index_10.IsNumber)(schema.minContains) && containsCount < schema.minContains) {
|
|
84
85
|
return false;
|
|
85
86
|
}
|
|
86
|
-
if ((0,
|
|
87
|
+
if ((0, index_10.IsNumber)(schema.maxContains) && containsCount > schema.maxContains) {
|
|
87
88
|
return false;
|
|
88
89
|
}
|
|
89
90
|
return true;
|
|
90
91
|
}
|
|
91
92
|
function FromAsyncIterator(schema, references, value) {
|
|
92
|
-
return (0,
|
|
93
|
+
return (0, index_10.IsAsyncIterator)(value);
|
|
93
94
|
}
|
|
94
95
|
function FromBigInt(schema, references, value) {
|
|
95
|
-
if (!(0,
|
|
96
|
+
if (!(0, index_10.IsBigInt)(value))
|
|
96
97
|
return false;
|
|
97
98
|
if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
98
99
|
return false;
|
|
@@ -112,13 +113,13 @@ function FromBigInt(schema, references, value) {
|
|
|
112
113
|
return true;
|
|
113
114
|
}
|
|
114
115
|
function FromBoolean(schema, references, value) {
|
|
115
|
-
return (0,
|
|
116
|
+
return (0, index_10.IsBoolean)(value);
|
|
116
117
|
}
|
|
117
118
|
function FromConstructor(schema, references, value) {
|
|
118
119
|
return Visit(schema.returns, references, value.prototype);
|
|
119
120
|
}
|
|
120
121
|
function FromDate(schema, references, value) {
|
|
121
|
-
if (!(0,
|
|
122
|
+
if (!(0, index_10.IsDate)(value))
|
|
122
123
|
return false;
|
|
123
124
|
if (IsDefined(schema.exclusiveMaximumTimestamp) && !(value.getTime() < schema.exclusiveMaximumTimestamp)) {
|
|
124
125
|
return false;
|
|
@@ -138,10 +139,10 @@ function FromDate(schema, references, value) {
|
|
|
138
139
|
return true;
|
|
139
140
|
}
|
|
140
141
|
function FromFunction(schema, references, value) {
|
|
141
|
-
return (0,
|
|
142
|
+
return (0, index_10.IsFunction)(value);
|
|
142
143
|
}
|
|
143
144
|
function FromInteger(schema, references, value) {
|
|
144
|
-
if (!(0,
|
|
145
|
+
if (!(0, index_10.IsInteger)(value)) {
|
|
145
146
|
return false;
|
|
146
147
|
}
|
|
147
148
|
if (IsDefined(schema.exclusiveMaximum) && !(value < schema.exclusiveMaximum)) {
|
|
@@ -178,7 +179,7 @@ function FromIntersect(schema, references, value) {
|
|
|
178
179
|
}
|
|
179
180
|
}
|
|
180
181
|
function FromIterator(schema, references, value) {
|
|
181
|
-
return (0,
|
|
182
|
+
return (0, index_10.IsIterator)(value);
|
|
182
183
|
}
|
|
183
184
|
function FromLiteral(schema, references, value) {
|
|
184
185
|
return value === schema.const;
|
|
@@ -190,7 +191,7 @@ function FromNot(schema, references, value) {
|
|
|
190
191
|
return !Visit(schema.not, references, value);
|
|
191
192
|
}
|
|
192
193
|
function FromNull(schema, references, value) {
|
|
193
|
-
return (0,
|
|
194
|
+
return (0, index_10.IsNull)(value);
|
|
194
195
|
}
|
|
195
196
|
function FromNumber(schema, references, value) {
|
|
196
197
|
if (!index_1.TypeSystemPolicy.IsNumberLike(value))
|
|
@@ -257,7 +258,7 @@ function FromObject(schema, references, value) {
|
|
|
257
258
|
}
|
|
258
259
|
}
|
|
259
260
|
function FromPromise(schema, references, value) {
|
|
260
|
-
return (0,
|
|
261
|
+
return (0, index_10.IsPromise)(value);
|
|
261
262
|
}
|
|
262
263
|
function FromRecord(schema, references, value) {
|
|
263
264
|
if (!index_1.TypeSystemPolicy.IsRecordLike(value)) {
|
|
@@ -290,7 +291,7 @@ function FromRef(schema, references, value) {
|
|
|
290
291
|
return Visit((0, index_2.Deref)(schema, references), references, value);
|
|
291
292
|
}
|
|
292
293
|
function FromString(schema, references, value) {
|
|
293
|
-
if (!(0,
|
|
294
|
+
if (!(0, index_10.IsString)(value)) {
|
|
294
295
|
return false;
|
|
295
296
|
}
|
|
296
297
|
if (IsDefined(schema.minLength)) {
|
|
@@ -315,16 +316,16 @@ function FromString(schema, references, value) {
|
|
|
315
316
|
return true;
|
|
316
317
|
}
|
|
317
318
|
function FromSymbol(schema, references, value) {
|
|
318
|
-
return (0,
|
|
319
|
+
return (0, index_10.IsSymbol)(value);
|
|
319
320
|
}
|
|
320
321
|
function FromTemplateLiteral(schema, references, value) {
|
|
321
|
-
return (0,
|
|
322
|
+
return (0, index_10.IsString)(value) && new RegExp(schema.pattern).test(value);
|
|
322
323
|
}
|
|
323
324
|
function FromThis(schema, references, value) {
|
|
324
325
|
return Visit((0, index_2.Deref)(schema, references), references, value);
|
|
325
326
|
}
|
|
326
327
|
function FromTuple(schema, references, value) {
|
|
327
|
-
if (!(0,
|
|
328
|
+
if (!(0, index_10.IsArray)(value)) {
|
|
328
329
|
return false;
|
|
329
330
|
}
|
|
330
331
|
if (schema.items === undefined && !(value.length === 0)) {
|
|
@@ -343,13 +344,13 @@ function FromTuple(schema, references, value) {
|
|
|
343
344
|
return true;
|
|
344
345
|
}
|
|
345
346
|
function FromUndefined(schema, references, value) {
|
|
346
|
-
return (0,
|
|
347
|
+
return (0, index_10.IsUndefined)(value);
|
|
347
348
|
}
|
|
348
349
|
function FromUnion(schema, references, value) {
|
|
349
350
|
return schema.anyOf.some((inner) => Visit(inner, references, value));
|
|
350
351
|
}
|
|
351
352
|
function FromUint8Array(schema, references, value) {
|
|
352
|
-
if (!(0,
|
|
353
|
+
if (!(0, index_10.IsUint8Array)(value)) {
|
|
353
354
|
return false;
|
|
354
355
|
}
|
|
355
356
|
if (IsDefined(schema.maxByteLength) && !(value.length <= schema.maxByteLength)) {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { TypeBoxError } from '../../type/error/index';
|
|
1
2
|
import type { TSchema } from '../../type/schema/index';
|
|
2
|
-
export declare class ValueConvertUnknownTypeError extends
|
|
3
|
+
export declare class ValueConvertUnknownTypeError extends TypeBoxError {
|
|
3
4
|
readonly schema: TSchema;
|
|
4
5
|
constructor(schema: TSchema);
|
|
5
6
|
}
|