bguard 0.2.0 → 0.4.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 +777 -447
- package/lib/CommonSchema-DOh3bsL-.d.ts +110 -0
- package/lib/CommonSchema-wi9MOXGF.d.mts +110 -0
- package/lib/InferType.d.mts +6 -14
- package/lib/InferType.d.ts +6 -14
- package/lib/asserts/array/index.d.mts +6 -5
- package/lib/asserts/array/index.d.ts +6 -5
- package/lib/asserts/array/index.js +8 -6
- package/lib/asserts/array/index.js.map +1 -1
- package/lib/asserts/array/index.mjs +7 -5
- package/lib/asserts/array/index.mjs.map +1 -1
- package/lib/asserts/array/maxArrayLength.d.mts +22 -0
- package/lib/asserts/array/maxArrayLength.d.ts +22 -0
- package/lib/asserts/array/maxArrayLength.js +20 -0
- package/lib/asserts/array/maxArrayLength.js.map +1 -0
- package/lib/asserts/array/maxArrayLength.mjs +20 -0
- package/lib/asserts/array/maxArrayLength.mjs.map +1 -0
- package/lib/asserts/array/minArrayLength.d.mts +22 -0
- package/lib/asserts/array/minArrayLength.d.ts +22 -0
- package/lib/asserts/array/minArrayLength.js +20 -0
- package/lib/asserts/array/minArrayLength.js.map +1 -0
- package/lib/asserts/array/minArrayLength.mjs +20 -0
- package/lib/asserts/array/minArrayLength.mjs.map +1 -0
- package/lib/asserts/bigint/bigintMax.js +4 -4
- package/lib/asserts/bigint/bigintMax.mjs +2 -2
- package/lib/asserts/bigint/bigintMaxExcluded.js +4 -4
- package/lib/asserts/bigint/bigintMaxExcluded.mjs +2 -2
- package/lib/asserts/bigint/bigintMin.js +4 -4
- package/lib/asserts/bigint/bigintMin.mjs +2 -2
- package/lib/asserts/bigint/bigintMinExcluded.js +4 -4
- package/lib/asserts/bigint/bigintMinExcluded.mjs +2 -2
- package/lib/asserts/bigint/index.d.mts +5 -4
- package/lib/asserts/bigint/index.d.ts +5 -4
- package/lib/asserts/bigint/index.js +11 -10
- package/lib/asserts/bigint/index.js.map +1 -1
- package/lib/asserts/bigint/index.mjs +10 -9
- package/lib/asserts/bigint/index.mjs.map +1 -1
- package/lib/asserts/boolean/index.d.mts +5 -4
- package/lib/asserts/boolean/index.d.ts +5 -4
- package/lib/asserts/boolean/index.js +9 -7
- package/lib/asserts/boolean/index.js.map +1 -1
- package/lib/asserts/boolean/index.mjs +8 -6
- package/lib/asserts/boolean/index.mjs.map +1 -1
- package/lib/asserts/date/dateMax.js +6 -6
- package/lib/asserts/date/dateMax.js.map +1 -1
- package/lib/asserts/date/dateMax.mjs +5 -5
- package/lib/asserts/date/dateMax.mjs.map +1 -1
- package/lib/asserts/date/dateMin.js +6 -6
- package/lib/asserts/date/dateMin.js.map +1 -1
- package/lib/asserts/date/dateMin.mjs +5 -5
- package/lib/asserts/date/dateMin.mjs.map +1 -1
- package/lib/asserts/date/index.d.mts +5 -4
- package/lib/asserts/date/index.d.ts +5 -4
- package/lib/asserts/date/index.js +8 -6
- package/lib/asserts/date/index.js.map +1 -1
- package/lib/asserts/date/index.mjs +7 -5
- package/lib/asserts/date/index.mjs.map +1 -1
- package/lib/asserts/mix/equalTo.js +4 -4
- package/lib/asserts/mix/equalTo.mjs +3 -3
- package/lib/asserts/mix/index.d.mts +5 -4
- package/lib/asserts/mix/index.d.ts +5 -4
- package/lib/asserts/mix/index.js +7 -5
- package/lib/asserts/mix/index.js.map +1 -1
- package/lib/asserts/mix/index.mjs +6 -4
- package/lib/asserts/mix/index.mjs.map +1 -1
- package/lib/asserts/mix/oneOfValues.js +4 -4
- package/lib/asserts/mix/oneOfValues.mjs +3 -3
- package/lib/asserts/number/index.d.mts +4 -3
- package/lib/asserts/number/index.d.ts +4 -3
- package/lib/asserts/number/index.js +11 -10
- package/lib/asserts/number/index.js.map +1 -1
- package/lib/asserts/number/index.mjs +10 -9
- package/lib/asserts/number/index.mjs.map +1 -1
- package/lib/asserts/number/max.js +4 -4
- package/lib/asserts/number/max.mjs +2 -2
- package/lib/asserts/number/maxExcluded.js +4 -4
- package/lib/asserts/number/maxExcluded.mjs +2 -2
- package/lib/asserts/number/min.js +4 -4
- package/lib/asserts/number/min.mjs +2 -2
- package/lib/asserts/number/minExcluded.js +4 -4
- package/lib/asserts/number/minExcluded.mjs +2 -2
- package/lib/asserts/number/negative.js +4 -4
- package/lib/asserts/number/negative.mjs +2 -2
- package/lib/asserts/number/positive.js +4 -4
- package/lib/asserts/number/positive.mjs +2 -2
- package/lib/asserts/object/index.d.mts +6 -5
- package/lib/asserts/object/index.d.ts +6 -5
- package/lib/asserts/object/index.js +8 -6
- package/lib/asserts/object/index.js.map +1 -1
- package/lib/asserts/object/index.mjs +7 -5
- package/lib/asserts/object/index.mjs.map +1 -1
- package/lib/asserts/object/maxKeys.d.mts +30 -0
- package/lib/asserts/object/maxKeys.d.ts +30 -0
- package/lib/asserts/object/maxKeys.js +23 -0
- package/lib/asserts/object/maxKeys.js.map +1 -0
- package/lib/asserts/object/maxKeys.mjs +23 -0
- package/lib/asserts/object/maxKeys.mjs.map +1 -0
- package/lib/asserts/string/atLeastOneDigit.js +4 -4
- package/lib/asserts/string/atLeastOneDigit.mjs +2 -2
- package/lib/asserts/string/atLeastOneLowerChar.js +4 -4
- package/lib/asserts/string/atLeastOneLowerChar.mjs +2 -2
- package/lib/asserts/string/atLeastOneSpecialChar.js +4 -4
- package/lib/asserts/string/atLeastOneSpecialChar.mjs +2 -2
- package/lib/asserts/string/atLeastOneUpperChar.js +4 -4
- package/lib/asserts/string/atLeastOneUpperChar.mjs +2 -2
- package/lib/asserts/string/contains.js +4 -4
- package/lib/asserts/string/contains.mjs +2 -2
- package/lib/asserts/string/email.js +4 -4
- package/lib/asserts/string/email.mjs +2 -2
- package/lib/asserts/string/endsWith.js +4 -4
- package/lib/asserts/string/endsWith.mjs +2 -2
- package/lib/asserts/string/index.d.mts +5 -4
- package/lib/asserts/string/index.d.ts +5 -4
- package/lib/asserts/string/index.js +11 -10
- package/lib/asserts/string/index.js.map +1 -1
- package/lib/asserts/string/index.mjs +10 -9
- package/lib/asserts/string/index.mjs.map +1 -1
- package/lib/asserts/string/isValidDate.d.mts +21 -0
- package/lib/asserts/string/isValidDate.d.ts +21 -0
- package/lib/asserts/string/isValidDate.js +25 -0
- package/lib/asserts/string/isValidDate.js.map +1 -0
- package/lib/asserts/string/isValidDate.mjs +25 -0
- package/lib/asserts/string/isValidDate.mjs.map +1 -0
- package/lib/asserts/string/isValidDateTime.d.mts +35 -0
- package/lib/asserts/string/isValidDateTime.d.ts +35 -0
- package/lib/asserts/string/isValidDateTime.js +32 -0
- package/lib/asserts/string/isValidDateTime.js.map +1 -0
- package/lib/asserts/string/isValidDateTime.mjs +32 -0
- package/lib/asserts/string/isValidDateTime.mjs.map +1 -0
- package/lib/asserts/string/isValidTime.d.mts +29 -0
- package/lib/asserts/string/isValidTime.d.ts +29 -0
- package/lib/asserts/string/isValidTime.js +32 -0
- package/lib/asserts/string/isValidTime.js.map +1 -0
- package/lib/asserts/string/isValidTime.mjs +32 -0
- package/lib/asserts/string/isValidTime.mjs.map +1 -0
- package/lib/asserts/string/lowerCase.js +4 -4
- package/lib/asserts/string/lowerCase.mjs +2 -2
- package/lib/asserts/string/maxLength.js +4 -4
- package/lib/asserts/string/maxLength.mjs +2 -2
- package/lib/asserts/string/minLength.js +4 -4
- package/lib/asserts/string/minLength.mjs +2 -2
- package/lib/asserts/string/regExp.js +4 -4
- package/lib/asserts/string/regExp.mjs +2 -2
- package/lib/asserts/string/startsWith.js +4 -4
- package/lib/asserts/string/startsWith.mjs +2 -2
- package/lib/asserts/string/upperCase.js +4 -4
- package/lib/asserts/string/upperCase.mjs +2 -2
- package/lib/asserts/string/uuid.js +4 -4
- package/lib/asserts/string/uuid.mjs +2 -2
- package/lib/asserts/string/uuidV1.js +4 -4
- package/lib/asserts/string/uuidV1.mjs +2 -2
- package/lib/asserts/string/uuidV2.js +4 -4
- package/lib/asserts/string/uuidV2.mjs +2 -2
- package/lib/asserts/string/uuidV3.js +4 -4
- package/lib/asserts/string/uuidV3.mjs +2 -2
- package/lib/asserts/string/uuidV4.js +4 -4
- package/lib/asserts/string/uuidV4.mjs +2 -2
- package/lib/asserts/string/uuidV5.js +4 -4
- package/lib/asserts/string/uuidV5.mjs +2 -2
- package/lib/asserts/string/validUrl.js +4 -4
- package/lib/asserts/string/validUrl.mjs +2 -2
- package/lib/{chunk-QLVLZRHT.js → chunk-2KQASECD.js} +5 -5
- package/lib/chunk-2KQASECD.js.map +1 -0
- package/lib/{chunk-WK4O3HH4.js → chunk-5P6AZJCN.js} +6 -5
- package/lib/chunk-5P6AZJCN.js.map +1 -0
- package/lib/{chunk-VGF3Y2EK.js → chunk-5WHCMWXK.js} +12 -9
- package/lib/chunk-5WHCMWXK.js.map +1 -0
- package/lib/{chunk-CQWI6YRI.js → chunk-5YYRBKDP.js} +4 -4
- package/lib/{chunk-CQWI6YRI.js.map → chunk-5YYRBKDP.js.map} +1 -1
- package/lib/{chunk-PJI3DRGP.mjs → chunk-6D5W7SSQ.mjs} +4 -4
- package/lib/chunk-6D5W7SSQ.mjs.map +1 -0
- package/lib/chunk-7NBOVYSP.mjs +136 -0
- package/lib/chunk-7NBOVYSP.mjs.map +1 -0
- package/lib/{chunk-XZTZC73V.mjs → chunk-BNCT7NOR.mjs} +36 -14
- package/lib/chunk-BNCT7NOR.mjs.map +1 -0
- package/lib/{chunk-M6OCVAUD.mjs → chunk-C3EDEQ3W.mjs} +3 -3
- package/lib/{chunk-Z7OEFOAD.mjs → chunk-CHUPE6SJ.mjs} +8 -8
- package/lib/chunk-CHUPE6SJ.mjs.map +1 -0
- package/lib/{chunk-4SR7J2I4.js → chunk-ECBOZ3BM.js} +8 -8
- package/lib/chunk-ECBOZ3BM.js.map +1 -0
- package/lib/{chunk-7XFLH6R2.mjs → chunk-FGZKNBDP.mjs} +6 -5
- package/lib/chunk-FGZKNBDP.mjs.map +1 -0
- package/lib/{chunk-TP5I237J.mjs → chunk-FP5QAEVQ.mjs} +14 -12
- package/lib/chunk-FP5QAEVQ.mjs.map +1 -0
- package/lib/{chunk-MTJJKWTP.js → chunk-G55SHHAR.js} +10 -8
- package/lib/chunk-G55SHHAR.js.map +1 -0
- package/lib/{chunk-QV4JDW3T.mjs → chunk-H32B3NHW.mjs} +2 -2
- package/lib/{chunk-54RHVQFA.mjs → chunk-H6LHV3EA.mjs} +2 -2
- package/lib/{chunk-WPHJ4QTB.js → chunk-HP3DRZWK.js} +5 -5
- package/lib/{chunk-WPHJ4QTB.js.map → chunk-HP3DRZWK.js.map} +1 -1
- package/lib/{chunk-4SPM2CSQ.js → chunk-IZ3CBVEE.js} +5 -5
- package/lib/{chunk-4SPM2CSQ.js.map → chunk-IZ3CBVEE.js.map} +1 -1
- package/lib/{chunk-YPAUFM3S.mjs → chunk-K7X6DVM2.mjs} +3 -3
- package/lib/{chunk-PEWNA24X.mjs → chunk-LPR3G5T3.mjs} +4 -4
- package/lib/chunk-LPR3G5T3.mjs.map +1 -0
- package/lib/{chunk-XFR7Y4XX.mjs → chunk-MHBY5OIQ.mjs} +3 -3
- package/lib/chunk-MHBY5OIQ.mjs.map +1 -0
- package/lib/{chunk-5EIBKBYS.js → chunk-MLNQZ445.js} +13 -13
- package/lib/chunk-MLNQZ445.js.map +1 -0
- package/lib/chunk-QIDWASOC.js +14 -0
- package/lib/{chunk-LWAEUS2F.js.map → chunk-QIDWASOC.js.map} +1 -1
- package/lib/chunk-SHJVKSEZ.js +136 -0
- package/lib/chunk-SHJVKSEZ.js.map +1 -0
- package/lib/{chunk-MRWZVTNH.mjs → chunk-TRGMD7LA.mjs} +7 -5
- package/lib/chunk-TRGMD7LA.mjs.map +1 -0
- package/lib/{chunk-5AO66YV5.mjs → chunk-TV5IZY4L.mjs} +9 -6
- package/lib/chunk-TV5IZY4L.mjs.map +1 -0
- package/lib/{chunk-N6YMNLXG.js → chunk-U4USIJV3.js} +11 -11
- package/lib/chunk-U4USIJV3.js.map +1 -0
- package/lib/{chunk-SEIKS6G7.js → chunk-UUY7DPHK.js} +19 -15
- package/lib/chunk-UUY7DPHK.js.map +1 -0
- package/lib/{chunk-356HMXOP.js → chunk-VBTWYDEM.js} +4 -4
- package/lib/chunk-VBTWYDEM.js.map +1 -0
- package/lib/chunk-VW4RMQD4.js +28 -0
- package/lib/chunk-VW4RMQD4.js.map +1 -0
- package/lib/{chunk-UV4I7JZR.mjs → chunk-WK4FJ5GD.mjs} +16 -12
- package/lib/chunk-WK4FJ5GD.mjs.map +1 -0
- package/lib/chunk-X762EUSZ.js +91 -0
- package/lib/chunk-X762EUSZ.js.map +1 -0
- package/lib/{chunk-GDSB7CDN.mjs → chunk-YBVMP4EI.mjs} +4 -4
- package/lib/chunk-YBVMP4EI.mjs.map +1 -0
- package/lib/{chunk-PVOHKGBW.mjs → chunk-YUMUCQKL.mjs} +4 -4
- package/lib/chunk-YUMUCQKL.mjs.map +1 -0
- package/lib/{chunk-32J4WVLE.js → chunk-Z6QU36MM.js} +17 -15
- package/lib/chunk-Z6QU36MM.js.map +1 -0
- package/lib/codeGen.d.mts +2 -1
- package/lib/codeGen.d.ts +2 -1
- package/lib/codeGen.js +4 -4
- package/lib/codeGen.mjs +3 -3
- package/lib/commonTypes.d.mts +26 -2
- package/lib/commonTypes.d.ts +26 -2
- package/lib/exceptions.d.mts +3 -2
- package/lib/exceptions.d.ts +3 -2
- package/lib/exceptions.js +2 -2
- package/lib/exceptions.mjs +1 -1
- package/lib/helpers/core.d.mts +2 -1
- package/lib/helpers/core.d.ts +2 -1
- package/lib/helpers/core.js +4 -4
- package/lib/helpers/core.mjs +3 -3
- package/lib/helpers/isValidDateInner.d.mts +3 -0
- package/lib/helpers/isValidDateInner.d.ts +3 -0
- package/lib/helpers/isValidDateInner.js +7 -0
- package/lib/helpers/isValidDateInner.js.map +1 -0
- package/lib/helpers/isValidDateInner.mjs +7 -0
- package/lib/helpers/setStrictType.d.mts +2 -1
- package/lib/helpers/setStrictType.d.ts +2 -1
- package/lib/helpers/setStrictType.js +5 -5
- package/lib/helpers/setStrictType.mjs +4 -4
- package/lib/index.d.mts +1 -8
- package/lib/index.d.ts +1 -8
- package/lib/index.js +7 -7
- package/lib/index.mjs +6 -6
- package/lib/parse.d.mts +1 -8
- package/lib/parse.d.ts +1 -8
- package/lib/parse.js +6 -6
- package/lib/parse.mjs +5 -5
- package/lib/parseOrFail.d.mts +1 -8
- package/lib/parseOrFail.d.ts +1 -8
- package/lib/parseOrFail.js +6 -6
- package/lib/parseOrFail.mjs +5 -5
- package/lib/schemas/ArraySchema.d.mts +4 -7
- package/lib/schemas/ArraySchema.d.ts +4 -7
- package/lib/schemas/ArraySchema.js +8 -6
- package/lib/schemas/ArraySchema.js.map +1 -1
- package/lib/schemas/ArraySchema.mjs +7 -5
- package/lib/schemas/BigIntSchema.d.mts +8 -10
- package/lib/schemas/BigIntSchema.d.ts +8 -10
- package/lib/schemas/BigIntSchema.js +11 -10
- package/lib/schemas/BigIntSchema.js.map +1 -1
- package/lib/schemas/BigIntSchema.mjs +10 -9
- package/lib/schemas/BooleanSchema.d.mts +7 -10
- package/lib/schemas/BooleanSchema.d.ts +7 -10
- package/lib/schemas/BooleanSchema.js +9 -7
- package/lib/schemas/BooleanSchema.js.map +1 -1
- package/lib/schemas/BooleanSchema.mjs +8 -6
- package/lib/schemas/CommonSchema.d.mts +2 -1
- package/lib/schemas/CommonSchema.d.ts +2 -1
- package/lib/schemas/CommonSchema.js +7 -5
- package/lib/schemas/CommonSchema.js.map +1 -1
- package/lib/schemas/CommonSchema.mjs +6 -4
- package/lib/schemas/DateSchema.d.mts +3 -2
- package/lib/schemas/DateSchema.d.ts +3 -2
- package/lib/schemas/DateSchema.js +8 -6
- package/lib/schemas/DateSchema.js.map +1 -1
- package/lib/schemas/DateSchema.mjs +7 -5
- package/lib/schemas/NumberSchema.d.mts +7 -10
- package/lib/schemas/NumberSchema.d.ts +7 -10
- package/lib/schemas/NumberSchema.js +11 -10
- package/lib/schemas/NumberSchema.js.map +1 -1
- package/lib/schemas/NumberSchema.mjs +10 -9
- package/lib/schemas/ObjectSchema.d.mts +4 -6
- package/lib/schemas/ObjectSchema.d.ts +4 -6
- package/lib/schemas/ObjectSchema.js +8 -6
- package/lib/schemas/ObjectSchema.js.map +1 -1
- package/lib/schemas/ObjectSchema.mjs +7 -5
- package/lib/schemas/StringSchema.d.mts +8 -10
- package/lib/schemas/StringSchema.d.ts +8 -10
- package/lib/schemas/StringSchema.js +11 -10
- package/lib/schemas/StringSchema.js.map +1 -1
- package/lib/schemas/StringSchema.mjs +10 -9
- package/lib/translationMap.js +3 -3
- package/lib/translationMap.mjs +2 -2
- package/package.json +1 -1
- package/lib/CommonSchema-9RtxTxdX.d.ts +0 -63
- package/lib/CommonSchema-DJmQ3F_9.d.mts +0 -63
- package/lib/chunk-2OINKFOA.js +0 -28
- package/lib/chunk-2OINKFOA.js.map +0 -1
- package/lib/chunk-32J4WVLE.js.map +0 -1
- package/lib/chunk-356HMXOP.js.map +0 -1
- package/lib/chunk-4SR7J2I4.js.map +0 -1
- package/lib/chunk-4UF67IQ2.mjs +0 -42
- package/lib/chunk-4UF67IQ2.mjs.map +0 -1
- package/lib/chunk-5AO66YV5.mjs.map +0 -1
- package/lib/chunk-5EIBKBYS.js.map +0 -1
- package/lib/chunk-7GIRQ5EA.js +0 -69
- package/lib/chunk-7GIRQ5EA.js.map +0 -1
- package/lib/chunk-7XFLH6R2.mjs.map +0 -1
- package/lib/chunk-EQNVYAKS.js +0 -42
- package/lib/chunk-EQNVYAKS.js.map +0 -1
- package/lib/chunk-GDSB7CDN.mjs.map +0 -1
- package/lib/chunk-LWAEUS2F.js +0 -14
- package/lib/chunk-MRWZVTNH.mjs.map +0 -1
- package/lib/chunk-MTJJKWTP.js.map +0 -1
- package/lib/chunk-N6YMNLXG.js.map +0 -1
- package/lib/chunk-PEWNA24X.mjs.map +0 -1
- package/lib/chunk-PJI3DRGP.mjs.map +0 -1
- package/lib/chunk-PVOHKGBW.mjs.map +0 -1
- package/lib/chunk-QLVLZRHT.js.map +0 -1
- package/lib/chunk-SEIKS6G7.js.map +0 -1
- package/lib/chunk-TP5I237J.mjs.map +0 -1
- package/lib/chunk-UV4I7JZR.mjs.map +0 -1
- package/lib/chunk-VGF3Y2EK.js.map +0 -1
- package/lib/chunk-WK4O3HH4.js.map +0 -1
- package/lib/chunk-XFR7Y4XX.mjs.map +0 -1
- package/lib/chunk-XZTZC73V.mjs.map +0 -1
- package/lib/chunk-Z7OEFOAD.mjs.map +0 -1
- package/lib/helpers/isValidDate.d.mts +0 -3
- package/lib/helpers/isValidDate.d.ts +0 -3
- package/lib/helpers/isValidDate.js +0 -7
- package/lib/helpers/isValidDate.js.map +0 -1
- package/lib/helpers/isValidDate.mjs +0 -7
- /package/lib/{chunk-M6OCVAUD.mjs.map → chunk-C3EDEQ3W.mjs.map} +0 -0
- /package/lib/{chunk-QV4JDW3T.mjs.map → chunk-H32B3NHW.mjs.map} +0 -0
- /package/lib/{chunk-54RHVQFA.mjs.map → chunk-H6LHV3EA.mjs.map} +0 -0
- /package/lib/{chunk-YPAUFM3S.mjs.map → chunk-K7X6DVM2.mjs.map} +0 -0
- /package/lib/helpers/{isValidDate.mjs.map → isValidDateInner.mjs.map} +0 -0
package/README.md
CHANGED
|
@@ -4,7 +4,82 @@
|
|
|
4
4
|
|
|
5
5
|
  
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Table of contents
|
|
8
|
+
|
|
9
|
+
* [Features](#h3_features)
|
|
10
|
+
* [Installation](#h3_installation)
|
|
11
|
+
* [Usage](#h3_usage)
|
|
12
|
+
* [Defining a Schema](#h4_usage_defining_a_schema)
|
|
13
|
+
* [Inferring TypeScript Types](#h4_usage_inferring_typescript_types)
|
|
14
|
+
* [Generating TypeScript Types with `codeGen`](#h4_usage_generating_typescript_types_with_codeGen)
|
|
15
|
+
* [Generating Named TypeScript Types with `codeGenWithName`](#h4_usage_generating_typescript_types_with_codeGenWithName)
|
|
16
|
+
* [Summary:](#h4_usage_summary)
|
|
17
|
+
* [Validating Data](#h3_validating_data)
|
|
18
|
+
* [`parse` Method](#h4_validating_data_parse)
|
|
19
|
+
* [`parseOrFail` Method](#h4_validating_data_parseOrFail)
|
|
20
|
+
* [Chaining Methods](#h3_chaining_methods)
|
|
21
|
+
* [nullable()](#h4_chaining_nullable)
|
|
22
|
+
* [optional()](#h4_chaining_optional)
|
|
23
|
+
* [id(value: string)](#h4_chaining_id)
|
|
24
|
+
* [description(value: string)](#h4_chaining_description)
|
|
25
|
+
* [Literals](#h3_literals)
|
|
26
|
+
* [Custom (Library Built-in) Assertions](#h3_custom_builtin_assertions)
|
|
27
|
+
* [Create Custom Assertions](#h3_create_custom_assertions)
|
|
28
|
+
* [Translation](#translation)
|
|
29
|
+
* [Using Translations](#h4_using_translation)
|
|
30
|
+
* [Common and Custom Translations](#common_and_custom_translations)
|
|
31
|
+
|
|
32
|
+
* [Built-in Custom Assert Documentation](#builtin_custom_assert_documentation)
|
|
33
|
+
|
|
34
|
+
* [string](#assertdir_string)
|
|
35
|
+
* [atLeastOneDigit](#assert_atLeastOneDigit_string)
|
|
36
|
+
* [atLeastOneLowerChar](#assert_atLeastOneLowerChar_string)
|
|
37
|
+
* [atLeastOneSpecialChar](#assert_atLeastOneSpecialChar_string)
|
|
38
|
+
* [atLeastOneUpperChar](#assert_atLeastOneUpperChar_string)
|
|
39
|
+
* [contains](#assert_contains_string)
|
|
40
|
+
* [email](#assert_email_string)
|
|
41
|
+
* [endsWith](#assert_endsWith_string)
|
|
42
|
+
* [isValidDate](#assert_isValidDate_string)
|
|
43
|
+
* [isValidDateTime](#assert_isValidDateTime_string)
|
|
44
|
+
* [isValidTime](#assert_isValidTime_string)
|
|
45
|
+
* [lowerCase](#assert_lowerCase_string)
|
|
46
|
+
* [maxLength](#assert_maxLength_string)
|
|
47
|
+
* [minLength](#assert_minLength_string)
|
|
48
|
+
* [regExp](#assert_regExp_string)
|
|
49
|
+
* [startsWith](#assert_startsWith_string)
|
|
50
|
+
* [upperCase](#assert_upperCase_string)
|
|
51
|
+
* [uuid](#assert_uuid_string)
|
|
52
|
+
* [uuidV1](#assert_uuidV1_string)
|
|
53
|
+
* [uuidV2](#assert_uuidV2_string)
|
|
54
|
+
* [uuidV3](#assert_uuidV3_string)
|
|
55
|
+
* [uuidV4](#assert_uuidV4_string)
|
|
56
|
+
* [uuidV5](#assert_uuidV5_string)
|
|
57
|
+
* [validUrl](#assert_validUrl_string)
|
|
58
|
+
* [number](#assertdir_number)
|
|
59
|
+
* [max](#assert_max_number)
|
|
60
|
+
* [maxExcluded](#assert_maxExcluded_number)
|
|
61
|
+
* [min](#assert_min_number)
|
|
62
|
+
* [minExcluded](#assert_minExcluded_number)
|
|
63
|
+
* [negative](#assert_negative_number)
|
|
64
|
+
* [positive](#assert_positive_number)
|
|
65
|
+
* [array](#assertdir_array)
|
|
66
|
+
* [maxArrayLength](#assert_maxArrayLength_array)
|
|
67
|
+
* [minArrayLength](#assert_minArrayLength_array)
|
|
68
|
+
* [bigint](#assertdir_bigint)
|
|
69
|
+
* [bigintMax](#assert_bigintMax_bigint)
|
|
70
|
+
* [bigintMaxExcluded](#assert_bigintMaxExcluded_bigint)
|
|
71
|
+
* [bigintMin](#assert_bigintMin_bigint)
|
|
72
|
+
* [bigintMinExcluded](#assert_bigintMinExcluded_bigint)
|
|
73
|
+
* [date](#assertdir_date)
|
|
74
|
+
* [dateMax](#assert_dateMax_date)
|
|
75
|
+
* [dateMin](#assert_dateMin_date)
|
|
76
|
+
* [mix](#assertdir_mix)
|
|
77
|
+
* [equalTo](#assert_equalTo_mix)
|
|
78
|
+
* [oneOfValues](#assert_oneOfValues_mix)
|
|
79
|
+
* [object](#assertdir_object)
|
|
80
|
+
* [maxKeys](#assert_maxKeys_object)
|
|
81
|
+
|
|
82
|
+
### <a id="h3_features"> Features </a>
|
|
8
83
|
|
|
9
84
|
- **Type Inference**: Automatically infer TypeScript types from your validation schemas.
|
|
10
85
|
- **Custom Assertions**: Add custom validation logic for your schemas.
|
|
@@ -14,17 +89,17 @@
|
|
|
14
89
|
- **Small Bundle Size**: Each assertion is in its own file, minimizing your final bundle size.
|
|
15
90
|
- **Lightweight**: No dependencies and optimized for performance.
|
|
16
91
|
|
|
17
|
-
### Installation
|
|
92
|
+
### <a id="h3_installation"> Installation </a>
|
|
18
93
|
|
|
19
94
|
```bash
|
|
20
95
|
npm install bguard
|
|
21
96
|
```
|
|
22
97
|
|
|
23
|
-
### Usage
|
|
98
|
+
### <a id="h3_usage"> Usage </a>
|
|
24
99
|
|
|
25
100
|
Here’s a basic example of how to use `bguard` to define and validate a schema.
|
|
26
101
|
|
|
27
|
-
#### Defining a Schema
|
|
102
|
+
#### <a id="h4_usage_defining_a_schema"> Defining a Schema </a>
|
|
28
103
|
|
|
29
104
|
Let's define a schema for a Student object:
|
|
30
105
|
|
|
@@ -57,7 +132,7 @@ const studentSchema = object({
|
|
|
57
132
|
|
|
58
133
|
```
|
|
59
134
|
|
|
60
|
-
#### Inferring TypeScript Types
|
|
135
|
+
#### <a id="h4_usage_inferring_typescript_types"> Inferring TypeScript Types </a>
|
|
61
136
|
|
|
62
137
|
Using the InferType utility, you can infer the TypeScript type of the schema:
|
|
63
138
|
|
|
@@ -83,7 +158,7 @@ type StudentSchema = {
|
|
|
83
158
|
|
|
84
159
|
```
|
|
85
160
|
|
|
86
|
-
#### Generating TypeScript Types with `codeGen`
|
|
161
|
+
#### <a id="h4_usage_generating_typescript_types_with_codeGen"> Generating TypeScript Types with `codeGen` </a>
|
|
87
162
|
|
|
88
163
|
If you prefer to generate TypeScript types as a string, you can use the `codeGen` function:
|
|
89
164
|
|
|
@@ -116,9 +191,9 @@ This would output a string:
|
|
|
116
191
|
}
|
|
117
192
|
```
|
|
118
193
|
|
|
119
|
-
|
|
194
|
+
> **Notice:** 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.
|
|
120
195
|
|
|
121
|
-
#### Generating Named TypeScript Types with `codeGenWithName`
|
|
196
|
+
#### <a id="h4_usage_generating_typescript_types_with_codeGenWithName"> Generating Named TypeScript Types with `codeGenWithName` </a>
|
|
122
197
|
|
|
123
198
|
For convenience, if you want to generate a complete type definition including a name, use the `codeGenWithName` function:
|
|
124
199
|
|
|
@@ -150,14 +225,13 @@ type StudentSchema = {
|
|
|
150
225
|
verified?: boolean | undefined;
|
|
151
226
|
}
|
|
152
227
|
```
|
|
153
|
-
|
|
154
|
-
#### Summary:
|
|
228
|
+
#### <a id="h4_usage_summary"> Summary: </a>
|
|
155
229
|
|
|
156
230
|
`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.
|
|
157
231
|
|
|
158
232
|
`codeGenWithName(typeName: string, schema: CommonSchema): string` - Generates a complete TypeScript type definition string, including the type keyword and type name.
|
|
159
233
|
|
|
160
|
-
### Validating Data
|
|
234
|
+
### <a id="h3_validating_data"> Validating Data </a>
|
|
161
235
|
|
|
162
236
|
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.
|
|
163
237
|
|
|
@@ -193,7 +267,7 @@ const invalidStudentData = {
|
|
|
193
267
|
};
|
|
194
268
|
```
|
|
195
269
|
|
|
196
|
-
#### `parse` Method
|
|
270
|
+
#### <a id="h4_validating_data_parse"> `parse` Method </a>
|
|
197
271
|
|
|
198
272
|
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.
|
|
199
273
|
|
|
@@ -216,7 +290,7 @@ Options:
|
|
|
216
290
|
- `lng`: Specifies the language for error messages. Default is `'default'`.
|
|
217
291
|
- `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.
|
|
218
292
|
|
|
219
|
-
#### `parseOrFail` Method
|
|
293
|
+
#### <a id="h4_validating_data_parseOrFail"> `parseOrFail` Method </a>
|
|
220
294
|
|
|
221
295
|
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
296
|
|
|
@@ -244,8 +318,6 @@ Options:
|
|
|
244
318
|
|
|
245
319
|
- `lng`: Specifies the language for error messages. Default is `'default'`.
|
|
246
320
|
|
|
247
|
-
####
|
|
248
|
-
|
|
249
321
|
Explanation
|
|
250
322
|
|
|
251
323
|
- **`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.
|
|
@@ -254,31 +326,117 @@ Explanation
|
|
|
254
326
|
|
|
255
327
|
- **Options**: Both methods accept options for language settings and error collection, enhancing flexibility in handling validation processes.
|
|
256
328
|
|
|
257
|
-
### Chaining Methods
|
|
329
|
+
### <a id="h3_chaining_methods"> Chaining Methods </a>
|
|
258
330
|
|
|
259
|
-
|
|
260
|
-
|
|
331
|
+
#### <a id="h4_chaining_nullable"> nullable() </a>
|
|
332
|
+
|
|
333
|
+
Allows the value to be `null`.
|
|
261
334
|
|
|
262
335
|
Example:
|
|
263
336
|
|
|
264
337
|
```typeScript
|
|
338
|
+
const nullableSchema = string().nullable().optional();
|
|
339
|
+
// This schema allows string or null values.
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
#### <a id="h4_chaining_optional"> optional() </a>
|
|
343
|
+
Allows the value to be `undefined`.
|
|
344
|
+
|
|
345
|
+
_Example_:
|
|
346
|
+
|
|
347
|
+
```typeScript
|
|
348
|
+
const optionalSchema = string().nullable().optional();
|
|
349
|
+
// This schema allows string or undefined values.
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
#### <a id="h4_chaining_default"> default(value: InferType<this>)</a>
|
|
353
|
+
Sets a default value if the received value is `undefined`. The default value must match the inferred type of the schema, ensuring compatibility.
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
> **Notice:** You cannot chain `default()` and `optional()` together, as they are contradictory. The `optional()` method allows the value to be `undefined`, while the `default()` method assigns a value if `undefined`. Attempting to chain both will throw a `BuildSchemaError` with the message: `"Cannot call method 'default' after method 'optional'"`.
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
> **Notice:** Additionally, `default()` must be the last method in the chain because it validates during schema build time that the default value is compatible with the rest of the schema. For example, if the schema is `number()`, the default value cannot be a `string`.
|
|
360
|
+
|
|
361
|
+
_Example_:
|
|
362
|
+
|
|
363
|
+
```typeScript
|
|
364
|
+
const schemaWithDefault = string().nullable().default('defaultString');
|
|
365
|
+
// This schema allows null values and sets 'defaultString' if the value is undefined.
|
|
366
|
+
|
|
367
|
+
const optionalSchema = string().nullable().optional();
|
|
368
|
+
// This schema allows both null and undefined values, but it does not provide a default value.
|
|
369
|
+
```
|
|
370
|
+
#### <a id="h4_chaining_id"> id(value: string) </a>
|
|
371
|
+
|
|
372
|
+
Assigns a unique identifier to the schema, useful for tracking or mapping validation errors. The `id` can be accessed via `err.meta?.id` in case of a validation error.
|
|
373
|
+
|
|
374
|
+
#### <a id="h4_chaining_description"> description(value: string) </a>
|
|
375
|
+
|
|
376
|
+
Provides a description for the schema, which can be used to give more context about the validation error. The `description` can be accessed via `err.meta?.description` in case of a validation error.
|
|
377
|
+
|
|
378
|
+
_Example_:
|
|
379
|
+
|
|
380
|
+
```typeScript
|
|
381
|
+
const addressSchema = string()
|
|
382
|
+
.id('address')
|
|
383
|
+
.description('Users address');
|
|
384
|
+
// This schema validates that string and assigns an ID and description for better error handling.
|
|
385
|
+
|
|
386
|
+
try {
|
|
387
|
+
parseOrFail(addressSchema, undefined);
|
|
388
|
+
} catch (e) {
|
|
389
|
+
const err = e as ValidationError;
|
|
390
|
+
console.log(err.message); // Output: 'The required value is missing'
|
|
391
|
+
console.log(err.pathToError); // Output: ''
|
|
392
|
+
console.log(err.meta?.id); // Output: 'address'
|
|
393
|
+
console.log(err.meta?.description); // Output: 'Users address'
|
|
394
|
+
}
|
|
395
|
+
```
|
|
396
|
+
#### <a id="h4_chaining_transformbeforevalidation"> transformBeforeValidation\<In\>(cb: TransformCallback<In, InferType\<Schema>\>) </a>
|
|
397
|
+
|
|
398
|
+
This method allows you to apply a transformation to the input value before any validation occurs. The transformation is applied before the schema's other methods (like `nullable`, `custom`, etc.). The callback function can receive an input of type `unknown` by default, but you can specify the type if you know it, such as `string`. The return value of the callback must be of the same type as the inferred type of the schema, ensuring that the overall type does not change.
|
|
399
|
+
|
|
400
|
+
<b>Order of Execution</b>:
|
|
401
|
+
First, transformations specified using transformBeforeValidation are applied.
|
|
402
|
+
Then, the schema checks for null or undefined based on methods like `nullable` or `optional`.
|
|
403
|
+
Finally, the `custom` validations and type checks are performed.
|
|
404
|
+
|
|
405
|
+
This method is particularly useful for normalizing or preparing data before validation, such as trimming whitespace, converting empty strings to null, or handling other preprocessing needs.
|
|
265
406
|
|
|
266
|
-
|
|
407
|
+
> **Notice:** Like default, `transformBeforeValidation` should be placed at the end of the chain. This ensures that the transformation is correctly applied after all other type checks are resolved, preserving the expected type.
|
|
408
|
+
|
|
409
|
+
_Example_:
|
|
410
|
+
|
|
411
|
+
```typescript
|
|
412
|
+
const stringOrNullSchema = string()
|
|
413
|
+
.nullable()
|
|
414
|
+
.custom(minLength(3))
|
|
415
|
+
.transformBeforeValidation((val) => val + '') // First, transform value to a string
|
|
416
|
+
.transformBeforeValidation((val: string) => (val === '' ? null : val)); // Second, convert empty strings to null
|
|
417
|
+
|
|
418
|
+
// Parsing 'test' will pass as 'test' is a valid string longer than 3 characters.
|
|
419
|
+
parseOrFail(stringOrNullSchema, 'test');
|
|
420
|
+
|
|
421
|
+
// Parsing '' will be transformed to null and will pass due to .nullable().
|
|
422
|
+
parseOrFail(stringOrNullSchema, '');
|
|
267
423
|
```
|
|
268
424
|
|
|
269
|
-
|
|
425
|
+
### <a id="h3_literals"> Literals </a>
|
|
426
|
+
|
|
427
|
+
- <b>String Literals</b>:
|
|
270
428
|
`string().equalTo('myStringValue')` will infer <b>'myStringValue'</b> as the type.
|
|
271
429
|
`string().oneOfValues(['foo', 'bar'])` will infer <b>'foo' | 'bar'</b> as the type.
|
|
272
430
|
|
|
273
|
-
- Number Literals
|
|
431
|
+
- <b>Number Literals</b>:
|
|
274
432
|
`number().equalTo(42)` will infer <b>42</b> as the type.
|
|
275
433
|
`number().oneOfValues([3, 5])` will infer <b>3 | 5</b> as the type.
|
|
276
434
|
|
|
277
|
-
- Boolean Literals
|
|
435
|
+
- <b>Boolean Literals</b>:
|
|
278
436
|
`boolean().onlyTrue()` will infer <b>true</b> as the type.
|
|
279
437
|
`boolean().onlyFalse()` will infer <b>false</b> as the type.
|
|
280
438
|
|
|
281
|
-
### Custom (Library Built-in) Assertions
|
|
439
|
+
### <a id="h3_custom_builtin_assertions"> Custom (Library Built-in) Assertions </a>
|
|
282
440
|
|
|
283
441
|
The `custom` method allows you to extend the validation schema with additional asserts. These asserts can either be user-defined or selected from the comprehensive set provided by the library. This flexibility ensures that you can tailor validations to meet specific requirements beyond the standard methods available.
|
|
284
442
|
All built-in asserts are documented in the [Built-in Custom Assert Documentation](#builtin_custom_assert_documentation) section.
|
|
@@ -294,7 +452,7 @@ const ageSchema = number().custom(min(18), max(120));
|
|
|
294
452
|
|
|
295
453
|
Library built-in assertions are imported from specific paths for better tree-shaking and smaller bundle sizes.
|
|
296
454
|
|
|
297
|
-
### Create Custom Assertions
|
|
455
|
+
### <a id="h3_create_custom_assertions"> Create Custom Assertions </a>
|
|
298
456
|
|
|
299
457
|
Bguard allows developers to create custom validation functions that can be integrated seamlessly with the library's existing functionality. Below is a detailed example demonstrating how to create a custom validation function, `minLength`, and how to properly document and map error messages for translations.
|
|
300
458
|
|
|
@@ -338,7 +496,7 @@ Explanation
|
|
|
338
496
|
3. The `minLengthErrorMessage` serves as the default message. If you want to provide translations, you can do so by mapping the error key in the translationMap.
|
|
339
497
|
For single-language applications, you can override the default message by directly passing your custom message to `guardException`.
|
|
340
498
|
|
|
341
|
-
### Translation
|
|
499
|
+
### <a id="translation"> Translation </a>
|
|
342
500
|
|
|
343
501
|
Bguard provides default translations for error messages, but you can customize them as needed. Each potential error has an `errorKey` and `errorMessage`.
|
|
344
502
|
|
|
@@ -378,7 +536,7 @@ With this setup, in the translation namespace 'SR', if the received value is 4,
|
|
|
378
536
|
|
|
379
537
|
> **Notice:** Do not overwrite the 'default' namespace. If a translation is missing, it will fall back to the 'default' translation.
|
|
380
538
|
|
|
381
|
-
#### Using Translations
|
|
539
|
+
#### <a id="h4_using_translation"> Using Translations </a>
|
|
382
540
|
|
|
383
541
|
To apply the new translation, both `parse` and `parseOrFail` functions accept a lng property in the options object provided as the third parameter:
|
|
384
542
|
|
|
@@ -388,7 +546,7 @@ parseOrFail(testSchema, { foo: 4 }, { lng: 'SR' });
|
|
|
388
546
|
parse(testSchema, { foo: 4 }, { lng: 'SR' });
|
|
389
547
|
```
|
|
390
548
|
|
|
391
|
-
#### Common and Custom Translations
|
|
549
|
+
#### <a id="common_and_custom_translations"> Common and Custom Translations </a>
|
|
392
550
|
|
|
393
551
|
We have two sets of translations: common errors and specific assertions.
|
|
394
552
|
|
|
@@ -451,525 +609,259 @@ console.log(maxLength.message); // Output: 'The received value length is greater
|
|
|
451
609
|
|
|
452
610
|
<b>3.</b> IDE Support:
|
|
453
611
|
Each key and message will be visible in text editors that support JSDoc IntelliSense.
|
|
612
|
+
### <a id="builtin_custom_assert_documentation"> Built-in Custom Assert Documentation </a>
|
|
454
613
|
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
#### bigint
|
|
614
|
+
#### <a id="assertdir_string"> string </a>
|
|
458
615
|
|
|
459
616
|
<b>Prerequisites</b>
|
|
460
617
|
|
|
461
618
|
```typescript
|
|
462
|
-
import {
|
|
619
|
+
import { string } from 'bguard/string';
|
|
463
620
|
```
|
|
464
621
|
|
|
465
|
-
* _Description_ Creates a new schema for validating
|
|
622
|
+
* _Description_ Creates a new schema for validating string values.
|
|
466
623
|
* _Example_
|
|
467
624
|
```typescript
|
|
468
|
-
const schema =
|
|
469
|
-
parseOrFail(schema,
|
|
470
|
-
parseOrFail(schema,
|
|
471
|
-
parseOrFail(schema, '42'); // Throws a validation error
|
|
625
|
+
const schema = string();
|
|
626
|
+
parseOrFail(schema, 'hello'); // Validates successfully
|
|
627
|
+
parseOrFail(schema, 123); // Throws a validation error
|
|
472
628
|
```
|
|
473
629
|
|
|
474
630
|
|
|
475
|
-
#####
|
|
631
|
+
##### <a id="assert_atLeastOneDigit_string"> atLeastOneDigit </a>
|
|
476
632
|
|
|
477
633
|
```typescript
|
|
478
|
-
import {
|
|
634
|
+
import { atLeastOneDigit } from 'bguard/string/atLeastOneDigit';
|
|
479
635
|
```
|
|
480
636
|
|
|
481
|
-
* _Description_ Asserts that a
|
|
482
|
-
*
|
|
483
|
-
* _Throws_ {ValidationError} if the received value exceeds the expected maximum value.
|
|
637
|
+
* _Description_ Asserts that a string value contains at least one digit.
|
|
638
|
+
* _Throws_ {ValidationError} if the received value does not contain at least one digit.
|
|
484
639
|
* _Example_
|
|
485
640
|
```typescript
|
|
486
|
-
const schema =
|
|
487
|
-
parseOrFail(schema,
|
|
488
|
-
parseOrFail(schema,
|
|
489
|
-
parseOrFail(schema, 101n); // Throws an error: 'The received value is greater than expected'
|
|
641
|
+
const schema = string().custom(atLeastOneDigit());
|
|
642
|
+
parseOrFail(schema, 'abc123'); // Valid
|
|
643
|
+
parseOrFail(schema, 'abcdef'); // Throws an error: 'The received value does not contain at least one digit'
|
|
490
644
|
```
|
|
491
|
-
* _See_ Error Translation Key = '
|
|
645
|
+
* _See_ Error Translation Key = 's:atLeastOneDigit'
|
|
492
646
|
|
|
493
647
|
|
|
494
|
-
#####
|
|
648
|
+
##### <a id="assert_atLeastOneLowerChar_string"> atLeastOneLowerChar </a>
|
|
495
649
|
|
|
496
650
|
```typescript
|
|
497
|
-
import {
|
|
651
|
+
import { atLeastOneLowerChar } from 'bguard/string/atLeastOneLowerChar';
|
|
498
652
|
```
|
|
499
653
|
|
|
500
|
-
* _Description_
|
|
501
|
-
*
|
|
502
|
-
* _Throws_ {ValidationError} if the received value is greater than or equal to the expected maximum value.
|
|
654
|
+
* _Description_ Asserts that a string value contains at least one lowercase character.
|
|
655
|
+
* _Throws_ {ValidationError} if the received value does not contain at least one lowercase character.
|
|
503
656
|
* _Example_
|
|
504
657
|
```typescript
|
|
505
|
-
const schema =
|
|
506
|
-
parseOrFail(schema,
|
|
507
|
-
parseOrFail(schema,
|
|
508
|
-
parseOrFail(schema, 101n); // Throws an error: 'The received value is greater than or equal to expected'
|
|
658
|
+
const schema = string().custom(atLeastOneLowerChar());
|
|
659
|
+
parseOrFail(schema, 'abcDEF'); // Valid
|
|
660
|
+
parseOrFail(schema, 'ABCDEF'); // Throws an error: 'The received value does not contain at least one lowercase character'
|
|
509
661
|
```
|
|
510
|
-
* _See_ Error Translation Key = '
|
|
662
|
+
* _See_ Error Translation Key = 's:atLeastOneLowerChar'
|
|
511
663
|
|
|
512
664
|
|
|
513
|
-
#####
|
|
665
|
+
##### <a id="assert_atLeastOneSpecialChar_string"> atLeastOneSpecialChar </a>
|
|
514
666
|
|
|
515
667
|
```typescript
|
|
516
|
-
import {
|
|
668
|
+
import { atLeastOneSpecialChar } from 'bguard/string/atLeastOneSpecialChar';
|
|
517
669
|
```
|
|
518
670
|
|
|
519
|
-
* _Description_ Asserts that a
|
|
520
|
-
* _Param_ {
|
|
521
|
-
* _Throws_ {ValidationError} if the received value
|
|
671
|
+
* _Description_ Asserts that a string value contains at least one special character.
|
|
672
|
+
* _Param_ {string} [allowedSpecialChars=* '@!#%&()^~{}'] The string containing allowed special characters. Defaults to '*@!#%&()^~{}'.
|
|
673
|
+
* _Throws_ {ValidationError} if the received value does not contain at least one of the allowed special characters.
|
|
522
674
|
* _Example_
|
|
523
675
|
```typescript
|
|
524
|
-
const schema =
|
|
525
|
-
parseOrFail(schema,
|
|
526
|
-
parseOrFail(schema,
|
|
527
|
-
|
|
676
|
+
const schema = string().custom(atLeastOneSpecialChar()); // Default special characters
|
|
677
|
+
parseOrFail(schema, 'abc!def'); // Valid
|
|
678
|
+
parseOrFail(schema, 'abcdef'); // Throws an error: 'The received value does not contain at least one special character'
|
|
679
|
+
|
|
680
|
+
const customSchema = string().custom(atLeastOneSpecialChar('@$')); // Custom special characters
|
|
681
|
+
parseOrFail(customSchema, 'abc@def'); // Valid
|
|
682
|
+
parseOrFail(customSchema, 'abcdef'); // Throws an error: 'The received value does not contain at least one special character'
|
|
528
683
|
```
|
|
529
|
-
* _See_ Error Translation Key = '
|
|
684
|
+
* _See_ Error Translation Key = 's:atLeastOneSpecialChar'
|
|
530
685
|
|
|
531
686
|
|
|
532
|
-
#####
|
|
687
|
+
##### <a id="assert_atLeastOneUpperChar_string"> atLeastOneUpperChar </a>
|
|
533
688
|
|
|
534
689
|
```typescript
|
|
535
|
-
import {
|
|
690
|
+
import { atLeastOneUpperChar } from 'bguard/string/atLeastOneUpperChar';
|
|
536
691
|
```
|
|
537
692
|
|
|
538
|
-
* _Description_ Asserts that a
|
|
539
|
-
*
|
|
540
|
-
* _Throws_ {ValidationError} if the received value is less than or equal to the expected minimum value.
|
|
693
|
+
* _Description_ Asserts that a string value contains at least one uppercase character.
|
|
694
|
+
* _Throws_ {ValidationError} if the received value does not contain at least one uppercase character.
|
|
541
695
|
* _Example_
|
|
542
696
|
```typescript
|
|
543
|
-
const schema =
|
|
544
|
-
parseOrFail(schema,
|
|
545
|
-
parseOrFail(schema,
|
|
546
|
-
parseOrFail(schema, 9n); // Throws an error: 'The received value is less than or equal to expected'
|
|
697
|
+
const schema = string().custom(atLeastOneUpperChar());
|
|
698
|
+
parseOrFail(schema, 'abcDEF'); // Valid
|
|
699
|
+
parseOrFail(schema, 'abcdef'); // Throws an error: 'The received value does not contain at least one uppercase character'
|
|
547
700
|
```
|
|
548
|
-
* _See_ Error Translation Key = '
|
|
701
|
+
* _See_ Error Translation Key = 's:atLeastOneUpperChar'
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
##### <a id="assert_contains_string"> contains </a>
|
|
549
705
|
|
|
550
|
-
#### date
|
|
551
|
-
|
|
552
|
-
<b>Prerequisites</b>
|
|
553
|
-
|
|
554
706
|
```typescript
|
|
555
|
-
import {
|
|
707
|
+
import { contains } from 'bguard/string/contains';
|
|
556
708
|
```
|
|
557
|
-
|
|
558
|
-
* _Description_
|
|
709
|
+
|
|
710
|
+
* _Description_ Asserts that a string value contains a specified substring.
|
|
711
|
+
* _Param_ {string} substring The substring that must be present in the string value.
|
|
712
|
+
* _Throws_ {ValidationError} if the received value does not contain the required substring.
|
|
559
713
|
* _Example_
|
|
560
714
|
```typescript
|
|
561
|
-
const schema =
|
|
562
|
-
parseOrFail(schema,
|
|
563
|
-
parseOrFail(schema, '
|
|
715
|
+
const schema = string().custom(contains('foo'));
|
|
716
|
+
parseOrFail(schema, 'foobar'); // Valid
|
|
717
|
+
parseOrFail(schema, 'bar'); // Throws an error: 'The received value does not contain the required substring'
|
|
564
718
|
```
|
|
565
|
-
|
|
719
|
+
* _See_ Error Translation Key = 's:contains'
|
|
566
720
|
|
|
567
|
-
|
|
721
|
+
|
|
722
|
+
##### <a id="assert_email_string"> email </a>
|
|
568
723
|
|
|
569
724
|
```typescript
|
|
570
|
-
import {
|
|
725
|
+
import { email } from 'bguard/string/email';
|
|
571
726
|
```
|
|
572
727
|
|
|
573
|
-
* _Description_ Asserts that a
|
|
574
|
-
*
|
|
575
|
-
* _Throws_ {ValidationError} if the received value is greater than the expected maximum value.
|
|
728
|
+
* _Description_ Asserts that a string value matches the email pattern. The pattern checks for a basic email format.
|
|
729
|
+
* _Throws_ {ValidationError} if the received value does not match the email pattern.
|
|
576
730
|
* _Example_
|
|
577
731
|
```typescript
|
|
578
|
-
const schema =
|
|
579
|
-
parseOrFail(schema,
|
|
580
|
-
parseOrFail(schema,
|
|
581
|
-
parseOrFail(schema, new Date('2025-01-01')); // Throws an error: 'The received value is greater than expected'
|
|
732
|
+
const schema = string().custom(email());
|
|
733
|
+
parseOrFail(schema, 'example@example.com'); // Valid
|
|
734
|
+
parseOrFail(schema, 'invalid-email'); // Throws an error: 'The received value does not match the required email pattern'
|
|
582
735
|
```
|
|
583
|
-
* _See_ Error Translation Key = '
|
|
736
|
+
* _See_ - Error Translation Key = 's:email'
|
|
584
737
|
|
|
585
738
|
|
|
586
|
-
#####
|
|
739
|
+
##### <a id="assert_endsWith_string"> endsWith </a>
|
|
587
740
|
|
|
588
741
|
```typescript
|
|
589
|
-
import {
|
|
742
|
+
import { endsWith } from 'bguard/string/endsWith';
|
|
590
743
|
```
|
|
591
744
|
|
|
592
|
-
* _Description_ Asserts that a
|
|
593
|
-
* _Param_ {
|
|
594
|
-
* _Throws_ {ValidationError} if the received value
|
|
745
|
+
* _Description_ Asserts that a string value ends with a specified substring.
|
|
746
|
+
* _Param_ {string} substring The substring that the string value must end with.
|
|
747
|
+
* _Throws_ {ValidationError} if the received value does not end with the required substring.
|
|
595
748
|
* _Example_
|
|
596
749
|
```typescript
|
|
597
|
-
const schema =
|
|
598
|
-
parseOrFail(schema,
|
|
599
|
-
parseOrFail(schema,
|
|
600
|
-
parseOrFail(schema, new Date('2022-12-31')); // Throws an error: 'The received value is less than expected'
|
|
750
|
+
const schema = string().custom(endsWith('bar'));
|
|
751
|
+
parseOrFail(schema, 'foobar'); // Valid
|
|
752
|
+
parseOrFail(schema, 'foofoo'); // Throws an error: 'The received value does not end with the required substring'
|
|
601
753
|
```
|
|
602
|
-
* _See_ Error Translation Key = '
|
|
754
|
+
* _See_ Error Translation Key = 's:endsWith'
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
##### <a id="assert_isValidDate_string"> isValidDate </a>
|
|
603
758
|
|
|
604
|
-
#### mix
|
|
605
|
-
|
|
606
|
-
<b>Prerequisites</b>
|
|
607
|
-
|
|
608
759
|
```typescript
|
|
609
|
-
import {
|
|
760
|
+
import { isValidDate } from 'bguard/string/isValidDate';
|
|
610
761
|
```
|
|
611
|
-
|
|
612
|
-
* _Description_
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
* _Param_ {T} valueTypes - An array of primitive types that the value can match.
|
|
762
|
+
|
|
763
|
+
* _Description_ Asserts that a string is a valid date in the format YYYY-MM-DD.
|
|
764
|
+
* _Throws_ {ValidationError} if the received string is not a valid date.
|
|
616
765
|
* _Example_
|
|
617
766
|
```typescript
|
|
618
|
-
const schema =
|
|
619
|
-
parseOrFail(schema,
|
|
620
|
-
parseOrFail(schema,
|
|
621
|
-
parseOrFail(schema,
|
|
767
|
+
const schema = string().custom(isValidDate());
|
|
768
|
+
parseOrFail(schema, "2020-01-01"); // Valid
|
|
769
|
+
parseOrFail(schema, "2020-1-1"); // Throws an error: 'The received value is not a valid date'
|
|
770
|
+
parseOrFail(schema, "2020-01-32"); // Throws an error: 'The received value is not a valid date'
|
|
622
771
|
```
|
|
623
|
-
|
|
772
|
+
* _See_ Error Translation Key = 's:isValidDate'
|
|
773
|
+
|
|
624
774
|
|
|
625
|
-
#####
|
|
775
|
+
##### <a id="assert_isValidDateTime_string"> isValidDateTime </a>
|
|
626
776
|
|
|
627
777
|
```typescript
|
|
628
|
-
import {
|
|
778
|
+
import { isValidDateTime } from 'bguard/string/isValidDateTime';
|
|
629
779
|
```
|
|
630
780
|
|
|
631
|
-
* _Description_
|
|
632
|
-
*
|
|
633
|
-
|
|
634
|
-
|
|
781
|
+
* _Description_ Asserts that a string value is a valid ISO 8601 datetime string.
|
|
782
|
+
* _Param_ {DateTimeOptions} options Options to control the validation:
|
|
783
|
+
- `offset`: If `true`, allows timezone offsets in the datetime string.
|
|
784
|
+
- `precision`: Specify the exact number of fractional second digits allowed (e.g., 3 for milliseconds).
|
|
785
|
+
* _Throws_ {ValidationError} if the received value is not a valid datetime string according to the options.
|
|
635
786
|
* _Example_
|
|
636
787
|
```typescript
|
|
637
|
-
const schema =
|
|
638
|
-
parseOrFail(schema,
|
|
639
|
-
parseOrFail(schema,
|
|
788
|
+
const schema = string().custom(isValidDateTime());
|
|
789
|
+
parseOrFail(schema, "2024-01-01T00:00:00Z"); // Valid
|
|
790
|
+
parseOrFail(schema, "2024-01-01T00:00:00.123Z"); // Valid
|
|
791
|
+
parseOrFail(schema, "2024-01-01T00:00:00+03:00"); // Invalid (no offsets allowed)
|
|
792
|
+
|
|
793
|
+
const schemaWithOffset = string().custom(isValidDateTime({ offset: true }));
|
|
794
|
+
parseOrFail(schemaWithOffset, "2024-01-01T00:00:00+04:00"); // Valid
|
|
795
|
+
|
|
796
|
+
const schemaWithPrecision = string().custom(isValidDateTime({ precision: 3 }));
|
|
797
|
+
parseOrFail(schemaWithPrecision, "2024-01-01T00:00:00.123Z"); // Valid
|
|
798
|
+
parseOrFail(schemaWithPrecision, "2024-01-01T00:00:00.123456Z"); // Invalid
|
|
640
799
|
```
|
|
641
|
-
* _See_ Error Translation Key = '
|
|
800
|
+
* _See_ Error Translation Key = 's:isValidDateTime'
|
|
642
801
|
|
|
643
802
|
|
|
644
|
-
#####
|
|
803
|
+
##### <a id="assert_isValidTime_string"> isValidTime </a>
|
|
645
804
|
|
|
646
805
|
```typescript
|
|
647
|
-
import {
|
|
806
|
+
import { isValidTime } from 'bguard/string/isValidTime';
|
|
648
807
|
```
|
|
649
808
|
|
|
650
|
-
* _Description_
|
|
651
|
-
*
|
|
652
|
-
*
|
|
653
|
-
* _Throws_ {ValidationError} If the received value does not match at least one of the expected values.
|
|
809
|
+
* _Description_ Asserts that a string is a valid time in the format HH:mm:ss, with optional fractional seconds.
|
|
810
|
+
* _Param_ {IsValidTimeOptions} options Optional settings to configure the validation.
|
|
811
|
+
* _Throws_ {ValidationError} if the received string is not a valid time.
|
|
654
812
|
* _Example_
|
|
655
813
|
```typescript
|
|
656
|
-
const schema =
|
|
657
|
-
parseOrFail(schema,
|
|
658
|
-
parseOrFail(schema,
|
|
659
|
-
parseOrFail(schema,
|
|
814
|
+
const schema = string().custom(isValidTime());
|
|
815
|
+
parseOrFail(schema, "00:00:00"); // Valid
|
|
816
|
+
parseOrFail(schema, "23:59:59.9999999"); // Valid
|
|
817
|
+
parseOrFail(schema, "00:00:00.256Z"); // Throws an error: 'The received value is not a valid time'
|
|
818
|
+
|
|
819
|
+
const schemaWithPrecision = string().custom(isValidTime({ precision: 3 }));
|
|
820
|
+
parseOrFail(schemaWithPrecision, "00:00:00.256"); // Valid
|
|
821
|
+
parseOrFail(schemaWithPrecision, "00:00:00"); // Throws an error: 'The received value is not a valid time'
|
|
660
822
|
```
|
|
661
|
-
* _See_ Error Translation Key = '
|
|
823
|
+
* _See_ Error Translation Key = 's:isValidTime'
|
|
824
|
+
|
|
825
|
+
|
|
826
|
+
##### <a id="assert_lowerCase_string"> lowerCase </a>
|
|
662
827
|
|
|
663
|
-
#### number
|
|
664
|
-
|
|
665
|
-
<b>Prerequisites</b>
|
|
666
|
-
|
|
667
828
|
```typescript
|
|
668
|
-
import {
|
|
829
|
+
import { lowerCase } from 'bguard/string/lowerCase';
|
|
669
830
|
```
|
|
670
|
-
|
|
671
|
-
* _Description_
|
|
831
|
+
|
|
832
|
+
* _Description_ Asserts that a string value is in lowercase.
|
|
833
|
+
* _Throws_ {ValidationError} if the received value is not in lowercase.
|
|
672
834
|
* _Example_
|
|
673
835
|
```typescript
|
|
674
|
-
const schema =
|
|
675
|
-
parseOrFail(schema,
|
|
676
|
-
parseOrFail(schema, '
|
|
836
|
+
const schema = string().custom(lowerCase());
|
|
837
|
+
parseOrFail(schema, 'valid'); // Valid
|
|
838
|
+
parseOrFail(schema, 'Invalid'); // Throws an error: 'The received value is not in lowercase'
|
|
677
839
|
```
|
|
678
|
-
|
|
840
|
+
* _See_ Error Translation Key = 's:lowerCase'
|
|
679
841
|
|
|
680
|
-
|
|
842
|
+
|
|
843
|
+
##### <a id="assert_maxLength_string"> maxLength </a>
|
|
681
844
|
|
|
682
845
|
```typescript
|
|
683
|
-
import {
|
|
846
|
+
import { maxLength } from 'bguard/string/maxLength';
|
|
684
847
|
```
|
|
685
848
|
|
|
686
|
-
* _Description_ Asserts that a
|
|
687
|
-
* _Param_ {number} expected The maximum
|
|
688
|
-
* _Throws_ {ValidationError} if the received value
|
|
849
|
+
* _Description_ Asserts that the length of a string value is not greater than a specified maximum length.
|
|
850
|
+
* _Param_ {number} expected The maximum allowed length for the string.
|
|
851
|
+
* _Throws_ {ValidationError} if the length of the received value is greater than the expected length.
|
|
689
852
|
* _Example_
|
|
690
853
|
```typescript
|
|
691
|
-
const schema =
|
|
692
|
-
parseOrFail(schema,
|
|
693
|
-
parseOrFail(schema,
|
|
694
|
-
parseOrFail(schema, 101); // Throws an error: 'The received value is greater than expected'
|
|
854
|
+
const schema = string().custom(maxLength(10));
|
|
855
|
+
parseOrFail(schema, 'short'); // Valid
|
|
856
|
+
parseOrFail(schema, 'this is a very long string'); // Throws an error: 'The received value length is greater than expected'
|
|
695
857
|
```
|
|
696
|
-
* _See_ Error Translation Key = '
|
|
858
|
+
* _See_ Error Translation Key = 's:maxLength'
|
|
697
859
|
|
|
698
860
|
|
|
699
|
-
#####
|
|
861
|
+
##### <a id="assert_minLength_string"> minLength </a>
|
|
700
862
|
|
|
701
863
|
```typescript
|
|
702
|
-
import {
|
|
703
|
-
```
|
|
704
|
-
|
|
705
|
-
* _Description_ - Asserts that a number value is strictly less than a specified maximum value (i.e., the maximum value is excluded).
|
|
706
|
-
* _Param_ {number} expected - The maximum allowable value, which is excluded.
|
|
707
|
-
* _Throws_ {ValidationError} if the received value is greater than or equal to the expected maximum value.
|
|
708
|
-
* _Example_
|
|
709
|
-
```typescript
|
|
710
|
-
const schema = number().custom(maxExcluded(100));
|
|
711
|
-
parseOrFail(schema, 99); // Valid
|
|
712
|
-
parseOrFail(schema, 100); // Throws an error: 'The received value is greater than or equal to expected'
|
|
713
|
-
parseOrFail(schema, 101); // Throws an error: 'The received value is greater than or equal to expected'
|
|
714
|
-
```
|
|
715
|
-
* _See_ Error Translation Key = 'n:maxExcluded'
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
##### min (number)
|
|
719
|
-
|
|
720
|
-
```typescript
|
|
721
|
-
import { min } from 'bguard/number/min';
|
|
722
|
-
```
|
|
723
|
-
|
|
724
|
-
* _Description_ Asserts that a number value is not less than a specified minimum value.
|
|
725
|
-
* _Param_ {number} expected The minimum allowable value.
|
|
726
|
-
* _Throws_ {ValidationError} if the received value is less than the expected minimum value.
|
|
727
|
-
* _Example_
|
|
728
|
-
```typescript
|
|
729
|
-
const schema = number().custom(min(10));
|
|
730
|
-
parseOrFail(schema, 11); // Valid
|
|
731
|
-
parseOrFail(schema, 10); // Valid
|
|
732
|
-
parseOrFail(schema, 9); // Throws an error: 'The received value is less than expected'
|
|
733
|
-
```
|
|
734
|
-
* _See_ Error Translation Key = 'n:min'
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
##### minExcluded (number)
|
|
738
|
-
|
|
739
|
-
```typescript
|
|
740
|
-
import { minExcluded } from 'bguard/number/minExcluded';
|
|
741
|
-
```
|
|
742
|
-
|
|
743
|
-
* _Description_ Asserts that a number value is strictly greater than a specified minimum value (i.e., the minimum value is excluded).
|
|
744
|
-
* _Param_ {number} expected The minimum allowable value, which is excluded.
|
|
745
|
-
* _Throws_ {ValidationError} if the received value is less than or equal to the expected minimum value.
|
|
746
|
-
* _Example_
|
|
747
|
-
```typescript
|
|
748
|
-
const schema = number().custom(minExcluded(10));
|
|
749
|
-
parseOrFail(schema, 11); // Valid
|
|
750
|
-
parseOrFail(schema, 10); // Throws an error: 'The received value is less than or equal to expected'
|
|
751
|
-
parseOrFail(schema, 9); // Throws an error: 'The received value is less than or equal to expected'
|
|
752
|
-
```
|
|
753
|
-
* _See_ Error Translation Key = 'n:minExcluded'
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
##### negative (number)
|
|
757
|
-
|
|
758
|
-
```typescript
|
|
759
|
-
import { negative } from 'bguard/number/negative';
|
|
760
|
-
```
|
|
761
|
-
|
|
762
|
-
* _Description_ Asserts that a number value is negative (less than zero).
|
|
763
|
-
* _Throws_ {ValidationError} if the received value is not negative.
|
|
764
|
-
* _Example_
|
|
765
|
-
```typescript
|
|
766
|
-
const schema = number().custom(negative());
|
|
767
|
-
parseOrFail(schema, -10); // Valid
|
|
768
|
-
parseOrFail(schema, 0); // Throws an error: 'The received value is not a negative number'
|
|
769
|
-
parseOrFail(schema, 5); // Throws an error: 'The received value is not a negative number'
|
|
770
|
-
```
|
|
771
|
-
* _See_ - Error Translation Key = 'n:negative'
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
##### positive (number)
|
|
775
|
-
|
|
776
|
-
```typescript
|
|
777
|
-
import { positive } from 'bguard/number/positive';
|
|
778
|
-
```
|
|
779
|
-
|
|
780
|
-
* _Description_ Asserts that a number value is positive (greater than zero).
|
|
781
|
-
* _Throws_ {ValidationError} if the received value is not positive.
|
|
782
|
-
* _Example_
|
|
783
|
-
```typescript
|
|
784
|
-
const schema = number().custom(positive());
|
|
785
|
-
parseOrFail(schema, 10); // Valid
|
|
786
|
-
parseOrFail(schema, 0); // Throws an error: 'The received value is not a positive number'
|
|
787
|
-
parseOrFail(schema, -5); // Throws an error: 'The received value is not a positive number'
|
|
788
|
-
```
|
|
789
|
-
* _See_ Error Translation Key = 'n:positive'
|
|
790
|
-
|
|
791
|
-
#### string
|
|
792
|
-
|
|
793
|
-
<b>Prerequisites</b>
|
|
794
|
-
|
|
795
|
-
```typescript
|
|
796
|
-
import { string } from 'bguard/string';
|
|
797
|
-
```
|
|
798
|
-
|
|
799
|
-
* _Description_ Creates a new schema for validating string values.
|
|
800
|
-
* _Example_
|
|
801
|
-
```typescript
|
|
802
|
-
const schema = string();
|
|
803
|
-
parseOrFail(schema, 'hello'); // Validates successfully
|
|
804
|
-
parseOrFail(schema, 123); // Throws a validation error
|
|
805
|
-
```
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
##### atLeastOneDigit (string)
|
|
809
|
-
|
|
810
|
-
```typescript
|
|
811
|
-
import { atLeastOneDigit } from 'bguard/string/atLeastOneDigit';
|
|
812
|
-
```
|
|
813
|
-
|
|
814
|
-
* _Description_ Asserts that a string value contains at least one digit.
|
|
815
|
-
* _Throws_ {ValidationError} if the received value does not contain at least one digit.
|
|
816
|
-
* _Example_
|
|
817
|
-
```typescript
|
|
818
|
-
const schema = string().custom(atLeastOneDigit());
|
|
819
|
-
parseOrFail(schema, 'abc123'); // Valid
|
|
820
|
-
parseOrFail(schema, 'abcdef'); // Throws an error: 'The received value does not contain at least one digit'
|
|
821
|
-
```
|
|
822
|
-
* _See_ Error Translation Key = 's:atLeastOneDigit'
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
##### atLeastOneLowerChar (string)
|
|
826
|
-
|
|
827
|
-
```typescript
|
|
828
|
-
import { atLeastOneLowerChar } from 'bguard/string/atLeastOneLowerChar';
|
|
829
|
-
```
|
|
830
|
-
|
|
831
|
-
* _Description_ Asserts that a string value contains at least one lowercase character.
|
|
832
|
-
* _Throws_ {ValidationError} if the received value does not contain at least one lowercase character.
|
|
833
|
-
* _Example_
|
|
834
|
-
```typescript
|
|
835
|
-
const schema = string().custom(atLeastOneLowerChar());
|
|
836
|
-
parseOrFail(schema, 'abcDEF'); // Valid
|
|
837
|
-
parseOrFail(schema, 'ABCDEF'); // Throws an error: 'The received value does not contain at least one lowercase character'
|
|
838
|
-
```
|
|
839
|
-
* _See_ Error Translation Key = 's:atLeastOneLowerChar'
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
##### atLeastOneSpecialChar (string)
|
|
843
|
-
|
|
844
|
-
```typescript
|
|
845
|
-
import { atLeastOneSpecialChar } from 'bguard/string/atLeastOneSpecialChar';
|
|
846
|
-
```
|
|
847
|
-
|
|
848
|
-
* _Description_ Asserts that a string value contains at least one special character.
|
|
849
|
-
* _Param_ {string} [allowedSpecialChars=* '@!#%&()^~{}'] The string containing allowed special characters. Defaults to '*@!#%&()^~{}'.
|
|
850
|
-
* _Throws_ {ValidationError} if the received value does not contain at least one of the allowed special characters.
|
|
851
|
-
* _Example_
|
|
852
|
-
```typescript
|
|
853
|
-
const schema = string().custom(atLeastOneSpecialChar()); // Default special characters
|
|
854
|
-
parseOrFail(schema, 'abc!def'); // Valid
|
|
855
|
-
parseOrFail(schema, 'abcdef'); // Throws an error: 'The received value does not contain at least one special character'
|
|
856
|
-
|
|
857
|
-
const customSchema = string().custom(atLeastOneSpecialChar('@$')); // Custom special characters
|
|
858
|
-
parseOrFail(customSchema, 'abc@def'); // Valid
|
|
859
|
-
parseOrFail(customSchema, 'abcdef'); // Throws an error: 'The received value does not contain at least one special character'
|
|
860
|
-
```
|
|
861
|
-
* _See_ Error Translation Key = 's:atLeastOneSpecialChar'
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
##### atLeastOneUpperChar (string)
|
|
865
|
-
|
|
866
|
-
```typescript
|
|
867
|
-
import { atLeastOneUpperChar } from 'bguard/string/atLeastOneUpperChar';
|
|
868
|
-
```
|
|
869
|
-
|
|
870
|
-
* _Description_ Asserts that a string value contains at least one uppercase character.
|
|
871
|
-
* _Throws_ {ValidationError} if the received value does not contain at least one uppercase character.
|
|
872
|
-
* _Example_
|
|
873
|
-
```typescript
|
|
874
|
-
const schema = string().custom(atLeastOneUpperChar());
|
|
875
|
-
parseOrFail(schema, 'abcDEF'); // Valid
|
|
876
|
-
parseOrFail(schema, 'abcdef'); // Throws an error: 'The received value does not contain at least one uppercase character'
|
|
877
|
-
```
|
|
878
|
-
* _See_ Error Translation Key = 's:atLeastOneUpperChar'
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
##### contains (string)
|
|
882
|
-
|
|
883
|
-
```typescript
|
|
884
|
-
import { contains } from 'bguard/string/contains';
|
|
885
|
-
```
|
|
886
|
-
|
|
887
|
-
* _Description_ Asserts that a string value contains a specified substring.
|
|
888
|
-
* _Param_ {string} substring The substring that must be present in the string value.
|
|
889
|
-
* _Throws_ {ValidationError} if the received value does not contain the required substring.
|
|
890
|
-
* _Example_
|
|
891
|
-
```typescript
|
|
892
|
-
const schema = string().custom(contains('foo'));
|
|
893
|
-
parseOrFail(schema, 'foobar'); // Valid
|
|
894
|
-
parseOrFail(schema, 'bar'); // Throws an error: 'The received value does not contain the required substring'
|
|
895
|
-
```
|
|
896
|
-
* _See_ Error Translation Key = 's:contains'
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
##### email (string)
|
|
900
|
-
|
|
901
|
-
```typescript
|
|
902
|
-
import { email } from 'bguard/string/email';
|
|
903
|
-
```
|
|
904
|
-
|
|
905
|
-
* _Description_ Asserts that a string value matches the email pattern. The pattern checks for a basic email format.
|
|
906
|
-
* _Throws_ {ValidationError} if the received value does not match the email pattern.
|
|
907
|
-
* _Example_
|
|
908
|
-
```typescript
|
|
909
|
-
const schema = string().custom(email());
|
|
910
|
-
parseOrFail(schema, 'example@example.com'); // Valid
|
|
911
|
-
parseOrFail(schema, 'invalid-email'); // Throws an error: 'The received value does not match the required email pattern'
|
|
912
|
-
```
|
|
913
|
-
* _See_ - Error Translation Key = 's:email'
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
##### endsWith (string)
|
|
917
|
-
|
|
918
|
-
```typescript
|
|
919
|
-
import { endsWith } from 'bguard/string/endsWith';
|
|
920
|
-
```
|
|
921
|
-
|
|
922
|
-
* _Description_ Asserts that a string value ends with a specified substring.
|
|
923
|
-
* _Param_ {string} substring The substring that the string value must end with.
|
|
924
|
-
* _Throws_ {ValidationError} if the received value does not end with the required substring.
|
|
925
|
-
* _Example_
|
|
926
|
-
```typescript
|
|
927
|
-
const schema = string().custom(endsWith('bar'));
|
|
928
|
-
parseOrFail(schema, 'foobar'); // Valid
|
|
929
|
-
parseOrFail(schema, 'foofoo'); // Throws an error: 'The received value does not end with the required substring'
|
|
930
|
-
```
|
|
931
|
-
* _See_ Error Translation Key = 's:endsWith'
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
##### lowerCase (string)
|
|
935
|
-
|
|
936
|
-
```typescript
|
|
937
|
-
import { lowerCase } from 'bguard/string/lowerCase';
|
|
938
|
-
```
|
|
939
|
-
|
|
940
|
-
* _Description_ Asserts that a string value is in lowercase.
|
|
941
|
-
* _Throws_ {ValidationError} if the received value is not in lowercase.
|
|
942
|
-
* _Example_
|
|
943
|
-
```typescript
|
|
944
|
-
const schema = string().custom(lowerCase());
|
|
945
|
-
parseOrFail(schema, 'valid'); // Valid
|
|
946
|
-
parseOrFail(schema, 'Invalid'); // Throws an error: 'The received value is not in lowercase'
|
|
947
|
-
```
|
|
948
|
-
* _See_ Error Translation Key = 's:lowerCase'
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
##### maxLength (string)
|
|
952
|
-
|
|
953
|
-
```typescript
|
|
954
|
-
import { maxLength } from 'bguard/string/maxLength';
|
|
955
|
-
```
|
|
956
|
-
|
|
957
|
-
* _Description_ Asserts that the length of a string value is not greater than a specified maximum length.
|
|
958
|
-
* _Param_ {number} expected The maximum allowed length for the string.
|
|
959
|
-
* _Throws_ {ValidationError} if the length of the received value is greater than the expected length.
|
|
960
|
-
* _Example_
|
|
961
|
-
```typescript
|
|
962
|
-
const schema = string().custom(maxLength(10));
|
|
963
|
-
parseOrFail(schema, 'short'); // Valid
|
|
964
|
-
parseOrFail(schema, 'this is a very long string'); // Throws an error: 'The received value length is greater than expected'
|
|
965
|
-
```
|
|
966
|
-
* _See_ Error Translation Key = 's:maxLength'
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
##### minLength (string)
|
|
970
|
-
|
|
971
|
-
```typescript
|
|
972
|
-
import { minLength } from 'bguard/string/minLength';
|
|
864
|
+
import { minLength } from 'bguard/string/minLength';
|
|
973
865
|
```
|
|
974
866
|
|
|
975
867
|
* _Description_ Asserts that the length of a string value is not less than a specified minimum length.
|
|
@@ -984,7 +876,7 @@ import { minLength } from 'bguard/string/minLength';
|
|
|
984
876
|
* _See_ Error Translation Key = 's:minLength'
|
|
985
877
|
|
|
986
878
|
|
|
987
|
-
##### regExp
|
|
879
|
+
##### <a id="assert_regExp_string"> regExp </a>
|
|
988
880
|
|
|
989
881
|
```typescript
|
|
990
882
|
import { regExp } from 'bguard/string/regExp';
|
|
@@ -1002,7 +894,7 @@ import { regExp } from 'bguard/string/regExp';
|
|
|
1002
894
|
* _See_ Error Translation Key = 's:regExp'
|
|
1003
895
|
|
|
1004
896
|
|
|
1005
|
-
##### startsWith
|
|
897
|
+
##### <a id="assert_startsWith_string"> startsWith </a>
|
|
1006
898
|
|
|
1007
899
|
```typescript
|
|
1008
900
|
import { startsWith } from 'bguard/string/startsWith';
|
|
@@ -1020,7 +912,7 @@ import { startsWith } from 'bguard/string/startsWith';
|
|
|
1020
912
|
* _See_ Error Translation Key = 's:startsWith'
|
|
1021
913
|
|
|
1022
914
|
|
|
1023
|
-
##### upperCase
|
|
915
|
+
##### <a id="assert_upperCase_string"> upperCase </a>
|
|
1024
916
|
|
|
1025
917
|
```typescript
|
|
1026
918
|
import { upperCase } from 'bguard/string/upperCase';
|
|
@@ -1038,7 +930,7 @@ import { upperCase } from 'bguard/string/upperCase';
|
|
|
1038
930
|
* _See_ Error Translation Key = 's:upperCase'
|
|
1039
931
|
|
|
1040
932
|
|
|
1041
|
-
##### uuid
|
|
933
|
+
##### <a id="assert_uuid_string"> uuid </a>
|
|
1042
934
|
|
|
1043
935
|
```typescript
|
|
1044
936
|
import { uuid } from 'bguard/string/uuid';
|
|
@@ -1055,7 +947,7 @@ import { uuid } from 'bguard/string/uuid';
|
|
|
1055
947
|
* _See_ Error Translation Key = 's:uuid'
|
|
1056
948
|
|
|
1057
949
|
|
|
1058
|
-
##### uuidV1
|
|
950
|
+
##### <a id="assert_uuidV1_string"> uuidV1 </a>
|
|
1059
951
|
|
|
1060
952
|
```typescript
|
|
1061
953
|
import { uuidV1 } from 'bguard/string/uuidV1';
|
|
@@ -1073,7 +965,7 @@ import { uuidV1 } from 'bguard/string/uuidV1';
|
|
|
1073
965
|
* _See_ Error Translation Key = 's:uuidV1'
|
|
1074
966
|
|
|
1075
967
|
|
|
1076
|
-
##### uuidV2
|
|
968
|
+
##### <a id="assert_uuidV2_string"> uuidV2 </a>
|
|
1077
969
|
|
|
1078
970
|
```typescript
|
|
1079
971
|
import { uuidV2 } from 'bguard/string/uuidV2';
|
|
@@ -1091,7 +983,7 @@ import { uuidV2 } from 'bguard/string/uuidV2';
|
|
|
1091
983
|
* _See_ Error Translation Key = 's:uuidV2'
|
|
1092
984
|
|
|
1093
985
|
|
|
1094
|
-
##### uuidV3
|
|
986
|
+
##### <a id="assert_uuidV3_string"> uuidV3 </a>
|
|
1095
987
|
|
|
1096
988
|
```typescript
|
|
1097
989
|
import { uuidV3 } from 'bguard/string/uuidV3';
|
|
@@ -1109,7 +1001,7 @@ import { uuidV3 } from 'bguard/string/uuidV3';
|
|
|
1109
1001
|
* _See_ Error Translation Key = 's:uuidV3'
|
|
1110
1002
|
|
|
1111
1003
|
|
|
1112
|
-
##### uuidV4
|
|
1004
|
+
##### <a id="assert_uuidV4_string"> uuidV4 </a>
|
|
1113
1005
|
|
|
1114
1006
|
```typescript
|
|
1115
1007
|
import { uuidV4 } from 'bguard/string/uuidV4';
|
|
@@ -1129,7 +1021,7 @@ import { uuidV4 } from 'bguard/string/uuidV4';
|
|
|
1129
1021
|
* _See_ Error Translation Key = 's:uuidV4'
|
|
1130
1022
|
|
|
1131
1023
|
|
|
1132
|
-
##### uuidV5
|
|
1024
|
+
##### <a id="assert_uuidV5_string"> uuidV5 </a>
|
|
1133
1025
|
|
|
1134
1026
|
```typescript
|
|
1135
1027
|
import { uuidV5 } from 'bguard/string/uuidV5';
|
|
@@ -1147,7 +1039,7 @@ import { uuidV5 } from 'bguard/string/uuidV5';
|
|
|
1147
1039
|
* _See_ Error Translation Key = 's:uuidV5'
|
|
1148
1040
|
|
|
1149
1041
|
|
|
1150
|
-
##### validUrl
|
|
1042
|
+
##### <a id="assert_validUrl_string"> validUrl </a>
|
|
1151
1043
|
|
|
1152
1044
|
```typescript
|
|
1153
1045
|
import { validUrl } from 'bguard/string/validUrl';
|
|
@@ -1166,5 +1058,443 @@ import { validUrl } from 'bguard/string/validUrl';
|
|
|
1166
1058
|
```
|
|
1167
1059
|
* _See_ Error Translation Key = 's:url'
|
|
1168
1060
|
|
|
1061
|
+
#### <a id="assertdir_number"> number </a>
|
|
1062
|
+
|
|
1063
|
+
<b>Prerequisites</b>
|
|
1064
|
+
|
|
1065
|
+
```typescript
|
|
1066
|
+
import { number } from 'bguard/number';
|
|
1067
|
+
```
|
|
1068
|
+
|
|
1069
|
+
* _Description_ Creates a new schema for validating number values.
|
|
1070
|
+
* _Example_
|
|
1071
|
+
```typescript
|
|
1072
|
+
const schema = number();
|
|
1073
|
+
parseOrFail(schema, 42); // Validates successfully
|
|
1074
|
+
parseOrFail(schema, '42'); // Throws a validation error
|
|
1075
|
+
```
|
|
1076
|
+
|
|
1077
|
+
|
|
1078
|
+
##### <a id="assert_max_number"> max </a>
|
|
1079
|
+
|
|
1080
|
+
```typescript
|
|
1081
|
+
import { max } from 'bguard/number/max';
|
|
1082
|
+
```
|
|
1083
|
+
|
|
1084
|
+
* _Description_ Asserts that a number value does not exceed a specified maximum value.
|
|
1085
|
+
* _Param_ {number} expected The maximum allowable value.
|
|
1086
|
+
* _Throws_ {ValidationError} if the received value exceeds the expected maximum value.
|
|
1087
|
+
* _Example_
|
|
1088
|
+
```typescript
|
|
1089
|
+
const schema = number().custom(max(100));
|
|
1090
|
+
parseOrFail(schema, 99); // Valid
|
|
1091
|
+
parseOrFail(schema, 100); // Valid
|
|
1092
|
+
parseOrFail(schema, 101); // Throws an error: 'The received value is greater than expected'
|
|
1093
|
+
```
|
|
1094
|
+
* _See_ Error Translation Key = 'n:max'
|
|
1095
|
+
|
|
1096
|
+
|
|
1097
|
+
##### <a id="assert_maxExcluded_number"> maxExcluded </a>
|
|
1098
|
+
|
|
1099
|
+
```typescript
|
|
1100
|
+
import { maxExcluded } from 'bguard/number/maxExcluded';
|
|
1101
|
+
```
|
|
1102
|
+
|
|
1103
|
+
* _Description_ - Asserts that a number value is strictly less than a specified maximum value (i.e., the maximum value is excluded).
|
|
1104
|
+
* _Param_ {number} expected - The maximum allowable value, which is excluded.
|
|
1105
|
+
* _Throws_ {ValidationError} if the received value is greater than or equal to the expected maximum value.
|
|
1106
|
+
* _Example_
|
|
1107
|
+
```typescript
|
|
1108
|
+
const schema = number().custom(maxExcluded(100));
|
|
1109
|
+
parseOrFail(schema, 99); // Valid
|
|
1110
|
+
parseOrFail(schema, 100); // Throws an error: 'The received value is greater than or equal to expected'
|
|
1111
|
+
parseOrFail(schema, 101); // Throws an error: 'The received value is greater than or equal to expected'
|
|
1112
|
+
```
|
|
1113
|
+
* _See_ Error Translation Key = 'n:maxExcluded'
|
|
1114
|
+
|
|
1115
|
+
|
|
1116
|
+
##### <a id="assert_min_number"> min </a>
|
|
1117
|
+
|
|
1118
|
+
```typescript
|
|
1119
|
+
import { min } from 'bguard/number/min';
|
|
1120
|
+
```
|
|
1121
|
+
|
|
1122
|
+
* _Description_ Asserts that a number value is not less than a specified minimum value.
|
|
1123
|
+
* _Param_ {number} expected The minimum allowable value.
|
|
1124
|
+
* _Throws_ {ValidationError} if the received value is less than the expected minimum value.
|
|
1125
|
+
* _Example_
|
|
1126
|
+
```typescript
|
|
1127
|
+
const schema = number().custom(min(10));
|
|
1128
|
+
parseOrFail(schema, 11); // Valid
|
|
1129
|
+
parseOrFail(schema, 10); // Valid
|
|
1130
|
+
parseOrFail(schema, 9); // Throws an error: 'The received value is less than expected'
|
|
1131
|
+
```
|
|
1132
|
+
* _See_ Error Translation Key = 'n:min'
|
|
1133
|
+
|
|
1134
|
+
|
|
1135
|
+
##### <a id="assert_minExcluded_number"> minExcluded </a>
|
|
1136
|
+
|
|
1137
|
+
```typescript
|
|
1138
|
+
import { minExcluded } from 'bguard/number/minExcluded';
|
|
1139
|
+
```
|
|
1140
|
+
|
|
1141
|
+
* _Description_ Asserts that a number value is strictly greater than a specified minimum value (i.e., the minimum value is excluded).
|
|
1142
|
+
* _Param_ {number} expected The minimum allowable value, which is excluded.
|
|
1143
|
+
* _Throws_ {ValidationError} if the received value is less than or equal to the expected minimum value.
|
|
1144
|
+
* _Example_
|
|
1145
|
+
```typescript
|
|
1146
|
+
const schema = number().custom(minExcluded(10));
|
|
1147
|
+
parseOrFail(schema, 11); // Valid
|
|
1148
|
+
parseOrFail(schema, 10); // Throws an error: 'The received value is less than or equal to expected'
|
|
1149
|
+
parseOrFail(schema, 9); // Throws an error: 'The received value is less than or equal to expected'
|
|
1150
|
+
```
|
|
1151
|
+
* _See_ Error Translation Key = 'n:minExcluded'
|
|
1152
|
+
|
|
1153
|
+
|
|
1154
|
+
##### <a id="assert_negative_number"> negative </a>
|
|
1155
|
+
|
|
1156
|
+
```typescript
|
|
1157
|
+
import { negative } from 'bguard/number/negative';
|
|
1158
|
+
```
|
|
1159
|
+
|
|
1160
|
+
* _Description_ Asserts that a number value is negative (less than zero).
|
|
1161
|
+
* _Throws_ {ValidationError} if the received value is not negative.
|
|
1162
|
+
* _Example_
|
|
1163
|
+
```typescript
|
|
1164
|
+
const schema = number().custom(negative());
|
|
1165
|
+
parseOrFail(schema, -10); // Valid
|
|
1166
|
+
parseOrFail(schema, 0); // Throws an error: 'The received value is not a negative number'
|
|
1167
|
+
parseOrFail(schema, 5); // Throws an error: 'The received value is not a negative number'
|
|
1168
|
+
```
|
|
1169
|
+
* _See_ - Error Translation Key = 'n:negative'
|
|
1170
|
+
|
|
1171
|
+
|
|
1172
|
+
##### <a id="assert_positive_number"> positive </a>
|
|
1173
|
+
|
|
1174
|
+
```typescript
|
|
1175
|
+
import { positive } from 'bguard/number/positive';
|
|
1176
|
+
```
|
|
1177
|
+
|
|
1178
|
+
* _Description_ Asserts that a number value is positive (greater than zero).
|
|
1179
|
+
* _Throws_ {ValidationError} if the received value is not positive.
|
|
1180
|
+
* _Example_
|
|
1181
|
+
```typescript
|
|
1182
|
+
const schema = number().custom(positive());
|
|
1183
|
+
parseOrFail(schema, 10); // Valid
|
|
1184
|
+
parseOrFail(schema, 0); // Throws an error: 'The received value is not a positive number'
|
|
1185
|
+
parseOrFail(schema, -5); // Throws an error: 'The received value is not a positive number'
|
|
1186
|
+
```
|
|
1187
|
+
* _See_ Error Translation Key = 'n:positive'
|
|
1188
|
+
|
|
1189
|
+
#### <a id="assertdir_array"> array </a>
|
|
1190
|
+
|
|
1191
|
+
<b>Prerequisites</b>
|
|
1192
|
+
|
|
1193
|
+
```typescript
|
|
1194
|
+
import { array } from 'bguard/array';
|
|
1195
|
+
```
|
|
1196
|
+
|
|
1197
|
+
* _Description_ Creates a new schema for validating arrays where each element must match the specified schema.
|
|
1198
|
+
|
|
1199
|
+
* _Param_ {T} arraySchema - The schema that each element of the array must match.
|
|
1200
|
+
* _Example_
|
|
1201
|
+
```typescript
|
|
1202
|
+
const schema = array(string());
|
|
1203
|
+
parseOrFail(schema, ['hello', 'world']); // Validates successfully
|
|
1204
|
+
parseOrFail(schema, ['hello', 123]); // Throws a validation error
|
|
1205
|
+
```
|
|
1206
|
+
|
|
1207
|
+
|
|
1208
|
+
##### <a id="assert_maxArrayLength_array"> maxArrayLength </a>
|
|
1209
|
+
|
|
1210
|
+
```typescript
|
|
1211
|
+
import { maxArrayLength } from 'bguard/array/maxArrayLength';
|
|
1212
|
+
```
|
|
1213
|
+
|
|
1214
|
+
* _Description_ Asserts that the length of an array is not greater than a specified maximum length.
|
|
1215
|
+
* _Param_ {number} expected The maximum allowed length for the array.
|
|
1216
|
+
* _Throws_ {ValidationError} if the length of the received value is greater than the expected length.
|
|
1217
|
+
* _Example_
|
|
1218
|
+
```typescript
|
|
1219
|
+
const schema = array(string()).custom(maxArrayLength(3));
|
|
1220
|
+
parseOrFail(schema, ['adequate', 'array']); // Valid
|
|
1221
|
+
parseOrFail(schema, ['adequate', 'array', 'length']); // Valid
|
|
1222
|
+
parseOrFail(schema, ['adequate', 'array', 'length', 'test']); // Throws an error: 'The received value length is greater than expected'
|
|
1223
|
+
```
|
|
1224
|
+
* _See_ Error Translation Key = 'a:maxArrayLength'
|
|
1225
|
+
|
|
1226
|
+
|
|
1227
|
+
##### <a id="assert_minArrayLength_array"> minArrayLength </a>
|
|
1228
|
+
|
|
1229
|
+
```typescript
|
|
1230
|
+
import { minArrayLength } from 'bguard/array/minArrayLength';
|
|
1231
|
+
```
|
|
1232
|
+
|
|
1233
|
+
* _Description_ Asserts that the length of na array is not less than a specified minimum length.
|
|
1234
|
+
* _Param_ {number} expected The minimum required length for the array.
|
|
1235
|
+
* _Throws_ {ValidationError} if the length of the received value is less than the expected length.
|
|
1236
|
+
* _Example_
|
|
1237
|
+
```typescript
|
|
1238
|
+
const schema = array(string()).custom(minArrayLength(3));
|
|
1239
|
+
parseOrFail(schema, ['short', 'array']); // Throws an error: 'The received value length is less than expected'
|
|
1240
|
+
parseOrFail(schema, ['adequate', 'array', 'length']); // Valid
|
|
1241
|
+
parseOrFail(schema, ['adequate', 'array', 'length', 'test']); // Valid
|
|
1242
|
+
```
|
|
1243
|
+
* _See_ Error Translation Key = 'a:minArrayLength'
|
|
1244
|
+
|
|
1245
|
+
#### <a id="assertdir_bigint"> bigint </a>
|
|
1246
|
+
|
|
1247
|
+
<b>Prerequisites</b>
|
|
1248
|
+
|
|
1249
|
+
```typescript
|
|
1250
|
+
import { bigint } from 'bguard/bigint';
|
|
1251
|
+
```
|
|
1252
|
+
|
|
1253
|
+
* _Description_ Creates a new schema for validating bigint values.
|
|
1254
|
+
* _Example_
|
|
1255
|
+
```typescript
|
|
1256
|
+
const schema = bigint();
|
|
1257
|
+
parseOrFail(schema, 42n); // Validates successfully
|
|
1258
|
+
parseOrFail(schema, 42); // Throws a validation error
|
|
1259
|
+
parseOrFail(schema, '42'); // Throws a validation error
|
|
1260
|
+
```
|
|
1261
|
+
|
|
1262
|
+
|
|
1263
|
+
##### <a id="assert_bigintMax_bigint"> bigintMax </a>
|
|
1264
|
+
|
|
1265
|
+
```typescript
|
|
1266
|
+
import { bigintMax } from 'bguard/bigint/bigintMax';
|
|
1267
|
+
```
|
|
1268
|
+
|
|
1269
|
+
* _Description_ Asserts that a bigint value does not exceed a specified maximum value.
|
|
1270
|
+
* _Param_ {bigint} expected The maximum allowable value.
|
|
1271
|
+
* _Throws_ {ValidationError} if the received value exceeds the expected maximum value.
|
|
1272
|
+
* _Example_
|
|
1273
|
+
```typescript
|
|
1274
|
+
const schema = bigint().custom(bigintMax(100n));
|
|
1275
|
+
parseOrFail(schema, 99n); // Valid
|
|
1276
|
+
parseOrFail(schema, 100n); // Valid
|
|
1277
|
+
parseOrFail(schema, 101n); // Throws an error: 'The received value is greater than expected'
|
|
1278
|
+
```
|
|
1279
|
+
* _See_ Error Translation Key = 'bi:max'
|
|
1280
|
+
|
|
1281
|
+
|
|
1282
|
+
##### <a id="assert_bigintMaxExcluded_bigint"> bigintMaxExcluded </a>
|
|
1283
|
+
|
|
1284
|
+
```typescript
|
|
1285
|
+
import { bigintMaxExcluded } from 'bguard/bigint/bigintMaxExcluded';
|
|
1286
|
+
```
|
|
1287
|
+
|
|
1288
|
+
* _Description_ - Asserts that a bigint value is strictly less than a specified maximum value (i.e., the maximum value is excluded).
|
|
1289
|
+
* _Param_ {bigint} expected - The maximum allowable value, which is excluded.
|
|
1290
|
+
* _Throws_ {ValidationError} if the received value is greater than or equal to the expected maximum value.
|
|
1291
|
+
* _Example_
|
|
1292
|
+
```typescript
|
|
1293
|
+
const schema = bigint().custom(bigintMaxExcluded(100n));
|
|
1294
|
+
parseOrFail(schema, 99n); // Valid
|
|
1295
|
+
parseOrFail(schema, 100n); // Throws an error: 'The received value is greater than or equal to expected'
|
|
1296
|
+
parseOrFail(schema, 101n); // Throws an error: 'The received value is greater than or equal to expected'
|
|
1297
|
+
```
|
|
1298
|
+
* _See_ Error Translation Key = 'bi:maxExcluded'
|
|
1299
|
+
|
|
1300
|
+
|
|
1301
|
+
##### <a id="assert_bigintMin_bigint"> bigintMin </a>
|
|
1302
|
+
|
|
1303
|
+
```typescript
|
|
1304
|
+
import { bigintMin } from 'bguard/bigint/bigintMin';
|
|
1305
|
+
```
|
|
1306
|
+
|
|
1307
|
+
* _Description_ Asserts that a bigint value is not less than a specified minimum value.
|
|
1308
|
+
* _Param_ {bigint} expected The minimum allowable value.
|
|
1309
|
+
* _Throws_ {ValidationError} if the received value is less than the expected minimum value.
|
|
1310
|
+
* _Example_
|
|
1311
|
+
```typescript
|
|
1312
|
+
const schema = bigint().custom(bigintMin(10n));
|
|
1313
|
+
parseOrFail(schema, 11n); // Valid
|
|
1314
|
+
parseOrFail(schema, 10n); // Valid
|
|
1315
|
+
parseOrFail(schema, 9n); // Throws an error: 'The received value is less than expected'
|
|
1316
|
+
```
|
|
1317
|
+
* _See_ Error Translation Key = 'bi:min'
|
|
1318
|
+
|
|
1319
|
+
|
|
1320
|
+
##### <a id="assert_bigintMinExcluded_bigint"> bigintMinExcluded </a>
|
|
1321
|
+
|
|
1322
|
+
```typescript
|
|
1323
|
+
import { bigintMinExcluded } from 'bguard/bigint/bigintMinExcluded';
|
|
1324
|
+
```
|
|
1325
|
+
|
|
1326
|
+
* _Description_ Asserts that a bigint value is strictly greater than a specified minimum value (i.e., the minimum value is excluded).
|
|
1327
|
+
* _Param_ {bigint} expected The minimum allowable value, which is excluded.
|
|
1328
|
+
* _Throws_ {ValidationError} if the received value is less than or equal to the expected minimum value.
|
|
1329
|
+
* _Example_
|
|
1330
|
+
```typescript
|
|
1331
|
+
const schema = bigint().custom(bigintMinExcluded(10n));
|
|
1332
|
+
parseOrFail(schema, 11n); // Valid
|
|
1333
|
+
parseOrFail(schema, 10n); // Throws an error: 'The received value is less than or equal to expected'
|
|
1334
|
+
parseOrFail(schema, 9n); // Throws an error: 'The received value is less than or equal to expected'
|
|
1335
|
+
```
|
|
1336
|
+
* _See_ Error Translation Key = 'bi:minExcluded'
|
|
1337
|
+
|
|
1338
|
+
#### <a id="assertdir_date"> date </a>
|
|
1339
|
+
|
|
1340
|
+
<b>Prerequisites</b>
|
|
1341
|
+
|
|
1342
|
+
```typescript
|
|
1343
|
+
import { date } from 'bguard/date';
|
|
1344
|
+
```
|
|
1345
|
+
|
|
1346
|
+
* _Description_ Creates a new schema for validating date values.
|
|
1347
|
+
* _Example_
|
|
1348
|
+
```typescript
|
|
1349
|
+
const schema = date();
|
|
1350
|
+
parseOrFail(schema, true); // Validates successfully
|
|
1351
|
+
parseOrFail(schema, 'true'); // Throws a validation error
|
|
1352
|
+
```
|
|
1353
|
+
|
|
1354
|
+
|
|
1355
|
+
##### <a id="assert_dateMax_date"> dateMax </a>
|
|
1356
|
+
|
|
1357
|
+
```typescript
|
|
1358
|
+
import { dateMax } from 'bguard/date/dateMax';
|
|
1359
|
+
```
|
|
1360
|
+
|
|
1361
|
+
* _Description_ Asserts that a date value is not greater than a specified maximum value.
|
|
1362
|
+
* _Param_ {Date | string} expected The maximum allowable value.
|
|
1363
|
+
* _Throws_ {ValidationError} if the received value is greater than the expected maximum value.
|
|
1364
|
+
* _Example_
|
|
1365
|
+
```typescript
|
|
1366
|
+
const schema = date().custom(dateMax('2024-12-31'));
|
|
1367
|
+
parseOrFail(schema, new Date('2024-12-30')); // Valid
|
|
1368
|
+
parseOrFail(schema, new Date('2024-12-31')); // Valid
|
|
1369
|
+
parseOrFail(schema, new Date('2025-01-01')); // Throws an error: 'The received value is greater than expected'
|
|
1370
|
+
```
|
|
1371
|
+
* _See_ Error Translation Key = 'dt:max'
|
|
1372
|
+
|
|
1373
|
+
|
|
1374
|
+
##### <a id="assert_dateMin_date"> dateMin </a>
|
|
1375
|
+
|
|
1376
|
+
```typescript
|
|
1377
|
+
import { dateMin } from 'bguard/date/dateMin';
|
|
1378
|
+
```
|
|
1379
|
+
|
|
1380
|
+
* _Description_ Asserts that a number value is not less than a specified minimum value.
|
|
1381
|
+
* _Param_ {Date | string} expected The minimum allowable value.
|
|
1382
|
+
* _Throws_ {ValidationError} if the received value is less than the expected minimum value.
|
|
1383
|
+
* _Example_
|
|
1384
|
+
```typescript
|
|
1385
|
+
const schema = date().custom(dateMin('2023-01-01'));
|
|
1386
|
+
parseOrFail(schema, new Date('2023-01-02')); // Valid
|
|
1387
|
+
parseOrFail(schema, new Date('2023-01-01')); // Valid
|
|
1388
|
+
parseOrFail(schema, new Date('2022-12-31')); // Throws an error: 'The received value is less than expected'
|
|
1389
|
+
```
|
|
1390
|
+
* _See_ Error Translation Key = 'dt:min'
|
|
1391
|
+
|
|
1392
|
+
#### <a id="assertdir_mix"> mix </a>
|
|
1393
|
+
|
|
1394
|
+
<b>Prerequisites</b>
|
|
1395
|
+
|
|
1396
|
+
```typescript
|
|
1397
|
+
import { oneOfTypes } from 'bguard/mix';
|
|
1398
|
+
```
|
|
1399
|
+
|
|
1400
|
+
* _Description_ Creates a new schema for validating values that can match any one of the specified primitive types.
|
|
1401
|
+
|
|
1402
|
+
|
|
1403
|
+
* _Param_ {T} valueTypes - An array of primitive types that the value can match.
|
|
1404
|
+
* _Example_
|
|
1405
|
+
```typescript
|
|
1406
|
+
const schema = oneOfTypes(['string', 'number']);
|
|
1407
|
+
parseOrFail(schema, 'hello'); // Validates successfully
|
|
1408
|
+
parseOrFail(schema, 42); // Validates successfully
|
|
1409
|
+
parseOrFail(schema, true); // Throws a validation error
|
|
1410
|
+
```
|
|
1411
|
+
|
|
1412
|
+
|
|
1413
|
+
##### <a id="assert_equalTo_mix"> equalTo </a>
|
|
1414
|
+
|
|
1415
|
+
```typescript
|
|
1416
|
+
import { equalTo } from 'bguard/mix/equalTo';
|
|
1417
|
+
```
|
|
1418
|
+
|
|
1419
|
+
* _Description_ Creates a custom assertion that checks if a value is equal to the expected value.
|
|
1420
|
+
* > **Notice:** It has already been implemented in the number, bigint and string schema. There is no need to use it as a custom assert.
|
|
1421
|
+
* _Param_ {unknown} expected The value that the received value is expected to match.
|
|
1422
|
+
* _Throws_ {ValidationError} If the received value does not match the expected value.
|
|
1423
|
+
* _Example_
|
|
1424
|
+
```typescript
|
|
1425
|
+
const schema = number().custom(equalTo(5)); // Define a schema with a custom assertion
|
|
1426
|
+
parseOrFail(schema, 5); // Valid
|
|
1427
|
+
parseOrFail(schema, 3); // Throws an error: 'The received value is not equal to expected'
|
|
1428
|
+
```
|
|
1429
|
+
* _See_ Error Translation Key = 'm:equalTo'
|
|
1430
|
+
|
|
1431
|
+
|
|
1432
|
+
##### <a id="assert_oneOfValues_mix"> oneOfValues </a>
|
|
1433
|
+
|
|
1434
|
+
```typescript
|
|
1435
|
+
import { oneOfValues } from 'bguard/mix/oneOfValues';
|
|
1436
|
+
```
|
|
1437
|
+
|
|
1438
|
+
* _Description_ Creates a custom assertion that checks if a value is equal to the one of expected values.
|
|
1439
|
+
* > **Notice:** It has already been implemented in the number, bigint and string schema. There is no need to use it as a custom assert.
|
|
1440
|
+
* _Param_ {unknown} expected The value that the received value is expected to match.
|
|
1441
|
+
* _Throws_ {ValidationError} If the received value does not match at least one of the expected values.
|
|
1442
|
+
* _Example_
|
|
1443
|
+
```typescript
|
|
1444
|
+
const schema = number().custom(oneOfValues([5, 4])); // Define a schema with a custom assertion
|
|
1445
|
+
parseOrFail(schema, 5); // Valid
|
|
1446
|
+
parseOrFail(schema, 4); // Valid
|
|
1447
|
+
parseOrFail(schema, 3); // Throws an error: 'The received value is not equal to expected'
|
|
1448
|
+
```
|
|
1449
|
+
* _See_ Error Translation Key = 'm:oneOfValues'
|
|
1450
|
+
|
|
1451
|
+
#### <a id="assertdir_object"> object </a>
|
|
1452
|
+
|
|
1453
|
+
<b>Prerequisites</b>
|
|
1454
|
+
|
|
1455
|
+
```typescript
|
|
1456
|
+
import { object } from 'bguard/object';
|
|
1457
|
+
```
|
|
1458
|
+
|
|
1459
|
+
* _Description_ Creates a new schema for validating objects where each property must match the specified schema.
|
|
1460
|
+
|
|
1461
|
+
* _Param_ {T} shapeSchema - The schema that each property of the object must match.
|
|
1462
|
+
* _Example_
|
|
1463
|
+
```typescript
|
|
1464
|
+
const schema = object({
|
|
1465
|
+
name: string(),
|
|
1466
|
+
age: number()
|
|
1467
|
+
});
|
|
1468
|
+
parseOrFail(schema, { name: 'John', age: 30 }); // Validates successfully
|
|
1469
|
+
parseOrFail(schema, { name: 'John', age: '30' }); // Throws a validation error
|
|
1470
|
+
```
|
|
1471
|
+
|
|
1472
|
+
|
|
1473
|
+
##### <a id="assert_maxKeys_object"> maxKeys </a>
|
|
1474
|
+
|
|
1475
|
+
```typescript
|
|
1476
|
+
import { maxKeys } from 'bguard/object/maxKeys';
|
|
1477
|
+
```
|
|
1478
|
+
|
|
1479
|
+
* _Description_ Ensures that the object has no more than the specified number of keys.
|
|
1480
|
+
* _Param_ {number} expected - The maximum number of keys allowed in the object.
|
|
1481
|
+
* _Throws_ {ValidationError} if the number of the received keys is greater than the expected value.
|
|
1482
|
+
* _Example_
|
|
1483
|
+
```typescript
|
|
1484
|
+
const schema = object({
|
|
1485
|
+
name: string(),
|
|
1486
|
+
email: string(),
|
|
1487
|
+
})
|
|
1488
|
+
.allowUnrecognized()
|
|
1489
|
+
.custom(maxKeys(2));
|
|
1490
|
+
|
|
1491
|
+
// This will pass
|
|
1492
|
+
parseOrFail(schema, { name: 'John', email: 'john@example.com' });
|
|
1493
|
+
|
|
1494
|
+
// This will throw an error because there are 3 keys
|
|
1495
|
+
parseOrFail(schema, { name: 'John', email: 'john@example.com', address: '123 Main St' });
|
|
1496
|
+
```
|
|
1497
|
+
* _See_ Error Translation Key = 'o:maxKeys'
|
|
1498
|
+
|
|
1169
1499
|
### Contributing
|
|
1170
|
-
Contributions are welcome! Please open an issue or submit a pull request for any bugs or feature requests.
|
|
1500
|
+
Contributions are welcome! Please open an issue or submit a pull request for any bugs or feature requests.
|