@sinclair/typebox 0.24.30 → 0.24.31

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.
@@ -2,7 +2,7 @@ export declare type FormatValidationFunction = (value: string) => boolean;
2
2
  /** Shared string formats used by the TypeCompiler and Value modules */
3
3
  export declare namespace Format {
4
4
  /** Clears all formats */
5
- function Clear(format: string): void;
5
+ function Clear(): void;
6
6
  /** Returns true if the string format exists */
7
7
  function Has(format: string): boolean;
8
8
  /** Sets a string format validation function */
package/format/format.js CHANGED
@@ -33,7 +33,7 @@ var Format;
33
33
  (function (Format) {
34
34
  const formats = new Map();
35
35
  /** Clears all formats */
36
- function Clear(format) {
36
+ function Clear() {
37
37
  return formats.clear();
38
38
  }
39
39
  Format.Clear = Clear;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sinclair/typebox",
3
- "version": "0.24.30",
3
+ "version": "0.24.31",
4
4
  "description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
5
5
  "keywords": [
6
6
  "typescript",
package/readme.md CHANGED
@@ -39,6 +39,8 @@ 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
+
42
44
  ```
43
45
 
44
46
  <a name="Overview"></a>
@@ -146,6 +148,8 @@ function receive(value: T) { // ... as a Type
146
148
  // ok...
147
149
  }
148
150
  }
151
+
152
+
149
153
  ```
150
154
 
151
155
  ## Types
@@ -340,6 +344,8 @@ The following table outlines the TypeBox mappings between TypeScript and JSON sc
340
344
  │ │ │ } │
341
345
  │ │ │ │
342
346
  └────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
347
+
348
+
343
349
  ```
344
350
 
345
351
  ## Modifiers
@@ -382,6 +388,8 @@ TypeBox provides modifiers that can be applied to an objects properties. This al
382
388
  │ │ │ } │
383
389
  │ │ │ │
384
390
  └────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
391
+
392
+
385
393
  ```
386
394
 
387
395
  ## Options
@@ -397,6 +405,8 @@ const T = Type.Number({ multipleOf: 2 })
397
405
 
398
406
  // array must have at least 5 integer values
399
407
  const T = Type.Array(Type.Integer(), { minItems: 5 })
408
+
409
+
400
410
  ```
401
411
 
402
412
  ## Extended Types
@@ -459,6 +469,8 @@ In addition to JSON schema types, TypeBox provides several extended types that a
459
469
  │ │ │ } │
460
470
  │ │ │ │
461
471
  └────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
472
+
473
+
462
474
  ```
463
475
 
464
476
  ## Reference Types
@@ -474,6 +486,8 @@ const T = Type.String({ $id: 'T' }) // const T = {
474
486
  const R = Type.Ref(T) // const R = {
475
487
  // $ref: 'T'
476
488
  // }
489
+
490
+
477
491
  ```
478
492
 
479
493
  ## Recursive Types
@@ -511,6 +525,8 @@ function test(node: Node) {
511
525
  .nodes[0].nodes[0]
512
526
  .id
513
527
  }
528
+
529
+
514
530
  ```
515
531
 
516
532
  ## Generic Types
@@ -541,6 +557,8 @@ const U = Nullable(Type.Number()) // const U = {
541
557
  // }
542
558
 
543
559
  type U = Static<typeof U> // type U = number | null
560
+
561
+
544
562
  ```
545
563
 
546
564
  ## Unsafe Types
@@ -553,6 +571,8 @@ const T = Type.Unsafe<string>({ type: 'number' }) // const T = {
553
571
  // }
554
572
 
555
573
  type T = Static<typeof T> // type T = string
574
+
575
+
556
576
  ```
557
577
 
558
578
  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.
@@ -593,6 +613,8 @@ const T = StringEnum(['A', 'B', 'C']) // const T = {
593
613
  // }
594
614
 
595
615
  type T = Static<typeof T> // type T = 'A' | 'B' | 'C'
616
+
617
+
596
618
  ```
597
619
 
598
620
  ## Conditional Types
@@ -644,6 +666,8 @@ The following table shows the TypeBox mappings between TypeScript and JSON schem
644
666
  │ ) │ │ │
645
667
  │ │ │ │
646
668
  └────────────────────────────────┴─────────────────────────────┴────────────────────────────────┘
669
+
670
+
647
671
  ```
648
672
 
649
673
  ## Values
@@ -705,6 +729,8 @@ if(TypeGuard.TString(T)) {
705
729
 
706
730
  // T is TString
707
731
  }
732
+
733
+
708
734
  ```
709
735
 
710
736
  ## Strict
@@ -732,6 +758,8 @@ const U = Type.Strict(T) // const U = {
732
758
  // }
733
759
  // }
734
760
  // }
761
+
762
+
735
763
  ```
736
764
 
737
765
  ## Validation
@@ -795,6 +823,8 @@ const T = Type.Object({
795
823
  //--------------------------------------------------------------------------------------------
796
824
 
797
825
  const R = ajv.validate(T, { x: 1, y: 2, z: 3 }) // const R = true
826
+
827
+
798
828
  ```
799
829
 
800
830
  Please refer to the official Ajv [documentation](https://ajv.js.org/guide/getting-started.html) for additional information on using Ajv.
@@ -819,6 +849,8 @@ const C = TypeCompiler.Compile(Type.Object({ // const C: TypeCheck<TObje
819
849
  })) // }>>
820
850
 
821
851
  const R = C.Check({ x: 1, y: 2, z: 3 }) // const R = true
852
+
853
+
822
854
  ```
823
855
 
824
856
  Validation errors can be read with the `Errors(...)` function.
@@ -860,22 +892,26 @@ console.log(C.Code()) // return function check(va
860
892
  // (typeof value === 'string')
861
893
  // )
862
894
  // }
895
+
896
+
863
897
  ```
864
898
 
865
899
  ## Formats
866
900
 
867
- Use the `Format` module to define custom string formats.
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.
868
904
 
869
905
  ```typescript
870
906
  import { Format } from '@sinclair/typebox/format'
871
907
  ```
872
908
 
873
- Formats are shared between `Value` and the `TypeCompiler` modules.
909
+ The following creates a custom format to validate Mongo `ObjectId` strings
874
910
 
875
911
  ```typescript
876
912
  //--------------------------------------------------------------------------------------------
877
913
  //
878
- // Use Format.Set(...) to define a format
914
+ // Format.Set(...) to create the format
879
915
  //
880
916
  //--------------------------------------------------------------------------------------------
881
917
 
@@ -883,15 +919,19 @@ Format.Set('ObjectId', value => /^[0-9a-fA-F]{24}$/.test(value))
883
919
 
884
920
  //--------------------------------------------------------------------------------------------
885
921
  //
886
- // The format is now available to TypeCompiler and Value modules
922
+ // The format can now be used by TypeCompiler and Value modules
887
923
  //
888
924
  //--------------------------------------------------------------------------------------------
889
925
 
890
926
  const T = Type.String({ format: 'ObjectId' })
891
927
 
892
- const R1 = TypeCompiler.Compile(T).Check('507f1f77bcf86cd799439011')
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
+
893
934
 
894
- const R2 = Value.Check(T, '507f1f77bcf86cd799439011')
895
935
  ```
896
936
 
897
937
  ## Benchmark
package/value/cast.d.ts CHANGED
@@ -3,6 +3,9 @@ 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
+ }
6
9
  export declare namespace ValueCast {
7
10
  function Visit(schema: Types.TSchema, references: Types.TSchema[], value: any): any;
8
11
  function Cast<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: any): Types.Static<T>;
package/value/cast.js CHANGED
@@ -80,6 +80,45 @@ class ValueCastUnknownTypeError extends Error {
80
80
  exports.ValueCastUnknownTypeError = ValueCastUnknownTypeError;
81
81
  var ValueCast;
82
82
  (function (ValueCast) {
83
+ // -----------------------------------------------------------
84
+ // Conversion
85
+ // -----------------------------------------------------------
86
+ function IsString(value) {
87
+ return typeof value === 'string';
88
+ }
89
+ function IsBoolean(value) {
90
+ return typeof value === 'boolean';
91
+ }
92
+ function IsNumber(value) {
93
+ return typeof value === 'number';
94
+ }
95
+ function IsBigInt(value) {
96
+ return typeof value === 'bigint';
97
+ }
98
+ function IsStringNumeric(value) {
99
+ return IsString(value) && !isNaN(value) && !isNaN(parseFloat(value));
100
+ }
101
+ function IsValueToString(value) {
102
+ return IsBigInt(value) || IsBoolean(value) || IsNumber(value);
103
+ }
104
+ function IsValueTrue(value) {
105
+ return (IsNumber(value) && value === 1) || (IsString(value) && ['true', 'TRUE', 'True'].includes(value));
106
+ }
107
+ function TryConvertString(value) {
108
+ return IsValueToString(value) ? value.toString() : value;
109
+ }
110
+ function TryConvertNumber(value) {
111
+ return IsStringNumeric(value) ? parseFloat(value) : value;
112
+ }
113
+ function TryConvertInteger(value) {
114
+ return IsStringNumeric(value) ? parseInt(value) : value;
115
+ }
116
+ function TryConvertBoolean(value) {
117
+ return IsValueTrue(value) ? true : value;
118
+ }
119
+ // -----------------------------------------------------------
120
+ // Casts
121
+ // -----------------------------------------------------------
83
122
  function Any(schema, references, value) {
84
123
  return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
85
124
  }
@@ -91,7 +130,8 @@ var ValueCast;
91
130
  return value.map((val) => Visit(schema.items, references, val));
92
131
  }
93
132
  function Boolean(schema, references, value) {
94
- return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
133
+ const conversion = TryConvertBoolean(value);
134
+ return check_1.ValueCheck.Check(schema, references, conversion) ? conversion : create_1.ValueCreate.Create(schema, references);
95
135
  }
96
136
  function Constructor(schema, references, value) {
97
137
  if (check_1.ValueCheck.Check(schema, references, value))
@@ -112,7 +152,8 @@ var ValueCast;
112
152
  return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
113
153
  }
114
154
  function Integer(schema, references, value) {
115
- return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
155
+ const conversion = TryConvertInteger(value);
156
+ return check_1.ValueCheck.Check(schema, references, conversion) ? conversion : create_1.ValueCreate.Create(schema, references);
116
157
  }
117
158
  function Literal(schema, references, value) {
118
159
  return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
@@ -121,7 +162,8 @@ var ValueCast;
121
162
  return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
122
163
  }
123
164
  function Number(schema, references, value) {
124
- return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
165
+ const conversion = TryConvertNumber(value);
166
+ return check_1.ValueCheck.Check(schema, references, conversion) ? conversion : create_1.ValueCreate.Create(schema, references);
125
167
  }
126
168
  function Object(schema, references, value) {
127
169
  if (check_1.ValueCheck.Check(schema, references, value))
@@ -169,7 +211,8 @@ var ValueCast;
169
211
  return Visit(reference, references, value);
170
212
  }
171
213
  function String(schema, references, value) {
172
- return check_1.ValueCheck.Check(schema, references, value) ? value : create_1.ValueCreate.Create(schema, references);
214
+ const conversion = TryConvertString(value);
215
+ return check_1.ValueCheck.Check(schema, references, conversion) ? conversion : create_1.ValueCreate.Create(schema, references);
173
216
  }
174
217
  function Tuple(schema, references, value) {
175
218
  if (check_1.ValueCheck.Check(schema, references, value))
package/value/value.d.ts CHANGED
@@ -10,9 +10,9 @@ export declare namespace Value {
10
10
  function Check<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): value is Types.Static<T>;
11
11
  /** Returns true if the value matches the given type. */
12
12
  function Check<T extends Types.TSchema>(schema: T, value: unknown): value is Types.Static<T>;
13
- /** Casts a value into a structure matching the given type. The result will be a new value that retains as much information of the original value as possible. */
13
+ /** Casts a value into a given type. The return value will retain as much information of the original value as possible. Cast will convert string, number and boolean values if a reasonable conversion is possible. */
14
14
  function Cast<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): Types.Static<T>;
15
- /** Casts a value into a structure matching the given type. The result will be a new value that retains as much information of the original value as possible. */
15
+ /** Casts a value into a given type. The return value will retain as much information of the original value as possible. Cast will convert string, number and boolean values if a reasonable conversion is possible. */
16
16
  function Cast<T extends Types.TSchema>(schema: T, value: unknown): Types.Static<T>;
17
17
  /** Returns an iterator for each error in this value. */
18
18
  function Errors<T extends Types.TSchema, R extends Types.TSchema[]>(schema: T, references: [...R], value: unknown): IterableIterator<ValueError>;