bguard 0.0.8 → 0.1.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/README.md +507 -23
- package/lib/{CommonSchema-BIe85W_A.d.mts → CommonSchema-BbGNki0L.d.ts} +7 -4
- package/lib/{CommonSchema-BIe85W_A.d.ts → CommonSchema-CjsYE4dC.d.mts} +7 -4
- package/lib/InferType.d.mts +2 -1
- package/lib/InferType.d.ts +2 -1
- package/lib/asserts/array/index.d.mts +4 -3
- package/lib/asserts/array/index.d.ts +4 -3
- package/lib/asserts/array/index.js +6 -6
- package/lib/asserts/array/index.mjs +5 -5
- package/lib/asserts/boolean/index.d.mts +4 -3
- package/lib/asserts/boolean/index.d.ts +4 -3
- package/lib/asserts/boolean/index.js +7 -6
- package/lib/asserts/boolean/index.js.map +1 -1
- package/lib/asserts/boolean/index.mjs +6 -5
- package/lib/asserts/mix/equalTo.d.mts +22 -0
- package/lib/asserts/mix/equalTo.d.ts +22 -0
- package/lib/asserts/mix/equalTo.js +9 -0
- package/lib/asserts/mix/equalTo.js.map +1 -0
- package/lib/asserts/mix/equalTo.mjs +9 -0
- package/lib/asserts/{common → mix}/index.d.mts +5 -4
- package/lib/asserts/{common → mix}/index.d.ts +5 -4
- package/lib/asserts/mix/index.js +10 -0
- package/lib/asserts/mix/index.js.map +1 -0
- package/lib/asserts/mix/index.mjs +10 -0
- package/lib/asserts/mix/oneOfValues.d.mts +23 -0
- package/lib/asserts/mix/oneOfValues.d.ts +23 -0
- package/lib/asserts/mix/oneOfValues.js +9 -0
- package/lib/asserts/mix/oneOfValues.js.map +1 -0
- package/lib/asserts/mix/oneOfValues.mjs +9 -0
- package/lib/asserts/number/index.d.mts +4 -3
- package/lib/asserts/number/index.d.ts +4 -3
- package/lib/asserts/number/index.js +10 -7
- package/lib/asserts/number/index.js.map +1 -1
- package/lib/asserts/number/index.mjs +9 -6
- package/lib/asserts/number/max.d.mts +15 -10
- package/lib/asserts/number/max.d.ts +15 -10
- package/lib/asserts/number/max.js +11 -4
- package/lib/asserts/number/max.js.map +1 -1
- package/lib/asserts/number/max.mjs +12 -5
- package/lib/asserts/number/max.mjs.map +1 -1
- package/lib/asserts/number/maxExcluded.d.mts +14 -9
- package/lib/asserts/number/maxExcluded.d.ts +14 -9
- package/lib/asserts/number/maxExcluded.js +11 -4
- package/lib/asserts/number/maxExcluded.js.map +1 -1
- package/lib/asserts/number/maxExcluded.mjs +12 -5
- package/lib/asserts/number/maxExcluded.mjs.map +1 -1
- package/lib/asserts/number/min.d.mts +15 -10
- package/lib/asserts/number/min.d.ts +15 -10
- package/lib/asserts/number/min.js +11 -4
- package/lib/asserts/number/min.js.map +1 -1
- package/lib/asserts/number/min.mjs +12 -5
- package/lib/asserts/number/min.mjs.map +1 -1
- package/lib/asserts/number/minExcluded.d.mts +15 -10
- package/lib/asserts/number/minExcluded.d.ts +15 -10
- package/lib/asserts/number/minExcluded.js +11 -4
- package/lib/asserts/number/minExcluded.js.map +1 -1
- package/lib/asserts/number/minExcluded.mjs +12 -5
- package/lib/asserts/number/minExcluded.mjs.map +1 -1
- package/lib/asserts/number/negative.d.mts +14 -9
- package/lib/asserts/number/negative.d.ts +14 -9
- package/lib/asserts/number/negative.js +11 -3
- package/lib/asserts/number/negative.js.map +1 -1
- package/lib/asserts/number/negative.mjs +12 -4
- package/lib/asserts/number/negative.mjs.map +1 -1
- package/lib/asserts/number/positive.d.mts +14 -9
- package/lib/asserts/number/positive.d.ts +14 -9
- package/lib/asserts/number/positive.js +11 -3
- package/lib/asserts/number/positive.js.map +1 -1
- package/lib/asserts/number/positive.mjs +12 -4
- package/lib/asserts/number/positive.mjs.map +1 -1
- package/lib/asserts/object/index.d.mts +4 -3
- package/lib/asserts/object/index.d.ts +4 -3
- package/lib/asserts/object/index.js +6 -6
- package/lib/asserts/object/index.mjs +5 -5
- package/lib/asserts/string/email.d.mts +13 -9
- package/lib/asserts/string/email.d.ts +13 -9
- package/lib/asserts/string/email.js +11 -4
- package/lib/asserts/string/email.js.map +1 -1
- package/lib/asserts/string/email.mjs +12 -5
- package/lib/asserts/string/email.mjs.map +1 -1
- package/lib/asserts/string/index.d.mts +4 -3
- package/lib/asserts/string/index.d.ts +4 -3
- package/lib/asserts/string/index.js +10 -7
- package/lib/asserts/string/index.js.map +1 -1
- package/lib/asserts/string/index.mjs +9 -6
- package/lib/asserts/string/maxLength.d.mts +14 -9
- package/lib/asserts/string/maxLength.d.ts +14 -9
- package/lib/asserts/string/maxLength.js +11 -4
- package/lib/asserts/string/maxLength.js.map +1 -1
- package/lib/asserts/string/maxLength.mjs +12 -5
- package/lib/asserts/string/maxLength.mjs.map +1 -1
- package/lib/asserts/string/minLength.d.mts +14 -9
- package/lib/asserts/string/minLength.d.ts +14 -9
- package/lib/asserts/string/minLength.js +11 -4
- package/lib/asserts/string/minLength.js.map +1 -1
- package/lib/asserts/string/minLength.mjs +12 -5
- package/lib/asserts/string/minLength.mjs.map +1 -1
- package/lib/asserts/string/regExp.d.mts +14 -9
- package/lib/asserts/string/regExp.d.ts +14 -9
- package/lib/asserts/string/regExp.js +11 -4
- package/lib/asserts/string/regExp.js.map +1 -1
- package/lib/asserts/string/regExp.mjs +12 -5
- package/lib/asserts/string/regExp.mjs.map +1 -1
- package/lib/{chunk-32IQXZRK.mjs → chunk-2PM3K3OE.mjs} +2 -2
- package/lib/{chunk-32IQXZRK.mjs.map → chunk-2PM3K3OE.mjs.map} +1 -1
- package/lib/chunk-3HTYHE2X.mjs +54 -0
- package/lib/chunk-3HTYHE2X.mjs.map +1 -0
- package/lib/chunk-3O4WCRC2.mjs +21 -0
- package/lib/chunk-3O4WCRC2.mjs.map +1 -0
- package/lib/chunk-4CYIMDP2.js +21 -0
- package/lib/chunk-4CYIMDP2.js.map +1 -0
- package/lib/{chunk-QKGQ4XZC.js → chunk-4RNK3I74.js} +3 -3
- package/lib/{chunk-QKGQ4XZC.js.map → chunk-4RNK3I74.js.map} +1 -1
- package/lib/chunk-5BVJ6YMH.js +54 -0
- package/lib/chunk-5BVJ6YMH.js.map +1 -0
- package/lib/chunk-6TH3DBDK.js +53 -0
- package/lib/chunk-6TH3DBDK.js.map +1 -0
- package/lib/chunk-7XFLH6R2.mjs +44 -0
- package/lib/chunk-7XFLH6R2.mjs.map +1 -0
- package/lib/{chunk-BGMR7A6X.mjs → chunk-B6TFELXR.mjs} +2 -2
- package/lib/{chunk-BGMR7A6X.mjs.map → chunk-B6TFELXR.mjs.map} +1 -1
- package/lib/{chunk-GEZME6V2.mjs → chunk-CCBCDJXS.mjs} +11 -6
- package/lib/chunk-CCBCDJXS.mjs.map +1 -0
- package/lib/chunk-CLG3QRZC.mjs +47 -0
- package/lib/chunk-CLG3QRZC.mjs.map +1 -0
- package/lib/chunk-DJSLA2ZV.js +27 -0
- package/lib/chunk-DJSLA2ZV.js.map +1 -0
- package/lib/{chunk-L7HUXYFV.mjs → chunk-DPBE7TAQ.mjs} +2 -2
- package/lib/chunk-DPBE7TAQ.mjs.map +1 -0
- package/lib/{chunk-APTTIN2L.js → chunk-EGEHSELT.js} +6 -6
- package/lib/chunk-EGEHSELT.js.map +1 -0
- package/lib/chunk-F5MSBL33.js +50 -0
- package/lib/chunk-F5MSBL33.js.map +1 -0
- package/lib/chunk-FTLRX2XG.mjs +50 -0
- package/lib/chunk-FTLRX2XG.mjs.map +1 -0
- package/lib/chunk-H7CPKW53.js +47 -0
- package/lib/chunk-H7CPKW53.js.map +1 -0
- package/lib/{chunk-A7I2JEQO.js → chunk-HDSGCAHH.js} +3 -3
- package/lib/{chunk-A7I2JEQO.js.map → chunk-HDSGCAHH.js.map} +1 -1
- package/lib/{chunk-L6A7NXUJ.js → chunk-IEUXIOL6.js} +3 -3
- package/lib/{chunk-L6A7NXUJ.js.map → chunk-IEUXIOL6.js.map} +1 -1
- package/lib/chunk-IRG4OQAB.js +63 -0
- package/lib/chunk-IRG4OQAB.js.map +1 -0
- package/lib/chunk-J4VKFJQK.js +21 -0
- package/lib/chunk-J4VKFJQK.js.map +1 -0
- package/lib/{chunk-A2SAWKJA.mjs → chunk-JDGZCBXF.mjs} +2 -2
- package/lib/{chunk-A2SAWKJA.mjs.map → chunk-JDGZCBXF.mjs.map} +1 -1
- package/lib/chunk-JSGZ7DKT.js +14 -0
- package/lib/chunk-JSGZ7DKT.js.map +1 -0
- package/lib/{chunk-AJ4WV5DG.mjs → chunk-MRISXZQ4.mjs} +3 -3
- package/lib/chunk-MRISXZQ4.mjs.map +1 -0
- package/lib/{chunk-A3UTB4RE.mjs → chunk-NDQM5OL4.mjs} +4 -4
- package/lib/chunk-NDQM5OL4.mjs.map +1 -0
- package/lib/chunk-NNFYXHLU.mjs +21 -0
- package/lib/chunk-NNFYXHLU.mjs.map +1 -0
- package/lib/{chunk-KCBOOOTF.mjs → chunk-NTC2XQ36.mjs} +2 -2
- package/lib/{chunk-KCBOOOTF.mjs.map → chunk-NTC2XQ36.mjs.map} +1 -1
- package/lib/chunk-PG2R7P7G.mjs +27 -0
- package/lib/chunk-PG2R7P7G.mjs.map +1 -0
- package/lib/chunk-PH2PMUZP.mjs +63 -0
- package/lib/chunk-PH2PMUZP.mjs.map +1 -0
- package/lib/chunk-QQNQ4IDN.js +13 -0
- package/lib/chunk-QQNQ4IDN.js.map +1 -0
- package/lib/{chunk-PJP63SJT.js → chunk-RL4MSI7R.js} +3 -3
- package/lib/{chunk-PJP63SJT.js.map → chunk-RL4MSI7R.js.map} +1 -1
- package/lib/chunk-RQH4LXBW.js +47 -0
- package/lib/chunk-RQH4LXBW.js.map +1 -0
- package/lib/chunk-RRQOET3X.js +28 -0
- package/lib/chunk-RRQOET3X.js.map +1 -0
- package/lib/chunk-RYVUYVEE.mjs +14 -0
- package/lib/chunk-RYVUYVEE.mjs.map +1 -0
- package/lib/{chunk-QYLH5ZAC.mjs → chunk-TCQY2R3U.mjs} +25 -4
- package/lib/chunk-TCQY2R3U.mjs.map +1 -0
- package/lib/{chunk-K2S3FS5K.js → chunk-VHEA4JZ5.js} +3 -3
- package/lib/{chunk-K2S3FS5K.js.map → chunk-VHEA4JZ5.js.map} +1 -1
- package/lib/{chunk-VPFIIM2X.js → chunk-WBERBAL4.js} +11 -6
- package/lib/chunk-WBERBAL4.js.map +1 -0
- package/lib/chunk-WK4O3HH4.js +44 -0
- package/lib/chunk-WK4O3HH4.js.map +1 -0
- package/lib/{chunk-3FEJUC3K.mjs → chunk-YML32TPA.mjs} +2 -2
- package/lib/{chunk-3FEJUC3K.mjs.map → chunk-YML32TPA.mjs.map} +1 -1
- package/lib/chunk-YUESB2WV.mjs +47 -0
- package/lib/chunk-YUESB2WV.mjs.map +1 -0
- package/lib/codeGen.d.mts +7 -0
- package/lib/codeGen.d.ts +7 -0
- package/lib/codeGen.js +51 -0
- package/lib/codeGen.js.map +1 -0
- package/lib/codeGen.mjs +51 -0
- package/lib/codeGen.mjs.map +1 -0
- package/lib/commonTypes.d.mts +31 -0
- package/lib/commonTypes.d.ts +31 -0
- package/lib/commonTypes.js +1 -0
- package/lib/commonTypes.js.map +1 -0
- package/lib/commonTypes.mjs +1 -0
- package/lib/exceptions.d.mts +4 -2
- package/lib/exceptions.d.ts +4 -2
- package/lib/exceptions.js +2 -2
- package/lib/exceptions.mjs +3 -3
- package/lib/helpers/core.d.mts +2 -0
- package/lib/helpers/core.d.ts +2 -0
- package/lib/helpers/core.js +10 -0
- package/lib/helpers/core.js.map +1 -0
- package/lib/helpers/core.mjs +10 -0
- package/lib/helpers/core.mjs.map +1 -0
- package/lib/helpers/setStrictType.d.mts +6 -0
- package/lib/helpers/setStrictType.d.ts +6 -0
- package/lib/helpers/setStrictType.js +9 -0
- package/lib/helpers/setStrictType.js.map +1 -0
- package/lib/helpers/setStrictType.mjs +9 -0
- package/lib/helpers/setStrictType.mjs.map +1 -0
- package/lib/index.d.mts +7 -4
- package/lib/index.d.ts +7 -4
- package/lib/index.js +31 -17
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +38 -24
- package/lib/parse.d.mts +66 -0
- package/lib/parse.d.ts +66 -0
- package/lib/parse.js +10 -0
- package/lib/parse.js.map +1 -0
- package/lib/parse.mjs +10 -0
- package/lib/parse.mjs.map +1 -0
- package/lib/{parseSchema.d.mts → parseOrFail.d.mts} +16 -5
- package/lib/{parseSchema.d.ts → parseOrFail.d.ts} +16 -5
- package/lib/parseOrFail.js +10 -0
- package/lib/parseOrFail.js.map +1 -0
- package/lib/parseOrFail.mjs +10 -0
- package/lib/parseOrFail.mjs.map +1 -0
- package/lib/schemas/ArraySchema.d.mts +2 -1
- package/lib/schemas/ArraySchema.d.ts +2 -1
- package/lib/schemas/ArraySchema.js +5 -5
- package/lib/schemas/ArraySchema.mjs +4 -4
- package/lib/schemas/BooleanSchema.d.mts +2 -1
- package/lib/schemas/BooleanSchema.d.ts +2 -1
- package/lib/schemas/BooleanSchema.js +6 -5
- package/lib/schemas/BooleanSchema.js.map +1 -1
- package/lib/schemas/BooleanSchema.mjs +5 -4
- package/lib/schemas/CommonSchema.d.mts +2 -1
- package/lib/schemas/CommonSchema.d.ts +2 -1
- package/lib/schemas/CommonSchema.js +4 -4
- package/lib/schemas/CommonSchema.mjs +3 -3
- package/lib/schemas/NumberSchema.d.mts +12 -1
- package/lib/schemas/NumberSchema.d.ts +12 -1
- package/lib/schemas/NumberSchema.js +9 -6
- package/lib/schemas/NumberSchema.js.map +1 -1
- package/lib/schemas/NumberSchema.mjs +8 -5
- package/lib/schemas/ObjectSchema.d.mts +20 -1
- package/lib/schemas/ObjectSchema.d.ts +20 -1
- package/lib/schemas/ObjectSchema.js +5 -5
- package/lib/schemas/ObjectSchema.mjs +4 -4
- package/lib/schemas/StringSchema.d.mts +12 -1
- package/lib/schemas/StringSchema.d.ts +12 -1
- package/lib/schemas/StringSchema.js +9 -6
- package/lib/schemas/StringSchema.js.map +1 -1
- package/lib/schemas/StringSchema.mjs +8 -5
- package/lib/translationMap.d.mts +12 -0
- package/lib/translationMap.d.ts +12 -0
- package/lib/translationMap.js +14 -0
- package/lib/translationMap.js.map +1 -0
- package/lib/translationMap.mjs +14 -0
- package/lib/translationMap.mjs.map +1 -0
- package/package.json +35 -18
- package/lib/asserts/common/equalTo.d.mts +0 -29
- package/lib/asserts/common/equalTo.d.ts +0 -29
- package/lib/asserts/common/equalTo.js +0 -8
- package/lib/asserts/common/equalTo.js.map +0 -1
- package/lib/asserts/common/equalTo.mjs +0 -8
- package/lib/asserts/common/index.js +0 -10
- package/lib/asserts/common/index.js.map +0 -1
- package/lib/asserts/common/index.mjs +0 -10
- package/lib/chunk-2AP4OQMT.js +0 -26
- package/lib/chunk-2AP4OQMT.js.map +0 -1
- package/lib/chunk-5NCJN6D2.js +0 -70
- package/lib/chunk-5NCJN6D2.js.map +0 -1
- package/lib/chunk-5ZMICHV3.js +0 -21
- package/lib/chunk-5ZMICHV3.js.map +0 -1
- package/lib/chunk-63IQFNFO.mjs +0 -27
- package/lib/chunk-63IQFNFO.mjs.map +0 -1
- package/lib/chunk-A3UTB4RE.mjs.map +0 -1
- package/lib/chunk-AJ4WV5DG.mjs.map +0 -1
- package/lib/chunk-APTTIN2L.js.map +0 -1
- package/lib/chunk-AXOP6HPJ.js +0 -14
- package/lib/chunk-AXOP6HPJ.js.map +0 -1
- package/lib/chunk-FCFO5J5Y.mjs +0 -14
- package/lib/chunk-FCFO5J5Y.mjs.map +0 -1
- package/lib/chunk-GEZME6V2.mjs.map +0 -1
- package/lib/chunk-GNBSKTFV.js +0 -27
- package/lib/chunk-GNBSKTFV.js.map +0 -1
- package/lib/chunk-IQ3QYABE.js +0 -32
- package/lib/chunk-IQ3QYABE.js.map +0 -1
- package/lib/chunk-L7HUXYFV.mjs.map +0 -1
- package/lib/chunk-LHTJHMKJ.mjs +0 -27
- package/lib/chunk-LHTJHMKJ.mjs.map +0 -1
- package/lib/chunk-NOADI6PO.mjs +0 -21
- package/lib/chunk-NOADI6PO.mjs.map +0 -1
- package/lib/chunk-NWY33MFU.js +0 -13
- package/lib/chunk-NWY33MFU.js.map +0 -1
- package/lib/chunk-P3Y6IWFA.mjs +0 -70
- package/lib/chunk-P3Y6IWFA.mjs.map +0 -1
- package/lib/chunk-PDVXBE27.js +0 -27
- package/lib/chunk-PDVXBE27.js.map +0 -1
- package/lib/chunk-QJOXKH7A.mjs +0 -26
- package/lib/chunk-QJOXKH7A.mjs.map +0 -1
- package/lib/chunk-QYLH5ZAC.mjs.map +0 -1
- package/lib/chunk-VPFIIM2X.js.map +0 -1
- package/lib/chunk-YXHH5BF6.js +0 -28
- package/lib/chunk-YXHH5BF6.js.map +0 -1
- package/lib/core.d.mts +0 -1
- package/lib/core.d.ts +0 -1
- package/lib/core.js +0 -10
- package/lib/core.js.map +0 -1
- package/lib/core.mjs +0 -10
- package/lib/parseSchema.js +0 -9
- package/lib/parseSchema.js.map +0 -1
- package/lib/parseSchema.mjs +0 -9
- /package/lib/asserts/{common → mix}/equalTo.mjs.map +0 -0
- /package/lib/asserts/{common → mix}/index.mjs.map +0 -0
- /package/lib/{core.mjs.map → asserts/mix/oneOfValues.mjs.map} +0 -0
- /package/lib/{parseSchema.mjs.map → commonTypes.mjs.map} +0 -0
package/README.md
CHANGED
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
|
|
5
5
|
  
