@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
|
@@ -384,6 +384,11 @@ export 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 (IsNumber(schema.maxLength))
|
|
@@ -503,6 +508,8 @@ export 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;
|
|
@@ -66,21 +66,22 @@ export var ValueErrorType;
|
|
|
66
66
|
ValueErrorType[ValueErrorType["ObjectRequiredProperty"] = 45] = "ObjectRequiredProperty";
|
|
67
67
|
ValueErrorType[ValueErrorType["Object"] = 46] = "Object";
|
|
68
68
|
ValueErrorType[ValueErrorType["Promise"] = 47] = "Promise";
|
|
69
|
-
ValueErrorType[ValueErrorType["
|
|
70
|
-
ValueErrorType[ValueErrorType["
|
|
71
|
-
ValueErrorType[ValueErrorType["
|
|
72
|
-
ValueErrorType[ValueErrorType["
|
|
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["
|
|
69
|
+
ValueErrorType[ValueErrorType["RegExp"] = 48] = "RegExp";
|
|
70
|
+
ValueErrorType[ValueErrorType["StringFormatUnknown"] = 49] = "StringFormatUnknown";
|
|
71
|
+
ValueErrorType[ValueErrorType["StringFormat"] = 50] = "StringFormat";
|
|
72
|
+
ValueErrorType[ValueErrorType["StringMaxLength"] = 51] = "StringMaxLength";
|
|
73
|
+
ValueErrorType[ValueErrorType["StringMinLength"] = 52] = "StringMinLength";
|
|
74
|
+
ValueErrorType[ValueErrorType["StringPattern"] = 53] = "StringPattern";
|
|
75
|
+
ValueErrorType[ValueErrorType["String"] = 54] = "String";
|
|
76
|
+
ValueErrorType[ValueErrorType["Symbol"] = 55] = "Symbol";
|
|
77
|
+
ValueErrorType[ValueErrorType["TupleLength"] = 56] = "TupleLength";
|
|
78
|
+
ValueErrorType[ValueErrorType["Tuple"] = 57] = "Tuple";
|
|
79
|
+
ValueErrorType[ValueErrorType["Uint8ArrayMaxByteLength"] = 58] = "Uint8ArrayMaxByteLength";
|
|
80
|
+
ValueErrorType[ValueErrorType["Uint8ArrayMinByteLength"] = 59] = "Uint8ArrayMinByteLength";
|
|
81
|
+
ValueErrorType[ValueErrorType["Uint8Array"] = 60] = "Uint8Array";
|
|
82
|
+
ValueErrorType[ValueErrorType["Undefined"] = 61] = "Undefined";
|
|
83
|
+
ValueErrorType[ValueErrorType["Union"] = 62] = "Union";
|
|
84
|
+
ValueErrorType[ValueErrorType["Void"] = 63] = "Void";
|
|
84
85
|
})(ValueErrorType || (ValueErrorType = {}));
|
|
85
86
|
// ------------------------------------------------------------------
|
|
86
87
|
// ValueErrors
|
|
@@ -391,6 +392,12 @@ function* FromRecord(schema, references, path, value) {
|
|
|
391
392
|
function* FromRef(schema, references, path, value) {
|
|
392
393
|
yield* Visit(Deref(schema, references), references, path, value);
|
|
393
394
|
}
|
|
395
|
+
function* FromRegExp(schema, references, path, value) {
|
|
396
|
+
const regex = new RegExp(schema.source, schema.flags);
|
|
397
|
+
if (!regex.test(value)) {
|
|
398
|
+
return yield Create(ValueErrorType.RegExp, schema, path, value);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
394
401
|
function* FromString(schema, references, path, value) {
|
|
395
402
|
if (!IsString(value))
|
|
396
403
|
return yield Create(ValueErrorType.String, schema, path, value);
|
|
@@ -529,6 +536,8 @@ function* Visit(schema, references, path, value) {
|
|
|
529
536
|
return yield* FromRecord(schema_, references_, path, value);
|
|
530
537
|
case 'Ref':
|
|
531
538
|
return yield* FromRef(schema_, references_, path, value);
|
|
539
|
+
case 'RegExp':
|
|
540
|
+
return yield* FromRegExp(schema_, references_, path, value);
|
|
532
541
|
case 'String':
|
|
533
542
|
return yield* FromString(schema_, references_, path, value);
|
|
534
543
|
case 'Symbol':
|
|
@@ -97,6 +97,8 @@ export function DefaultErrorFunction(error) {
|
|
|
97
97
|
return 'Required property';
|
|
98
98
|
case ValueErrorType.Promise:
|
|
99
99
|
return 'Expected Promise';
|
|
100
|
+
case ValueErrorType.RegExp:
|
|
101
|
+
return 'Expected string to match regular expression';
|
|
100
102
|
case ValueErrorType.StringFormatUnknown:
|
|
101
103
|
return `Unknown format '${error.schema.format}'`;
|
|
102
104
|
case ValueErrorType.StringFormat:
|
|
@@ -8,6 +8,9 @@ function DateType(value) {
|
|
|
8
8
|
function Uint8ArrayType(value) {
|
|
9
9
|
return new Uint8Array(value);
|
|
10
10
|
}
|
|
11
|
+
function RegExpType(value) {
|
|
12
|
+
return new RegExp(value.source, value.flags);
|
|
13
|
+
}
|
|
11
14
|
function ObjectType(value) {
|
|
12
15
|
const clonedProperties = Object.getOwnPropertyNames(value).reduce((acc, key) => ({ ...acc, [key]: Visit(value[key]) }), {});
|
|
13
16
|
const clonedSymbols = Object.getOwnPropertySymbols(value).reduce((acc, key) => ({ ...acc, [key]: Visit(value[key]) }), {});
|
|
@@ -18,8 +21,9 @@ function Visit(value) {
|
|
|
18
21
|
return (ValueGuard.IsArray(value) ? ArrayType(value) :
|
|
19
22
|
ValueGuard.IsDate(value) ? DateType(value) :
|
|
20
23
|
ValueGuard.IsUint8Array(value) ? Uint8ArrayType(value) :
|
|
21
|
-
ValueGuard.
|
|
22
|
-
value)
|
|
24
|
+
ValueGuard.IsRegExp(value) ? RegExpType(value) :
|
|
25
|
+
ValueGuard.IsObject(value) ? ObjectType(value) :
|
|
26
|
+
value);
|
|
23
27
|
}
|
|
24
28
|
/** Clones a value */
|
|
25
29
|
export function Clone(value) {
|