@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,6 +5,7 @@ import { type TObject, type TProperties, type TAdditionalProperties, type Object
|
|
|
5
5
|
import { type TLiteral, type TLiteralValue } from '../literal/index.mjs';
|
|
6
6
|
import { type TNever } from '../never/index.mjs';
|
|
7
7
|
import { type TUnion } from '../union/index.mjs';
|
|
8
|
+
import { type TRegExp } from '../regexp/index.mjs';
|
|
8
9
|
import { type TString } from '../string/index.mjs';
|
|
9
10
|
import { type TInteger } from '../integer/index.mjs';
|
|
10
11
|
import { type TNumber } from '../number/index.mjs';
|
|
@@ -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 {};
|
|
@@ -13,7 +13,7 @@ import { IsUndefined } from '../guard/value.mjs';
|
|
|
13
13
|
// ------------------------------------------------------------------
|
|
14
14
|
// TypeGuard
|
|
15
15
|
// ------------------------------------------------------------------
|
|
16
|
-
import { IsInteger, IsLiteral, IsNumber, IsString, IsTemplateLiteral, IsUnion } from '../guard/type.mjs';
|
|
16
|
+
import { IsInteger, IsLiteral, IsNumber, IsString, IsRegExp, IsTemplateLiteral, IsUnion } from '../guard/type.mjs';
|
|
17
17
|
// ------------------------------------------------------------------
|
|
18
18
|
// RecordCreateFromPattern
|
|
19
19
|
// ------------------------------------------------------------------
|
|
@@ -49,6 +49,10 @@ function FromLiteralKey(K, T, options) {
|
|
|
49
49
|
return RecordCreateFromKeys([K.toString()], T, options);
|
|
50
50
|
}
|
|
51
51
|
// prettier-ignore
|
|
52
|
+
function FromRegExpKey(K, T, options) {
|
|
53
|
+
return RecordCreateFromPattern(K.source, T, options);
|
|
54
|
+
}
|
|
55
|
+
// prettier-ignore
|
|
52
56
|
function FromStringKey(K, T, options) {
|
|
53
57
|
const pattern = IsUndefined(K.pattern) ? PatternStringExact : K.pattern;
|
|
54
58
|
return RecordCreateFromPattern(pattern, T, options);
|
|
@@ -70,8 +74,9 @@ export function Record(K, T, options = {}) {
|
|
|
70
74
|
return (IsUnion(K) ? FromUnionKey(K.anyOf, T, options) :
|
|
71
75
|
IsTemplateLiteral(K) ? FromTemplateLiteralKey(K, T, options) :
|
|
72
76
|
IsLiteral(K) ? FromLiteralKey(K.const, T, options) :
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
+
IsInteger(K) ? FromIntegerKey(K, T, options) :
|
|
78
|
+
IsNumber(K) ? FromNumberKey(K, T, options) :
|
|
79
|
+
IsRegExp(K) ? FromRegExpKey(K, T, options) :
|
|
80
|
+
IsString(K) ? FromStringKey(K, T, options) :
|
|
81
|
+
Never(options));
|
|
77
82
|
}
|
|
@@ -1,7 +1,14 @@
|
|
|
1
1
|
import type { SchemaOptions } from '../schema/index.mjs';
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import type { TSchema } from '../schema/index.mjs';
|
|
3
|
+
import { Kind } from '../symbols/index.mjs';
|
|
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;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { IsString } from '../guard/value.mjs';
|
|
2
2
|
import { Kind } from '../symbols/index.mjs';
|
|
3
|
-
/** `[JavaScript]` Creates a
|
|
3
|
+
/** `[JavaScript]` Creates a RegExp type */
|
|
4
4
|
export function RegExp(unresolved, options = {}) {
|
|
5
|
-
const
|
|
6
|
-
return { ...options, [Kind]: '
|
|
5
|
+
const expr = IsString(unresolved) ? new globalThis.RegExp(unresolved) : unresolved;
|
|
6
|
+
return { ...options, [Kind]: 'RegExp', type: 'RegExp', source: expr.source, flags: expr.flags };
|
|
7
7
|
}
|
|
@@ -19,6 +19,7 @@ import type { TPromise } from '../promise/index.mjs';
|
|
|
19
19
|
import type { TRecord } from '../record/index.mjs';
|
|
20
20
|
import type { TThis } from '../recursive/index.mjs';
|
|
21
21
|
import type { TRef } from '../ref/index.mjs';
|
|
22
|
+
import type { TRegExp } from '../regexp/index.mjs';
|
|
22
23
|
import type { TString } from '../string/index.mjs';
|
|
23
24
|
import type { TSymbol } from '../symbol/index.mjs';
|
|
24
25
|
import type { TTemplateLiteral } from '../template-literal/index.mjs';
|
|
@@ -29,4 +30,4 @@ import type { TUnion } from '../union/index.mjs';
|
|
|
29
30
|
import type { TUnknown } from '../unknown/index.mjs';
|
|
30
31
|
import type { TVoid } from '../void/index.mjs';
|
|
31
32
|
import type { TSchema } from './schema.mjs';
|
|
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>;
|
|
@@ -62,7 +62,7 @@ export class JavaScriptTypeBuilder extends JsonTypeBuilder {
|
|
|
62
62
|
Promise(item, options = {}) {
|
|
63
63
|
return Promise(item, options);
|
|
64
64
|
}
|
|
65
|
-
/** `[JavaScript]` Creates a
|
|
65
|
+
/** `[JavaScript]` Creates a RegExp type */
|
|
66
66
|
RegExp(unresolved, options = {}) {
|
|
67
67
|
return RegExp(unresolved, options);
|
|
68
68
|
}
|
|
@@ -1,22 +1,9 @@
|
|
|
1
1
|
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
2
2
|
import type { TSchema } from '../../type/schema/index.mjs';
|
|
3
3
|
import type { Static } from '../../type/static/index.mjs';
|
|
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>;
|
|
@@ -8,33 +8,10 @@ import { Deref } from '../deref/index.mjs';
|
|
|
8
8
|
// ------------------------------------------------------------------
|
|
9
9
|
// Errors
|
|
10
10
|
// ------------------------------------------------------------------
|
|
11
|
-
export class
|
|
11
|
+
export class ValueCastError extends TypeBoxError {
|
|
12
12
|
schema;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
super('Array cast produced invalid data due to uniqueItems constraint');
|
|
16
|
-
this.schema = schema;
|
|
17
|
-
this.value = value;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
export class ValueCastNeverTypeError extends TypeBoxError {
|
|
21
|
-
schema;
|
|
22
|
-
constructor(schema) {
|
|
23
|
-
super('Never types cannot be cast');
|
|
24
|
-
this.schema = schema;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
export class ValueCastRecursiveTypeError extends TypeBoxError {
|
|
28
|
-
schema;
|
|
29
|
-
constructor(schema) {
|
|
30
|
-
super('Cannot cast recursive schemas');
|
|
31
|
-
this.schema = schema;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
export class ValueCastUnknownTypeError extends TypeBoxError {
|
|
35
|
-
schema;
|
|
36
|
-
constructor(schema) {
|
|
37
|
-
super('Unknown type');
|
|
13
|
+
constructor(schema, message) {
|
|
14
|
+
super(message);
|
|
38
15
|
this.schema = schema;
|
|
39
16
|
}
|
|
40
17
|
}
|
|
@@ -106,7 +83,7 @@ function FromArray(schema, references, value) {
|
|
|
106
83
|
return casted;
|
|
107
84
|
const unique = [...new Set(casted)];
|
|
108
85
|
if (!Check(schema, references, unique))
|
|
109
|
-
throw new
|
|
86
|
+
throw new ValueCastError(schema, 'Array cast produced invalid data due to uniqueItems constraint');
|
|
110
87
|
return unique;
|
|
111
88
|
}
|
|
112
89
|
function FromConstructor(schema, references, value) {
|
|
@@ -127,7 +104,7 @@ function FromIntersect(schema, references, value) {
|
|
|
127
104
|
return Check(schema, references, mapped) ? mapped : Create(schema, references);
|
|
128
105
|
}
|
|
129
106
|
function FromNever(schema, references, value) {
|
|
130
|
-
throw new
|
|
107
|
+
throw new ValueCastError(schema, 'Never types cannot be cast');
|
|
131
108
|
}
|
|
132
109
|
function FromObject(schema, references, value) {
|
|
133
110
|
if (Check(schema, references, value))
|
|
@@ -10,7 +10,6 @@ import { Never } from '../../type/never/index.mjs';
|
|
|
10
10
|
// ------------------------------------------------------------------
|
|
11
11
|
// ValueGuard
|
|
12
12
|
// ------------------------------------------------------------------
|
|
13
|
-
// prettier-ignore
|
|
14
13
|
import { IsArray, IsUint8Array, IsDate, IsPromise, IsFunction, IsAsyncIterator, IsIterator, IsBoolean, IsNumber, IsBigInt, IsString, IsSymbol, IsInteger, IsNull, IsUndefined } from '../guard/index.mjs';
|
|
15
14
|
// ------------------------------------------------------------------
|
|
16
15
|
// TypeGuard
|
|
@@ -286,6 +285,10 @@ function FromRecord(schema, references, value) {
|
|
|
286
285
|
function FromRef(schema, references, value) {
|
|
287
286
|
return Visit(Deref(schema, references), references, value);
|
|
288
287
|
}
|
|
288
|
+
function FromRegExp(schema, references, value) {
|
|
289
|
+
const regex = new RegExp(schema.source, schema.flags);
|
|
290
|
+
return regex.test(value);
|
|
291
|
+
}
|
|
289
292
|
function FromString(schema, references, value) {
|
|
290
293
|
if (!IsString(value)) {
|
|
291
294
|
return false;
|
|
@@ -413,6 +416,8 @@ function Visit(schema, references, value) {
|
|
|
413
416
|
return FromRecord(schema_, references_, value);
|
|
414
417
|
case 'Ref':
|
|
415
418
|
return FromRef(schema_, references_, value);
|
|
419
|
+
case 'RegExp':
|
|
420
|
+
return FromRegExp(schema_, references_, value);
|
|
416
421
|
case 'String':
|
|
417
422
|
return FromString(schema_, references_, value);
|
|
418
423
|
case 'Symbol':
|
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
2
1
|
import type { TSchema } from '../../type/schema/index.mjs';
|
|
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,26 +1,14 @@
|
|
|
1
1
|
import { Clone } from '../clone/index.mjs';
|
|
2
2
|
import { Check } from '../check/index.mjs';
|
|
3
3
|
import { Deref } from '../deref/index.mjs';
|
|
4
|
-
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
5
4
|
import { IsObject as IsObjectType } from '../../type/guard/type.mjs';
|
|
6
5
|
import { Kind } from '../../type/symbols/index.mjs';
|
|
7
6
|
import { Composite } from '../../type/composite/index.mjs';
|
|
8
7
|
// ------------------------------------------------------------------
|
|
9
8
|
// ValueGuard
|
|
10
9
|
// ------------------------------------------------------------------
|
|
11
|
-
// prettier-ignore
|
|
12
10
|
import { IsArray, IsObject, IsDate, IsUndefined, IsString, IsNumber, IsBoolean, IsBigInt, IsSymbol } from '../guard/index.mjs';
|
|
13
11
|
// ------------------------------------------------------------------
|
|
14
|
-
// Errors
|
|
15
|
-
// ------------------------------------------------------------------
|
|
16
|
-
export class ValueConvertUnknownTypeError extends TypeBoxError {
|
|
17
|
-
schema;
|
|
18
|
-
constructor(schema) {
|
|
19
|
-
super('Unknown type');
|
|
20
|
-
this.schema = schema;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
// ------------------------------------------------------------------
|
|
24
12
|
// Conversions
|
|
25
13
|
// ------------------------------------------------------------------
|
|
26
14
|
function IsStringNumeric(value) {
|
|
@@ -1,30 +1,9 @@
|
|
|
1
1
|
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
2
2
|
import type { TSchema } from '../../type/schema/index.mjs';
|
|
3
3
|
import type { Static } from '../../type/static/index.mjs';
|
|
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>;
|
|
@@ -9,50 +9,13 @@ import { TypeBoxError } from '../../type/error/index.mjs';
|
|
|
9
9
|
// ------------------------------------------------------------------
|
|
10
10
|
// Errors
|
|
11
11
|
// ------------------------------------------------------------------
|
|
12
|
-
export class
|
|
12
|
+
export class ValueCreateError extends TypeBoxError {
|
|
13
13
|
schema;
|
|
14
|
-
constructor(schema) {
|
|
15
|
-
super(
|
|
14
|
+
constructor(schema, message) {
|
|
15
|
+
super(message);
|
|
16
16
|
this.schema = schema;
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
|
-
export class ValueCreateNeverTypeError extends TypeBoxError {
|
|
20
|
-
schema;
|
|
21
|
-
constructor(schema) {
|
|
22
|
-
super('Never types cannot be created');
|
|
23
|
-
this.schema = schema;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
export class ValueCreateNotTypeError extends TypeBoxError {
|
|
27
|
-
schema;
|
|
28
|
-
constructor(schema) {
|
|
29
|
-
super('Not types must have a default value');
|
|
30
|
-
this.schema = schema;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
export class ValueCreateIntersectTypeError extends TypeBoxError {
|
|
34
|
-
schema;
|
|
35
|
-
constructor(schema) {
|
|
36
|
-
super('Intersect produced invalid value. Consider using a default value.');
|
|
37
|
-
this.schema = schema;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
export class ValueCreateTempateLiteralTypeError extends TypeBoxError {
|
|
41
|
-
schema;
|
|
42
|
-
constructor(schema) {
|
|
43
|
-
super('Can only create template literal values from patterns that produce finite sequences. Consider using a default value.');
|
|
44
|
-
this.schema = schema;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
export class ValueCreateRecursiveInstantiationError extends TypeBoxError {
|
|
48
|
-
schema;
|
|
49
|
-
recursiveMaxDepth;
|
|
50
|
-
constructor(schema, recursiveMaxDepth) {
|
|
51
|
-
super('Value cannot be created as recursive type may produce value of infinite size. Consider using a default.');
|
|
52
|
-
this.schema = schema;
|
|
53
|
-
this.recursiveMaxDepth = recursiveMaxDepth;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
19
|
// ------------------------------------------------------------------
|
|
57
20
|
// Create
|
|
58
21
|
// ------------------------------------------------------------------
|
|
@@ -66,10 +29,10 @@ function FromAny(schema, references) {
|
|
|
66
29
|
}
|
|
67
30
|
function FromArray(schema, references) {
|
|
68
31
|
if (schema.uniqueItems === true && !HasPropertyKey(schema, 'default')) {
|
|
69
|
-
throw new
|
|
32
|
+
throw new ValueCreateError(schema, 'Array with the uniqueItems constraint requires a default value');
|
|
70
33
|
}
|
|
71
34
|
else if ('contains' in schema && !HasPropertyKey(schema, 'default')) {
|
|
72
|
-
throw new
|
|
35
|
+
throw new ValueCreateError(schema, 'Array with the contains constraint requires a default value');
|
|
73
36
|
}
|
|
74
37
|
else if ('default' in schema) {
|
|
75
38
|
return schema.default;
|
|
@@ -176,7 +139,7 @@ function FromIntersect(schema, references) {
|
|
|
176
139
|
return typeof next === 'object' ? { ...acc, ...next } : next;
|
|
177
140
|
}, {});
|
|
178
141
|
if (!Check(schema, references, value))
|
|
179
|
-
throw new
|
|
142
|
+
throw new ValueCreateError(schema, 'Intersect produced invalid value. Consider using a default value.');
|
|
180
143
|
return value;
|
|
181
144
|
}
|
|
182
145
|
}
|
|
@@ -197,14 +160,19 @@ function FromLiteral(schema, references) {
|
|
|
197
160
|
}
|
|
198
161
|
}
|
|
199
162
|
function FromNever(schema, references) {
|
|
200
|
-
|
|
163
|
+
if (HasPropertyKey(schema, 'default')) {
|
|
164
|
+
return schema.default;
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
throw new ValueCreateError(schema, 'Never types cannot be created. Consider using a default value.');
|
|
168
|
+
}
|
|
201
169
|
}
|
|
202
170
|
function FromNot(schema, references) {
|
|
203
171
|
if (HasPropertyKey(schema, 'default')) {
|
|
204
172
|
return schema.default;
|
|
205
173
|
}
|
|
206
174
|
else {
|
|
207
|
-
throw new
|
|
175
|
+
throw new ValueCreateError(schema, 'Not types must have a default value');
|
|
208
176
|
}
|
|
209
177
|
}
|
|
210
178
|
function FromNull(schema, references) {
|
|
@@ -269,10 +237,18 @@ function FromRef(schema, references) {
|
|
|
269
237
|
return Visit(Deref(schema, references), references);
|
|
270
238
|
}
|
|
271
239
|
}
|
|
240
|
+
function FromRegExp(schema, references) {
|
|
241
|
+
if (HasPropertyKey(schema, 'default')) {
|
|
242
|
+
return schema.default;
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
throw new ValueCreateError(schema, 'RegExp types cannot be created. Consider using a default value.');
|
|
246
|
+
}
|
|
247
|
+
}
|
|
272
248
|
function FromString(schema, references) {
|
|
273
249
|
if (schema.pattern !== undefined) {
|
|
274
250
|
if (!HasPropertyKey(schema, 'default')) {
|
|
275
|
-
throw new
|
|
251
|
+
throw new ValueCreateError(schema, 'String types with patterns must specify a default value');
|
|
276
252
|
}
|
|
277
253
|
else {
|
|
278
254
|
return schema.default;
|
|
@@ -280,7 +256,7 @@ function FromString(schema, references) {
|
|
|
280
256
|
}
|
|
281
257
|
else if (schema.format !== undefined) {
|
|
282
258
|
if (!HasPropertyKey(schema, 'default')) {
|
|
283
|
-
throw new
|
|
259
|
+
throw new ValueCreateError(schema, 'String types with formats must specify a default value');
|
|
284
260
|
}
|
|
285
261
|
else {
|
|
286
262
|
return schema.default;
|
|
@@ -291,9 +267,8 @@ function FromString(schema, references) {
|
|
|
291
267
|
return schema.default;
|
|
292
268
|
}
|
|
293
269
|
else if (schema.minLength !== undefined) {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
.join('');
|
|
270
|
+
// prettier-ignore
|
|
271
|
+
return Array.from({ length: schema.minLength }).map(() => ' ').join('');
|
|
297
272
|
}
|
|
298
273
|
else {
|
|
299
274
|
return '';
|
|
@@ -316,13 +291,13 @@ function FromTemplateLiteral(schema, references) {
|
|
|
316
291
|
return schema.default;
|
|
317
292
|
}
|
|
318
293
|
if (!IsTemplateLiteralFinite(schema))
|
|
319
|
-
throw new
|
|
294
|
+
throw new ValueCreateError(schema, 'Can only create template literals that produce a finite variants. Consider using a default value.');
|
|
320
295
|
const generated = TemplateLiteralGenerate(schema);
|
|
321
296
|
return generated[0];
|
|
322
297
|
}
|
|
323
298
|
function FromThis(schema, references) {
|
|
324
299
|
if (recursiveDepth++ > recursiveMaxDepth)
|
|
325
|
-
throw new
|
|
300
|
+
throw new ValueCreateError(schema, 'Cannot create recursive type as it appears possibly infinite. Consider using a default.');
|
|
326
301
|
if (HasPropertyKey(schema, 'default')) {
|
|
327
302
|
return schema.default;
|
|
328
303
|
}
|
|
@@ -439,6 +414,8 @@ function Visit(schema, references) {
|
|
|
439
414
|
return FromRecord(schema_, references_);
|
|
440
415
|
case 'Ref':
|
|
441
416
|
return FromRef(schema_, references_);
|
|
417
|
+
case 'RegExp':
|
|
418
|
+
return FromRegExp(schema_, references_);
|
|
442
419
|
case 'String':
|
|
443
420
|
return FromString(schema_, references_);
|
|
444
421
|
case 'Symbol':
|
|
@@ -461,7 +438,7 @@ function Visit(schema, references) {
|
|
|
461
438
|
return FromVoid(schema_, references_);
|
|
462
439
|
default:
|
|
463
440
|
if (!TypeRegistry.Has(schema_[Kind]))
|
|
464
|
-
throw new
|
|
441
|
+
throw new ValueCreateError(schema_, 'Unknown type');
|
|
465
442
|
return FromKind(schema_, references_);
|
|
466
443
|
}
|
|
467
444
|
}
|
|
@@ -4,12 +4,10 @@ import { Kind } from '../../type/symbols/index.mjs';
|
|
|
4
4
|
// ------------------------------------------------------------------
|
|
5
5
|
// ValueGuard
|
|
6
6
|
// ------------------------------------------------------------------
|
|
7
|
-
// prettier-ignore
|
|
8
7
|
import { IsString, IsObject, IsArray, IsUndefined } from '../guard/index.mjs';
|
|
9
8
|
// ------------------------------------------------------------------
|
|
10
9
|
// TypeGuard
|
|
11
10
|
// ------------------------------------------------------------------
|
|
12
|
-
// prettier-ignore
|
|
13
11
|
import { IsSchema } from '../../type/guard/type.mjs';
|
|
14
12
|
// ------------------------------------------------------------------
|
|
15
13
|
// ValueOrDefault
|
|
@@ -30,11 +30,11 @@ export declare const Edit: import("../../type/union/union-type.mjs").TUnion<[imp
|
|
|
30
30
|
type: import("../../type/literal/literal.mjs").TLiteral<"delete">;
|
|
31
31
|
path: import("../../type/string/string.mjs").TString;
|
|
32
32
|
}>]>;
|
|
33
|
-
export declare class
|
|
34
|
-
readonly
|
|
35
|
-
constructor(
|
|
33
|
+
export declare class ValueDeltaError extends TypeBoxError {
|
|
34
|
+
readonly value: unknown;
|
|
35
|
+
constructor(value: unknown, message: string);
|
|
36
36
|
}
|
|
37
|
-
export declare class
|
|
37
|
+
export declare class ValueDeltaSymbolError extends ValueDeltaError {
|
|
38
38
|
readonly value: unknown;
|
|
39
39
|
constructor(value: unknown);
|
|
40
40
|
}
|
|
@@ -25,17 +25,17 @@ export const Edit = CreateUnion([Insert, Update, Delete]);
|
|
|
25
25
|
// ------------------------------------------------------------------
|
|
26
26
|
// Errors
|
|
27
27
|
// ------------------------------------------------------------------
|
|
28
|
-
export class
|
|
29
|
-
|
|
30
|
-
constructor(
|
|
31
|
-
super(
|
|
32
|
-
this.
|
|
28
|
+
export class ValueDeltaError extends TypeBoxError {
|
|
29
|
+
value;
|
|
30
|
+
constructor(value, message) {
|
|
31
|
+
super(message);
|
|
32
|
+
this.value = value;
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
|
-
export class
|
|
35
|
+
export class ValueDeltaSymbolError extends ValueDeltaError {
|
|
36
36
|
value;
|
|
37
37
|
constructor(value) {
|
|
38
|
-
super('
|
|
38
|
+
super(value, 'Cannot diff objects with symbol keys');
|
|
39
39
|
this.value = value;
|
|
40
40
|
}
|
|
41
41
|
}
|
|
@@ -61,7 +61,7 @@ function* ObjectType(path, current, next) {
|
|
|
61
61
|
const nextKeys = [...globalThis.Object.keys(next), ...globalThis.Object.getOwnPropertySymbols(next)];
|
|
62
62
|
for (const key of currentKeys) {
|
|
63
63
|
if (IsSymbol(key))
|
|
64
|
-
throw new
|
|
64
|
+
throw new ValueDeltaSymbolError(key);
|
|
65
65
|
if (IsUndefined(next[key]) && nextKeys.includes(key))
|
|
66
66
|
yield CreateUpdate(`${path}/${globalThis.String(key)}`, undefined);
|
|
67
67
|
}
|
|
@@ -69,18 +69,18 @@ function* ObjectType(path, current, next) {
|
|
|
69
69
|
if (IsUndefined(current[key]) || IsUndefined(next[key]))
|
|
70
70
|
continue;
|
|
71
71
|
if (IsSymbol(key))
|
|
72
|
-
throw new
|
|
72
|
+
throw new ValueDeltaSymbolError(key);
|
|
73
73
|
yield* Visit(`${path}/${globalThis.String(key)}`, current[key], next[key]);
|
|
74
74
|
}
|
|
75
75
|
for (const key of nextKeys) {
|
|
76
76
|
if (IsSymbol(key))
|
|
77
|
-
throw new
|
|
77
|
+
throw new ValueDeltaSymbolError(key);
|
|
78
78
|
if (IsUndefined(current[key]))
|
|
79
79
|
yield CreateInsert(`${path}/${globalThis.String(key)}`, next[key]);
|
|
80
80
|
}
|
|
81
81
|
for (const key of currentKeys.reverse()) {
|
|
82
82
|
if (IsSymbol(key))
|
|
83
|
-
throw new
|
|
83
|
+
throw new ValueDeltaSymbolError(key);
|
|
84
84
|
if (IsUndefined(next[key]) && !nextKeys.includes(key))
|
|
85
85
|
yield CreateDelete(`${path}/${globalThis.String(key)}`);
|
|
86
86
|
}
|
|
@@ -123,7 +123,7 @@ function* Visit(path, current, next) {
|
|
|
123
123
|
return yield* TypedArrayType(path, current, next);
|
|
124
124
|
if (IsValueType(current))
|
|
125
125
|
return yield* ValueType(path, current, next);
|
|
126
|
-
throw new
|
|
126
|
+
throw new ValueDeltaError(current, 'Unable to create diff edits for unknown value');
|
|
127
127
|
}
|
|
128
128
|
// ------------------------------------------------------------------
|
|
129
129
|
// Diff
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
export { ValueError, ValueErrorType, ValueErrorIterator } from '../errors/index.mjs';
|
|
2
|
-
export { Cast,
|
|
2
|
+
export { Cast, ValueCastError } from './cast/index.mjs';
|
|
3
3
|
export { Check } from './check/index.mjs';
|
|
4
4
|
export { Clean } from './clean/index.mjs';
|
|
5
5
|
export { Clone } from './clone/index.mjs';
|
|
6
|
-
export { Convert
|
|
7
|
-
export { Create,
|
|
6
|
+
export { Convert } from './convert/index.mjs';
|
|
7
|
+
export { Create, ValueCreateError } from './create/index.mjs';
|
|
8
8
|
export { Default } from './default/index.mjs';
|
|
9
|
-
export { Diff, Patch, Edit, Delete, Insert, Update,
|
|
9
|
+
export { Diff, Patch, Edit, Delete, Insert, Update, ValueDeltaError } from './delta/index.mjs';
|
|
10
10
|
export { Equal } from './equal/index.mjs';
|
|
11
11
|
export { Hash, ValueHashError } from './hash/index.mjs';
|
|
12
|
-
export { Mutate, type Mutable
|
|
12
|
+
export { Mutate, ValueMutateError, type Mutable } from './mutate/index.mjs';
|
|
13
13
|
export { ValuePointer } from './pointer/index.mjs';
|
|
14
|
-
export { TransformDecode
|
|
15
|
-
export { ArrayType, HasPropertyKey, IsArray, IsAsyncIterator, IsBigInt, IsBoolean, IsDate, IsFunction, IsInteger, IsIterator, IsNull, IsNumber, IsObject, IsPlainObject, IsPromise, IsString, IsSymbol, IsTypedArray, IsUint8Array, IsUndefined, IsValueType, ObjectType, TypedArrayType, ValueType, } from './guard/index.mjs';
|
|
14
|
+
export { TransformDecode, TransformEncode, HasTransform, TransformDecodeCheckError, TransformDecodeError, TransformEncodeCheckError, TransformEncodeError } from './transform/index.mjs';
|
|
15
|
+
export { ArrayType, HasPropertyKey, IsArray, IsAsyncIterator, IsBigInt, IsBoolean, IsDate, IsFunction, IsInteger, IsIterator, IsNull, IsNumber, IsObject, IsPlainObject, IsPromise, IsString, IsSymbol, IsTypedArray, IsUint8Array, IsUndefined, IsValueType, type ObjectType, type TypedArrayType, type ValueType, } from './guard/index.mjs';
|
|
16
16
|
export { Value } from './value/index.mjs';
|
|
@@ -5,19 +5,19 @@ export { ValueErrorType, ValueErrorIterator } from '../errors/index.mjs';
|
|
|
5
5
|
// ------------------------------------------------------------------
|
|
6
6
|
// Value Operators
|
|
7
7
|
// ------------------------------------------------------------------
|
|
8
|
-
export { Cast,
|
|
8
|
+
export { Cast, ValueCastError } from './cast/index.mjs';
|
|
9
9
|
export { Check } from './check/index.mjs';
|
|
10
10
|
export { Clean } from './clean/index.mjs';
|
|
11
11
|
export { Clone } from './clone/index.mjs';
|
|
12
|
-
export { Convert
|
|
13
|
-
export { Create,
|
|
12
|
+
export { Convert } from './convert/index.mjs';
|
|
13
|
+
export { Create, ValueCreateError } from './create/index.mjs';
|
|
14
14
|
export { Default } from './default/index.mjs';
|
|
15
|
-
export { Diff, Patch, Edit, Delete, Insert, Update,
|
|
15
|
+
export { Diff, Patch, Edit, Delete, Insert, Update, ValueDeltaError } from './delta/index.mjs';
|
|
16
16
|
export { Equal } from './equal/index.mjs';
|
|
17
17
|
export { Hash, ValueHashError } from './hash/index.mjs';
|
|
18
|
-
export { Mutate,
|
|
18
|
+
export { Mutate, ValueMutateError } from './mutate/index.mjs';
|
|
19
19
|
export { ValuePointer } from './pointer/index.mjs';
|
|
20
|
-
export { TransformDecode
|
|
20
|
+
export { TransformDecode, TransformEncode, HasTransform, TransformDecodeCheckError, TransformDecodeError, TransformEncodeCheckError, TransformEncodeError } from './transform/index.mjs';
|
|
21
21
|
// ------------------------------------------------------------------
|
|
22
22
|
// Value Guards
|
|
23
23
|
// ------------------------------------------------------------------
|