|
|
6
6
|
|
|
7
|
-
|
|
8
7
|
### Features
|
|
9
8
|
|
|
10
9
|
- **Type Inference**: Automatically infer TypeScript types from your validation schemas.
|
|
@@ -31,7 +30,7 @@ Let's define a schema for a Student object:
|
|
|
31
30
|
|
|
32
31
|
```typeScript
|
|
33
32
|
|
|
34
|
-
import {
|
|
33
|
+
import { InferType, string, number, array, object, boolean } from 'bguard';
|
|
35
34
|
import { email } from 'bguard/string/email';
|
|
36
35
|
import { min } from 'bguard/number/min';
|
|
37
36
|
import { max } from 'bguard/number/max';
|
|
@@ -58,9 +57,7 @@ const studentSchema = object({
|
|
|
58
57
|
Using the InferType utility, you can infer the TypeScript type of the schema:
|
|
59
58
|
|
|
60
59
|
```typescript
|
|
61
|
-
|
|
62
60
|
type StudentSchema = InferType<typeof studentSchema>;
|
|
63
|
-
|
|
64
61
|
```
|
|
65
62
|
|
|
66
63
|
This will generate the following type:
|
|
@@ -81,13 +78,91 @@ type StudentSchema = {
|
|
|
81
78
|
|
|
82
79
|
```
|
|
83
80
|
|
|
84
|
-
#### Validating Data
|
|
85
81
|
|
|
86
|
-
|
|
82
|
+
#### Generating TypeScript Types with `codeGen`
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
If you prefer to generate TypeScript types as a string, you can use the `codeGen` function:
|
|
86
|
+
|
|
87
|
+
```typeScript
|
|
88
|
+
import { codeGen } from 'bguard/codeGen';
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
The `codeGen` function takes a schema and returns a string representing the inferred TypeScript type. This string can be written to a file or used in other ways where a static type definition is needed.
|
|
92
|
+
|
|
93
|
+
Example
|
|
94
|
+
|
|
95
|
+
```typeScript
|
|
96
|
+
const typeString = codeGen(studentSchema);
|
|
97
|
+
console.log(typeString);
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
This would output a string:
|
|
87
101
|
|
|
88
102
|
```typeScript
|
|
103
|
+
{
|
|
104
|
+
email?: string | undefined;
|
|
105
|
+
age: number;
|
|
106
|
+
address: string | null;
|
|
107
|
+
classes: ({
|
|
108
|
+
name: string;
|
|
109
|
+
mandatory: boolean;
|
|
110
|
+
rooms: number[];
|
|
111
|
+
} | undefined)[];
|
|
112
|
+
verified?: boolean | undefined;
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
Note: The returned string does not include a type name or the `=` symbol. You would need to add these manually if you want a complete type definition.
|
|
116
|
+
|
|
117
|
+
#### Generating Named TypeScript Types with `codeGenWithName`
|
|
118
|
+
|
|
119
|
+
For convenience, if you want to generate a complete type definition including a name, use the `codeGenWithName` function:
|
|
120
|
+
|
|
121
|
+
```typeScript
|
|
122
|
+
import { codeGenWithName } from 'bguard/codeGen';
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
This function takes two parameters: the name of the type and the schema.
|
|
126
|
+
|
|
127
|
+
Example:
|
|
128
|
+
|
|
129
|
+
```typeScript
|
|
130
|
+
const namedTypeString = codeGenWithName('StudentSchema', studentSchema);
|
|
131
|
+
console.log(namedTypeString);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
This would output a string:
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
```typeScript
|
|
138
|
+
type StudentSchema = {
|
|
139
|
+
email?: string | undefined;
|
|
140
|
+
age: number;
|
|
141
|
+
address: string | null;
|
|
142
|
+
classes: ({
|
|
143
|
+
name: string;
|
|
144
|
+
mandatory: boolean;
|
|
145
|
+
rooms: number[];
|
|
146
|
+
} | undefined)[];
|
|
147
|
+
verified?: boolean | undefined;
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
#### Summary:
|
|
151
|
+
|
|
152
|
+
`codeGen(schema: CommonSchema): string` - Generates a string of the TypeScript type based on the schema. You need to manually add a type name and assignment if needed.
|
|
153
|
+
|
|
154
|
+
`codeGenWithName(typeName: string, schema: CommonSchema): string` - Generates a complete TypeScript type definition string, including the type keyword and type name.
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
### Validating Data
|
|
89
158
|
|
|
90
|
-
|
|
159
|
+
This library provides two methods to parse data against schemas: `parse` and `parseOrFail`. These methods help in validating the data and obtaining structured errors if any issues are found during validation.
|
|
160
|
+
|
|
161
|
+
Let's use above mentioned `studentSchema` and:
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
// example of valid received data
|
|
165
|
+
const validStudentData = {
|
|
91
166
|
age: 21,
|
|
92
167
|
address: '123 Main St',
|
|
93
168
|
classes: [
|
|
@@ -100,49 +175,458 @@ const studentData = {
|
|
|
100
175
|
email: 'student@example.com',
|
|
101
176
|
};
|
|
102
177
|
|
|
103
|
-
|
|
178
|
+
// example of invalid received data with multiple errors
|
|
179
|
+
const invalidStudentData = {
|
|
180
|
+
age: -5,
|
|
181
|
+
address: undefined,
|
|
182
|
+
classes: [
|
|
183
|
+
{
|
|
184
|
+
name: true,
|
|
185
|
+
mandatory: 'true',
|
|
186
|
+
rooms: null,
|
|
187
|
+
},
|
|
188
|
+
],
|
|
189
|
+
email: 'invalid-example',
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
#### `parse` Method
|
|
196
|
+
|
|
197
|
+
The `parse` method validates the data and returns a tuple containing errors and the parsed value. This method allows you to choose whether to collect all errors or stop at the first error using an options flag.
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
**Syntax:**
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
import { parse } from 'bguard';
|
|
204
|
+
// import other dependencies
|
|
205
|
+
|
|
206
|
+
const [errors, parsedValue] = parse(studentSchema, validStudentData, { getAllErrors: true });
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Returns:
|
|
210
|
+
|
|
211
|
+
- If validation succeeds: `[undefined, parsedValue]`
|
|
212
|
+
- If there are validation errors: `[ValidationErrorData[], undefined]`
|
|
213
|
+
|
|
214
|
+
Options:
|
|
215
|
+
|
|
216
|
+
- `lng`: Specifies the language for error messages. Default is `'default'`.
|
|
217
|
+
- `getAllErrors`: If `true`, collects all validation errors. If `false` or `undefined`, stops at the first error. Turning off `getAllErrors` provides a runtime optimization, as it stops validation at the first error, avoiding unnecessary checks for the remaining received value.
|
|
104
218
|
|
|
219
|
+
#### `parseOrFail` Method
|
|
105
220
|
|
|
221
|
+
The `parseOrFail` method validates the data and throws an error on the first validation failure. It is useful when you want to halt processing immediately upon encountering an error.
|
|
222
|
+
|
|
223
|
+
**Syntax:**
|
|
224
|
+
|
|
225
|
+
```typeScript
|
|
226
|
+
import { parseOrFail } from 'bguard';
|
|
227
|
+
// import other dependencies
|
|
228
|
+
|
|
229
|
+
try {
|
|
230
|
+
// Attempt to parse and validate the studentData using the studentSchema
|
|
231
|
+
const validatedData = parseOrFail(studentSchema, validStudentData);
|
|
232
|
+
// If the data is valid, validatedData will contain the parsed value with inferred TypeScript types
|
|
233
|
+
} catch (error) {
|
|
234
|
+
// If the data does not conform to the schema, an error will be thrown
|
|
235
|
+
console.error(error.message); // Logs the first validation error message, if any
|
|
236
|
+
}
|
|
106
237
|
```
|
|
238
|
+
Throws:
|
|
239
|
+
- `ValidationError`: If any validation rule fails, this error is thrown with details of the first encountered error.
|
|
240
|
+
|
|
241
|
+
Options:
|
|
242
|
+
- `lng`: Specifies the language for error messages. Default is `'default'`.
|
|
243
|
+
|
|
107
244
|
|
|
108
|
-
|
|
245
|
+
####
|
|
109
246
|
|
|
247
|
+
### Explanation
|
|
110
248
|
|
|
249
|
+
- **`parse` Method**: This method returns a tuple where the first element is an array of validation errors (if any), and the second element is the successfully parsed value (or `undefined` if errors exist). It allows collecting all errors by setting the `getAllErrors` flag.
|
|
250
|
+
|
|
251
|
+
- **`parseOrFail` Method**: This method throws a `ValidationError` when the first validation rule fails, making it suitable for scenarios where early termination of validation is desired.
|
|
252
|
+
|
|
253
|
+
- **Options**: Both methods accept options for language settings and error collection, enhancing flexibility in handling validation processes.
|
|
111
254
|
|
|
112
255
|
### Chaining Methods
|
|
113
256
|
|
|
114
|
-
|
|
115
|
-
|
|
257
|
+
- `nullable()`: Allows the value to be null.
|
|
258
|
+
- `optional()`: Allows the value to be undefined.
|
|
116
259
|
|
|
117
|
-
|
|
260
|
+
Example:
|
|
118
261
|
|
|
119
|
-
|
|
262
|
+
```typeScript
|
|
120
263
|
|
|
121
264
|
const schema = string().nullable().optional();
|
|
122
265
|
```
|
|
123
266
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
- Number Literals:
|
|
128
|
-
`number().equalTo(42)` will infer 42 as the type.
|
|
129
|
-
|
|
130
|
-
- Boolean Literals:
|
|
131
|
-
`boolean().onlyTrue()` will infer true as the type.
|
|
132
|
-
`boolean().onlyFalse()` will infer false as the type.
|
|
267
|
+
- String Literals:
|
|
268
|
+
`string().equalTo('myStringValue')` will infer <b>'myStringValue'</b> as the type.
|
|
269
|
+
`string().oneOfValues(['foo', 'bar'])` will infer <b>'foo' | 'bar'</b> as the type.
|
|
133
270
|
|
|
271
|
+
- Number Literals:
|
|
272
|
+
`number().equalTo(42)` will infer <b>42</b> as the type.
|
|
273
|
+
`number().oneOfValues([3, 5])` will infer <b>3 | 5</b> as the type.
|
|
134
274
|
|
|
275
|
+
- Boolean Literals:
|
|
276
|
+
`boolean().onlyTrue()` will infer <b>true</b> as the type.
|
|
277
|
+
`boolean().onlyFalse()` will infer <b>false</b> as the type.
|
|
135
278
|
|
|
136
279
|
### Custom Assertions
|
|
280
|
+
|
|
137
281
|
You can extend the validation with custom assertions:
|
|
138
282
|
|
|
139
|
-
|
|
283
|
+
```typeScript
|
|
140
284
|
import { min } from 'bguard/number/min';
|
|
141
285
|
import { max } from 'bguard/number/max';
|
|
142
286
|
|
|
143
287
|
const ageSchema = number().custom(min(18), max(120));
|
|
144
288
|
```
|
|
289
|
+
|
|
145
290
|
Assertions are imported from specific paths for better tree-shaking and smaller bundle sizes.
|
|
146
291
|
|
|
292
|
+
|
|
293
|
+
### Translation
|
|
294
|
+
|
|
295
|
+
Bguard provides default translations for error messages, but you can customize them as needed. Each potential error has an `errorKey` and `errorMessage`.
|
|
296
|
+
|
|
297
|
+
Example:
|
|
298
|
+
|
|
299
|
+
Consider the schema:
|
|
300
|
+
|
|
301
|
+
```typeScript
|
|
302
|
+
const testSchema = object({ foo: number().custom(min(5)) });
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
The `min` function has:
|
|
306
|
+
|
|
307
|
+
```typeScript
|
|
308
|
+
const minErrorMessage = 'The received value is less than expected'; // Default error message
|
|
309
|
+
const minErrorKey = 'n:min'; // Error key
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
If you want to change the error message for `min`, you can do so by importing the `setLocale` function and setting your custom message:
|
|
313
|
+
|
|
314
|
+
```typeScript
|
|
315
|
+
import { setLocale } from 'bguard/translationMap';
|
|
316
|
+
|
|
317
|
+
setLocale('SR', {
|
|
318
|
+
'n:min': 'The received value {{r}} found on path {{p}} is less than expected value {{e}}',
|
|
319
|
+
// ... continue adding other translations
|
|
320
|
+
});
|
|
321
|
+
```
|
|
322
|
+
With this setup, in the translation namespace 'SR', if the received value is 4, you'll get an error message like:
|
|
323
|
+
|
|
324
|
+
`'The received value 4 found on path .foo is less than expected value 5'`
|
|
325
|
+
|
|
326
|
+
- `{{r}}` - Replaced with the received value.
|
|
327
|
+
- `{{p}}` - Replaced with the path to the error.
|
|
328
|
+
- `{{e}}` - Replaced with the expected value.
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
> **Notice:** Do not overwrite the 'default' namespace. If a translation is missing, it will fall back to the 'default' translation.
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
#### Using Translations
|
|
336
|
+
|
|
337
|
+
To apply the new translation, both `parse` and `parseOrFail` functions accept a lng property in the options object provided as the third parameter:
|
|
338
|
+
|
|
339
|
+
```typeScript
|
|
340
|
+
parseOrFail(testSchema, { foo: 4 }, { lng: 'SR' });
|
|
341
|
+
// or
|
|
342
|
+
parse(testSchema, { foo: 4 }, { lng: 'SR' });
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
#### Common and Custom Translations
|
|
347
|
+
|
|
348
|
+
We have two sets of translations: common errors and specific assertions.
|
|
349
|
+
|
|
350
|
+
<b>Common Error Translations</b>:
|
|
351
|
+
|
|
352
|
+
```
|
|
353
|
+
'c:optional': 'The required value is missing',
|
|
354
|
+
'c:nullable': 'Value should not be null',
|
|
355
|
+
'c:array': 'Expected an array but received a different type',
|
|
356
|
+
'c:objectType': 'Expected an object but received a different type',
|
|
357
|
+
'c:objectTypeAsArray': 'Expected an object but received an array. Invalid type of data',
|
|
358
|
+
'c:unrecognizedProperty': 'This property is not allowed in the object',
|
|
359
|
+
'c:requiredProperty': 'Missing required property in the object',
|
|
360
|
+
'c:invalidType': 'Invalid type of data',
|
|
361
|
+
'c:isBoolean': 'The received value is not {{e}}',
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
<b>Custom Assertion Translations</b>:
|
|
365
|
+
|
|
366
|
+
For custom assertions, each key and message are located in separate files for better code splitting. There are multiple ways to identify a key:
|
|
367
|
+
|
|
368
|
+
1. Key Construction:
|
|
369
|
+
Keys are constructed as `'{typeId}:{functionName}'`, where `typeId` represents:
|
|
370
|
+
|
|
371
|
+
- c - common
|
|
372
|
+
- n - numbers
|
|
373
|
+
- s - strings
|
|
374
|
+
- b - boolean
|
|
375
|
+
- a - array
|
|
376
|
+
- o - object
|
|
377
|
+
- sy - symbol
|
|
378
|
+
- f - function
|
|
379
|
+
- bi - bigint
|
|
380
|
+
- m - mixed
|
|
381
|
+
|
|
382
|
+
Each `typeId` maps to the folder from which custom assertions are retrieved (except 'common', as explained above).
|
|
383
|
+
|
|
384
|
+
Example:
|
|
385
|
+
|
|
386
|
+
```typeScript
|
|
387
|
+
import { maxLength } from 'bguard/string/maxLength';
|
|
388
|
+
```
|
|
389
|
+
The function located in `'bguard/string/maxLength'` will have the key `'s:maxLength'`.
|
|
390
|
+
|
|
391
|
+
2. Assertion Function Properties:
|
|
392
|
+
|
|
393
|
+
Each assert function has two additional properties: `key` and `message`.
|
|
394
|
+
|
|
395
|
+
```typeScript
|
|
396
|
+
import { maxLength } from 'bguard/string/maxLength';
|
|
397
|
+
|
|
398
|
+
console.log(maxLength.key); // Output: 's:maxLength'
|
|
399
|
+
console.log(maxLength.message); // Output: 'The received value length is greater than expected'
|
|
400
|
+
```
|
|
401
|
+
> **Notice:** Do not directly change these values.
|
|
402
|
+
|
|
403
|
+
3. IDE Support:
|
|
404
|
+
Each key and message will be visible in text editors that support JSDoc IntelliSense.### Custom Assert Documentation
|
|
405
|
+
|
|
406
|
+
#### mix
|
|
407
|
+
|
|
408
|
+
##### equalTo (mix)
|
|
409
|
+
|
|
410
|
+
```typescript
|
|
411
|
+
import { equalTo } from 'bguard/mix/equalTo';
|
|
412
|
+
```
|
|
413
|
+
|
|
414
|
+
* _Description_ Creates a custom assertion that checks if a value is equal to the expected value.
|
|
415
|
+
* > **Notice:** It has already been implemented in the number and string schema. There is no need to use it as a custom assert.
|
|
416
|
+
* _Param_ {unknown} expected The value that the received value is expected to match.
|
|
417
|
+
* _Throws_ {ValidationError} If the received value does not match the expected value.
|
|
418
|
+
* _Example_
|
|
419
|
+
```typescript
|
|
420
|
+
const schema = number().custom(equalTo(5)); // Define a schema with a custom assertion
|
|
421
|
+
parseOrFail(schema, 5); // Valid
|
|
422
|
+
parseOrFail(schema, 3); // Throws an error: 'The received value is not equal to expected'
|
|
423
|
+
```
|
|
424
|
+
* _See_ Error Translation Key = 'm:equalTo'
|
|
425
|
+
|
|
426
|
+
|
|
427
|
+
##### oneOfValues (mix)
|
|
428
|
+
|
|
429
|
+
```typescript
|
|
430
|
+
import { oneOfValues } from 'bguard/mix/oneOfValues';
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
* _Description_ Creates a custom assertion that checks if a value is equal to the one of expected values.
|
|
434
|
+
* > **Notice:** It has already been implemented in the number and string schema. There is no need to use it as a custom assert.
|
|
435
|
+
* _Param_ {unknown} expected The value that the received value is expected to match.
|
|
436
|
+
* _Throws_ {ValidationError} If the received value does not match at least one of the expected values.
|
|
437
|
+
* _Example_
|
|
438
|
+
```typescript
|
|
439
|
+
const schema = number().custom(oneOfValues([5, 4])); // Define a schema with a custom assertion
|
|
440
|
+
parseOrFail(schema, 5); // Valid
|
|
441
|
+
parseOrFail(schema, 4); // Valid
|
|
442
|
+
parseOrFail(schema, 3); // Throws an error: 'The received value is not equal to expected'
|
|
443
|
+
```
|
|
444
|
+
* _See_ Error Translation Key = 'm:oneOfValues'
|
|
445
|
+
|
|
446
|
+
#### number
|
|
447
|
+
|
|
448
|
+
##### max (number)
|
|
449
|
+
|
|
450
|
+
```typescript
|
|
451
|
+
import { max } from 'bguard/number/max';
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
* _Description_ Asserts that a number value does not exceed a specified maximum value.
|
|
455
|
+
* _Param_ {number} expected The maximum allowable value.
|
|
456
|
+
* _Throws_ {ValidationError} if the received value exceeds the expected maximum value.
|
|
457
|
+
* _Example_
|
|
458
|
+
```typescript
|
|
459
|
+
const schema = number().custom(max(100));
|
|
460
|
+
parseOrFail(schema, 99); // Valid
|
|
461
|
+
parseOrFail(schema, 100); // Valid
|
|
462
|
+
parseOrFail(schema, 101); // Throws an error: 'The received value is greater than expected'
|
|
463
|
+
```
|
|
464
|
+
* _See_ Error Translation Key = 'n:max'
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
##### maxExcluded (number)
|
|
468
|
+
|
|
469
|
+
```typescript
|
|
470
|
+
import { maxExcluded } from 'bguard/number/maxExcluded';
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
* _Description_ - Asserts that a number value is strictly less than a specified maximum value (i.e., the maximum value is excluded).
|
|
474
|
+
* _Param_ {number} expected - The maximum allowable value, which is excluded.
|
|
475
|
+
* _Throws_ {ValidationError} if the received value is greater than or equal to the expected maximum value.
|
|
476
|
+
* _Example_
|
|
477
|
+
```typescript
|
|
478
|
+
const schema = number().custom(maxExcluded(100));
|
|
479
|
+
parseOrFail(schema, 99); // Valid
|
|
480
|
+
parseOrFail(schema, 100); // Throws an error: 'The received value is greater than or equal to expected'
|
|
481
|
+
parseOrFail(schema, 101); // Throws an error: 'The received value is greater than or equal to expected'
|
|
482
|
+
```
|
|
483
|
+
* _See_ Error Translation Key = 'n:maxExcluded'
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
##### min (number)
|
|
487
|
+
|
|
488
|
+
```typescript
|
|
489
|
+
import { min } from 'bguard/number/min';
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
* _Description_ Asserts that a number value is not less than a specified minimum value.
|
|
493
|
+
* _Param_ {number} expected The minimum allowable value.
|
|
494
|
+
* _Throws_ {ValidationError} if the received value is less than the expected minimum value.
|
|
495
|
+
* _Example_
|
|
496
|
+
```typescript
|
|
497
|
+
const schema = number().custom(min(10));
|
|
498
|
+
parseOrFail(schema, 11); // Valid
|
|
499
|
+
parseOrFail(schema, 10); // Valid
|
|
500
|
+
parseOrFail(schema, 9); // Throws an error: 'The received value is less than expected'
|
|
501
|
+
```
|
|
502
|
+
* _See_ Error Translation Key = 'n:min'
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
##### minExcluded (number)
|
|
506
|
+
|
|
507
|
+
```typescript
|
|
508
|
+
import { minExcluded } from 'bguard/number/minExcluded';
|
|
509
|
+
```
|
|
510
|
+
|
|
511
|
+
* _Description_ Asserts that a number value is strictly greater than a specified minimum value (i.e., the minimum value is excluded).
|
|
512
|
+
* _Param_ {number} expected The minimum allowable value, which is excluded.
|
|
513
|
+
* _Throws_ {ValidationError} if the received value is less than or equal to the expected minimum value.
|
|
514
|
+
* _Example_
|
|
515
|
+
```typescript
|
|
516
|
+
const schema = number().custom(minExcluded(10));
|
|
517
|
+
parseOrFail(schema, 11); // Valid
|
|
518
|
+
parseOrFail(schema, 10); // Throws an error: 'The received value is less than or equal to expected'
|
|
519
|
+
parseOrFail(schema, 9); // Throws an error: 'The received value is less than or equal to expected'
|
|
520
|
+
```
|
|
521
|
+
* _See_ Error Translation Key = 'n:minExcluded'
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
##### negative (number)
|
|
525
|
+
|
|
526
|
+
```typescript
|
|
527
|
+
import { negative } from 'bguard/number/negative';
|
|
528
|
+
```
|
|
529
|
+
|
|
530
|
+
* _Description_ Asserts that a number value is negative (less than zero).
|
|
531
|
+
* _Throws_ {ValidationError} if the received value is not negative.
|
|
532
|
+
* _Example_
|
|
533
|
+
```typescript
|
|
534
|
+
const schema = number().custom(negative());
|
|
535
|
+
parseOrFail(schema, -10); // Valid
|
|
536
|
+
parseOrFail(schema, 0); // Throws an error: 'The received value is not a negative number'
|
|
537
|
+
parseOrFail(schema, 5); // Throws an error: 'The received value is not a negative number'
|
|
538
|
+
```
|
|
539
|
+
* _See_ - Error Translation Key = 'n:negative'
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
##### positive (number)
|
|
543
|
+
|
|
544
|
+
```typescript
|
|
545
|
+
import { positive } from 'bguard/number/positive';
|
|
546
|
+
```
|
|
547
|
+
|
|
548
|
+
* _Description_ Asserts that a number value is positive (greater than zero).
|
|
549
|
+
* _Throws_ {ValidationError} if the received value is not positive.
|
|
550
|
+
* _Example_
|
|
551
|
+
```typescript
|
|
552
|
+
const schema = number().custom(positive());
|
|
553
|
+
parseOrFail(schema, 10); // Valid
|
|
554
|
+
parseOrFail(schema, 0); // Throws an error: 'The received value is not a positive number'
|
|
555
|
+
parseOrFail(schema, -5); // Throws an error: 'The received value is not a positive number'
|
|
556
|
+
```
|
|
557
|
+
* _See_ Error Translation Key = 'n:positive'
|
|
558
|
+
|
|
559
|
+
#### string
|
|
560
|
+
|
|
561
|
+
##### email (string)
|
|
562
|
+
|
|
563
|
+
```typescript
|
|
564
|
+
import { email } from 'bguard/string/email';
|
|
565
|
+
```
|
|
566
|
+
|
|
567
|
+
* _Description_ Asserts that a string value matches the email pattern. The pattern checks for a basic email format.
|
|
568
|
+
* _Throws_ {ValidationError} if the received value does not match the email pattern.
|
|
569
|
+
* _Example_
|
|
570
|
+
```typescript
|
|
571
|
+
const schema = string().custom(email());
|
|
572
|
+
parseOrFail(schema, 'example@example.com'); // Valid
|
|
573
|
+
parseOrFail(schema, 'invalid-email'); // Throws an error: 'The received value does not match the required email pattern'
|
|
574
|
+
```
|
|
575
|
+
* _See_ - Error Translation Key = 's:email'
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
##### maxLength (string)
|
|
579
|
+
|
|
580
|
+
```typescript
|
|
581
|
+
import { maxLength } from 'bguard/string/maxLength';
|
|
582
|
+
```
|
|
583
|
+
|
|
584
|
+
* _Description_ Asserts that the length of a string value is not greater than a specified maximum length.
|
|
585
|
+
* _Param_ {number} expected The maximum allowed length for the string.
|
|
586
|
+
* _Throws_ {ValidationError} if the length of the received value is greater than the expected length.
|
|
587
|
+
* _Example_
|
|
588
|
+
```typescript
|
|
589
|
+
const schema = string().custom(maxLength(10));
|
|
590
|
+
parseOrFail(schema, 'short'); // Valid
|
|
591
|
+
parseOrFail(schema, 'this is a very long string'); // Throws an error: 'The received value length is greater than expected'
|
|
592
|
+
```
|
|
593
|
+
* _See_ Error Translation Key = 's:maxLength'
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
##### minLength (string)
|
|
597
|
+
|
|
598
|
+
```typescript
|
|
599
|
+
import { minLength } from 'bguard/string/minLength';
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
* _Description_ Asserts that the length of a string value is not less than a specified minimum length.
|
|
603
|
+
* _Param_ {number} expected The minimum required length for the string.
|
|
604
|
+
* _Throws_ {ValidationError} if the length of the received value is less than the expected length.
|
|
605
|
+
* _Example_
|
|
606
|
+
```typescript
|
|
607
|
+
const schema = string().custom(minLength(5));
|
|
608
|
+
parseOrFail(schema, 'short'); // Throws an error: 'The received value length is less than expected'
|
|
609
|
+
parseOrFail(schema, 'adequate'); // Valid
|
|
610
|
+
```
|
|
611
|
+
* _See_ Error Translation Key = 's:minLength'
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
##### regExp (string)
|
|
615
|
+
|
|
616
|
+
```typescript
|
|
617
|
+
import { regExp } from 'bguard/string/regExp';
|
|
618
|
+
```
|
|
619
|
+
|
|
620
|
+
* _Description_ Asserts that a string value matches a specified regular expression pattern.
|
|
621
|
+
* _Param_ {RegExp} expected The regular expression pattern that the string value should match.
|
|
622
|
+
* _Throws_ {ValidationError} if the received value does not match the expected pattern.
|
|
623
|
+
* _Example_
|
|
624
|
+
```typescript
|
|
625
|
+
const schema = string().custom(regExp(/^[A-Za-z0-9]+$/)); // Validates against alphanumeric pattern
|
|
626
|
+
parseOrFail(schema, 'valid123'); // Valid
|
|
627
|
+
parseOrFail(schema, 'invalid!@#'); // Throws an error: 'The received value does not match the required text pattern'
|
|
628
|
+
```
|
|
629
|
+
* _See_ Error Translation Key = 's:regExp'
|
|
630
|
+
|
|
147
631
|
### Contributing
|
|
148
632
|
Contributions are welcome! Please open an issue or submit a pull request for any bugs or feature requests.
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
+
import { ExceptionContext, PrimitiveType, RequiredValidation } from './commonTypes.js';
|
|
2
|
+
|
|
1
3
|
declare const ctxSymbol: unique symbol;
|
|
2
4
|
|
|
3
|
-
declare function innerCheck(schema: CommonSchema, receivedValue: unknown,
|
|
5
|
+
declare function innerCheck(schema: CommonSchema, receivedValue: unknown, exCtx: ExceptionContext): void | {} | null;
|
|
4
6
|
|
|
5
|
-
type RequiredValidation = (received: any, pathToError: string) => void;
|
|
6
7
|
type ObjectShapeSchemaType = Record<string, CommonSchema>;
|
|
7
|
-
type PrimitiveType = 'number' | 'string' | 'boolean' | 'undefined' | 'object' | 'function' | 'symbol' | 'bigint';
|
|
8
8
|
interface ValidatorContext {
|
|
9
9
|
type: PrimitiveType[];
|
|
10
10
|
isNullable?: boolean;
|
|
@@ -12,6 +12,9 @@ interface ValidatorContext {
|
|
|
12
12
|
requiredValidations: RequiredValidation[];
|
|
13
13
|
array?: CommonSchema;
|
|
14
14
|
object?: ObjectShapeSchemaType;
|
|
15
|
+
allowUnrecognizedObjectProps?: boolean;
|
|
16
|
+
strictType?: boolean;
|
|
17
|
+
strictTypeValue?: unknown;
|
|
15
18
|
}
|
|
16
19
|
declare class CommonSchema {
|
|
17
20
|
[ctxSymbol]: ValidatorContext;
|
|
@@ -56,4 +59,4 @@ type WithMix<Y = unknown> = CommonSchema & {
|
|
|
56
59
|
type ExtractFromMix<T> = T extends WithMix<infer X> ? X : never;
|
|
57
60
|
type MapMixTypes<T extends PrimitiveType[]> = T extends (infer U)[] ? U extends keyof TypeMapping ? TypeMapping[U] : never : never;
|
|
58
61
|
|
|
59
|
-
export { CommonSchema as C, type ExtractFromMix as E, type MapMixTypes as M, type ObjectShapeSchemaType as O, type
|
|
62
|
+
export { CommonSchema as C, type ExtractFromMix as E, type MapMixTypes as M, type ObjectShapeSchemaType as O, type ValidatorContext as V, type WithUndefined as W, type WithNull as a, type WithMix as b, ctxSymbol as c, innerCheck as i };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
+
import { ExceptionContext, PrimitiveType, RequiredValidation } from './commonTypes.mjs';
|
|
2
|
+
|
|
1
3
|
declare const ctxSymbol: unique symbol;
|
|
2
4
|
|
|
3
|
-
declare function innerCheck(schema: CommonSchema, receivedValue: unknown,
|
|
5
|
+
declare function innerCheck(schema: CommonSchema, receivedValue: unknown, exCtx: ExceptionContext): void | {} | null;
|
|
4
6
|
|
|
5
|
-
type RequiredValidation = (received: any, pathToError: string) => void;
|
|
6
7
|
type ObjectShapeSchemaType = Record<string, CommonSchema>;
|
|
7
|
-
type PrimitiveType = 'number' | 'string' | 'boolean' | 'undefined' | 'object' | 'function' | 'symbol' | 'bigint';
|
|
8
8
|
interface ValidatorContext {
|
|
9
9
|
type: PrimitiveType[];
|
|
10
10
|
isNullable?: boolean;
|
|
@@ -12,6 +12,9 @@ interface ValidatorContext {
|
|
|
12
12
|
requiredValidations: RequiredValidation[];
|
|
13
13
|
array?: CommonSchema;
|
|
14
14
|
object?: ObjectShapeSchemaType;
|
|
15
|
+
allowUnrecognizedObjectProps?: boolean;
|
|
16
|
+
strictType?: boolean;
|
|
17
|
+
strictTypeValue?: unknown;
|
|
15
18
|
}
|
|
16
19
|
declare class CommonSchema {
|
|
17
20
|
[ctxSymbol]: ValidatorContext;
|
|
@@ -56,4 +59,4 @@ type WithMix<Y = unknown> = CommonSchema & {
|
|
|
56
59
|
type ExtractFromMix<T> = T extends WithMix<infer X> ? X : never;
|
|
57
60
|
type MapMixTypes<T extends PrimitiveType[]> = T extends (infer U)[] ? U extends keyof TypeMapping ? TypeMapping[U] : never : never;
|
|
58
61
|
|
|
59
|
-
export { CommonSchema as C, type ExtractFromMix as E, type MapMixTypes as M, type ObjectShapeSchemaType as O, type
|
|
62
|
+
export { CommonSchema as C, type ExtractFromMix as E, type MapMixTypes as M, type ObjectShapeSchemaType as O, type ValidatorContext as V, type WithUndefined as W, type WithNull as a, type WithMix as b, ctxSymbol as c, innerCheck as i };
|
package/lib/InferType.d.mts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { ArraySchema, ExtractFromArray } from './schemas/ArraySchema.mjs';
|
|
2
2
|
import { WithBoolean, BooleanSchema, ExtractFromBoolean } from './schemas/BooleanSchema.mjs';
|
|
3
|
-
import { W as WithUndefined, a as WithNull, b as WithMix, E as ExtractFromMix, C as CommonSchema } from './CommonSchema-
|
|
3
|
+
import { W as WithUndefined, a as WithNull, b as WithMix, E as ExtractFromMix, C as CommonSchema } from './CommonSchema-CjsYE4dC.mjs';
|
|
4
4
|
import { WithNumber, NumberSchema, ExtractFromNumber } from './schemas/NumberSchema.mjs';
|
|
5
5
|
import { ObjectSchema, WithObject } from './schemas/ObjectSchema.mjs';
|
|
6
6
|
import { WithString, StringSchema, ExtractFromString } from './schemas/StringSchema.mjs';
|
|
7
|
+
import './commonTypes.mjs';
|
|
7
8
|
|
|
8
9
|
type InferType<T> = T extends WithUndefined<WithNull<WithString<StringSchema>>> ? ExtractFromString<T> | null | undefined : T extends WithUndefined<WithString<StringSchema>> ? ExtractFromString<T> | undefined : T extends WithNull<WithString<StringSchema>> ? ExtractFromString<T> | null : T extends WithString<StringSchema> ? ExtractFromString<T> : T extends WithUndefined<WithNull<StringSchema>> ? string | null | undefined : T extends WithUndefined<StringSchema> ? string | undefined : T extends WithNull<StringSchema> ? string | null : T extends StringSchema ? string : T extends WithUndefined<WithNull<WithNumber<NumberSchema>>> ? ExtractFromNumber<T> | null | undefined : T extends WithUndefined<WithNumber<NumberSchema>> ? ExtractFromNumber<T> | undefined : T extends WithNull<WithNumber<NumberSchema>> ? ExtractFromNumber<T> | null : T extends WithNumber<NumberSchema> ? ExtractFromNumber<T> : T extends WithUndefined<WithNull<NumberSchema>> ? number | null | undefined : T extends WithUndefined<NumberSchema> ? number | undefined : T extends WithNull<NumberSchema> ? number | null : T extends NumberSchema ? number : T extends WithUndefined<WithNull<WithBoolean<BooleanSchema>>> ? ExtractFromBoolean<T> | null | undefined : T extends WithUndefined<WithBoolean<BooleanSchema>> ? ExtractFromBoolean<T> | undefined : T extends WithNull<WithBoolean<BooleanSchema>> ? ExtractFromBoolean<T> | null : T extends WithBoolean<BooleanSchema> ? ExtractFromBoolean<T> : T extends WithUndefined<WithNull<BooleanSchema>> ? boolean | null | undefined : T extends WithUndefined<BooleanSchema> ? boolean | undefined : T extends WithNull<BooleanSchema> ? boolean | null : T extends BooleanSchema ? boolean : T extends WithUndefined<WithNull<ArraySchema>> ? InferType<ExtractFromArray<T>>[] | null | undefined : T extends WithUndefined<ArraySchema> ? InferType<ExtractFromArray<T>>[] | undefined : T extends WithNull<ArraySchema> ? InferType<ExtractFromArray<T>>[] | null : T extends ArraySchema ? InferType<ExtractFromArray<T>>[] : T extends WithUndefined<WithNull<ObjectSchema>> ? ExtractFromObject<T> | null | undefined : T extends WithUndefined<ObjectSchema> ? ExtractFromObject<T> | undefined : T extends WithNull<ObjectSchema> ? ExtractFromObject<T> | null : T extends ObjectSchema ? ExtractFromObject<T> : T extends WithUndefined<WithNull<WithMix>> ? ExtractFromMix<T> | null | undefined : T extends WithUndefined<WithMix> ? ExtractFromMix<T> | undefined : T extends WithNull<WithMix> ? ExtractFromMix<T> | null : T extends WithMix ? ExtractFromMix<T> : unknown;
|
|
9
10
|
type Merge<T> = T extends infer U ? {
|