@sinclair/typebox 0.24.31 → 0.24.34
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/errors/errors.d.ts +10 -8
- package/errors/errors.js +22 -8
- package/package.json +1 -1
- package/readme.md +34 -74
- package/value/cast.d.ts +0 -3
- package/value/cast.js +12 -9
package/errors/errors.d.ts
CHANGED
|
@@ -31,14 +31,16 @@ export declare enum ValueErrorType {
|
|
|
31
31
|
StringMinLength = 28,
|
|
32
32
|
StringMaxLength = 29,
|
|
33
33
|
StringPattern = 30,
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
34
|
+
StringFormatUnknown = 31,
|
|
35
|
+
StringFormat = 32,
|
|
36
|
+
TupleZeroLength = 33,
|
|
37
|
+
TupleLength = 34,
|
|
38
|
+
Undefined = 35,
|
|
39
|
+
Union = 36,
|
|
40
|
+
Uint8Array = 37,
|
|
41
|
+
Uint8ArrayMinByteLength = 38,
|
|
42
|
+
Uint8ArrayMaxByteLength = 39,
|
|
43
|
+
Void = 40
|
|
42
44
|
}
|
|
43
45
|
export interface ValueError {
|
|
44
46
|
type: ValueErrorType;
|
package/errors/errors.js
CHANGED
|
@@ -29,6 +29,7 @@ THE SOFTWARE.
|
|
|
29
29
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
30
|
exports.ValueErrors = exports.ValueErrorsUnknownTypeError = exports.ValueErrorType = void 0;
|
|
31
31
|
const Types = require("../typebox");
|
|
32
|
+
const index_1 = require("../format/index");
|
|
32
33
|
// -------------------------------------------------------------------
|
|
33
34
|
// ValueErrorType
|
|
34
35
|
// -------------------------------------------------------------------
|
|
@@ -65,14 +66,16 @@ var ValueErrorType;
|
|
|
65
66
|
ValueErrorType[ValueErrorType["StringMinLength"] = 28] = "StringMinLength";
|
|
66
67
|
ValueErrorType[ValueErrorType["StringMaxLength"] = 29] = "StringMaxLength";
|
|
67
68
|
ValueErrorType[ValueErrorType["StringPattern"] = 30] = "StringPattern";
|
|
68
|
-
ValueErrorType[ValueErrorType["
|
|
69
|
-
ValueErrorType[ValueErrorType["
|
|
70
|
-
ValueErrorType[ValueErrorType["
|
|
71
|
-
ValueErrorType[ValueErrorType["
|
|
72
|
-
ValueErrorType[ValueErrorType["
|
|
73
|
-
ValueErrorType[ValueErrorType["
|
|
74
|
-
ValueErrorType[ValueErrorType["
|
|
75
|
-
ValueErrorType[ValueErrorType["
|
|
69
|
+
ValueErrorType[ValueErrorType["StringFormatUnknown"] = 31] = "StringFormatUnknown";
|
|
70
|
+
ValueErrorType[ValueErrorType["StringFormat"] = 32] = "StringFormat";
|
|
71
|
+
ValueErrorType[ValueErrorType["TupleZeroLength"] = 33] = "TupleZeroLength";
|
|
72
|
+
ValueErrorType[ValueErrorType["TupleLength"] = 34] = "TupleLength";
|
|
73
|
+
ValueErrorType[ValueErrorType["Undefined"] = 35] = "Undefined";
|
|
74
|
+
ValueErrorType[ValueErrorType["Union"] = 36] = "Union";
|
|
75
|
+
ValueErrorType[ValueErrorType["Uint8Array"] = 37] = "Uint8Array";
|
|
76
|
+
ValueErrorType[ValueErrorType["Uint8ArrayMinByteLength"] = 38] = "Uint8ArrayMinByteLength";
|
|
77
|
+
ValueErrorType[ValueErrorType["Uint8ArrayMaxByteLength"] = 39] = "Uint8ArrayMaxByteLength";
|
|
78
|
+
ValueErrorType[ValueErrorType["Void"] = 40] = "Void";
|
|
76
79
|
})(ValueErrorType = exports.ValueErrorType || (exports.ValueErrorType = {}));
|
|
77
80
|
// -------------------------------------------------------------------
|
|
78
81
|
// ValueErrors
|
|
@@ -250,6 +253,17 @@ var ValueErrors;
|
|
|
250
253
|
yield { type: ValueErrorType.StringPattern, schema, path, value, message: `Expected string to match pattern ${schema.pattern}` };
|
|
251
254
|
}
|
|
252
255
|
}
|
|
256
|
+
if (schema.format !== undefined) {
|
|
257
|
+
if (!index_1.Format.Has(schema.format)) {
|
|
258
|
+
yield { type: ValueErrorType.StringFormatUnknown, schema, path, value, message: `Unknown string format '${schema.format}'` };
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
const format = index_1.Format.Get(schema.format);
|
|
262
|
+
if (!format(value)) {
|
|
263
|
+
yield { type: ValueErrorType.StringFormat, schema, path, value, message: `Expected string to match format '${schema.format}'` };
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
253
267
|
}
|
|
254
268
|
function* Tuple(schema, references, path, value) {
|
|
255
269
|
if (!globalThis.Array.isArray(value)) {
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -39,8 +39,6 @@ import { Static, Type } from '@sinclair/typebox'
|
|
|
39
39
|
const T = Type.String() // const T = { type: 'string' }
|
|
40
40
|
|
|
41
41
|
type T = Static<typeof T> // type T = string
|
|
42
|
-
|
|
43
|
-
|
|
44
42
|
```
|
|
45
43
|
|
|
46
44
|
<a name="Overview"></a>
|
|
@@ -67,11 +65,11 @@ License MIT
|
|
|
67
65
|
- [Unsafe Types](#unsafe-types)
|
|
68
66
|
- [Conditional Types](#conditional-types)
|
|
69
67
|
- [Values](#values)
|
|
68
|
+
- [Formats](#formats)
|
|
70
69
|
- [Guards](#guards)
|
|
71
70
|
- [Strict](#strict)
|
|
72
71
|
- [Validation](#validation)
|
|
73
72
|
- [Compiler](#compiler)
|
|
74
|
-
- [Formats](#formats)
|
|
75
73
|
- [Benchmark](#benchmark)
|
|
76
74
|
- [Contribute](#contribute)
|
|
77
75
|
|
|
@@ -148,8 +146,6 @@ function receive(value: T) { // ... as a Type
|
|
|
148
146
|
// ok...
|
|
149
147
|
}
|
|
150
148
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
149
|
```
|
|
154
150
|
|
|
155
151
|
## Types
|
|
@@ -344,8 +340,6 @@ The following table outlines the TypeBox mappings between TypeScript and JSON sc
|
|
|
344
340
|
│ │ │ } │
|
|
345
341
|
│ │ │ │
|
|
346
342
|
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
|
|
347
|
-
|
|
348
|
-
|
|
349
343
|
```
|
|
350
344
|
|
|
351
345
|
## Modifiers
|
|
@@ -388,8 +382,6 @@ TypeBox provides modifiers that can be applied to an objects properties. This al
|
|
|
388
382
|
│ │ │ } │
|
|
389
383
|
│ │ │ │
|
|
390
384
|
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
|
|
391
|
-
|
|
392
|
-
|
|
393
385
|
```
|
|
394
386
|
|
|
395
387
|
## Options
|
|
@@ -405,8 +397,6 @@ const T = Type.Number({ multipleOf: 2 })
|
|
|
405
397
|
|
|
406
398
|
// array must have at least 5 integer values
|
|
407
399
|
const T = Type.Array(Type.Integer(), { minItems: 5 })
|
|
408
|
-
|
|
409
|
-
|
|
410
400
|
```
|
|
411
401
|
|
|
412
402
|
## Extended Types
|
|
@@ -469,8 +459,6 @@ In addition to JSON schema types, TypeBox provides several extended types that a
|
|
|
469
459
|
│ │ │ } │
|
|
470
460
|
│ │ │ │
|
|
471
461
|
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
|
|
472
|
-
|
|
473
|
-
|
|
474
462
|
```
|
|
475
463
|
|
|
476
464
|
## Reference Types
|
|
@@ -486,8 +474,6 @@ const T = Type.String({ $id: 'T' }) // const T = {
|
|
|
486
474
|
const R = Type.Ref(T) // const R = {
|
|
487
475
|
// $ref: 'T'
|
|
488
476
|
// }
|
|
489
|
-
|
|
490
|
-
|
|
491
477
|
```
|
|
492
478
|
|
|
493
479
|
## Recursive Types
|
|
@@ -525,8 +511,6 @@ function test(node: Node) {
|
|
|
525
511
|
.nodes[0].nodes[0]
|
|
526
512
|
.id
|
|
527
513
|
}
|
|
528
|
-
|
|
529
|
-
|
|
530
514
|
```
|
|
531
515
|
|
|
532
516
|
## Generic Types
|
|
@@ -557,8 +541,6 @@ const U = Nullable(Type.Number()) // const U = {
|
|
|
557
541
|
// }
|
|
558
542
|
|
|
559
543
|
type U = Static<typeof U> // type U = number | null
|
|
560
|
-
|
|
561
|
-
|
|
562
544
|
```
|
|
563
545
|
|
|
564
546
|
## Unsafe Types
|
|
@@ -571,8 +553,6 @@ const T = Type.Unsafe<string>({ type: 'number' }) // const T = {
|
|
|
571
553
|
// }
|
|
572
554
|
|
|
573
555
|
type T = Static<typeof T> // type T = string
|
|
574
|
-
|
|
575
|
-
|
|
576
556
|
```
|
|
577
557
|
|
|
578
558
|
This function can be used to create custom schemas for validators that require specific schema representations. An example of this might be OpenAPI's `nullable` and `enum` schemas which are not provided by TypeBox. The following demonstrates using `Type.Unsafe(...)` to create these types.
|
|
@@ -613,8 +593,6 @@ const T = StringEnum(['A', 'B', 'C']) // const T = {
|
|
|
613
593
|
// }
|
|
614
594
|
|
|
615
595
|
type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
|
|
616
|
-
|
|
617
|
-
|
|
618
596
|
```
|
|
619
597
|
|
|
620
598
|
## Conditional Types
|
|
@@ -666,8 +644,6 @@ The following table shows the TypeBox mappings between TypeScript and JSON schem
|
|
|
666
644
|
│ ) │ │ │
|
|
667
645
|
│ │ │ │
|
|
668
646
|
└────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
|
|
669
|
-
|
|
670
|
-
|
|
671
647
|
```
|
|
672
648
|
|
|
673
649
|
## Values
|
|
@@ -712,8 +688,40 @@ const A = Value.Cast(T, null) // const A = { x: 0, y: 0 }
|
|
|
712
688
|
const B = Value.Cast(T, { x: 1 }) // const B = { x: 1, y: 0 }
|
|
713
689
|
|
|
714
690
|
const C = Value.Cast(T, { x: 1, y: 2, z: 3 }) // const C = { x: 1, y: 2 }
|
|
691
|
+
```
|
|
692
|
+
|
|
693
|
+
## Formats
|
|
694
|
+
|
|
695
|
+
Use the format module to create user defined string formats. This module enables programmatic validation of strings that cannot be easily validated with [pattern](https://json-schema.org/understanding-json-schema/reference/regular_expressions.html) expressions. The format module is used by the Value and TypeCompiler modules only. If using Ajv, please refer to the official Ajv format documentation located [here](https://ajv.js.org/guide/formats.html).
|
|
696
|
+
|
|
697
|
+
The format module is an optional import.
|
|
698
|
+
|
|
699
|
+
```typescript
|
|
700
|
+
import { Format } from '@sinclair/typebox/format'
|
|
701
|
+
```
|
|
702
|
+
|
|
703
|
+
The following demonstrates its use.
|
|
704
|
+
|
|
705
|
+
```typescript
|
|
706
|
+
//--------------------------------------------------------------------------------------------
|
|
707
|
+
//
|
|
708
|
+
// Use Format.Set(format, func) to define custom format
|
|
709
|
+
//
|
|
710
|
+
//--------------------------------------------------------------------------------------------
|
|
711
|
+
|
|
712
|
+
Format.Set('palindrome', value => value === value.split('').reverse().join(''))
|
|
715
713
|
|
|
714
|
+
//--------------------------------------------------------------------------------------------
|
|
715
|
+
//
|
|
716
|
+
// Use the format property on string types
|
|
717
|
+
//
|
|
718
|
+
//--------------------------------------------------------------------------------------------
|
|
719
|
+
|
|
720
|
+
const T = Type.String({ format: 'palindrome' })
|
|
721
|
+
|
|
722
|
+
Value.Check(T, 'kayak') // true
|
|
716
723
|
|
|
724
|
+
Value.Check(T, 'engine') // false
|
|
717
725
|
```
|
|
718
726
|
|
|
719
727
|
## Guards
|
|
@@ -729,8 +737,6 @@ if(TypeGuard.TString(T)) {
|
|
|
729
737
|
|
|
730
738
|
// T is TString
|
|
731
739
|
}
|
|
732
|
-
|
|
733
|
-
|
|
734
740
|
```
|
|
735
741
|
|
|
736
742
|
## Strict
|
|
@@ -758,8 +764,6 @@ const U = Type.Strict(T) // const U = {
|
|
|
758
764
|
// }
|
|
759
765
|
// }
|
|
760
766
|
// }
|
|
761
|
-
|
|
762
|
-
|
|
763
767
|
```
|
|
764
768
|
|
|
765
769
|
## Validation
|
|
@@ -823,8 +827,6 @@ const T = Type.Object({
|
|
|
823
827
|
//--------------------------------------------------------------------------------------------
|
|
824
828
|
|
|
825
829
|
const R = ajv.validate(T, { x: 1, y: 2, z: 3 }) // const R = true
|
|
826
|
-
|
|
827
|
-
|
|
828
830
|
```
|
|
829
831
|
|
|
830
832
|
Please refer to the official Ajv [documentation](https://ajv.js.org/guide/getting-started.html) for additional information on using Ajv.
|
|
@@ -848,9 +850,7 @@ const C = TypeCompiler.Compile(Type.Object({ // const C: TypeCheck<TObje
|
|
|
848
850
|
z: Type.Number() // z: TNumber;
|
|
849
851
|
})) // }>>
|
|
850
852
|
|
|
851
|
-
const R = C.Check({ x: 1, y: 2, z: 3 }) // const R = true
|
|
852
|
-
|
|
853
|
-
|
|
853
|
+
const R = C.Check({ x: 1, y: 2, z: 3 }) // const R = true
|
|
854
854
|
```
|
|
855
855
|
|
|
856
856
|
Validation errors can be read with the `Errors(...)` function.
|
|
@@ -892,46 +892,6 @@ console.log(C.Code()) // return function check(va
|
|
|
892
892
|
// (typeof value === 'string')
|
|
893
893
|
// )
|
|
894
894
|
// }
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
```
|
|
898
|
-
|
|
899
|
-
## Formats
|
|
900
|
-
|
|
901
|
-
Use the `Format` module to create custom string formats. Formats provide greater flexibility over [string patterns](https://json-schema.org/understanding-json-schema/reference/regular_expressions.html), but may come at a cost of schema portability. Formats are internally shared between the TypeCompiler and Value modules. TypeBox does not provide any built in formats by default.
|
|
902
|
-
|
|
903
|
-
The format module is an optional import.
|
|
904
|
-
|
|
905
|
-
```typescript
|
|
906
|
-
import { Format } from '@sinclair/typebox/format'
|
|
907
|
-
```
|
|
908
|
-
|
|
909
|
-
The following creates a custom format to validate Mongo `ObjectId` strings
|
|
910
|
-
|
|
911
|
-
```typescript
|
|
912
|
-
//--------------------------------------------------------------------------------------------
|
|
913
|
-
//
|
|
914
|
-
// Format.Set(...) to create the format
|
|
915
|
-
//
|
|
916
|
-
//--------------------------------------------------------------------------------------------
|
|
917
|
-
|
|
918
|
-
Format.Set('ObjectId', value => /^[0-9a-fA-F]{24}$/.test(value))
|
|
919
|
-
|
|
920
|
-
//--------------------------------------------------------------------------------------------
|
|
921
|
-
//
|
|
922
|
-
// The format can now be used by TypeCompiler and Value modules
|
|
923
|
-
//
|
|
924
|
-
//--------------------------------------------------------------------------------------------
|
|
925
|
-
|
|
926
|
-
const T = Type.String({ format: 'ObjectId' })
|
|
927
|
-
|
|
928
|
-
const V = '507f1f77bcf86cd799439011'
|
|
929
|
-
|
|
930
|
-
const R1 = TypeCompiler.Compile(T).Check(V) // R1 = true
|
|
931
|
-
|
|
932
|
-
const R2 = Value.Check(T, V) // R2 = true
|
|
933
|
-
|
|
934
|
-
|
|
935
895
|
```
|
|
936
896
|
|
|
937
897
|
## Benchmark
|
package/value/cast.d.ts
CHANGED
|
@@ -3,9 +3,6 @@ export declare class ValueCastUnknownTypeError extends Error {
|
|
|
3
3
|
readonly schema: Types.TSchema;
|
|
4
4
|
constructor(schema: Types.TSchema);
|
|
5
5
|
}
|
|
6
|
-
export interface ValueCastOptions {
|
|
7
|
-
convert: boolean;
|
|
8
|
-
}
|
|
9
6
|
export declare namespace ValueCast {
|
|
10
7
|
function Visit(schema: Types.TSchema, references: Types.TSchema[], value: any): any;
|
|
11
8
|
function Cast<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: any): Types.Static<T>;
|
package/value/cast.js
CHANGED
|
@@ -81,7 +81,7 @@ exports.ValueCastUnknownTypeError = ValueCastUnknownTypeError;
|
|
|
81
81
|
var ValueCast;
|
|
82
82
|
(function (ValueCast) {
|
|
83
83
|
// -----------------------------------------------------------
|
|
84
|
-
//
|
|
84
|
+
// Convert
|
|
85
85
|
// -----------------------------------------------------------
|
|
86
86
|
function IsString(value) {
|
|
87
87
|
return typeof value === 'string';
|
|
@@ -89,12 +89,12 @@ var ValueCast;
|
|
|
89
89
|
function IsBoolean(value) {
|
|
90
90
|
return typeof value === 'boolean';
|
|
91
91
|
}
|
|
92
|
-
function IsNumber(value) {
|
|
93
|
-
return typeof value === 'number';
|
|
94
|
-
}
|
|
95
92
|
function IsBigInt(value) {
|
|
96
93
|
return typeof value === 'bigint';
|
|
97
94
|
}
|
|
95
|
+
function IsNumber(value) {
|
|
96
|
+
return typeof value === 'number';
|
|
97
|
+
}
|
|
98
98
|
function IsStringNumeric(value) {
|
|
99
99
|
return IsString(value) && !isNaN(value) && !isNaN(parseFloat(value));
|
|
100
100
|
}
|
|
@@ -102,22 +102,25 @@ var ValueCast;
|
|
|
102
102
|
return IsBigInt(value) || IsBoolean(value) || IsNumber(value);
|
|
103
103
|
}
|
|
104
104
|
function IsValueTrue(value) {
|
|
105
|
-
return (IsNumber(value) && value === 1) || (IsString(value) &&
|
|
105
|
+
return value === true || (IsNumber(value) && value === 1) || (IsBigInt(value) && value === 1n) || (IsString(value) && (value.toLowerCase() === 'true' || value === '1'));
|
|
106
|
+
}
|
|
107
|
+
function IsValueFalse(value) {
|
|
108
|
+
return value === false || (IsNumber(value) && value === 0) || (IsBigInt(value) && value === 0n) || (IsString(value) && (value.toLowerCase() === 'false' || value === '0'));
|
|
106
109
|
}
|
|
107
110
|
function TryConvertString(value) {
|
|
108
111
|
return IsValueToString(value) ? value.toString() : value;
|
|
109
112
|
}
|
|
110
113
|
function TryConvertNumber(value) {
|
|
111
|
-
return IsStringNumeric(value) ? parseFloat(value) : value;
|
|
114
|
+
return IsStringNumeric(value) ? parseFloat(value) : IsValueTrue(value) ? 1 : value;
|
|
112
115
|
}
|
|
113
116
|
function TryConvertInteger(value) {
|
|
114
|
-
return IsStringNumeric(value) ? parseInt(value) : value;
|
|
117
|
+
return IsStringNumeric(value) ? parseInt(value) : IsValueTrue(value) ? 1 : value;
|
|
115
118
|
}
|
|
116
119
|
function TryConvertBoolean(value) {
|
|
117
|
-
return IsValueTrue(value) ? true : value;
|
|
120
|
+
return IsValueTrue(value) ? true : IsValueFalse(value) ? false : value;
|
|
118
121
|
}
|
|
119
122
|
// -----------------------------------------------------------
|
|
120
|
-
//
|
|
123
|
+
// Cast
|
|
121
124
|
// -----------------------------------------------------------
|
|
122
125
|
function Any(schema, references, value) {
|
|
123
126
|
return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
|