@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
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import { TypeBoxError } from '../../type/error/index.mjs';
|
|
2
|
-
export declare class
|
|
3
|
-
constructor();
|
|
4
|
-
}
|
|
5
|
-
export declare class ValueMutateInvalidRootMutationError extends TypeBoxError {
|
|
6
|
-
constructor();
|
|
2
|
+
export declare class ValueMutateError extends TypeBoxError {
|
|
3
|
+
constructor(message: string);
|
|
7
4
|
}
|
|
8
5
|
export type Mutable = {
|
|
9
6
|
[key: string]: unknown;
|
|
@@ -5,14 +5,9 @@ import { TypeBoxError } from '../../type/error/index.mjs';
|
|
|
5
5
|
// ------------------------------------------------------------------
|
|
6
6
|
// Errors
|
|
7
7
|
// ------------------------------------------------------------------
|
|
8
|
-
export class
|
|
9
|
-
constructor() {
|
|
10
|
-
super(
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
export class ValueMutateInvalidRootMutationError extends TypeBoxError {
|
|
14
|
-
constructor() {
|
|
15
|
-
super('Only object and array types can be mutated at the root level');
|
|
8
|
+
export class ValueMutateError extends TypeBoxError {
|
|
9
|
+
constructor(message) {
|
|
10
|
+
super(message);
|
|
16
11
|
}
|
|
17
12
|
}
|
|
18
13
|
function ObjectType(root, path, current, next) {
|
|
@@ -90,8 +85,8 @@ function IsMismatchedValue(current, next) {
|
|
|
90
85
|
/** `[Mutable]` Performs a deep mutable value assignment while retaining internal references */
|
|
91
86
|
export function Mutate(current, next) {
|
|
92
87
|
if (IsNonMutableValue(current) || IsNonMutableValue(next))
|
|
93
|
-
throw new
|
|
88
|
+
throw new ValueMutateError('Only object and array types can be mutated at the root level');
|
|
94
89
|
if (IsMismatchedValue(current, next))
|
|
95
|
-
throw new
|
|
90
|
+
throw new ValueMutateError('Cannot assign due type mismatch of assignable values');
|
|
96
91
|
Visit(current, '', current, next);
|
|
97
92
|
}
|
|
@@ -384,6 +384,11 @@ var TypeCompiler;
|
|
|
384
384
|
return yield `${CreateFunctionName(schema.$ref)}(${value})`;
|
|
385
385
|
yield* Visit(target, references, value);
|
|
386
386
|
}
|
|
387
|
+
function* FromRegExp(schema, references, value) {
|
|
388
|
+
const variable = CreateVariable(`${new RegExp(schema.source, schema.flags)};`);
|
|
389
|
+
yield `(typeof ${value} === 'string')`;
|
|
390
|
+
yield `${variable}.test(${value})`;
|
|
391
|
+
}
|
|
387
392
|
function* FromString(schema, references, value) {
|
|
388
393
|
yield `(typeof ${value} === 'string')`;
|
|
389
394
|
if ((0, index_11.IsNumber)(schema.maxLength))
|
|
@@ -503,6 +508,8 @@ var TypeCompiler;
|
|
|
503
508
|
return yield* FromRecord(schema_, references_, value);
|
|
504
509
|
case 'Ref':
|
|
505
510
|
return yield* FromRef(schema_, references_, value);
|
|
511
|
+
case 'RegExp':
|
|
512
|
+
return yield* FromRegExp(schema_, references_, value);
|
|
506
513
|
case 'String':
|
|
507
514
|
return yield* FromString(schema_, references_, value);
|
|
508
515
|
case 'Symbol':
|
|
@@ -49,21 +49,22 @@ export declare enum ValueErrorType {
|
|
|
49
49
|
ObjectRequiredProperty = 45,
|
|
50
50
|
Object = 46,
|
|
51
51
|
Promise = 47,
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
52
|
+
RegExp = 48,
|
|
53
|
+
StringFormatUnknown = 49,
|
|
54
|
+
StringFormat = 50,
|
|
55
|
+
StringMaxLength = 51,
|
|
56
|
+
StringMinLength = 52,
|
|
57
|
+
StringPattern = 53,
|
|
58
|
+
String = 54,
|
|
59
|
+
Symbol = 55,
|
|
60
|
+
TupleLength = 56,
|
|
61
|
+
Tuple = 57,
|
|
62
|
+
Uint8ArrayMaxByteLength = 58,
|
|
63
|
+
Uint8ArrayMinByteLength = 59,
|
|
64
|
+
Uint8Array = 60,
|
|
65
|
+
Undefined = 61,
|
|
66
|
+
Union = 62,
|
|
67
|
+
Void = 63
|
|
67
68
|
}
|
|
68
69
|
export interface ValueError {
|
|
69
70
|
type: ValueErrorType;
|
|
@@ -70,21 +70,22 @@ var ValueErrorType;
|
|
|
70
70
|
ValueErrorType[ValueErrorType["ObjectRequiredProperty"] = 45] = "ObjectRequiredProperty";
|
|
71
71
|
ValueErrorType[ValueErrorType["Object"] = 46] = "Object";
|
|
72
72
|
ValueErrorType[ValueErrorType["Promise"] = 47] = "Promise";
|
|
73
|
-
ValueErrorType[ValueErrorType["
|
|
74
|
-
ValueErrorType[ValueErrorType["
|
|
75
|
-
ValueErrorType[ValueErrorType["
|
|
76
|
-
ValueErrorType[ValueErrorType["
|
|
77
|
-
ValueErrorType[ValueErrorType["
|
|
78
|
-
ValueErrorType[ValueErrorType["
|
|
79
|
-
ValueErrorType[ValueErrorType["
|
|
80
|
-
ValueErrorType[ValueErrorType["
|
|
81
|
-
ValueErrorType[ValueErrorType["
|
|
82
|
-
ValueErrorType[ValueErrorType["
|
|
83
|
-
ValueErrorType[ValueErrorType["
|
|
84
|
-
ValueErrorType[ValueErrorType["
|
|
85
|
-
ValueErrorType[ValueErrorType["
|
|
86
|
-
ValueErrorType[ValueErrorType["
|
|
87
|
-
ValueErrorType[ValueErrorType["
|
|
73
|
+
ValueErrorType[ValueErrorType["RegExp"] = 48] = "RegExp";
|
|
74
|
+
ValueErrorType[ValueErrorType["StringFormatUnknown"] = 49] = "StringFormatUnknown";
|
|
75
|
+
ValueErrorType[ValueErrorType["StringFormat"] = 50] = "StringFormat";
|
|
76
|
+
ValueErrorType[ValueErrorType["StringMaxLength"] = 51] = "StringMaxLength";
|
|
77
|
+
ValueErrorType[ValueErrorType["StringMinLength"] = 52] = "StringMinLength";
|
|
78
|
+
ValueErrorType[ValueErrorType["StringPattern"] = 53] = "StringPattern";
|
|
79
|
+
ValueErrorType[ValueErrorType["String"] = 54] = "String";
|
|
80
|
+
ValueErrorType[ValueErrorType["Symbol"] = 55] = "Symbol";
|
|
81
|
+
ValueErrorType[ValueErrorType["TupleLength"] = 56] = "TupleLength";
|
|
82
|
+
ValueErrorType[ValueErrorType["Tuple"] = 57] = "Tuple";
|
|
83
|
+
ValueErrorType[ValueErrorType["Uint8ArrayMaxByteLength"] = 58] = "Uint8ArrayMaxByteLength";
|
|
84
|
+
ValueErrorType[ValueErrorType["Uint8ArrayMinByteLength"] = 59] = "Uint8ArrayMinByteLength";
|
|
85
|
+
ValueErrorType[ValueErrorType["Uint8Array"] = 60] = "Uint8Array";
|
|
86
|
+
ValueErrorType[ValueErrorType["Undefined"] = 61] = "Undefined";
|
|
87
|
+
ValueErrorType[ValueErrorType["Union"] = 62] = "Union";
|
|
88
|
+
ValueErrorType[ValueErrorType["Void"] = 63] = "Void";
|
|
88
89
|
})(ValueErrorType || (exports.ValueErrorType = ValueErrorType = {}));
|
|
89
90
|
// ------------------------------------------------------------------
|
|
90
91
|
// ValueErrors
|
|
@@ -395,6 +396,12 @@ function* FromRecord(schema, references, path, value) {
|
|
|
395
396
|
function* FromRef(schema, references, path, value) {
|
|
396
397
|
yield* Visit((0, index_5.Deref)(schema, references), references, path, value);
|
|
397
398
|
}
|
|
399
|
+
function* FromRegExp(schema, references, path, value) {
|
|
400
|
+
const regex = new RegExp(schema.source, schema.flags);
|
|
401
|
+
if (!regex.test(value)) {
|
|
402
|
+
return yield Create(ValueErrorType.RegExp, schema, path, value);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
398
405
|
function* FromString(schema, references, path, value) {
|
|
399
406
|
if (!(0, index_9.IsString)(value))
|
|
400
407
|
return yield Create(ValueErrorType.String, schema, path, value);
|
|
@@ -533,6 +540,8 @@ function* Visit(schema, references, path, value) {
|
|
|
533
540
|
return yield* FromRecord(schema_, references_, path, value);
|
|
534
541
|
case 'Ref':
|
|
535
542
|
return yield* FromRef(schema_, references_, path, value);
|
|
543
|
+
case 'RegExp':
|
|
544
|
+
return yield* FromRegExp(schema_, references_, path, value);
|
|
536
545
|
case 'String':
|
|
537
546
|
return yield* FromString(schema_, references_, path, value);
|
|
538
547
|
case 'Symbol':
|
|
@@ -101,6 +101,8 @@ function DefaultErrorFunction(error) {
|
|
|
101
101
|
return 'Required property';
|
|
102
102
|
case errors_1.ValueErrorType.Promise:
|
|
103
103
|
return 'Expected Promise';
|
|
104
|
+
case errors_1.ValueErrorType.RegExp:
|
|
105
|
+
return 'Expected string to match regular expression';
|
|
104
106
|
case errors_1.ValueErrorType.StringFormatUnknown:
|
|
105
107
|
return `Unknown format '${error.schema.format}'`;
|
|
106
108
|
case errors_1.ValueErrorType.StringFormat:
|
|
@@ -12,6 +12,9 @@ function DateType(value) {
|
|
|
12
12
|
function Uint8ArrayType(value) {
|
|
13
13
|
return new Uint8Array(value);
|
|
14
14
|
}
|
|
15
|
+
function RegExpType(value) {
|
|
16
|
+
return new RegExp(value.source, value.flags);
|
|
17
|
+
}
|
|
15
18
|
function ObjectType(value) {
|
|
16
19
|
const clonedProperties = Object.getOwnPropertyNames(value).reduce((acc, key) => ({ ...acc, [key]: Visit(value[key]) }), {});
|
|
17
20
|
const clonedSymbols = Object.getOwnPropertySymbols(value).reduce((acc, key) => ({ ...acc, [key]: Visit(value[key]) }), {});
|
|
@@ -22,8 +25,9 @@ function Visit(value) {
|
|
|
22
25
|
return (ValueGuard.IsArray(value) ? ArrayType(value) :
|
|
23
26
|
ValueGuard.IsDate(value) ? DateType(value) :
|
|
24
27
|
ValueGuard.IsUint8Array(value) ? Uint8ArrayType(value) :
|
|
25
|
-
ValueGuard.
|
|
26
|
-
value)
|
|
28
|
+
ValueGuard.IsRegExp(value) ? RegExpType(value) :
|
|
29
|
+
ValueGuard.IsObject(value) ? ObjectType(value) :
|
|
30
|
+
value);
|
|
27
31
|
}
|
|
28
32
|
/** Clones a value */
|
|
29
33
|
function Clone(value) {
|