@sinclair/typebox 0.32.0-dev-27 → 0.32.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/build/import/compiler/compiler.mjs +7 -0
- package/build/import/errors/errors.d.mts +16 -15
- package/build/import/errors/errors.mjs +24 -15
- package/build/import/errors/function.mjs +2 -0
- package/build/import/type/clone/value.mjs +6 -2
- package/build/import/type/extends/extends-check.mjs +137 -125
- package/build/import/type/guard/type.d.mts +3 -0
- package/build/import/type/guard/type.mjs +10 -0
- package/build/import/type/guard/value.d.mts +2 -0
- package/build/import/type/guard/value.mjs +4 -0
- package/build/import/type/indexed/indexed-property-keys.d.mts +1 -1
- package/build/import/type/record/record.d.mts +3 -1
- package/build/import/type/record/record.mjs +10 -5
- package/build/import/type/regexp/regexp.d.mts +11 -4
- package/build/import/type/regexp/regexp.mjs +3 -3
- package/build/import/type/schema/anyschema.d.mts +2 -1
- package/build/import/type/type/javascript.d.mts +2 -2
- package/build/import/type/type/javascript.mjs +1 -1
- package/build/import/value/cast/cast.d.mts +2 -15
- package/build/import/value/cast/cast.mjs +5 -28
- package/build/import/value/check/check.mjs +6 -1
- package/build/import/value/convert/convert.d.mts +0 -5
- package/build/import/value/convert/convert.mjs +0 -12
- package/build/import/value/create/create.d.mts +2 -23
- package/build/import/value/create/create.mjs +30 -53
- package/build/import/value/default/default.mjs +0 -2
- package/build/import/value/delta/delta.d.mts +4 -4
- package/build/import/value/delta/delta.mjs +12 -12
- package/build/import/value/index.d.mts +7 -7
- package/build/import/value/index.mjs +6 -6
- package/build/import/value/mutate/mutate.d.mts +2 -5
- package/build/import/value/mutate/mutate.mjs +5 -10
- package/build/require/compiler/compiler.js +7 -0
- package/build/require/errors/errors.d.ts +16 -15
- package/build/require/errors/errors.js +24 -15
- package/build/require/errors/function.js +2 -0
- package/build/require/type/clone/value.js +6 -2
- package/build/require/type/extends/extends-check.js +137 -125
- package/build/require/type/guard/type.d.ts +3 -0
- package/build/require/type/guard/type.js +12 -1
- package/build/require/type/guard/value.d.ts +2 -0
- package/build/require/type/guard/value.js +6 -1
- package/build/require/type/indexed/indexed-property-keys.d.ts +1 -1
- package/build/require/type/record/record.d.ts +3 -1
- package/build/require/type/record/record.js +9 -4
- package/build/require/type/regexp/regexp.d.ts +11 -4
- package/build/require/type/regexp/regexp.js +3 -3
- package/build/require/type/schema/anyschema.d.ts +2 -1
- package/build/require/type/type/javascript.d.ts +2 -2
- package/build/require/type/type/javascript.js +1 -1
- package/build/require/value/cast/cast.d.ts +2 -15
- package/build/require/value/cast/cast.js +7 -29
- package/build/require/value/check/check.js +6 -1
- package/build/require/value/convert/convert.d.ts +0 -5
- package/build/require/value/convert/convert.js +31 -43
- package/build/require/value/create/create.d.ts +2 -23
- package/build/require/value/create/create.js +32 -54
- package/build/require/value/default/default.js +0 -2
- package/build/require/value/delta/delta.d.ts +4 -4
- package/build/require/value/delta/delta.js +14 -14
- package/build/require/value/index.d.ts +7 -7
- package/build/require/value/index.js +8 -19
- package/build/require/value/mutate/mutate.d.ts +2 -5
- package/build/require/value/mutate/mutate.js +7 -13
- package/package.json +1 -1
- package/readme.md +43 -36
|
@@ -5,7 +5,7 @@ import type { TNumber } from '../number/index';
|
|
|
5
5
|
import type { TSchema } from '../schema/index';
|
|
6
6
|
import type { TUnion } from '../union/index';
|
|
7
7
|
type TFromTemplateLiteral<T extends TTemplateLiteral, R extends string[] = TTemplateLiteralGenerate<T>> = (R);
|
|
8
|
-
type TFromUnion<T extends TSchema[], Acc extends string[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromUnion<R, [...TIndexPropertyKeys<L
|
|
8
|
+
type TFromUnion<T extends TSchema[], Acc extends string[] = []> = (T extends [infer L extends TSchema, ...infer R extends TSchema[]] ? TFromUnion<R, [...Acc, ...TIndexPropertyKeys<L>]> : Acc);
|
|
9
9
|
type TFromLiteral<T extends TLiteralValue> = (T extends PropertyKey ? [`${T}`] : []);
|
|
10
10
|
export type TIndexPropertyKeys<T extends TSchema> = (T extends TTemplateLiteral ? TFromTemplateLiteral<T> : T extends TUnion<infer S> ? TFromUnion<S> : T extends TLiteral<infer S> ? TFromLiteral<S> : T extends TNumber ? ['[number]'] : T extends TInteger ? ['[number]'] : [
|
|
11
11
|
]);
|
|
@@ -5,6 +5,7 @@ import { type TObject, type TProperties, type TAdditionalProperties, type Object
|
|
|
5
5
|
import { type TLiteral, type TLiteralValue } from '../literal/index';
|
|
6
6
|
import { type TNever } from '../never/index';
|
|
7
7
|
import { type TUnion } from '../union/index';
|
|
8
|
+
import { type TRegExp } from '../regexp/index';
|
|
8
9
|
import { type TString } from '../string/index';
|
|
9
10
|
import { type TInteger } from '../integer/index';
|
|
10
11
|
import { type TNumber } from '../number/index';
|
|
@@ -30,6 +31,7 @@ type TFromUnionKey<K extends TSchema[], T extends TSchema, P extends TProperties
|
|
|
30
31
|
type TFromLiteralKey<K extends TLiteralValue, T extends TSchema> = (Ensure<TObject<{
|
|
31
32
|
[_ in Assert<K, PropertyKey>]: T;
|
|
32
33
|
}>>);
|
|
34
|
+
type TFromRegExpKey<_ extends TRegExp, T extends TSchema> = (Ensure<TRecord<TRegExp, T>>);
|
|
33
35
|
type TFromStringKey<_ extends TString, T extends TSchema> = (Ensure<TRecord<TString, T>>);
|
|
34
36
|
type TFromIntegerKey<_ extends TSchema, T extends TSchema> = (Ensure<TRecord<TNumber, T>>);
|
|
35
37
|
type TFromNumberKey<_ extends TSchema, T extends TSchema> = (Ensure<TRecord<TNumber, T>>);
|
|
@@ -43,7 +45,7 @@ export interface TRecord<K extends TSchema = TSchema, T extends TSchema = TSchem
|
|
|
43
45
|
};
|
|
44
46
|
additionalProperties: TAdditionalProperties;
|
|
45
47
|
}
|
|
46
|
-
export type TRecordOrObject<K extends TSchema, T extends TSchema> = K extends TTemplateLiteral ? TFromTemplateLiteralKey<K, T> : K extends TEnum<infer S> ? TFromEnumKey<S, T> : K extends TUnion<infer S> ? TFromUnionKey<S, T> : K extends TLiteral<infer S> ? TFromLiteralKey<S, T> : K extends TInteger ? TFromIntegerKey<K, T> : K extends TNumber ? TFromNumberKey<K, T> : K extends TString ? TFromStringKey<K, T> : TNever;
|
|
48
|
+
export type TRecordOrObject<K extends TSchema, T extends TSchema> = K extends TTemplateLiteral ? TFromTemplateLiteralKey<K, T> : K extends TEnum<infer S> ? TFromEnumKey<S, T> : K extends TUnion<infer S> ? TFromUnionKey<S, T> : K extends TLiteral<infer S> ? TFromLiteralKey<S, T> : K extends TInteger ? TFromIntegerKey<K, T> : K extends TNumber ? TFromNumberKey<K, T> : K extends TRegExp ? TFromRegExpKey<K, T> : K extends TString ? TFromStringKey<K, T> : TNever;
|
|
47
49
|
/** `[Json]` Creates a Record type */
|
|
48
50
|
export declare function Record<K extends TSchema, T extends TSchema>(K: K, T: T, options?: ObjectOptions): TRecordOrObject<K, T>;
|
|
49
51
|
export {};
|
|
@@ -53,6 +53,10 @@ function FromLiteralKey(K, T, options) {
|
|
|
53
53
|
return RecordCreateFromKeys([K.toString()], T, options);
|
|
54
54
|
}
|
|
55
55
|
// prettier-ignore
|
|
56
|
+
function FromRegExpKey(K, T, options) {
|
|
57
|
+
return RecordCreateFromPattern(K.source, T, options);
|
|
58
|
+
}
|
|
59
|
+
// prettier-ignore
|
|
56
60
|
function FromStringKey(K, T, options) {
|
|
57
61
|
const pattern = (0, value_1.IsUndefined)(K.pattern) ? index_5.PatternStringExact : K.pattern;
|
|
58
62
|
return RecordCreateFromPattern(pattern, T, options);
|
|
@@ -74,9 +78,10 @@ function Record(K, T, options = {}) {
|
|
|
74
78
|
return ((0, type_2.IsUnion)(K) ? FromUnionKey(K.anyOf, T, options) :
|
|
75
79
|
(0, type_2.IsTemplateLiteral)(K) ? FromTemplateLiteralKey(K, T, options) :
|
|
76
80
|
(0, type_2.IsLiteral)(K) ? FromLiteralKey(K.const, T, options) :
|
|
77
|
-
(0, type_2.
|
|
78
|
-
(0, type_2.
|
|
79
|
-
(0, type_2.
|
|
80
|
-
(0,
|
|
81
|
+
(0, type_2.IsInteger)(K) ? FromIntegerKey(K, T, options) :
|
|
82
|
+
(0, type_2.IsNumber)(K) ? FromNumberKey(K, T, options) :
|
|
83
|
+
(0, type_2.IsRegExp)(K) ? FromRegExpKey(K, T, options) :
|
|
84
|
+
(0, type_2.IsString)(K) ? FromStringKey(K, T, options) :
|
|
85
|
+
(0, index_2.Never)(options));
|
|
81
86
|
}
|
|
82
87
|
exports.Record = Record;
|
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import type { SchemaOptions } from '../schema/index';
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import type { TSchema } from '../schema/index';
|
|
3
|
+
import { Kind } from '../symbols/index';
|
|
4
|
+
export interface TRegExp extends TSchema {
|
|
5
|
+
[Kind]: 'RegExp';
|
|
6
|
+
static: `${string}`;
|
|
7
|
+
type: 'RegExp';
|
|
8
|
+
source: string;
|
|
9
|
+
flags: string;
|
|
10
|
+
}
|
|
11
|
+
/** `[JavaScript]` Creates a RegExp type */
|
|
5
12
|
export declare function RegExp(pattern: string, options?: SchemaOptions): TRegExp;
|
|
6
|
-
/** `[JavaScript]` Creates a
|
|
13
|
+
/** `[JavaScript]` Creates a RegExp type */
|
|
7
14
|
export declare function RegExp(regex: RegExp, options?: SchemaOptions): TRegExp;
|
|
@@ -4,9 +4,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
4
4
|
exports.RegExp = void 0;
|
|
5
5
|
const value_1 = require("../guard/value");
|
|
6
6
|
const index_1 = require("../symbols/index");
|
|
7
|
-
/** `[JavaScript]` Creates a
|
|
7
|
+
/** `[JavaScript]` Creates a RegExp type */
|
|
8
8
|
function RegExp(unresolved, options = {}) {
|
|
9
|
-
const
|
|
10
|
-
return { ...options, [index_1.Kind]: '
|
|
9
|
+
const expr = (0, value_1.IsString)(unresolved) ? new globalThis.RegExp(unresolved) : unresolved;
|
|
10
|
+
return { ...options, [index_1.Kind]: 'RegExp', type: 'RegExp', source: expr.source, flags: expr.flags };
|
|
11
11
|
}
|
|
12
12
|
exports.RegExp = RegExp;
|
|
@@ -19,6 +19,7 @@ import type { TPromise } from '../promise/index';
|
|
|
19
19
|
import type { TRecord } from '../record/index';
|
|
20
20
|
import type { TThis } from '../recursive/index';
|
|
21
21
|
import type { TRef } from '../ref/index';
|
|
22
|
+
import type { TRegExp } from '../regexp/index';
|
|
22
23
|
import type { TString } from '../string/index';
|
|
23
24
|
import type { TSymbol } from '../symbol/index';
|
|
24
25
|
import type { TTemplateLiteral } from '../template-literal/index';
|
|
@@ -29,4 +30,4 @@ import type { TUnion } from '../union/index';
|
|
|
29
30
|
import type { TUnknown } from '../unknown/index';
|
|
30
31
|
import type { TVoid } from '../void/index';
|
|
31
32
|
import type { TSchema } from './schema';
|
|
32
|
-
export type TAnySchema = TSchema | TAny | TArray | TAsyncIterator | TBigInt | TBoolean | TConstructor | TDate | TEnum | TFunction | TInteger | TIntersect | TIterator | TLiteral | TNot | TNull | TNumber | TObject | TPromise | TRecord | TRef | TString | TSymbol | TTemplateLiteral | TThis | TTuple | TUndefined | TUnion | TUint8Array | TUnknown | TVoid;
|
|
33
|
+
export type TAnySchema = TSchema | TAny | TArray | TAsyncIterator | TBigInt | TBoolean | TConstructor | TDate | TEnum | TFunction | TInteger | TIntersect | TIterator | TLiteral | TNot | TNull | TNumber | TObject | TPromise | TRecord | TRef | TRegExp | TString | TSymbol | TTemplateLiteral | TThis | TTuple | TUndefined | TUnion | TUint8Array | TUnknown | TVoid;
|
|
@@ -41,9 +41,9 @@ export declare class JavaScriptTypeBuilder extends JsonTypeBuilder {
|
|
|
41
41
|
Parameters<T extends TFunction<TSchema[], TSchema>>(schema: T, options?: SchemaOptions): TParameters<T>;
|
|
42
42
|
/** `[JavaScript]` Creates a Promise type */
|
|
43
43
|
Promise<T extends TSchema>(item: T, options?: SchemaOptions): TPromise<T>;
|
|
44
|
-
/** `[JavaScript]` Creates a
|
|
44
|
+
/** `[JavaScript]` Creates a RegExp type */
|
|
45
45
|
RegExp(pattern: string, options?: SchemaOptions): TRegExp;
|
|
46
|
-
/** `[JavaScript]` Creates a
|
|
46
|
+
/** `[JavaScript]` Creates a RegExp type */
|
|
47
47
|
RegExp(regex: RegExp, options?: SchemaOptions): TRegExp;
|
|
48
48
|
/** `[JavaScript]` Extracts the ReturnType from the given Function type */
|
|
49
49
|
ReturnType<T extends TFunction<any[], any>>(schema: T, options?: SchemaOptions): TReturnType<T>;
|
|
@@ -66,7 +66,7 @@ class JavaScriptTypeBuilder extends json_1.JsonTypeBuilder {
|
|
|
66
66
|
Promise(item, options = {}) {
|
|
67
67
|
return (0, index_11.Promise)(item, options);
|
|
68
68
|
}
|
|
69
|
-
/** `[JavaScript]` Creates a
|
|
69
|
+
/** `[JavaScript]` Creates a RegExp type */
|
|
70
70
|
RegExp(unresolved, options = {}) {
|
|
71
71
|
return (0, index_12.RegExp)(unresolved, options);
|
|
72
72
|
}
|
|
@@ -1,22 +1,9 @@
|
|
|
1
1
|
import { TypeBoxError } from '../../type/error/index';
|
|
2
2
|
import type { TSchema } from '../../type/schema/index';
|
|
3
3
|
import type { Static } from '../../type/static/index';
|
|
4
|
-
export declare class
|
|
4
|
+
export declare class ValueCastError extends TypeBoxError {
|
|
5
5
|
readonly schema: TSchema;
|
|
6
|
-
|
|
7
|
-
constructor(schema: TSchema, value: unknown);
|
|
8
|
-
}
|
|
9
|
-
export declare class ValueCastNeverTypeError extends TypeBoxError {
|
|
10
|
-
readonly schema: TSchema;
|
|
11
|
-
constructor(schema: TSchema);
|
|
12
|
-
}
|
|
13
|
-
export declare class ValueCastRecursiveTypeError extends TypeBoxError {
|
|
14
|
-
readonly schema: TSchema;
|
|
15
|
-
constructor(schema: TSchema);
|
|
16
|
-
}
|
|
17
|
-
export declare class ValueCastUnknownTypeError extends TypeBoxError {
|
|
18
|
-
readonly schema: TSchema;
|
|
19
|
-
constructor(schema: TSchema);
|
|
6
|
+
constructor(schema: TSchema, message: string);
|
|
20
7
|
}
|
|
21
8
|
/** Casts a value into a given type and references. The return value will retain as much information of the original value as possible. */
|
|
22
9
|
export declare function Cast<T extends TSchema>(schema: T, references: TSchema[], value: unknown): Static<T>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.Cast = exports.
|
|
4
|
+
exports.Cast = exports.ValueCastError = void 0;
|
|
5
5
|
const index_1 = require("../guard/index");
|
|
6
6
|
const index_2 = require("../../type/error/index");
|
|
7
7
|
const index_3 = require("../../type/symbols/index");
|
|
@@ -12,35 +12,13 @@ const index_7 = require("../deref/index");
|
|
|
12
12
|
// ------------------------------------------------------------------
|
|
13
13
|
// Errors
|
|
14
14
|
// ------------------------------------------------------------------
|
|
15
|
-
class
|
|
16
|
-
constructor(schema,
|
|
17
|
-
super(
|
|
15
|
+
class ValueCastError extends index_2.TypeBoxError {
|
|
16
|
+
constructor(schema, message) {
|
|
17
|
+
super(message);
|
|
18
18
|
this.schema = schema;
|
|
19
|
-
this.value = value;
|
|
20
19
|
}
|
|
21
20
|
}
|
|
22
|
-
exports.
|
|
23
|
-
class ValueCastNeverTypeError extends index_2.TypeBoxError {
|
|
24
|
-
constructor(schema) {
|
|
25
|
-
super('Never types cannot be cast');
|
|
26
|
-
this.schema = schema;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
exports.ValueCastNeverTypeError = ValueCastNeverTypeError;
|
|
30
|
-
class ValueCastRecursiveTypeError extends index_2.TypeBoxError {
|
|
31
|
-
constructor(schema) {
|
|
32
|
-
super('Cannot cast recursive schemas');
|
|
33
|
-
this.schema = schema;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
exports.ValueCastRecursiveTypeError = ValueCastRecursiveTypeError;
|
|
37
|
-
class ValueCastUnknownTypeError extends index_2.TypeBoxError {
|
|
38
|
-
constructor(schema) {
|
|
39
|
-
super('Unknown type');
|
|
40
|
-
this.schema = schema;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
exports.ValueCastUnknownTypeError = ValueCastUnknownTypeError;
|
|
21
|
+
exports.ValueCastError = ValueCastError;
|
|
44
22
|
// ------------------------------------------------------------------
|
|
45
23
|
// The following will score a schema against a value. For objects,
|
|
46
24
|
// the score is the tally of points awarded for each property of
|
|
@@ -109,7 +87,7 @@ function FromArray(schema, references, value) {
|
|
|
109
87
|
return casted;
|
|
110
88
|
const unique = [...new Set(casted)];
|
|
111
89
|
if (!(0, index_5.Check)(schema, references, unique))
|
|
112
|
-
throw new
|
|
90
|
+
throw new ValueCastError(schema, 'Array cast produced invalid data due to uniqueItems constraint');
|
|
113
91
|
return unique;
|
|
114
92
|
}
|
|
115
93
|
function FromConstructor(schema, references, value) {
|
|
@@ -130,7 +108,7 @@ function FromIntersect(schema, references, value) {
|
|
|
130
108
|
return (0, index_5.Check)(schema, references, mapped) ? mapped : (0, index_4.Create)(schema, references);
|
|
131
109
|
}
|
|
132
110
|
function FromNever(schema, references, value) {
|
|
133
|
-
throw new
|
|
111
|
+
throw new ValueCastError(schema, 'Never types cannot be cast');
|
|
134
112
|
}
|
|
135
113
|
function FromObject(schema, references, value) {
|
|
136
114
|
if ((0, index_5.Check)(schema, references, value))
|
|
@@ -14,7 +14,6 @@ const index_9 = require("../../type/never/index");
|
|
|
14
14
|
// ------------------------------------------------------------------
|
|
15
15
|
// ValueGuard
|
|
16
16
|
// ------------------------------------------------------------------
|
|
17
|
-
// prettier-ignore
|
|
18
17
|
const index_10 = require("../guard/index");
|
|
19
18
|
// ------------------------------------------------------------------
|
|
20
19
|
// TypeGuard
|
|
@@ -290,6 +289,10 @@ function FromRecord(schema, references, value) {
|
|
|
290
289
|
function FromRef(schema, references, value) {
|
|
291
290
|
return Visit((0, index_2.Deref)(schema, references), references, value);
|
|
292
291
|
}
|
|
292
|
+
function FromRegExp(schema, references, value) {
|
|
293
|
+
const regex = new RegExp(schema.source, schema.flags);
|
|
294
|
+
return regex.test(value);
|
|
295
|
+
}
|
|
293
296
|
function FromString(schema, references, value) {
|
|
294
297
|
if (!(0, index_10.IsString)(value)) {
|
|
295
298
|
return false;
|
|
@@ -417,6 +420,8 @@ function Visit(schema, references, value) {
|
|
|
417
420
|
return FromRecord(schema_, references_, value);
|
|
418
421
|
case 'Ref':
|
|
419
422
|
return FromRef(schema_, references_, value);
|
|
423
|
+
case 'RegExp':
|
|
424
|
+
return FromRegExp(schema_, references_, value);
|
|
420
425
|
case 'String':
|
|
421
426
|
return FromString(schema_, references_, value);
|
|
422
427
|
case 'Symbol':
|
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import { TypeBoxError } from '../../type/error/index';
|
|
2
1
|
import type { TSchema } from '../../type/schema/index';
|
|
3
|
-
export declare class ValueConvertUnknownTypeError extends TypeBoxError {
|
|
4
|
-
readonly schema: TSchema;
|
|
5
|
-
constructor(schema: TSchema);
|
|
6
|
-
}
|
|
7
2
|
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
8
3
|
export declare function Convert<T extends TSchema>(schema: T, references: TSchema[], value: unknown): unknown;
|
|
9
4
|
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
@@ -1,58 +1,46 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.Convert =
|
|
4
|
+
exports.Convert = void 0;
|
|
5
5
|
const index_1 = require("../clone/index");
|
|
6
6
|
const index_2 = require("../check/index");
|
|
7
7
|
const index_3 = require("../deref/index");
|
|
8
|
-
const index_4 = require("../../type/error/index");
|
|
9
8
|
const type_1 = require("../../type/guard/type");
|
|
10
|
-
const
|
|
11
|
-
const
|
|
9
|
+
const index_4 = require("../../type/symbols/index");
|
|
10
|
+
const index_5 = require("../../type/composite/index");
|
|
12
11
|
// ------------------------------------------------------------------
|
|
13
12
|
// ValueGuard
|
|
14
13
|
// ------------------------------------------------------------------
|
|
15
|
-
|
|
16
|
-
const index_7 = require("../guard/index");
|
|
17
|
-
// ------------------------------------------------------------------
|
|
18
|
-
// Errors
|
|
19
|
-
// ------------------------------------------------------------------
|
|
20
|
-
class ValueConvertUnknownTypeError extends index_4.TypeBoxError {
|
|
21
|
-
constructor(schema) {
|
|
22
|
-
super('Unknown type');
|
|
23
|
-
this.schema = schema;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
exports.ValueConvertUnknownTypeError = ValueConvertUnknownTypeError;
|
|
14
|
+
const index_6 = require("../guard/index");
|
|
27
15
|
// ------------------------------------------------------------------
|
|
28
16
|
// Conversions
|
|
29
17
|
// ------------------------------------------------------------------
|
|
30
18
|
function IsStringNumeric(value) {
|
|
31
|
-
return (0,
|
|
19
|
+
return (0, index_6.IsString)(value) && !isNaN(value) && !isNaN(parseFloat(value));
|
|
32
20
|
}
|
|
33
21
|
function IsValueToString(value) {
|
|
34
|
-
return (0,
|
|
22
|
+
return (0, index_6.IsBigInt)(value) || (0, index_6.IsBoolean)(value) || (0, index_6.IsNumber)(value);
|
|
35
23
|
}
|
|
36
24
|
function IsValueTrue(value) {
|
|
37
|
-
return value === true || ((0,
|
|
25
|
+
return value === true || ((0, index_6.IsNumber)(value) && value === 1) || ((0, index_6.IsBigInt)(value) && value === BigInt('1')) || ((0, index_6.IsString)(value) && (value.toLowerCase() === 'true' || value === '1'));
|
|
38
26
|
}
|
|
39
27
|
function IsValueFalse(value) {
|
|
40
|
-
return value === false || ((0,
|
|
28
|
+
return value === false || ((0, index_6.IsNumber)(value) && (value === 0 || Object.is(value, -0))) || ((0, index_6.IsBigInt)(value) && value === BigInt('0')) || ((0, index_6.IsString)(value) && (value.toLowerCase() === 'false' || value === '0' || value === '-0'));
|
|
41
29
|
}
|
|
42
30
|
function IsTimeStringWithTimeZone(value) {
|
|
43
|
-
return (0,
|
|
31
|
+
return (0, index_6.IsString)(value) && /^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i.test(value);
|
|
44
32
|
}
|
|
45
33
|
function IsTimeStringWithoutTimeZone(value) {
|
|
46
|
-
return (0,
|
|
34
|
+
return (0, index_6.IsString)(value) && /^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)?$/i.test(value);
|
|
47
35
|
}
|
|
48
36
|
function IsDateTimeStringWithTimeZone(value) {
|
|
49
|
-
return (0,
|
|
37
|
+
return (0, index_6.IsString)(value) && /^\d\d\d\d-[0-1]\d-[0-3]\dt(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i.test(value);
|
|
50
38
|
}
|
|
51
39
|
function IsDateTimeStringWithoutTimeZone(value) {
|
|
52
|
-
return (0,
|
|
40
|
+
return (0, index_6.IsString)(value) && /^\d\d\d\d-[0-1]\d-[0-3]\dt(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)?$/i.test(value);
|
|
53
41
|
}
|
|
54
42
|
function IsDateString(value) {
|
|
55
|
-
return (0,
|
|
43
|
+
return (0, index_6.IsString)(value) && /^\d\d\d\d-[0-1]\d-[0-3]\d$/i.test(value);
|
|
56
44
|
}
|
|
57
45
|
// ------------------------------------------------------------------
|
|
58
46
|
// Convert
|
|
@@ -71,31 +59,31 @@ function TryConvertLiteralBoolean(value, target) {
|
|
|
71
59
|
}
|
|
72
60
|
// prettier-ignore
|
|
73
61
|
function TryConvertLiteral(schema, value) {
|
|
74
|
-
return ((0,
|
|
75
|
-
(0,
|
|
76
|
-
(0,
|
|
62
|
+
return ((0, index_6.IsString)(schema.const) ? TryConvertLiteralString(value, schema.const) :
|
|
63
|
+
(0, index_6.IsNumber)(schema.const) ? TryConvertLiteralNumber(value, schema.const) :
|
|
64
|
+
(0, index_6.IsBoolean)(schema.const) ? TryConvertLiteralBoolean(value, schema.const) :
|
|
77
65
|
(0, index_1.Clone)(value));
|
|
78
66
|
}
|
|
79
67
|
function TryConvertBoolean(value) {
|
|
80
68
|
return IsValueTrue(value) ? true : IsValueFalse(value) ? false : value;
|
|
81
69
|
}
|
|
82
70
|
function TryConvertBigInt(value) {
|
|
83
|
-
return IsStringNumeric(value) ? BigInt(parseInt(value)) : (0,
|
|
71
|
+
return IsStringNumeric(value) ? BigInt(parseInt(value)) : (0, index_6.IsNumber)(value) ? BigInt(value | 0) : IsValueFalse(value) ? BigInt(0) : IsValueTrue(value) ? BigInt(1) : value;
|
|
84
72
|
}
|
|
85
73
|
function TryConvertString(value) {
|
|
86
|
-
return IsValueToString(value) ? value.toString() : (0,
|
|
74
|
+
return IsValueToString(value) ? value.toString() : (0, index_6.IsSymbol)(value) && value.description !== undefined ? value.description.toString() : value;
|
|
87
75
|
}
|
|
88
76
|
function TryConvertNumber(value) {
|
|
89
77
|
return IsStringNumeric(value) ? parseFloat(value) : IsValueTrue(value) ? 1 : IsValueFalse(value) ? 0 : value;
|
|
90
78
|
}
|
|
91
79
|
function TryConvertInteger(value) {
|
|
92
|
-
return IsStringNumeric(value) ? parseInt(value) : (0,
|
|
80
|
+
return IsStringNumeric(value) ? parseInt(value) : (0, index_6.IsNumber)(value) ? value | 0 : IsValueTrue(value) ? 1 : IsValueFalse(value) ? 0 : value;
|
|
93
81
|
}
|
|
94
82
|
function TryConvertNull(value) {
|
|
95
|
-
return (0,
|
|
83
|
+
return (0, index_6.IsString)(value) && value.toLowerCase() === 'null' ? null : value;
|
|
96
84
|
}
|
|
97
85
|
function TryConvertUndefined(value) {
|
|
98
|
-
return (0,
|
|
86
|
+
return (0, index_6.IsString)(value) && value === 'undefined' ? undefined : value;
|
|
99
87
|
}
|
|
100
88
|
// ------------------------------------------------------------------
|
|
101
89
|
// note: this function may return an invalid dates for the regex
|
|
@@ -106,8 +94,8 @@ function TryConvertUndefined(value) {
|
|
|
106
94
|
// ------------------------------------------------------------------
|
|
107
95
|
// prettier-ignore
|
|
108
96
|
function TryConvertDate(value) {
|
|
109
|
-
return ((0,
|
|
110
|
-
(0,
|
|
97
|
+
return ((0, index_6.IsDate)(value) ? value :
|
|
98
|
+
(0, index_6.IsNumber)(value) ? new Date(value) :
|
|
111
99
|
IsValueTrue(value) ? new Date(1) :
|
|
112
100
|
IsValueFalse(value) ? new Date(0) :
|
|
113
101
|
IsStringNumeric(value) ? new Date(parseInt(value)) :
|
|
@@ -128,7 +116,7 @@ function Default(value) {
|
|
|
128
116
|
// Convert
|
|
129
117
|
// ------------------------------------------------------------------
|
|
130
118
|
function FromArray(schema, references, value) {
|
|
131
|
-
if ((0,
|
|
119
|
+
if ((0, index_6.IsArray)(value)) {
|
|
132
120
|
return value.map((value) => Visit(schema.items, references, value));
|
|
133
121
|
}
|
|
134
122
|
return value;
|
|
@@ -149,7 +137,7 @@ function FromInteger(schema, references, value) {
|
|
|
149
137
|
function FromIntersect(schema, references, value) {
|
|
150
138
|
const allObjects = schema.allOf.every(schema => (0, type_1.IsObject)(schema));
|
|
151
139
|
if (allObjects)
|
|
152
|
-
return Visit((0,
|
|
140
|
+
return Visit((0, index_5.Composite)(schema.allOf), references, value);
|
|
153
141
|
return Visit(schema.allOf[0], references, value); // todo: fix this
|
|
154
142
|
}
|
|
155
143
|
function FromLiteral(schema, references, value) {
|
|
@@ -163,11 +151,11 @@ function FromNumber(schema, references, value) {
|
|
|
163
151
|
}
|
|
164
152
|
// prettier-ignore
|
|
165
153
|
function FromObject(schema, references, value) {
|
|
166
|
-
const isConvertable = (0,
|
|
154
|
+
const isConvertable = (0, index_6.IsObject)(value);
|
|
167
155
|
if (!isConvertable)
|
|
168
156
|
return value;
|
|
169
157
|
return Object.getOwnPropertyNames(schema.properties).reduce((value, key) => {
|
|
170
|
-
return !(0,
|
|
158
|
+
return !(0, index_6.IsUndefined)(value[key])
|
|
171
159
|
? ({ ...value, [key]: Visit(schema.properties[key], references, value[key]) })
|
|
172
160
|
: ({ ...value });
|
|
173
161
|
}, value);
|
|
@@ -188,14 +176,14 @@ function FromString(schema, references, value) {
|
|
|
188
176
|
return TryConvertString(value);
|
|
189
177
|
}
|
|
190
178
|
function FromSymbol(schema, references, value) {
|
|
191
|
-
return (0,
|
|
179
|
+
return (0, index_6.IsString)(value) || (0, index_6.IsNumber)(value) ? Symbol(value) : value;
|
|
192
180
|
}
|
|
193
181
|
function FromThis(schema, references, value) {
|
|
194
182
|
return Visit((0, index_3.Deref)(schema, references), references, value);
|
|
195
183
|
}
|
|
196
184
|
// prettier-ignore
|
|
197
185
|
function FromTuple(schema, references, value) {
|
|
198
|
-
const isConvertable = (0,
|
|
186
|
+
const isConvertable = (0, index_6.IsArray)(value) && !(0, index_6.IsUndefined)(schema.items);
|
|
199
187
|
if (!isConvertable)
|
|
200
188
|
return value;
|
|
201
189
|
return value.map((value, index) => {
|
|
@@ -217,9 +205,9 @@ function FromUnion(schema, references, value) {
|
|
|
217
205
|
return value;
|
|
218
206
|
}
|
|
219
207
|
function Visit(schema, references, value) {
|
|
220
|
-
const references_ = (0,
|
|
208
|
+
const references_ = (0, index_6.IsString)(schema.$id) ? [...references, schema] : references;
|
|
221
209
|
const schema_ = schema;
|
|
222
|
-
switch (schema[
|
|
210
|
+
switch (schema[index_4.Kind]) {
|
|
223
211
|
case 'Array':
|
|
224
212
|
return FromArray(schema_, references_, value);
|
|
225
213
|
case 'BigInt':
|
|
@@ -1,30 +1,9 @@
|
|
|
1
1
|
import { TypeBoxError } from '../../type/error/index';
|
|
2
2
|
import type { TSchema } from '../../type/schema/index';
|
|
3
3
|
import type { Static } from '../../type/static/index';
|
|
4
|
-
export declare class
|
|
4
|
+
export declare class ValueCreateError extends TypeBoxError {
|
|
5
5
|
readonly schema: TSchema;
|
|
6
|
-
constructor(schema: TSchema);
|
|
7
|
-
}
|
|
8
|
-
export declare class ValueCreateNeverTypeError extends TypeBoxError {
|
|
9
|
-
readonly schema: TSchema;
|
|
10
|
-
constructor(schema: TSchema);
|
|
11
|
-
}
|
|
12
|
-
export declare class ValueCreateNotTypeError extends TypeBoxError {
|
|
13
|
-
readonly schema: TSchema;
|
|
14
|
-
constructor(schema: TSchema);
|
|
15
|
-
}
|
|
16
|
-
export declare class ValueCreateIntersectTypeError extends TypeBoxError {
|
|
17
|
-
readonly schema: TSchema;
|
|
18
|
-
constructor(schema: TSchema);
|
|
19
|
-
}
|
|
20
|
-
export declare class ValueCreateTempateLiteralTypeError extends TypeBoxError {
|
|
21
|
-
readonly schema: TSchema;
|
|
22
|
-
constructor(schema: TSchema);
|
|
23
|
-
}
|
|
24
|
-
export declare class ValueCreateRecursiveInstantiationError extends TypeBoxError {
|
|
25
|
-
readonly schema: TSchema;
|
|
26
|
-
readonly recursiveMaxDepth: number;
|
|
27
|
-
constructor(schema: TSchema, recursiveMaxDepth: number);
|
|
6
|
+
constructor(schema: TSchema, message: string);
|
|
28
7
|
}
|
|
29
8
|
/** Creates a value from the given schema and references */
|
|
30
9
|
export declare function Create<T extends TSchema>(schema: T, references: TSchema[]): Static<T>;
|