bguard 0.2.0 → 0.3.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 +562 -380
- package/lib/{CommonSchema-DJmQ3F_9.d.mts → CommonSchema-CiY7spmg.d.mts} +9 -1
- package/lib/{CommonSchema-9RtxTxdX.d.ts → CommonSchema-D9QVJBJe.d.ts} +9 -1
- package/lib/InferType.d.mts +1 -1
- package/lib/InferType.d.ts +1 -1
- package/lib/asserts/array/index.d.mts +1 -1
- package/lib/asserts/array/index.d.ts +1 -1
- 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 +1 -1
- package/lib/asserts/bigint/index.d.ts +1 -1
- 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 +1 -1
- package/lib/asserts/boolean/index.d.ts +1 -1
- 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 +1 -1
- package/lib/asserts/date/index.d.ts +1 -1
- 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 +1 -1
- package/lib/asserts/mix/index.d.ts +1 -1
- 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 +1 -1
- package/lib/asserts/number/index.d.ts +1 -1
- 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 +1 -1
- package/lib/asserts/object/index.d.ts +1 -1
- 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/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 +1 -1
- package/lib/asserts/string/index.d.ts +1 -1
- 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-32J4WVLE.js → chunk-2D5QE7F4.js} +17 -15
- package/lib/chunk-2D5QE7F4.js.map +1 -0
- package/lib/{chunk-XFR7Y4XX.mjs → chunk-2MFBOVP2.mjs} +3 -3
- package/lib/chunk-2MFBOVP2.mjs.map +1 -0
- package/lib/chunk-3IOQH3K4.js +74 -0
- package/lib/chunk-3IOQH3K4.js.map +1 -0
- package/lib/chunk-3ZPLJ5KC.js +14 -0
- package/lib/{chunk-LWAEUS2F.js.map → chunk-3ZPLJ5KC.js.map} +1 -1
- package/lib/{chunk-QV4JDW3T.mjs → chunk-4GDBM5LP.mjs} +2 -2
- package/lib/{chunk-PVOHKGBW.mjs → chunk-5HT3PYSG.mjs} +4 -4
- package/lib/chunk-5HT3PYSG.mjs.map +1 -0
- package/lib/{chunk-XZTZC73V.mjs → chunk-5LLJ22IG.mjs} +25 -17
- package/lib/chunk-5LLJ22IG.mjs.map +1 -0
- package/lib/{chunk-MRWZVTNH.mjs → chunk-ADSMWGDJ.mjs} +4 -4
- package/lib/{chunk-VGF3Y2EK.js → chunk-AN3VHQEQ.js} +7 -7
- package/lib/{chunk-VGF3Y2EK.js.map → chunk-AN3VHQEQ.js.map} +1 -1
- package/lib/chunk-AOUFWACR.mjs +74 -0
- package/lib/chunk-AOUFWACR.mjs.map +1 -0
- package/lib/{chunk-WK4O3HH4.js → chunk-AY7HKAG2.js} +2 -3
- package/lib/chunk-AY7HKAG2.js.map +1 -0
- package/lib/{chunk-M6OCVAUD.mjs → chunk-BUXOCPHI.mjs} +3 -3
- package/lib/{chunk-54RHVQFA.mjs → chunk-D42ISCFK.mjs} +2 -2
- package/lib/{chunk-5EIBKBYS.js → chunk-EU3ZZF7Y.js} +13 -13
- package/lib/chunk-EU3ZZF7Y.js.map +1 -0
- package/lib/{chunk-PEWNA24X.mjs → chunk-KBZUNXJB.mjs} +4 -4
- package/lib/chunk-KBZUNXJB.mjs.map +1 -0
- package/lib/chunk-LYNVPN3R.js +28 -0
- package/lib/chunk-LYNVPN3R.js.map +1 -0
- package/lib/{chunk-7XFLH6R2.mjs → chunk-NP64RC7B.mjs} +2 -3
- package/lib/chunk-NP64RC7B.mjs.map +1 -0
- package/lib/{chunk-YPAUFM3S.mjs → chunk-OHEMRF7Y.mjs} +3 -3
- package/lib/{chunk-QLVLZRHT.js → chunk-OJSTQQ3N.js} +5 -5
- package/lib/chunk-OJSTQQ3N.js.map +1 -0
- package/lib/{chunk-5AO66YV5.mjs → chunk-OKF3BVUD.mjs} +4 -4
- package/lib/{chunk-N6YMNLXG.js → chunk-OO6BTBVP.js} +11 -11
- package/lib/chunk-OO6BTBVP.js.map +1 -0
- package/lib/{chunk-SEIKS6G7.js → chunk-P6V6S6TZ.js} +19 -15
- package/lib/chunk-P6V6S6TZ.js.map +1 -0
- package/lib/{chunk-MTJJKWTP.js → chunk-PESVL5YJ.js} +7 -7
- package/lib/{chunk-MTJJKWTP.js.map → chunk-PESVL5YJ.js.map} +1 -1
- package/lib/{chunk-7GIRQ5EA.js → chunk-PIBDJJUU.js} +31 -23
- package/lib/chunk-PIBDJJUU.js.map +1 -0
- package/lib/{chunk-WPHJ4QTB.js → chunk-SPWF5IFU.js} +5 -5
- package/lib/{chunk-WPHJ4QTB.js.map → chunk-SPWF5IFU.js.map} +1 -1
- package/lib/{chunk-Z7OEFOAD.mjs → chunk-TQWUSDQD.mjs} +8 -8
- package/lib/chunk-TQWUSDQD.mjs.map +1 -0
- package/lib/{chunk-CQWI6YRI.js → chunk-UZ2PWO4O.js} +4 -4
- package/lib/{chunk-CQWI6YRI.js.map → chunk-UZ2PWO4O.js.map} +1 -1
- package/lib/{chunk-356HMXOP.js → chunk-VBTWYDEM.js} +4 -4
- package/lib/chunk-VBTWYDEM.js.map +1 -0
- package/lib/{chunk-4SPM2CSQ.js → chunk-W2GOLG3K.js} +5 -5
- package/lib/{chunk-4SPM2CSQ.js.map → chunk-W2GOLG3K.js.map} +1 -1
- package/lib/{chunk-GDSB7CDN.mjs → chunk-YBVMP4EI.mjs} +4 -4
- package/lib/chunk-YBVMP4EI.mjs.map +1 -0
- package/lib/{chunk-PJI3DRGP.mjs → chunk-YPAFWVDL.mjs} +4 -4
- package/lib/chunk-YPAFWVDL.mjs.map +1 -0
- package/lib/{chunk-UV4I7JZR.mjs → chunk-ZMEQCIRN.mjs} +16 -12
- package/lib/chunk-ZMEQCIRN.mjs.map +1 -0
- package/lib/{chunk-4SR7J2I4.js → chunk-ZVW3IHBW.js} +8 -8
- package/lib/chunk-ZVW3IHBW.js.map +1 -0
- package/lib/{chunk-TP5I237J.mjs → chunk-ZWE5Q37U.mjs} +14 -12
- package/lib/chunk-ZWE5Q37U.mjs.map +1 -0
- package/lib/codeGen.d.mts +1 -1
- package/lib/codeGen.d.ts +1 -1
- package/lib/codeGen.js +4 -4
- package/lib/codeGen.mjs +3 -3
- package/lib/exceptions.js +2 -2
- package/lib/exceptions.mjs +1 -1
- package/lib/helpers/core.d.mts +1 -1
- package/lib/helpers/core.d.ts +1 -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 +1 -1
- package/lib/helpers/setStrictType.d.ts +1 -1
- package/lib/helpers/setStrictType.js +5 -5
- package/lib/helpers/setStrictType.mjs +4 -4
- package/lib/index.d.mts +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +7 -7
- package/lib/index.mjs +6 -6
- package/lib/parse.d.mts +1 -1
- package/lib/parse.d.ts +1 -1
- package/lib/parse.js +6 -6
- package/lib/parse.mjs +5 -5
- package/lib/parseOrFail.d.mts +1 -1
- package/lib/parseOrFail.d.ts +1 -1
- package/lib/parseOrFail.js +6 -6
- package/lib/parseOrFail.mjs +5 -5
- package/lib/schemas/ArraySchema.d.mts +2 -2
- package/lib/schemas/ArraySchema.d.ts +2 -2
- 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 +3 -2
- package/lib/schemas/BigIntSchema.d.ts +3 -2
- 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 +2 -2
- package/lib/schemas/BooleanSchema.d.ts +2 -2
- 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 +1 -1
- package/lib/schemas/CommonSchema.d.ts +1 -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 +2 -2
- package/lib/schemas/DateSchema.d.ts +2 -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 +2 -2
- package/lib/schemas/NumberSchema.d.ts +2 -2
- 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 +2 -2
- package/lib/schemas/ObjectSchema.d.ts +2 -2
- 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 +3 -2
- package/lib/schemas/StringSchema.d.ts +3 -2
- 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/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-5EIBKBYS.js.map +0 -1
- 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-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-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-QV4JDW3T.mjs.map → chunk-4GDBM5LP.mjs.map} +0 -0
- /package/lib/{chunk-MRWZVTNH.mjs.map → chunk-ADSMWGDJ.mjs.map} +0 -0
- /package/lib/{chunk-M6OCVAUD.mjs.map → chunk-BUXOCPHI.mjs.map} +0 -0
- /package/lib/{chunk-54RHVQFA.mjs.map → chunk-D42ISCFK.mjs.map} +0 -0
- /package/lib/{chunk-YPAUFM3S.mjs.map → chunk-OHEMRF7Y.mjs.map} +0 -0
- /package/lib/{chunk-5AO66YV5.mjs.map → chunk-OKF3BVUD.mjs.map} +0 -0
- /package/lib/helpers/{isValidDate.mjs.map → isValidDateInner.mjs.map} +0 -0
package/README.md
CHANGED
|
@@ -116,7 +116,7 @@ This would output a string:
|
|
|
116
116
|
}
|
|
117
117
|
```
|
|
118
118
|
|
|
119
|
-
|
|
119
|
+
> **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
120
|
|
|
121
121
|
#### Generating Named TypeScript Types with `codeGenWithName`
|
|
122
122
|
|
|
@@ -256,14 +256,25 @@ Explanation
|
|
|
256
256
|
|
|
257
257
|
### Chaining Methods
|
|
258
258
|
|
|
259
|
-
- `nullable()`: Allows the value to be null
|
|
260
|
-
- `optional()`: Allows the value to be undefined
|
|
259
|
+
- `nullable()`: Allows the value to be `null`.
|
|
260
|
+
- `optional()`: Allows the value to be `undefined`.
|
|
261
|
+
- `default(value: unknown)`: Sets a default value if the received value is `undefined`.
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
> **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'"`.
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
> **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`.
|
|
268
|
+
|
|
261
269
|
|
|
262
270
|
Example:
|
|
263
271
|
|
|
264
272
|
```typeScript
|
|
273
|
+
const schemaWithDefault = string().nullable().default('defaultString');
|
|
274
|
+
// This schema allows null values and sets 'defaultString' if the value is undefined.
|
|
265
275
|
|
|
266
|
-
const
|
|
276
|
+
const optionalSchema = string().nullable().optional();
|
|
277
|
+
// This schema allows both null and undefined values, but it does not provide a default value.
|
|
267
278
|
```
|
|
268
279
|
|
|
269
280
|
- String Literals:
|
|
@@ -454,717 +465,888 @@ Each key and message will be visible in text editors that support JSDoc IntelliS
|
|
|
454
465
|
|
|
455
466
|
### Built-in Custom Assert Documentation {#builtin_custom_assert_documentation}
|
|
456
467
|
|
|
457
|
-
|
|
468
|
+
* [string](#assertdir_string)
|
|
469
|
+
* [atLeastOneDigit](#assert_atLeastOneDigit_string)
|
|
470
|
+
* [atLeastOneLowerChar](#assert_atLeastOneLowerChar_string)
|
|
471
|
+
* [atLeastOneSpecialChar](#assert_atLeastOneSpecialChar_string)
|
|
472
|
+
* [atLeastOneUpperChar](#assert_atLeastOneUpperChar_string)
|
|
473
|
+
* [contains](#assert_contains_string)
|
|
474
|
+
* [email](#assert_email_string)
|
|
475
|
+
* [endsWith](#assert_endsWith_string)
|
|
476
|
+
* [isValidDate](#assert_isValidDate_string)
|
|
477
|
+
* [isValidDateTime](#assert_isValidDateTime_string)
|
|
478
|
+
* [isValidTime](#assert_isValidTime_string)
|
|
479
|
+
* [lowerCase](#assert_lowerCase_string)
|
|
480
|
+
* [maxLength](#assert_maxLength_string)
|
|
481
|
+
* [minLength](#assert_minLength_string)
|
|
482
|
+
* [regExp](#assert_regExp_string)
|
|
483
|
+
* [startsWith](#assert_startsWith_string)
|
|
484
|
+
* [upperCase](#assert_upperCase_string)
|
|
485
|
+
* [uuid](#assert_uuid_string)
|
|
486
|
+
* [uuidV1](#assert_uuidV1_string)
|
|
487
|
+
* [uuidV2](#assert_uuidV2_string)
|
|
488
|
+
* [uuidV3](#assert_uuidV3_string)
|
|
489
|
+
* [uuidV4](#assert_uuidV4_string)
|
|
490
|
+
* [uuidV5](#assert_uuidV5_string)
|
|
491
|
+
* [validUrl](#assert_validUrl_string)
|
|
492
|
+
* [number](#assertdir_number)
|
|
493
|
+
* [max](#assert_max_number)
|
|
494
|
+
* [maxExcluded](#assert_maxExcluded_number)
|
|
495
|
+
* [min](#assert_min_number)
|
|
496
|
+
* [minExcluded](#assert_minExcluded_number)
|
|
497
|
+
* [negative](#assert_negative_number)
|
|
498
|
+
* [positive](#assert_positive_number)
|
|
499
|
+
* [array](#assertdir_array)
|
|
500
|
+
* [maxArrayLength](#assert_maxArrayLength_array)
|
|
501
|
+
* [minArrayLength](#assert_minArrayLength_array)
|
|
502
|
+
* [bigint](#assertdir_bigint)
|
|
503
|
+
* [bigintMax](#assert_bigintMax_bigint)
|
|
504
|
+
* [bigintMaxExcluded](#assert_bigintMaxExcluded_bigint)
|
|
505
|
+
* [bigintMin](#assert_bigintMin_bigint)
|
|
506
|
+
* [bigintMinExcluded](#assert_bigintMinExcluded_bigint)
|
|
507
|
+
* [date](#assertdir_date)
|
|
508
|
+
* [dateMax](#assert_dateMax_date)
|
|
509
|
+
* [dateMin](#assert_dateMin_date)
|
|
510
|
+
* [mix](#assertdir_mix)
|
|
511
|
+
* [equalTo](#assert_equalTo_mix)
|
|
512
|
+
* [oneOfValues](#assert_oneOfValues_mix)
|
|
513
|
+
|
|
514
|
+
#### string {#assertdir_string}
|
|
458
515
|
|
|
459
516
|
<b>Prerequisites</b>
|
|
460
517
|
|
|
461
518
|
```typescript
|
|
462
|
-
import {
|
|
519
|
+
import { string } from 'bguard/string';
|
|
463
520
|
```
|
|
464
521
|
|
|
465
|
-
* _Description_ Creates a new schema for validating
|
|
522
|
+
* _Description_ Creates a new schema for validating string values.
|
|
466
523
|
* _Example_
|
|
467
524
|
```typescript
|
|
468
|
-
const schema =
|
|
469
|
-
parseOrFail(schema,
|
|
470
|
-
parseOrFail(schema,
|
|
471
|
-
parseOrFail(schema, '42'); // Throws a validation error
|
|
525
|
+
const schema = string();
|
|
526
|
+
parseOrFail(schema, 'hello'); // Validates successfully
|
|
527
|
+
parseOrFail(schema, 123); // Throws a validation error
|
|
472
528
|
```
|
|
473
529
|
|
|
474
530
|
|
|
475
|
-
#####
|
|
531
|
+
##### atLeastOneDigit (string) {#assert_atLeastOneDigit_string}
|
|
476
532
|
|
|
477
533
|
```typescript
|
|
478
|
-
import {
|
|
534
|
+
import { atLeastOneDigit } from 'bguard/string/atLeastOneDigit';
|
|
479
535
|
```
|
|
480
536
|
|
|
481
|
-
* _Description_ Asserts that a
|
|
482
|
-
*
|
|
483
|
-
* _Throws_ {ValidationError} if the received value exceeds the expected maximum value.
|
|
537
|
+
* _Description_ Asserts that a string value contains at least one digit.
|
|
538
|
+
* _Throws_ {ValidationError} if the received value does not contain at least one digit.
|
|
484
539
|
* _Example_
|
|
485
540
|
```typescript
|
|
486
|
-
const schema =
|
|
487
|
-
parseOrFail(schema,
|
|
488
|
-
parseOrFail(schema,
|
|
489
|
-
parseOrFail(schema, 101n); // Throws an error: 'The received value is greater than expected'
|
|
541
|
+
const schema = string().custom(atLeastOneDigit());
|
|
542
|
+
parseOrFail(schema, 'abc123'); // Valid
|
|
543
|
+
parseOrFail(schema, 'abcdef'); // Throws an error: 'The received value does not contain at least one digit'
|
|
490
544
|
```
|
|
491
|
-
* _See_ Error Translation Key = '
|
|
545
|
+
* _See_ Error Translation Key = 's:atLeastOneDigit'
|
|
492
546
|
|
|
493
547
|
|
|
494
|
-
#####
|
|
548
|
+
##### atLeastOneLowerChar (string) {#assert_atLeastOneLowerChar_string}
|
|
495
549
|
|
|
496
550
|
```typescript
|
|
497
|
-
import {
|
|
551
|
+
import { atLeastOneLowerChar } from 'bguard/string/atLeastOneLowerChar';
|
|
498
552
|
```
|
|
499
553
|
|
|
500
|
-
* _Description_
|
|
501
|
-
*
|
|
502
|
-
* _Throws_ {ValidationError} if the received value is greater than or equal to the expected maximum value.
|
|
554
|
+
* _Description_ Asserts that a string value contains at least one lowercase character.
|
|
555
|
+
* _Throws_ {ValidationError} if the received value does not contain at least one lowercase character.
|
|
503
556
|
* _Example_
|
|
504
557
|
```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'
|
|
558
|
+
const schema = string().custom(atLeastOneLowerChar());
|
|
559
|
+
parseOrFail(schema, 'abcDEF'); // Valid
|
|
560
|
+
parseOrFail(schema, 'ABCDEF'); // Throws an error: 'The received value does not contain at least one lowercase character'
|
|
509
561
|
```
|
|
510
|
-
* _See_ Error Translation Key = '
|
|
562
|
+
* _See_ Error Translation Key = 's:atLeastOneLowerChar'
|
|
511
563
|
|
|
512
564
|
|
|
513
|
-
#####
|
|
565
|
+
##### atLeastOneSpecialChar (string) {#assert_atLeastOneSpecialChar_string}
|
|
514
566
|
|
|
515
567
|
```typescript
|
|
516
|
-
import {
|
|
568
|
+
import { atLeastOneSpecialChar } from 'bguard/string/atLeastOneSpecialChar';
|
|
517
569
|
```
|
|
518
570
|
|
|
519
|
-
* _Description_ Asserts that a
|
|
520
|
-
* _Param_ {
|
|
521
|
-
* _Throws_ {ValidationError} if the received value
|
|
571
|
+
* _Description_ Asserts that a string value contains at least one special character.
|
|
572
|
+
* _Param_ {string} [allowedSpecialChars=* '@!#%&()^~{}'] The string containing allowed special characters. Defaults to '*@!#%&()^~{}'.
|
|
573
|
+
* _Throws_ {ValidationError} if the received value does not contain at least one of the allowed special characters.
|
|
522
574
|
* _Example_
|
|
523
575
|
```typescript
|
|
524
|
-
const schema =
|
|
525
|
-
parseOrFail(schema,
|
|
526
|
-
parseOrFail(schema,
|
|
527
|
-
|
|
576
|
+
const schema = string().custom(atLeastOneSpecialChar()); // Default special characters
|
|
577
|
+
parseOrFail(schema, 'abc!def'); // Valid
|
|
578
|
+
parseOrFail(schema, 'abcdef'); // Throws an error: 'The received value does not contain at least one special character'
|
|
579
|
+
|
|
580
|
+
const customSchema = string().custom(atLeastOneSpecialChar('@$')); // Custom special characters
|
|
581
|
+
parseOrFail(customSchema, 'abc@def'); // Valid
|
|
582
|
+
parseOrFail(customSchema, 'abcdef'); // Throws an error: 'The received value does not contain at least one special character'
|
|
528
583
|
```
|
|
529
|
-
* _See_ Error Translation Key = '
|
|
584
|
+
* _See_ Error Translation Key = 's:atLeastOneSpecialChar'
|
|
530
585
|
|
|
531
586
|
|
|
532
|
-
#####
|
|
587
|
+
##### atLeastOneUpperChar (string) {#assert_atLeastOneUpperChar_string}
|
|
533
588
|
|
|
534
589
|
```typescript
|
|
535
|
-
import {
|
|
590
|
+
import { atLeastOneUpperChar } from 'bguard/string/atLeastOneUpperChar';
|
|
536
591
|
```
|
|
537
592
|
|
|
538
|
-
* _Description_ Asserts that a
|
|
539
|
-
*
|
|
540
|
-
* _Throws_ {ValidationError} if the received value is less than or equal to the expected minimum value.
|
|
593
|
+
* _Description_ Asserts that a string value contains at least one uppercase character.
|
|
594
|
+
* _Throws_ {ValidationError} if the received value does not contain at least one uppercase character.
|
|
541
595
|
* _Example_
|
|
542
596
|
```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'
|
|
597
|
+
const schema = string().custom(atLeastOneUpperChar());
|
|
598
|
+
parseOrFail(schema, 'abcDEF'); // Valid
|
|
599
|
+
parseOrFail(schema, 'abcdef'); // Throws an error: 'The received value does not contain at least one uppercase character'
|
|
547
600
|
```
|
|
548
|
-
* _See_ Error Translation Key = '
|
|
601
|
+
* _See_ Error Translation Key = 's:atLeastOneUpperChar'
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
##### contains (string) {#assert_contains_string}
|
|
549
605
|
|
|
550
|
-
#### date
|
|
551
|
-
|
|
552
|
-
<b>Prerequisites</b>
|
|
553
|
-
|
|
554
606
|
```typescript
|
|
555
|
-
import {
|
|
607
|
+
import { contains } from 'bguard/string/contains';
|
|
556
608
|
```
|
|
557
|
-
|
|
558
|
-
* _Description_
|
|
609
|
+
|
|
610
|
+
* _Description_ Asserts that a string value contains a specified substring.
|
|
611
|
+
* _Param_ {string} substring The substring that must be present in the string value.
|
|
612
|
+
* _Throws_ {ValidationError} if the received value does not contain the required substring.
|
|
559
613
|
* _Example_
|
|
560
614
|
```typescript
|
|
561
|
-
const schema =
|
|
562
|
-
parseOrFail(schema,
|
|
563
|
-
parseOrFail(schema, '
|
|
615
|
+
const schema = string().custom(contains('foo'));
|
|
616
|
+
parseOrFail(schema, 'foobar'); // Valid
|
|
617
|
+
parseOrFail(schema, 'bar'); // Throws an error: 'The received value does not contain the required substring'
|
|
564
618
|
```
|
|
565
|
-
|
|
619
|
+
* _See_ Error Translation Key = 's:contains'
|
|
566
620
|
|
|
567
|
-
|
|
621
|
+
|
|
622
|
+
##### email (string) {#assert_email_string}
|
|
568
623
|
|
|
569
624
|
```typescript
|
|
570
|
-
import {
|
|
625
|
+
import { email } from 'bguard/string/email';
|
|
571
626
|
```
|
|
572
627
|
|
|
573
|
-
* _Description_ Asserts that a
|
|
574
|
-
*
|
|
575
|
-
* _Throws_ {ValidationError} if the received value is greater than the expected maximum value.
|
|
628
|
+
* _Description_ Asserts that a string value matches the email pattern. The pattern checks for a basic email format.
|
|
629
|
+
* _Throws_ {ValidationError} if the received value does not match the email pattern.
|
|
576
630
|
* _Example_
|
|
577
631
|
```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'
|
|
632
|
+
const schema = string().custom(email());
|
|
633
|
+
parseOrFail(schema, 'example@example.com'); // Valid
|
|
634
|
+
parseOrFail(schema, 'invalid-email'); // Throws an error: 'The received value does not match the required email pattern'
|
|
582
635
|
```
|
|
583
|
-
* _See_ Error Translation Key = '
|
|
636
|
+
* _See_ - Error Translation Key = 's:email'
|
|
584
637
|
|
|
585
638
|
|
|
586
|
-
#####
|
|
639
|
+
##### endsWith (string) {#assert_endsWith_string}
|
|
587
640
|
|
|
588
641
|
```typescript
|
|
589
|
-
import {
|
|
642
|
+
import { endsWith } from 'bguard/string/endsWith';
|
|
590
643
|
```
|
|
591
644
|
|
|
592
|
-
* _Description_ Asserts that a
|
|
593
|
-
* _Param_ {
|
|
594
|
-
* _Throws_ {ValidationError} if the received value
|
|
645
|
+
* _Description_ Asserts that a string value ends with a specified substring.
|
|
646
|
+
* _Param_ {string} substring The substring that the string value must end with.
|
|
647
|
+
* _Throws_ {ValidationError} if the received value does not end with the required substring.
|
|
595
648
|
* _Example_
|
|
596
649
|
```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'
|
|
650
|
+
const schema = string().custom(endsWith('bar'));
|
|
651
|
+
parseOrFail(schema, 'foobar'); // Valid
|
|
652
|
+
parseOrFail(schema, 'foofoo'); // Throws an error: 'The received value does not end with the required substring'
|
|
601
653
|
```
|
|
602
|
-
* _See_ Error Translation Key = '
|
|
654
|
+
* _See_ Error Translation Key = 's:endsWith'
|
|
655
|
+
|
|
656
|
+
|
|
657
|
+
##### isValidDate (string) {#assert_isValidDate_string}
|
|
603
658
|
|
|
604
|
-
#### mix
|
|
605
|
-
|
|
606
|
-
<b>Prerequisites</b>
|
|
607
|
-
|
|
608
659
|
```typescript
|
|
609
|
-
import {
|
|
660
|
+
import { isValidDate } from 'bguard/string/isValidDate';
|
|
610
661
|
```
|
|
611
|
-
|
|
612
|
-
* _Description_
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
* _Param_ {T} valueTypes - An array of primitive types that the value can match.
|
|
662
|
+
|
|
663
|
+
* _Description_ Asserts that a string is a valid date in the format YYYY-MM-DD.
|
|
664
|
+
* _Throws_ {ValidationError} if the received string is not a valid date.
|
|
616
665
|
* _Example_
|
|
617
666
|
```typescript
|
|
618
|
-
const schema =
|
|
619
|
-
parseOrFail(schema,
|
|
620
|
-
parseOrFail(schema,
|
|
621
|
-
parseOrFail(schema,
|
|
667
|
+
const schema = string().custom(isValidDate());
|
|
668
|
+
parseOrFail(schema, "2020-01-01"); // Valid
|
|
669
|
+
parseOrFail(schema, "2020-1-1"); // Throws an error: 'The received value is not a valid date'
|
|
670
|
+
parseOrFail(schema, "2020-01-32"); // Throws an error: 'The received value is not a valid date'
|
|
622
671
|
```
|
|
623
|
-
|
|
672
|
+
* _See_ Error Translation Key = 's:isValidDate'
|
|
673
|
+
|
|
624
674
|
|
|
625
|
-
#####
|
|
675
|
+
##### isValidDateTime (string) {#assert_isValidDateTime_string}
|
|
626
676
|
|
|
627
677
|
```typescript
|
|
628
|
-
import {
|
|
678
|
+
import { isValidDateTime } from 'bguard/string/isValidDateTime';
|
|
629
679
|
```
|
|
630
680
|
|
|
631
|
-
* _Description_
|
|
632
|
-
*
|
|
633
|
-
|
|
634
|
-
|
|
681
|
+
* _Description_ Asserts that a string value is a valid ISO 8601 datetime string.
|
|
682
|
+
* _Param_ {DateTimeOptions} options Options to control the validation:
|
|
683
|
+
- `offset`: If `true`, allows timezone offsets in the datetime string.
|
|
684
|
+
- `precision`: Specify the exact number of fractional second digits allowed (e.g., 3 for milliseconds).
|
|
685
|
+
* _Throws_ {ValidationError} if the received value is not a valid datetime string according to the options.
|
|
635
686
|
* _Example_
|
|
636
687
|
```typescript
|
|
637
|
-
const schema =
|
|
638
|
-
parseOrFail(schema,
|
|
639
|
-
parseOrFail(schema,
|
|
688
|
+
const schema = string().custom(isValidDateTime());
|
|
689
|
+
parseOrFail(schema, "2024-01-01T00:00:00Z"); // Valid
|
|
690
|
+
parseOrFail(schema, "2024-01-01T00:00:00.123Z"); // Valid
|
|
691
|
+
parseOrFail(schema, "2024-01-01T00:00:00+03:00"); // Invalid (no offsets allowed)
|
|
692
|
+
|
|
693
|
+
const schemaWithOffset = string().custom(isValidDateTime({ offset: true }));
|
|
694
|
+
parseOrFail(schemaWithOffset, "2024-01-01T00:00:00+04:00"); // Valid
|
|
695
|
+
|
|
696
|
+
const schemaWithPrecision = string().custom(isValidDateTime({ precision: 3 }));
|
|
697
|
+
parseOrFail(schemaWithPrecision, "2024-01-01T00:00:00.123Z"); // Valid
|
|
698
|
+
parseOrFail(schemaWithPrecision, "2024-01-01T00:00:00.123456Z"); // Invalid
|
|
640
699
|
```
|
|
641
|
-
* _See_ Error Translation Key = '
|
|
700
|
+
* _See_ Error Translation Key = 's:isValidDateTime'
|
|
642
701
|
|
|
643
702
|
|
|
644
|
-
#####
|
|
703
|
+
##### isValidTime (string) {#assert_isValidTime_string}
|
|
645
704
|
|
|
646
705
|
```typescript
|
|
647
|
-
import {
|
|
706
|
+
import { isValidTime } from 'bguard/string/isValidTime';
|
|
648
707
|
```
|
|
649
708
|
|
|
650
|
-
* _Description_
|
|
651
|
-
*
|
|
652
|
-
*
|
|
653
|
-
* _Throws_ {ValidationError} If the received value does not match at least one of the expected values.
|
|
709
|
+
* _Description_ Asserts that a string is a valid time in the format HH:mm:ss, with optional fractional seconds.
|
|
710
|
+
* _Param_ {IsValidTimeOptions} options Optional settings to configure the validation.
|
|
711
|
+
* _Throws_ {ValidationError} if the received string is not a valid time.
|
|
654
712
|
* _Example_
|
|
655
713
|
```typescript
|
|
656
|
-
const schema =
|
|
657
|
-
parseOrFail(schema,
|
|
658
|
-
parseOrFail(schema,
|
|
659
|
-
parseOrFail(schema,
|
|
714
|
+
const schema = string().custom(isValidTime());
|
|
715
|
+
parseOrFail(schema, "00:00:00"); // Valid
|
|
716
|
+
parseOrFail(schema, "23:59:59.9999999"); // Valid
|
|
717
|
+
parseOrFail(schema, "00:00:00.256Z"); // Throws an error: 'The received value is not a valid time'
|
|
718
|
+
|
|
719
|
+
const schemaWithPrecision = string().custom(isValidTime({ precision: 3 }));
|
|
720
|
+
parseOrFail(schemaWithPrecision, "00:00:00.256"); // Valid
|
|
721
|
+
parseOrFail(schemaWithPrecision, "00:00:00"); // Throws an error: 'The received value is not a valid time'
|
|
660
722
|
```
|
|
661
|
-
* _See_ Error Translation Key = '
|
|
723
|
+
* _See_ Error Translation Key = 's:isValidTime'
|
|
724
|
+
|
|
725
|
+
|
|
726
|
+
##### lowerCase (string) {#assert_lowerCase_string}
|
|
662
727
|
|
|
663
|
-
#### number
|
|
664
|
-
|
|
665
|
-
<b>Prerequisites</b>
|
|
666
|
-
|
|
667
728
|
```typescript
|
|
668
|
-
import {
|
|
729
|
+
import { lowerCase } from 'bguard/string/lowerCase';
|
|
669
730
|
```
|
|
670
|
-
|
|
671
|
-
* _Description_
|
|
731
|
+
|
|
732
|
+
* _Description_ Asserts that a string value is in lowercase.
|
|
733
|
+
* _Throws_ {ValidationError} if the received value is not in lowercase.
|
|
672
734
|
* _Example_
|
|
673
735
|
```typescript
|
|
674
|
-
const schema =
|
|
675
|
-
parseOrFail(schema,
|
|
676
|
-
parseOrFail(schema, '
|
|
736
|
+
const schema = string().custom(lowerCase());
|
|
737
|
+
parseOrFail(schema, 'valid'); // Valid
|
|
738
|
+
parseOrFail(schema, 'Invalid'); // Throws an error: 'The received value is not in lowercase'
|
|
677
739
|
```
|
|
678
|
-
|
|
740
|
+
* _See_ Error Translation Key = 's:lowerCase'
|
|
679
741
|
|
|
680
|
-
|
|
742
|
+
|
|
743
|
+
##### maxLength (string) {#assert_maxLength_string}
|
|
681
744
|
|
|
682
745
|
```typescript
|
|
683
|
-
import {
|
|
746
|
+
import { maxLength } from 'bguard/string/maxLength';
|
|
684
747
|
```
|
|
685
748
|
|
|
686
|
-
* _Description_ Asserts that a
|
|
687
|
-
* _Param_ {number} expected The maximum
|
|
688
|
-
* _Throws_ {ValidationError} if the received value
|
|
749
|
+
* _Description_ Asserts that the length of a string value is not greater than a specified maximum length.
|
|
750
|
+
* _Param_ {number} expected The maximum allowed length for the string.
|
|
751
|
+
* _Throws_ {ValidationError} if the length of the received value is greater than the expected length.
|
|
689
752
|
* _Example_
|
|
690
753
|
```typescript
|
|
691
|
-
const schema =
|
|
692
|
-
parseOrFail(schema,
|
|
693
|
-
parseOrFail(schema,
|
|
694
|
-
parseOrFail(schema, 101); // Throws an error: 'The received value is greater than expected'
|
|
754
|
+
const schema = string().custom(maxLength(10));
|
|
755
|
+
parseOrFail(schema, 'short'); // Valid
|
|
756
|
+
parseOrFail(schema, 'this is a very long string'); // Throws an error: 'The received value length is greater than expected'
|
|
695
757
|
```
|
|
696
|
-
* _See_ Error Translation Key = '
|
|
758
|
+
* _See_ Error Translation Key = 's:maxLength'
|
|
697
759
|
|
|
698
760
|
|
|
699
|
-
#####
|
|
761
|
+
##### minLength (string) {#assert_minLength_string}
|
|
700
762
|
|
|
701
763
|
```typescript
|
|
702
|
-
import {
|
|
764
|
+
import { minLength } from 'bguard/string/minLength';
|
|
703
765
|
```
|
|
704
766
|
|
|
705
|
-
* _Description_
|
|
706
|
-
* _Param_ {number} expected
|
|
707
|
-
* _Throws_ {ValidationError} if the received value is
|
|
767
|
+
* _Description_ Asserts that the length of a string value is not less than a specified minimum length.
|
|
768
|
+
* _Param_ {number} expected The minimum required length for the string.
|
|
769
|
+
* _Throws_ {ValidationError} if the length of the received value is less than the expected length.
|
|
708
770
|
* _Example_
|
|
709
771
|
```typescript
|
|
710
|
-
const schema =
|
|
711
|
-
parseOrFail(schema,
|
|
712
|
-
parseOrFail(schema,
|
|
713
|
-
parseOrFail(schema, 101); // Throws an error: 'The received value is greater than or equal to expected'
|
|
772
|
+
const schema = string().custom(minLength(5));
|
|
773
|
+
parseOrFail(schema, 'short'); // Throws an error: 'The received value length is less than expected'
|
|
774
|
+
parseOrFail(schema, 'adequate'); // Valid
|
|
714
775
|
```
|
|
715
|
-
* _See_ Error Translation Key = '
|
|
776
|
+
* _See_ Error Translation Key = 's:minLength'
|
|
716
777
|
|
|
717
778
|
|
|
718
|
-
#####
|
|
779
|
+
##### regExp (string) {#assert_regExp_string}
|
|
719
780
|
|
|
720
781
|
```typescript
|
|
721
|
-
import {
|
|
782
|
+
import { regExp } from 'bguard/string/regExp';
|
|
722
783
|
```
|
|
723
784
|
|
|
724
|
-
* _Description_ Asserts that a
|
|
725
|
-
* _Param_ {
|
|
726
|
-
* _Throws_ {ValidationError} if the received value
|
|
785
|
+
* _Description_ Asserts that a string value matches a specified regular expression pattern.
|
|
786
|
+
* _Param_ {RegExp} expected The regular expression pattern that the string value should match.
|
|
787
|
+
* _Throws_ {ValidationError} if the received value does not match the expected pattern.
|
|
727
788
|
* _Example_
|
|
728
789
|
```typescript
|
|
729
|
-
const schema =
|
|
730
|
-
parseOrFail(schema,
|
|
731
|
-
parseOrFail(schema,
|
|
732
|
-
parseOrFail(schema, 9); // Throws an error: 'The received value is less than expected'
|
|
790
|
+
const schema = string().custom(regExp(/^[A-Za-z0-9]+$/)); // Validates against alphanumeric pattern
|
|
791
|
+
parseOrFail(schema, 'valid123'); // Valid
|
|
792
|
+
parseOrFail(schema, 'invalid!@#'); // Throws an error: 'The received value does not match the required text pattern'
|
|
733
793
|
```
|
|
734
|
-
* _See_ Error Translation Key = '
|
|
794
|
+
* _See_ Error Translation Key = 's:regExp'
|
|
735
795
|
|
|
736
796
|
|
|
737
|
-
#####
|
|
797
|
+
##### startsWith (string) {#assert_startsWith_string}
|
|
738
798
|
|
|
739
799
|
```typescript
|
|
740
|
-
import {
|
|
800
|
+
import { startsWith } from 'bguard/string/startsWith';
|
|
741
801
|
```
|
|
742
802
|
|
|
743
|
-
* _Description_ Asserts that a
|
|
744
|
-
* _Param_ {
|
|
745
|
-
* _Throws_ {ValidationError} if the received value
|
|
803
|
+
* _Description_ Asserts that a string value starts with a specified substring.
|
|
804
|
+
* _Param_ {string} substring The substring that the string value must start with.
|
|
805
|
+
* _Throws_ {ValidationError} if the received value does not start with the required substring.
|
|
746
806
|
* _Example_
|
|
747
807
|
```typescript
|
|
748
|
-
const schema =
|
|
749
|
-
parseOrFail(schema,
|
|
750
|
-
parseOrFail(schema,
|
|
751
|
-
parseOrFail(schema, 9); // Throws an error: 'The received value is less than or equal to expected'
|
|
808
|
+
const schema = string().custom(startsWith('foo'));
|
|
809
|
+
parseOrFail(schema, 'foobar'); // Valid
|
|
810
|
+
parseOrFail(schema, 'barfoo'); // Throws an error: 'The received value does not start with the required substring'
|
|
752
811
|
```
|
|
753
|
-
* _See_ Error Translation Key = '
|
|
812
|
+
* _See_ Error Translation Key = 's:startsWith'
|
|
754
813
|
|
|
755
814
|
|
|
756
|
-
#####
|
|
815
|
+
##### upperCase (string) {#assert_upperCase_string}
|
|
757
816
|
|
|
758
817
|
```typescript
|
|
759
|
-
import {
|
|
818
|
+
import { upperCase } from 'bguard/string/upperCase';
|
|
760
819
|
```
|
|
761
820
|
|
|
762
|
-
* _Description_ Asserts that a
|
|
763
|
-
* _Throws_ {ValidationError} if the received value is not
|
|
821
|
+
* _Description_ Asserts that a string value is entirely in uppercase.
|
|
822
|
+
* _Throws_ {ValidationError} if the received value is not in uppercase.
|
|
764
823
|
* _Example_
|
|
765
824
|
```typescript
|
|
766
|
-
const schema =
|
|
767
|
-
parseOrFail(schema,
|
|
768
|
-
parseOrFail(schema,
|
|
769
|
-
parseOrFail(schema,
|
|
825
|
+
const schema = string().custom(upperCase());
|
|
826
|
+
parseOrFail(schema, 'VALID'); // Valid
|
|
827
|
+
parseOrFail(schema, 'INVALID'); // Throws an error: 'The received value is not in uppercase'
|
|
828
|
+
parseOrFail(schema, 'Valid'); // Throws an error: 'The received value is not in uppercase'
|
|
770
829
|
```
|
|
771
|
-
* _See_
|
|
830
|
+
* _See_ Error Translation Key = 's:upperCase'
|
|
772
831
|
|
|
773
832
|
|
|
774
|
-
#####
|
|
833
|
+
##### uuid (string) {#assert_uuid_string}
|
|
775
834
|
|
|
776
835
|
```typescript
|
|
777
|
-
import {
|
|
836
|
+
import { uuid } from 'bguard/string/uuid';
|
|
778
837
|
```
|
|
779
838
|
|
|
780
|
-
* _Description_ Asserts that a
|
|
781
|
-
* _Throws_ {ValidationError} if the received value is not
|
|
839
|
+
* _Description_ Asserts that a string value matches the UUID format.
|
|
840
|
+
* _Throws_ {ValidationError} if the received value is not a valid UUID.
|
|
782
841
|
* _Example_
|
|
783
842
|
```typescript
|
|
784
|
-
const schema =
|
|
785
|
-
parseOrFail(schema,
|
|
786
|
-
parseOrFail(schema,
|
|
787
|
-
parseOrFail(schema, -5); // Throws an error: 'The received value is not a positive number'
|
|
843
|
+
const schema = string().custom(uuid());
|
|
844
|
+
parseOrFail(schema, '123e4567-e89b-12d3-a456-426614174000'); // Valid
|
|
845
|
+
parseOrFail(schema, 'invalid-uuid'); // Throws an error: 'The received value is not a valid UUID'
|
|
788
846
|
```
|
|
789
|
-
* _See_ Error Translation Key = '
|
|
847
|
+
* _See_ Error Translation Key = 's:uuid'
|
|
848
|
+
|
|
849
|
+
|
|
850
|
+
##### uuidV1 (string) {#assert_uuidV1_string}
|
|
790
851
|
|
|
791
|
-
#### string
|
|
792
|
-
|
|
793
|
-
<b>Prerequisites</b>
|
|
794
|
-
|
|
795
852
|
```typescript
|
|
796
|
-
import {
|
|
853
|
+
import { uuidV1 } from 'bguard/string/uuidV1';
|
|
797
854
|
```
|
|
798
|
-
|
|
799
|
-
* _Description_
|
|
855
|
+
|
|
856
|
+
* _Description_ Asserts that a string value matches the UUID v1 format.
|
|
857
|
+
* _Throws_ {ValidationError} if the received value is not a valid UUID v1.
|
|
800
858
|
* _Example_
|
|
801
859
|
```typescript
|
|
802
|
-
const schema = string();
|
|
803
|
-
parseOrFail(schema, '
|
|
804
|
-
parseOrFail(schema,
|
|
860
|
+
const schema = string().custom(uuidV1());
|
|
861
|
+
parseOrFail(schema, '550e8400-e29b-11d4-a716-446655440000'); // Valid
|
|
862
|
+
parseOrFail(schema, '550e8400-e29b-21d4-a716-446655440000'); // Throws an error: 'The received value is not a valid UUID v1'
|
|
863
|
+
parseOrFail(schema, 'invalid-uuid'); // Throws an error: 'The received value is not a valid UUID v1'
|
|
805
864
|
```
|
|
806
|
-
|
|
865
|
+
* _See_ Error Translation Key = 's:uuidV1'
|
|
807
866
|
|
|
808
|
-
|
|
867
|
+
|
|
868
|
+
##### uuidV2 (string) {#assert_uuidV2_string}
|
|
809
869
|
|
|
810
870
|
```typescript
|
|
811
|
-
import {
|
|
871
|
+
import { uuidV2 } from 'bguard/string/uuidV2';
|
|
812
872
|
```
|
|
813
873
|
|
|
814
|
-
* _Description_ Asserts that a string value
|
|
815
|
-
* _Throws_ {ValidationError} if the received value
|
|
874
|
+
* _Description_ Asserts that a string value matches the UUID v2 format.
|
|
875
|
+
* _Throws_ {ValidationError} if the received value is not a valid UUID v2.
|
|
816
876
|
* _Example_
|
|
817
877
|
```typescript
|
|
818
|
-
const schema = string().custom(
|
|
819
|
-
parseOrFail(schema, '
|
|
820
|
-
parseOrFail(schema, '
|
|
878
|
+
const schema = string().custom(uuidV2());
|
|
879
|
+
parseOrFail(schema, '550e8400-e29b-21d4-a716-446655440000'); // Valid
|
|
880
|
+
parseOrFail(schema, '550e8400-e29b-31d4-d716-446655440000'); // Throws an error: 'The received value is not a valid UUID v2'
|
|
881
|
+
parseOrFail(schema, 'invalid-uuid'); // Throws an error: 'The received value is not a valid UUID v2'
|
|
821
882
|
```
|
|
822
|
-
* _See_ Error Translation Key = 's:
|
|
883
|
+
* _See_ Error Translation Key = 's:uuidV2'
|
|
823
884
|
|
|
824
885
|
|
|
825
|
-
#####
|
|
886
|
+
##### uuidV3 (string) {#assert_uuidV3_string}
|
|
826
887
|
|
|
827
888
|
```typescript
|
|
828
|
-
import {
|
|
889
|
+
import { uuidV3 } from 'bguard/string/uuidV3';
|
|
829
890
|
```
|
|
830
891
|
|
|
831
|
-
* _Description_ Asserts that a string value
|
|
832
|
-
* _Throws_ {ValidationError} if the received value
|
|
892
|
+
* _Description_ Asserts that a string value matches the UUID v3 format.
|
|
893
|
+
* _Throws_ {ValidationError} if the received value is not a valid UUID v3.
|
|
833
894
|
* _Example_
|
|
834
895
|
```typescript
|
|
835
|
-
const schema = string().custom(
|
|
836
|
-
parseOrFail(schema, '
|
|
837
|
-
parseOrFail(schema, '
|
|
896
|
+
const schema = string().custom(uuidV3());
|
|
897
|
+
parseOrFail(schema, '550e8400-e29b-38d1-a456-426614174000'); // Valid
|
|
898
|
+
parseOrFail(schema, '550e8400-e29b-28d1-a456-426614174000'); // Throws an error: 'The received value is not a valid UUID v3'
|
|
899
|
+
parseOrFail(schema, 'invalid-uuid'); // Throws an error: 'The received value is not a valid UUID v3'
|
|
838
900
|
```
|
|
839
|
-
* _See_ Error Translation Key = 's:
|
|
901
|
+
* _See_ Error Translation Key = 's:uuidV3'
|
|
840
902
|
|
|
841
903
|
|
|
842
|
-
#####
|
|
904
|
+
##### uuidV4 (string) {#assert_uuidV4_string}
|
|
843
905
|
|
|
844
906
|
```typescript
|
|
845
|
-
import {
|
|
907
|
+
import { uuidV4 } from 'bguard/string/uuidV4';
|
|
846
908
|
```
|
|
847
909
|
|
|
848
|
-
* _Description_ Asserts that a string value
|
|
849
|
-
*
|
|
850
|
-
* _Throws_ {ValidationError} if the received value does not contain at least one of the allowed special characters.
|
|
910
|
+
* _Description_ Asserts that a string value matches the UUID v4 format.
|
|
911
|
+
* _Throws_ {ValidationError} if the received value is not a valid UUID v4.
|
|
851
912
|
* _Example_
|
|
852
913
|
```typescript
|
|
853
|
-
const schema = string().custom(
|
|
854
|
-
parseOrFail(schema, '
|
|
855
|
-
parseOrFail(schema, '
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
parseOrFail(
|
|
859
|
-
parseOrFail(customSchema, 'abcdef'); // Throws an error: 'The received value does not contain at least one special character'
|
|
914
|
+
const schema = string().custom(uuidV4());
|
|
915
|
+
parseOrFail(schema, '123e4567-e89b-42d3-a456-426614174000'); // Valid
|
|
916
|
+
parseOrFail(schema, '123e4567-e89b-12d3-a456-426614174000'); // Throws an error: 'The received value is not a valid UUID v4'
|
|
917
|
+
parseOrFail(schema, '123e4567-e89b-a2d3-a456-426614174000'); // Throws an error: 'The received value is not a valid UUID v4'
|
|
918
|
+
parseOrFail(schema, '123e4567-e89b-42d3-c456-426614174000'); // Throws an error: 'The received value is not a valid UUID v4'
|
|
919
|
+
parseOrFail(schema, 'invalid-uuid'); // Throws an error: 'The received value is not a valid UUID v4'
|
|
860
920
|
```
|
|
861
|
-
* _See_ Error Translation Key = 's:
|
|
921
|
+
* _See_ Error Translation Key = 's:uuidV4'
|
|
862
922
|
|
|
863
923
|
|
|
864
|
-
#####
|
|
924
|
+
##### uuidV5 (string) {#assert_uuidV5_string}
|
|
865
925
|
|
|
866
926
|
```typescript
|
|
867
|
-
import {
|
|
927
|
+
import { uuidV5 } from 'bguard/string/uuidV5';
|
|
868
928
|
```
|
|
869
929
|
|
|
870
|
-
* _Description_ Asserts that a string value
|
|
871
|
-
* _Throws_ {ValidationError} if the received value
|
|
930
|
+
* _Description_ Asserts that a string value matches the UUID v5 format.
|
|
931
|
+
* _Throws_ {ValidationError} if the received value is not a valid UUID v5.
|
|
872
932
|
* _Example_
|
|
873
933
|
```typescript
|
|
874
|
-
const schema = string().custom(
|
|
875
|
-
parseOrFail(schema, '
|
|
876
|
-
parseOrFail(schema, '
|
|
934
|
+
const schema = string().custom(uuidV5());
|
|
935
|
+
parseOrFail(schema, '550e8400-e29b-51d4-a716-446655440000'); // Valid
|
|
936
|
+
parseOrFail(schema, '550e8400-e29b-41d4-a716-446655440000'); // Throws an error: 'The received value is not a valid UUID v5'
|
|
937
|
+
parseOrFail(schema, 'invalid-uuid'); // Throws an error: 'The received value is not a valid UUID v5'
|
|
877
938
|
```
|
|
878
|
-
* _See_ Error Translation Key = 's:
|
|
939
|
+
* _See_ Error Translation Key = 's:uuidV5'
|
|
879
940
|
|
|
880
941
|
|
|
881
|
-
#####
|
|
942
|
+
##### validUrl (string) {#assert_validUrl_string}
|
|
882
943
|
|
|
883
944
|
```typescript
|
|
884
|
-
import {
|
|
945
|
+
import { validUrl } from 'bguard/string/validUrl';
|
|
885
946
|
```
|
|
886
947
|
|
|
887
|
-
* _Description_ Asserts that
|
|
888
|
-
* _Param_ {string}
|
|
889
|
-
* _Throws_ {ValidationError} if the received value does not
|
|
948
|
+
* _Description_ Asserts that the string value is a valid URL with optional protocol validation.
|
|
949
|
+
* _Param_ {string} [protocol] The protocol that the URL must start with (e.g., 'http'). If not provided, any URL starting with 'http://' or 'https://' is considered valid.
|
|
950
|
+
* _Throws_ {ValidationError} if the received value does not match the expected URL pattern.
|
|
890
951
|
* _Example_
|
|
891
952
|
```typescript
|
|
892
|
-
const schema = string().custom(
|
|
893
|
-
parseOrFail(schema, '
|
|
894
|
-
parseOrFail(schema, '
|
|
953
|
+
const schema = string().custom(validUrl()); // Validates any URL starting with 'http://' or 'https://'
|
|
954
|
+
parseOrFail(schema, 'http://example.com'); // Valid
|
|
955
|
+
parseOrFail(schema, 'https://example.com'); // Valid
|
|
956
|
+
parseOrFail(schema, 'ftp://example.com'); // Throws an error
|
|
957
|
+
parseOrFail(schema, 'http:example.com'); // Throws an error
|
|
895
958
|
```
|
|
896
|
-
* _See_ Error Translation Key = 's:
|
|
959
|
+
* _See_ Error Translation Key = 's:url'
|
|
897
960
|
|
|
961
|
+
#### number {#assertdir_number}
|
|
962
|
+
|
|
963
|
+
<b>Prerequisites</b>
|
|
964
|
+
|
|
965
|
+
```typescript
|
|
966
|
+
import { number } from 'bguard/number';
|
|
967
|
+
```
|
|
968
|
+
|
|
969
|
+
* _Description_ Creates a new schema for validating number values.
|
|
970
|
+
* _Example_
|
|
971
|
+
```typescript
|
|
972
|
+
const schema = number();
|
|
973
|
+
parseOrFail(schema, 42); // Validates successfully
|
|
974
|
+
parseOrFail(schema, '42'); // Throws a validation error
|
|
975
|
+
```
|
|
976
|
+
|
|
898
977
|
|
|
899
|
-
#####
|
|
978
|
+
##### max (number) {#assert_max_number}
|
|
900
979
|
|
|
901
980
|
```typescript
|
|
902
|
-
import {
|
|
981
|
+
import { max } from 'bguard/number/max';
|
|
903
982
|
```
|
|
904
983
|
|
|
905
|
-
* _Description_ Asserts that a
|
|
906
|
-
*
|
|
984
|
+
* _Description_ Asserts that a number value does not exceed a specified maximum value.
|
|
985
|
+
* _Param_ {number} expected The maximum allowable value.
|
|
986
|
+
* _Throws_ {ValidationError} if the received value exceeds the expected maximum value.
|
|
907
987
|
* _Example_
|
|
908
988
|
```typescript
|
|
909
|
-
const schema =
|
|
910
|
-
parseOrFail(schema,
|
|
911
|
-
parseOrFail(schema,
|
|
989
|
+
const schema = number().custom(max(100));
|
|
990
|
+
parseOrFail(schema, 99); // Valid
|
|
991
|
+
parseOrFail(schema, 100); // Valid
|
|
992
|
+
parseOrFail(schema, 101); // Throws an error: 'The received value is greater than expected'
|
|
912
993
|
```
|
|
913
|
-
* _See_
|
|
994
|
+
* _See_ Error Translation Key = 'n:max'
|
|
914
995
|
|
|
915
996
|
|
|
916
|
-
#####
|
|
997
|
+
##### maxExcluded (number) {#assert_maxExcluded_number}
|
|
917
998
|
|
|
918
999
|
```typescript
|
|
919
|
-
import {
|
|
1000
|
+
import { maxExcluded } from 'bguard/number/maxExcluded';
|
|
920
1001
|
```
|
|
921
1002
|
|
|
922
|
-
* _Description_ Asserts that a
|
|
923
|
-
* _Param_ {
|
|
924
|
-
* _Throws_ {ValidationError} if the received value
|
|
1003
|
+
* _Description_ - Asserts that a number value is strictly less than a specified maximum value (i.e., the maximum value is excluded).
|
|
1004
|
+
* _Param_ {number} expected - The maximum allowable value, which is excluded.
|
|
1005
|
+
* _Throws_ {ValidationError} if the received value is greater than or equal to the expected maximum value.
|
|
925
1006
|
* _Example_
|
|
926
1007
|
```typescript
|
|
927
|
-
const schema =
|
|
928
|
-
parseOrFail(schema,
|
|
929
|
-
parseOrFail(schema,
|
|
1008
|
+
const schema = number().custom(maxExcluded(100));
|
|
1009
|
+
parseOrFail(schema, 99); // Valid
|
|
1010
|
+
parseOrFail(schema, 100); // Throws an error: 'The received value is greater than or equal to expected'
|
|
1011
|
+
parseOrFail(schema, 101); // Throws an error: 'The received value is greater than or equal to expected'
|
|
930
1012
|
```
|
|
931
|
-
* _See_ Error Translation Key = '
|
|
1013
|
+
* _See_ Error Translation Key = 'n:maxExcluded'
|
|
932
1014
|
|
|
933
1015
|
|
|
934
|
-
#####
|
|
1016
|
+
##### min (number) {#assert_min_number}
|
|
935
1017
|
|
|
936
1018
|
```typescript
|
|
937
|
-
import {
|
|
1019
|
+
import { min } from 'bguard/number/min';
|
|
938
1020
|
```
|
|
939
1021
|
|
|
940
|
-
* _Description_ Asserts that a
|
|
941
|
-
*
|
|
1022
|
+
* _Description_ Asserts that a number value is not less than a specified minimum value.
|
|
1023
|
+
* _Param_ {number} expected The minimum allowable value.
|
|
1024
|
+
* _Throws_ {ValidationError} if the received value is less than the expected minimum value.
|
|
942
1025
|
* _Example_
|
|
943
1026
|
```typescript
|
|
944
|
-
const schema =
|
|
945
|
-
parseOrFail(schema,
|
|
946
|
-
parseOrFail(schema,
|
|
1027
|
+
const schema = number().custom(min(10));
|
|
1028
|
+
parseOrFail(schema, 11); // Valid
|
|
1029
|
+
parseOrFail(schema, 10); // Valid
|
|
1030
|
+
parseOrFail(schema, 9); // Throws an error: 'The received value is less than expected'
|
|
947
1031
|
```
|
|
948
|
-
* _See_ Error Translation Key = '
|
|
1032
|
+
* _See_ Error Translation Key = 'n:min'
|
|
949
1033
|
|
|
950
1034
|
|
|
951
|
-
#####
|
|
1035
|
+
##### minExcluded (number) {#assert_minExcluded_number}
|
|
952
1036
|
|
|
953
1037
|
```typescript
|
|
954
|
-
import {
|
|
1038
|
+
import { minExcluded } from 'bguard/number/minExcluded';
|
|
955
1039
|
```
|
|
956
1040
|
|
|
957
|
-
* _Description_ Asserts that
|
|
958
|
-
* _Param_ {number} expected The
|
|
959
|
-
* _Throws_ {ValidationError} if the
|
|
1041
|
+
* _Description_ Asserts that a number value is strictly greater than a specified minimum value (i.e., the minimum value is excluded).
|
|
1042
|
+
* _Param_ {number} expected The minimum allowable value, which is excluded.
|
|
1043
|
+
* _Throws_ {ValidationError} if the received value is less than or equal to the expected minimum value.
|
|
960
1044
|
* _Example_
|
|
961
1045
|
```typescript
|
|
962
|
-
const schema =
|
|
963
|
-
parseOrFail(schema,
|
|
964
|
-
parseOrFail(schema,
|
|
1046
|
+
const schema = number().custom(minExcluded(10));
|
|
1047
|
+
parseOrFail(schema, 11); // Valid
|
|
1048
|
+
parseOrFail(schema, 10); // Throws an error: 'The received value is less than or equal to expected'
|
|
1049
|
+
parseOrFail(schema, 9); // Throws an error: 'The received value is less than or equal to expected'
|
|
965
1050
|
```
|
|
966
|
-
* _See_ Error Translation Key = '
|
|
1051
|
+
* _See_ Error Translation Key = 'n:minExcluded'
|
|
967
1052
|
|
|
968
1053
|
|
|
969
|
-
#####
|
|
1054
|
+
##### negative (number) {#assert_negative_number}
|
|
970
1055
|
|
|
971
1056
|
```typescript
|
|
972
|
-
import {
|
|
1057
|
+
import { negative } from 'bguard/number/negative';
|
|
973
1058
|
```
|
|
974
1059
|
|
|
975
|
-
* _Description_ Asserts that
|
|
976
|
-
*
|
|
977
|
-
* _Throws_ {ValidationError} if the length of the received value is less than the expected length.
|
|
1060
|
+
* _Description_ Asserts that a number value is negative (less than zero).
|
|
1061
|
+
* _Throws_ {ValidationError} if the received value is not negative.
|
|
978
1062
|
* _Example_
|
|
979
1063
|
```typescript
|
|
980
|
-
const schema =
|
|
981
|
-
parseOrFail(schema,
|
|
982
|
-
parseOrFail(schema,
|
|
1064
|
+
const schema = number().custom(negative());
|
|
1065
|
+
parseOrFail(schema, -10); // Valid
|
|
1066
|
+
parseOrFail(schema, 0); // Throws an error: 'The received value is not a negative number'
|
|
1067
|
+
parseOrFail(schema, 5); // Throws an error: 'The received value is not a negative number'
|
|
983
1068
|
```
|
|
984
|
-
* _See_ Error Translation Key = '
|
|
1069
|
+
* _See_ - Error Translation Key = 'n:negative'
|
|
985
1070
|
|
|
986
1071
|
|
|
987
|
-
#####
|
|
1072
|
+
##### positive (number) {#assert_positive_number}
|
|
988
1073
|
|
|
989
1074
|
```typescript
|
|
990
|
-
import {
|
|
1075
|
+
import { positive } from 'bguard/number/positive';
|
|
991
1076
|
```
|
|
992
1077
|
|
|
993
|
-
* _Description_ Asserts that a
|
|
994
|
-
*
|
|
995
|
-
* _Throws_ {ValidationError} if the received value does not match the expected pattern.
|
|
1078
|
+
* _Description_ Asserts that a number value is positive (greater than zero).
|
|
1079
|
+
* _Throws_ {ValidationError} if the received value is not positive.
|
|
996
1080
|
* _Example_
|
|
997
1081
|
```typescript
|
|
998
|
-
const schema =
|
|
999
|
-
parseOrFail(schema,
|
|
1000
|
-
parseOrFail(schema,
|
|
1082
|
+
const schema = number().custom(positive());
|
|
1083
|
+
parseOrFail(schema, 10); // Valid
|
|
1084
|
+
parseOrFail(schema, 0); // Throws an error: 'The received value is not a positive number'
|
|
1085
|
+
parseOrFail(schema, -5); // Throws an error: 'The received value is not a positive number'
|
|
1001
1086
|
```
|
|
1002
|
-
* _See_ Error Translation Key = '
|
|
1087
|
+
* _See_ Error Translation Key = 'n:positive'
|
|
1003
1088
|
|
|
1089
|
+
#### array {#assertdir_array}
|
|
1090
|
+
|
|
1091
|
+
<b>Prerequisites</b>
|
|
1092
|
+
|
|
1093
|
+
```typescript
|
|
1094
|
+
import { array } from 'bguard/array';
|
|
1095
|
+
```
|
|
1096
|
+
|
|
1097
|
+
* _Description_ Creates a new schema for validating arrays where each element must match the specified schema.
|
|
1098
|
+
|
|
1099
|
+
* _Param_ {T} arraySchema - The schema that each element of the array must match.
|
|
1100
|
+
* _Example_
|
|
1101
|
+
```typescript
|
|
1102
|
+
const schema = array(string());
|
|
1103
|
+
parseOrFail(schema, ['hello', 'world']); // Validates successfully
|
|
1104
|
+
parseOrFail(schema, ['hello', 123]); // Throws a validation error
|
|
1105
|
+
```
|
|
1106
|
+
|
|
1004
1107
|
|
|
1005
|
-
#####
|
|
1108
|
+
##### maxArrayLength (array) {#assert_maxArrayLength_array}
|
|
1006
1109
|
|
|
1007
1110
|
```typescript
|
|
1008
|
-
import {
|
|
1111
|
+
import { maxArrayLength } from 'bguard/array/maxArrayLength';
|
|
1009
1112
|
```
|
|
1010
1113
|
|
|
1011
|
-
* _Description_ Asserts that
|
|
1012
|
-
* _Param_ {
|
|
1013
|
-
* _Throws_ {ValidationError} if the received value
|
|
1114
|
+
* _Description_ Asserts that the length of an array is not greater than a specified maximum length.
|
|
1115
|
+
* _Param_ {number} expected The maximum allowed length for the array.
|
|
1116
|
+
* _Throws_ {ValidationError} if the length of the received value is greater than the expected length.
|
|
1014
1117
|
* _Example_
|
|
1015
1118
|
```typescript
|
|
1016
|
-
const schema = string().custom(
|
|
1017
|
-
parseOrFail(schema, '
|
|
1018
|
-
parseOrFail(schema, '
|
|
1119
|
+
const schema = array(string()).custom(maxArrayLength(3));
|
|
1120
|
+
parseOrFail(schema, ['adequate', 'array']); // Valid
|
|
1121
|
+
parseOrFail(schema, ['adequate', 'array', 'length']); // Valid
|
|
1122
|
+
parseOrFail(schema, ['adequate', 'array', 'length', 'test']); // Throws an error: 'The received value length is greater than expected'
|
|
1019
1123
|
```
|
|
1020
|
-
* _See_ Error Translation Key = '
|
|
1124
|
+
* _See_ Error Translation Key = 'a:maxArrayLength'
|
|
1021
1125
|
|
|
1022
1126
|
|
|
1023
|
-
#####
|
|
1127
|
+
##### minArrayLength (array) {#assert_minArrayLength_array}
|
|
1024
1128
|
|
|
1025
1129
|
```typescript
|
|
1026
|
-
import {
|
|
1130
|
+
import { minArrayLength } from 'bguard/array/minArrayLength';
|
|
1027
1131
|
```
|
|
1028
1132
|
|
|
1029
|
-
* _Description_ Asserts that
|
|
1030
|
-
*
|
|
1133
|
+
* _Description_ Asserts that the length of na array is not less than a specified minimum length.
|
|
1134
|
+
* _Param_ {number} expected The minimum required length for the array.
|
|
1135
|
+
* _Throws_ {ValidationError} if the length of the received value is less than the expected length.
|
|
1031
1136
|
* _Example_
|
|
1032
1137
|
```typescript
|
|
1033
|
-
const schema = string().custom(
|
|
1034
|
-
parseOrFail(schema, '
|
|
1035
|
-
parseOrFail(schema, '
|
|
1036
|
-
parseOrFail(schema, '
|
|
1138
|
+
const schema = array(string()).custom(minArrayLength(3));
|
|
1139
|
+
parseOrFail(schema, ['short', 'array']); // Throws an error: 'The received value length is less than expected'
|
|
1140
|
+
parseOrFail(schema, ['adequate', 'array', 'length']); // Valid
|
|
1141
|
+
parseOrFail(schema, ['adequate', 'array', 'length', 'test']); // Valid
|
|
1037
1142
|
```
|
|
1038
|
-
* _See_ Error Translation Key = '
|
|
1143
|
+
* _See_ Error Translation Key = 'a:minArrayLength'
|
|
1039
1144
|
|
|
1145
|
+
#### bigint {#assertdir_bigint}
|
|
1146
|
+
|
|
1147
|
+
<b>Prerequisites</b>
|
|
1148
|
+
|
|
1149
|
+
```typescript
|
|
1150
|
+
import { bigint } from 'bguard/bigint';
|
|
1151
|
+
```
|
|
1152
|
+
|
|
1153
|
+
* _Description_ Creates a new schema for validating bigint values.
|
|
1154
|
+
* _Example_
|
|
1155
|
+
```typescript
|
|
1156
|
+
const schema = bigint();
|
|
1157
|
+
parseOrFail(schema, 42n); // Validates successfully
|
|
1158
|
+
parseOrFail(schema, 42); // Throws a validation error
|
|
1159
|
+
parseOrFail(schema, '42'); // Throws a validation error
|
|
1160
|
+
```
|
|
1161
|
+
|
|
1040
1162
|
|
|
1041
|
-
#####
|
|
1163
|
+
##### bigintMax (bigint) {#assert_bigintMax_bigint}
|
|
1042
1164
|
|
|
1043
1165
|
```typescript
|
|
1044
|
-
import {
|
|
1166
|
+
import { bigintMax } from 'bguard/bigint/bigintMax';
|
|
1045
1167
|
```
|
|
1046
1168
|
|
|
1047
|
-
* _Description_ Asserts that a
|
|
1048
|
-
*
|
|
1169
|
+
* _Description_ Asserts that a bigint value does not exceed a specified maximum value.
|
|
1170
|
+
* _Param_ {bigint} expected The maximum allowable value.
|
|
1171
|
+
* _Throws_ {ValidationError} if the received value exceeds the expected maximum value.
|
|
1049
1172
|
* _Example_
|
|
1050
1173
|
```typescript
|
|
1051
|
-
const schema =
|
|
1052
|
-
parseOrFail(schema,
|
|
1053
|
-
parseOrFail(schema,
|
|
1174
|
+
const schema = bigint().custom(bigintMax(100n));
|
|
1175
|
+
parseOrFail(schema, 99n); // Valid
|
|
1176
|
+
parseOrFail(schema, 100n); // Valid
|
|
1177
|
+
parseOrFail(schema, 101n); // Throws an error: 'The received value is greater than expected'
|
|
1054
1178
|
```
|
|
1055
|
-
* _See_ Error Translation Key = '
|
|
1179
|
+
* _See_ Error Translation Key = 'bi:max'
|
|
1056
1180
|
|
|
1057
1181
|
|
|
1058
|
-
#####
|
|
1182
|
+
##### bigintMaxExcluded (bigint) {#assert_bigintMaxExcluded_bigint}
|
|
1059
1183
|
|
|
1060
1184
|
```typescript
|
|
1061
|
-
import {
|
|
1185
|
+
import { bigintMaxExcluded } from 'bguard/bigint/bigintMaxExcluded';
|
|
1062
1186
|
```
|
|
1063
1187
|
|
|
1064
|
-
* _Description_ Asserts that a
|
|
1065
|
-
*
|
|
1188
|
+
* _Description_ - Asserts that a bigint value is strictly less than a specified maximum value (i.e., the maximum value is excluded).
|
|
1189
|
+
* _Param_ {bigint} expected - The maximum allowable value, which is excluded.
|
|
1190
|
+
* _Throws_ {ValidationError} if the received value is greater than or equal to the expected maximum value.
|
|
1066
1191
|
* _Example_
|
|
1067
1192
|
```typescript
|
|
1068
|
-
const schema =
|
|
1069
|
-
parseOrFail(schema,
|
|
1070
|
-
parseOrFail(schema,
|
|
1071
|
-
parseOrFail(schema,
|
|
1193
|
+
const schema = bigint().custom(bigintMaxExcluded(100n));
|
|
1194
|
+
parseOrFail(schema, 99n); // Valid
|
|
1195
|
+
parseOrFail(schema, 100n); // Throws an error: 'The received value is greater than or equal to expected'
|
|
1196
|
+
parseOrFail(schema, 101n); // Throws an error: 'The received value is greater than or equal to expected'
|
|
1072
1197
|
```
|
|
1073
|
-
* _See_ Error Translation Key = '
|
|
1198
|
+
* _See_ Error Translation Key = 'bi:maxExcluded'
|
|
1074
1199
|
|
|
1075
1200
|
|
|
1076
|
-
#####
|
|
1201
|
+
##### bigintMin (bigint) {#assert_bigintMin_bigint}
|
|
1077
1202
|
|
|
1078
1203
|
```typescript
|
|
1079
|
-
import {
|
|
1204
|
+
import { bigintMin } from 'bguard/bigint/bigintMin';
|
|
1080
1205
|
```
|
|
1081
1206
|
|
|
1082
|
-
* _Description_ Asserts that a
|
|
1083
|
-
*
|
|
1207
|
+
* _Description_ Asserts that a bigint value is not less than a specified minimum value.
|
|
1208
|
+
* _Param_ {bigint} expected The minimum allowable value.
|
|
1209
|
+
* _Throws_ {ValidationError} if the received value is less than the expected minimum value.
|
|
1084
1210
|
* _Example_
|
|
1085
1211
|
```typescript
|
|
1086
|
-
const schema =
|
|
1087
|
-
parseOrFail(schema,
|
|
1088
|
-
parseOrFail(schema,
|
|
1089
|
-
parseOrFail(schema,
|
|
1212
|
+
const schema = bigint().custom(bigintMin(10n));
|
|
1213
|
+
parseOrFail(schema, 11n); // Valid
|
|
1214
|
+
parseOrFail(schema, 10n); // Valid
|
|
1215
|
+
parseOrFail(schema, 9n); // Throws an error: 'The received value is less than expected'
|
|
1090
1216
|
```
|
|
1091
|
-
* _See_ Error Translation Key = '
|
|
1217
|
+
* _See_ Error Translation Key = 'bi:min'
|
|
1092
1218
|
|
|
1093
1219
|
|
|
1094
|
-
#####
|
|
1220
|
+
##### bigintMinExcluded (bigint) {#assert_bigintMinExcluded_bigint}
|
|
1095
1221
|
|
|
1096
1222
|
```typescript
|
|
1097
|
-
import {
|
|
1223
|
+
import { bigintMinExcluded } from 'bguard/bigint/bigintMinExcluded';
|
|
1098
1224
|
```
|
|
1099
1225
|
|
|
1100
|
-
* _Description_ Asserts that a
|
|
1101
|
-
*
|
|
1226
|
+
* _Description_ Asserts that a bigint value is strictly greater than a specified minimum value (i.e., the minimum value is excluded).
|
|
1227
|
+
* _Param_ {bigint} expected The minimum allowable value, which is excluded.
|
|
1228
|
+
* _Throws_ {ValidationError} if the received value is less than or equal to the expected minimum value.
|
|
1102
1229
|
* _Example_
|
|
1103
1230
|
```typescript
|
|
1104
|
-
const schema =
|
|
1105
|
-
parseOrFail(schema,
|
|
1106
|
-
parseOrFail(schema,
|
|
1107
|
-
parseOrFail(schema,
|
|
1231
|
+
const schema = bigint().custom(bigintMinExcluded(10n));
|
|
1232
|
+
parseOrFail(schema, 11n); // Valid
|
|
1233
|
+
parseOrFail(schema, 10n); // Throws an error: 'The received value is less than or equal to expected'
|
|
1234
|
+
parseOrFail(schema, 9n); // Throws an error: 'The received value is less than or equal to expected'
|
|
1108
1235
|
```
|
|
1109
|
-
* _See_ Error Translation Key = '
|
|
1236
|
+
* _See_ Error Translation Key = 'bi:minExcluded'
|
|
1110
1237
|
|
|
1238
|
+
#### date {#assertdir_date}
|
|
1239
|
+
|
|
1240
|
+
<b>Prerequisites</b>
|
|
1241
|
+
|
|
1242
|
+
```typescript
|
|
1243
|
+
import { date } from 'bguard/date';
|
|
1244
|
+
```
|
|
1245
|
+
|
|
1246
|
+
* _Description_ Creates a new schema for validating date values.
|
|
1247
|
+
* _Example_
|
|
1248
|
+
```typescript
|
|
1249
|
+
const schema = date();
|
|
1250
|
+
parseOrFail(schema, true); // Validates successfully
|
|
1251
|
+
parseOrFail(schema, 'true'); // Throws a validation error
|
|
1252
|
+
```
|
|
1253
|
+
|
|
1111
1254
|
|
|
1112
|
-
#####
|
|
1255
|
+
##### dateMax (date) {#assert_dateMax_date}
|
|
1113
1256
|
|
|
1114
1257
|
```typescript
|
|
1115
|
-
import {
|
|
1258
|
+
import { dateMax } from 'bguard/date/dateMax';
|
|
1116
1259
|
```
|
|
1117
1260
|
|
|
1118
|
-
* _Description_ Asserts that a
|
|
1119
|
-
*
|
|
1261
|
+
* _Description_ Asserts that a date value is not greater than a specified maximum value.
|
|
1262
|
+
* _Param_ {Date | string} expected The maximum allowable value.
|
|
1263
|
+
* _Throws_ {ValidationError} if the received value is greater than the expected maximum value.
|
|
1120
1264
|
* _Example_
|
|
1121
1265
|
```typescript
|
|
1122
|
-
const schema =
|
|
1123
|
-
parseOrFail(schema, '
|
|
1124
|
-
parseOrFail(schema, '
|
|
1125
|
-
parseOrFail(schema, '
|
|
1126
|
-
parseOrFail(schema, '123e4567-e89b-42d3-c456-426614174000'); // Throws an error: 'The received value is not a valid UUID v4'
|
|
1127
|
-
parseOrFail(schema, 'invalid-uuid'); // Throws an error: 'The received value is not a valid UUID v4'
|
|
1266
|
+
const schema = date().custom(dateMax('2024-12-31'));
|
|
1267
|
+
parseOrFail(schema, new Date('2024-12-30')); // Valid
|
|
1268
|
+
parseOrFail(schema, new Date('2024-12-31')); // Valid
|
|
1269
|
+
parseOrFail(schema, new Date('2025-01-01')); // Throws an error: 'The received value is greater than expected'
|
|
1128
1270
|
```
|
|
1129
|
-
* _See_ Error Translation Key = '
|
|
1271
|
+
* _See_ Error Translation Key = 'dt:max'
|
|
1130
1272
|
|
|
1131
1273
|
|
|
1132
|
-
#####
|
|
1274
|
+
##### dateMin (date) {#assert_dateMin_date}
|
|
1133
1275
|
|
|
1134
1276
|
```typescript
|
|
1135
|
-
import {
|
|
1277
|
+
import { dateMin } from 'bguard/date/dateMin';
|
|
1136
1278
|
```
|
|
1137
1279
|
|
|
1138
|
-
* _Description_ Asserts that a
|
|
1139
|
-
*
|
|
1280
|
+
* _Description_ Asserts that a number value is not less than a specified minimum value.
|
|
1281
|
+
* _Param_ {Date | string} expected The minimum allowable value.
|
|
1282
|
+
* _Throws_ {ValidationError} if the received value is less than the expected minimum value.
|
|
1140
1283
|
* _Example_
|
|
1141
1284
|
```typescript
|
|
1142
|
-
const schema =
|
|
1143
|
-
parseOrFail(schema, '
|
|
1144
|
-
parseOrFail(schema, '
|
|
1145
|
-
parseOrFail(schema, '
|
|
1285
|
+
const schema = date().custom(dateMin('2023-01-01'));
|
|
1286
|
+
parseOrFail(schema, new Date('2023-01-02')); // Valid
|
|
1287
|
+
parseOrFail(schema, new Date('2023-01-01')); // Valid
|
|
1288
|
+
parseOrFail(schema, new Date('2022-12-31')); // Throws an error: 'The received value is less than expected'
|
|
1146
1289
|
```
|
|
1147
|
-
* _See_ Error Translation Key = '
|
|
1290
|
+
* _See_ Error Translation Key = 'dt:min'
|
|
1148
1291
|
|
|
1292
|
+
#### mix {#assertdir_mix}
|
|
1293
|
+
|
|
1294
|
+
<b>Prerequisites</b>
|
|
1295
|
+
|
|
1296
|
+
```typescript
|
|
1297
|
+
import { oneOfTypes } from 'bguard/mix';
|
|
1298
|
+
```
|
|
1299
|
+
|
|
1300
|
+
* _Description_ Creates a new schema for validating values that can match any one of the specified primitive types.
|
|
1301
|
+
|
|
1302
|
+
|
|
1303
|
+
* _Param_ {T} valueTypes - An array of primitive types that the value can match.
|
|
1304
|
+
* _Example_
|
|
1305
|
+
```typescript
|
|
1306
|
+
const schema = oneOfTypes(['string', 'number']);
|
|
1307
|
+
parseOrFail(schema, 'hello'); // Validates successfully
|
|
1308
|
+
parseOrFail(schema, 42); // Validates successfully
|
|
1309
|
+
parseOrFail(schema, true); // Throws a validation error
|
|
1310
|
+
```
|
|
1311
|
+
|
|
1149
1312
|
|
|
1150
|
-
#####
|
|
1313
|
+
##### equalTo (mix) {#assert_equalTo_mix}
|
|
1151
1314
|
|
|
1152
1315
|
```typescript
|
|
1153
|
-
import {
|
|
1316
|
+
import { equalTo } from 'bguard/mix/equalTo';
|
|
1154
1317
|
```
|
|
1155
1318
|
|
|
1156
|
-
* _Description_
|
|
1157
|
-
*
|
|
1158
|
-
*
|
|
1319
|
+
* _Description_ Creates a custom assertion that checks if a value is equal to the expected value.
|
|
1320
|
+
* > **Notice:** It has already been implemented in the number, bigint and string schema. There is no need to use it as a custom assert.
|
|
1321
|
+
* _Param_ {unknown} expected The value that the received value is expected to match.
|
|
1322
|
+
* _Throws_ {ValidationError} If the received value does not match the expected value.
|
|
1159
1323
|
* _Example_
|
|
1160
1324
|
```typescript
|
|
1161
|
-
const schema =
|
|
1162
|
-
parseOrFail(schema,
|
|
1163
|
-
parseOrFail(schema,
|
|
1164
|
-
parseOrFail(schema, 'ftp://example.com'); // Throws an error
|
|
1165
|
-
parseOrFail(schema, 'http:example.com'); // Throws an error
|
|
1325
|
+
const schema = number().custom(equalTo(5)); // Define a schema with a custom assertion
|
|
1326
|
+
parseOrFail(schema, 5); // Valid
|
|
1327
|
+
parseOrFail(schema, 3); // Throws an error: 'The received value is not equal to expected'
|
|
1166
1328
|
```
|
|
1167
|
-
* _See_ Error Translation Key = '
|
|
1329
|
+
* _See_ Error Translation Key = 'm:equalTo'
|
|
1330
|
+
|
|
1331
|
+
|
|
1332
|
+
##### oneOfValues (mix) {#assert_oneOfValues_mix}
|
|
1333
|
+
|
|
1334
|
+
```typescript
|
|
1335
|
+
import { oneOfValues } from 'bguard/mix/oneOfValues';
|
|
1336
|
+
```
|
|
1337
|
+
|
|
1338
|
+
* _Description_ Creates a custom assertion that checks if a value is equal to the one of expected values.
|
|
1339
|
+
* > **Notice:** It has already been implemented in the number, bigint and string schema. There is no need to use it as a custom assert.
|
|
1340
|
+
* _Param_ {unknown} expected The value that the received value is expected to match.
|
|
1341
|
+
* _Throws_ {ValidationError} If the received value does not match at least one of the expected values.
|
|
1342
|
+
* _Example_
|
|
1343
|
+
```typescript
|
|
1344
|
+
const schema = number().custom(oneOfValues([5, 4])); // Define a schema with a custom assertion
|
|
1345
|
+
parseOrFail(schema, 5); // Valid
|
|
1346
|
+
parseOrFail(schema, 4); // Valid
|
|
1347
|
+
parseOrFail(schema, 3); // Throws an error: 'The received value is not equal to expected'
|
|
1348
|
+
```
|
|
1349
|
+
* _See_ Error Translation Key = 'm:oneOfValues'
|
|
1168
1350
|
|
|
1169
1351
|
### Contributing
|
|
1170
1352
|
Contributions are welcome! Please open an issue or submit a pull request for any bugs or feature requests.
|