bguard 0.3.0 → 0.4.1
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 +269 -121
- 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 +7 -7
- package/lib/asserts/array/index.js.map +1 -1
- package/lib/asserts/array/index.mjs +6 -6
- package/lib/asserts/array/index.mjs.map +1 -1
- package/lib/asserts/array/maxArrayLength.js +4 -4
- package/lib/asserts/array/maxArrayLength.mjs +2 -2
- package/lib/asserts/array/minArrayLength.js +4 -4
- package/lib/asserts/array/minArrayLength.mjs +2 -2
- 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 +10 -10
- package/lib/asserts/bigint/index.js.map +1 -1
- package/lib/asserts/bigint/index.mjs +9 -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 +8 -8
- package/lib/asserts/boolean/index.js.map +1 -1
- package/lib/asserts/boolean/index.mjs +7 -7
- package/lib/asserts/boolean/index.mjs.map +1 -1
- package/lib/asserts/date/dateMax.js +5 -5
- package/lib/asserts/date/dateMax.mjs +2 -2
- package/lib/asserts/date/dateMin.js +5 -5
- package/lib/asserts/date/dateMin.mjs +2 -2
- package/lib/asserts/date/index.d.mts +5 -4
- package/lib/asserts/date/index.d.ts +5 -4
- package/lib/asserts/date/index.js +7 -7
- package/lib/asserts/date/index.js.map +1 -1
- package/lib/asserts/date/index.mjs +6 -6
- 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 +6 -6
- package/lib/asserts/mix/index.js.map +1 -1
- package/lib/asserts/mix/index.mjs +5 -5
- 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 +10 -10
- package/lib/asserts/number/index.js.map +1 -1
- package/lib/asserts/number/index.mjs +9 -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 +7 -7
- package/lib/asserts/object/index.js.map +1 -1
- package/lib/asserts/object/index.mjs +6 -6
- 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 +10 -10
- package/lib/asserts/string/index.js.map +1 -1
- package/lib/asserts/string/index.mjs +9 -9
- package/lib/asserts/string/index.mjs.map +1 -1
- package/lib/asserts/string/isValidDate.js +4 -4
- package/lib/asserts/string/isValidDate.mjs +2 -2
- package/lib/asserts/string/isValidDateTime.js +4 -4
- package/lib/asserts/string/isValidDateTime.mjs +2 -2
- package/lib/asserts/string/isValidTime.js +5 -5
- package/lib/asserts/string/isValidTime.mjs +2 -2
- 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-OJSTQQ3N.js → chunk-2KQASECD.js} +5 -5
- package/lib/{chunk-OJSTQQ3N.js.map → chunk-2KQASECD.js.map} +1 -1
- package/lib/{chunk-AY7HKAG2.js → chunk-5P6AZJCN.js} +5 -3
- package/lib/chunk-5P6AZJCN.js.map +1 -0
- package/lib/{chunk-AN3VHQEQ.js → chunk-5WHCMWXK.js} +12 -9
- package/lib/chunk-5WHCMWXK.js.map +1 -0
- package/lib/{chunk-UZ2PWO4O.js → chunk-5YYRBKDP.js} +4 -4
- package/lib/{chunk-UZ2PWO4O.js.map → chunk-5YYRBKDP.js.map} +1 -1
- package/lib/{chunk-YPAFWVDL.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-5LLJ22IG.mjs → chunk-BNCT7NOR.mjs} +21 -7
- package/lib/chunk-BNCT7NOR.mjs.map +1 -0
- package/lib/{chunk-BUXOCPHI.mjs → chunk-C3EDEQ3W.mjs} +3 -3
- package/lib/{chunk-TQWUSDQD.mjs → chunk-CHUPE6SJ.mjs} +6 -6
- package/lib/chunk-CHUPE6SJ.mjs.map +1 -0
- package/lib/{chunk-ZVW3IHBW.js → chunk-ECBOZ3BM.js} +8 -8
- package/lib/chunk-ECBOZ3BM.js.map +1 -0
- package/lib/{chunk-NP64RC7B.mjs → chunk-FGZKNBDP.mjs} +5 -3
- package/lib/chunk-FGZKNBDP.mjs.map +1 -0
- package/lib/{chunk-ZWE5Q37U.mjs → chunk-FP5QAEVQ.mjs} +6 -6
- package/lib/chunk-FP5QAEVQ.mjs.map +1 -0
- package/lib/{chunk-PESVL5YJ.js → chunk-G55SHHAR.js} +10 -8
- package/lib/chunk-G55SHHAR.js.map +1 -0
- package/lib/{chunk-4GDBM5LP.mjs → chunk-H32B3NHW.mjs} +2 -2
- package/lib/{chunk-D42ISCFK.mjs → chunk-H6LHV3EA.mjs} +2 -2
- package/lib/{chunk-SPWF5IFU.js → chunk-HP3DRZWK.js} +5 -5
- package/lib/{chunk-SPWF5IFU.js.map → chunk-HP3DRZWK.js.map} +1 -1
- package/lib/{chunk-W2GOLG3K.js → chunk-IZ3CBVEE.js} +5 -5
- package/lib/{chunk-W2GOLG3K.js.map → chunk-IZ3CBVEE.js.map} +1 -1
- package/lib/{chunk-OHEMRF7Y.mjs → chunk-K7X6DVM2.mjs} +3 -3
- package/lib/{chunk-KBZUNXJB.mjs → chunk-LPR3G5T3.mjs} +4 -4
- package/lib/{chunk-KBZUNXJB.mjs.map → chunk-LPR3G5T3.mjs.map} +1 -1
- package/lib/{chunk-2MFBOVP2.mjs → chunk-MHBY5OIQ.mjs} +3 -3
- package/lib/{chunk-EU3ZZF7Y.js → chunk-MLNQZ445.js} +13 -13
- package/lib/chunk-MLNQZ445.js.map +1 -0
- package/lib/chunk-QIDWASOC.js +14 -0
- package/lib/{chunk-3ZPLJ5KC.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-ADSMWGDJ.mjs → chunk-TRGMD7LA.mjs} +7 -5
- package/lib/chunk-TRGMD7LA.mjs.map +1 -0
- package/lib/{chunk-OKF3BVUD.mjs → chunk-TV5IZY4L.mjs} +9 -6
- package/lib/chunk-TV5IZY4L.mjs.map +1 -0
- package/lib/{chunk-OO6BTBVP.js → chunk-U4USIJV3.js} +11 -11
- package/lib/{chunk-OO6BTBVP.js.map → chunk-U4USIJV3.js.map} +1 -1
- package/lib/{chunk-P6V6S6TZ.js → chunk-UUY7DPHK.js} +12 -12
- package/lib/chunk-UUY7DPHK.js.map +1 -0
- package/lib/chunk-VW4RMQD4.js +28 -0
- package/lib/{chunk-LYNVPN3R.js.map → chunk-VW4RMQD4.js.map} +1 -1
- package/lib/{chunk-ZMEQCIRN.mjs → chunk-WK4FJ5GD.mjs} +6 -6
- package/lib/chunk-WK4FJ5GD.mjs.map +1 -0
- package/lib/{chunk-PIBDJJUU.js → chunk-X762EUSZ.js} +31 -17
- package/lib/chunk-X762EUSZ.js.map +1 -0
- package/lib/{chunk-5HT3PYSG.mjs → chunk-YUMUCQKL.mjs} +4 -4
- package/lib/{chunk-5HT3PYSG.mjs.map → chunk-YUMUCQKL.mjs.map} +1 -1
- package/lib/{chunk-2D5QE7F4.js → chunk-Z6QU36MM.js} +12 -12
- 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 +3 -3
- package/lib/codeGen.mjs +2 -2
- 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 +3 -3
- package/lib/helpers/core.mjs +2 -2
- package/lib/helpers/setStrictType.d.mts +2 -1
- package/lib/helpers/setStrictType.d.ts +2 -1
- package/lib/helpers/setStrictType.js +4 -4
- package/lib/helpers/setStrictType.mjs +3 -3
- package/lib/index.d.mts +1 -8
- package/lib/index.d.ts +1 -8
- package/lib/index.js +6 -6
- package/lib/index.mjs +5 -5
- package/lib/parse.d.mts +1 -8
- package/lib/parse.d.ts +1 -8
- package/lib/parse.js +5 -5
- package/lib/parse.mjs +4 -4
- package/lib/parseOrFail.d.mts +1 -8
- package/lib/parseOrFail.d.ts +1 -8
- package/lib/parseOrFail.js +5 -5
- package/lib/parseOrFail.mjs +4 -4
- package/lib/schemas/ArraySchema.d.mts +3 -6
- package/lib/schemas/ArraySchema.d.ts +3 -6
- package/lib/schemas/ArraySchema.js +7 -7
- package/lib/schemas/ArraySchema.mjs +6 -6
- package/lib/schemas/BigIntSchema.d.mts +6 -9
- package/lib/schemas/BigIntSchema.d.ts +6 -9
- package/lib/schemas/BigIntSchema.js +10 -10
- package/lib/schemas/BigIntSchema.mjs +9 -9
- package/lib/schemas/BooleanSchema.d.mts +6 -9
- package/lib/schemas/BooleanSchema.d.ts +6 -9
- package/lib/schemas/BooleanSchema.js +8 -8
- package/lib/schemas/BooleanSchema.mjs +7 -7
- package/lib/schemas/CommonSchema.d.mts +2 -1
- package/lib/schemas/CommonSchema.d.ts +2 -1
- package/lib/schemas/CommonSchema.js +6 -6
- package/lib/schemas/CommonSchema.mjs +5 -5
- package/lib/schemas/DateSchema.d.mts +2 -1
- package/lib/schemas/DateSchema.d.ts +2 -1
- package/lib/schemas/DateSchema.js +7 -7
- package/lib/schemas/DateSchema.mjs +6 -6
- package/lib/schemas/NumberSchema.d.mts +6 -9
- package/lib/schemas/NumberSchema.d.ts +6 -9
- package/lib/schemas/NumberSchema.js +10 -10
- package/lib/schemas/NumberSchema.mjs +9 -9
- package/lib/schemas/ObjectSchema.d.mts +3 -5
- package/lib/schemas/ObjectSchema.d.ts +3 -5
- package/lib/schemas/ObjectSchema.js +7 -7
- package/lib/schemas/ObjectSchema.mjs +6 -6
- package/lib/schemas/StringSchema.d.mts +6 -9
- package/lib/schemas/StringSchema.d.ts +6 -9
- package/lib/schemas/StringSchema.js +10 -10
- package/lib/schemas/StringSchema.mjs +9 -9
- package/lib/translationMap.js +3 -3
- package/lib/translationMap.mjs +2 -2
- package/package.json +1 -1
- package/lib/CommonSchema-CiY7spmg.d.mts +0 -71
- package/lib/CommonSchema-D9QVJBJe.d.ts +0 -71
- package/lib/chunk-2D5QE7F4.js.map +0 -1
- package/lib/chunk-3IOQH3K4.js +0 -74
- package/lib/chunk-3IOQH3K4.js.map +0 -1
- package/lib/chunk-3ZPLJ5KC.js +0 -14
- package/lib/chunk-5LLJ22IG.mjs.map +0 -1
- package/lib/chunk-ADSMWGDJ.mjs.map +0 -1
- package/lib/chunk-AN3VHQEQ.js.map +0 -1
- package/lib/chunk-AOUFWACR.mjs +0 -74
- package/lib/chunk-AOUFWACR.mjs.map +0 -1
- package/lib/chunk-AY7HKAG2.js.map +0 -1
- package/lib/chunk-EU3ZZF7Y.js.map +0 -1
- package/lib/chunk-LYNVPN3R.js +0 -28
- package/lib/chunk-NP64RC7B.mjs.map +0 -1
- package/lib/chunk-OKF3BVUD.mjs.map +0 -1
- package/lib/chunk-P6V6S6TZ.js.map +0 -1
- package/lib/chunk-PESVL5YJ.js.map +0 -1
- package/lib/chunk-PIBDJJUU.js.map +0 -1
- package/lib/chunk-TQWUSDQD.mjs.map +0 -1
- package/lib/chunk-YPAFWVDL.mjs.map +0 -1
- package/lib/chunk-ZMEQCIRN.mjs.map +0 -1
- package/lib/chunk-ZVW3IHBW.js.map +0 -1
- package/lib/chunk-ZWE5Q37U.mjs.map +0 -1
- /package/lib/{chunk-BUXOCPHI.mjs.map → chunk-C3EDEQ3W.mjs.map} +0 -0
- /package/lib/{chunk-4GDBM5LP.mjs.map → chunk-H32B3NHW.mjs.map} +0 -0
- /package/lib/{chunk-D42ISCFK.mjs.map → chunk-H6LHV3EA.mjs.map} +0 -0
- /package/lib/{chunk-OHEMRF7Y.mjs.map → chunk-K7X6DVM2.mjs.map} +0 -0
- /package/lib/{chunk-2MFBOVP2.mjs.map → chunk-MHBY5OIQ.mjs.map} +0 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import {
|
|
2
|
+
parseOrFail
|
|
3
|
+
} from "./chunk-TRGMD7LA.mjs";
|
|
4
|
+
import {
|
|
5
|
+
ctxSymbol
|
|
6
|
+
} from "./chunk-BNCT7NOR.mjs";
|
|
7
|
+
import {
|
|
8
|
+
BuildSchemaError
|
|
9
|
+
} from "./chunk-FGZKNBDP.mjs";
|
|
10
|
+
|
|
11
|
+
// src/schemas/CommonSchema.ts
|
|
12
|
+
var CommonSchema = class {
|
|
13
|
+
[ctxSymbol];
|
|
14
|
+
constructor(ctx) {
|
|
15
|
+
this[ctxSymbol] = ctx;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* @param validators - One or more custom validation functions.
|
|
19
|
+
* @returns {this} The schema instance with the added custom validation.
|
|
20
|
+
*/
|
|
21
|
+
custom(...validators) {
|
|
22
|
+
this.defaultValueCheck();
|
|
23
|
+
this[ctxSymbol].requiredValidations.push(...validators);
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Marks the schema as nullable, allowing the value to be `null`.
|
|
28
|
+
*
|
|
29
|
+
* @returns {WithNull<this>} The schema instance marked as nullable.
|
|
30
|
+
*/
|
|
31
|
+
nullable() {
|
|
32
|
+
this.defaultValueCheck();
|
|
33
|
+
this[ctxSymbol].isNullable = true;
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Marks the schema as optional, allowing the value to be `undefined`.
|
|
38
|
+
*
|
|
39
|
+
* @returns {WithUndefined<this>} The schema instance marked as optional.
|
|
40
|
+
*/
|
|
41
|
+
optional() {
|
|
42
|
+
this.defaultValueCheck();
|
|
43
|
+
this[ctxSymbol].isOptional = true;
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Marks the schema as optional, allowing the value to be `undefined`.
|
|
48
|
+
*
|
|
49
|
+
* @returns {this} The schema instance. This method should be used as a last one because it does the check of previous methods and
|
|
50
|
+
*/
|
|
51
|
+
default(defaultValue) {
|
|
52
|
+
const ctx = this[ctxSymbol];
|
|
53
|
+
if (ctx.isOptional) {
|
|
54
|
+
throw new BuildSchemaError(`Cannot call method 'default' after method 'optional'`);
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
parseOrFail(this, defaultValue);
|
|
58
|
+
} catch (e) {
|
|
59
|
+
throw new BuildSchemaError(e.message);
|
|
60
|
+
}
|
|
61
|
+
this[ctxSymbol].defaultValue = defaultValue;
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Applies a transformation to the input value before any validation occurs.
|
|
66
|
+
* The transformation should return a value of the same type as the inferred type of the schema,
|
|
67
|
+
* ensuring that the overall type is not altered.
|
|
68
|
+
*
|
|
69
|
+
* @template In - The type of the input value before transformation (defaults to `unknown`).
|
|
70
|
+
* @param {TransformCallback<In, InferType<this>>} cb - The callback function that performs the transformation.
|
|
71
|
+
* @returns {this} The updated schema with the applied transformation.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* const schema = string()
|
|
75
|
+
* .nullable()
|
|
76
|
+
* .transformBeforeValidation((val) => val + '') // Ensure the value is a string
|
|
77
|
+
* .transformBeforeValidation((val: string) => (val === '' ? null : val)); // Convert empty strings to null
|
|
78
|
+
*
|
|
79
|
+
* // Parse 'test' will pass as 'test' is a valid string longer than 3 characters.
|
|
80
|
+
* parseOrFail(schema, 'test');
|
|
81
|
+
*
|
|
82
|
+
* // Parsing '' will be transformed to null and will pass due to .nullable().
|
|
83
|
+
* parseOrFail(schema, '');
|
|
84
|
+
*/
|
|
85
|
+
transformBeforeValidation(cb) {
|
|
86
|
+
const ctx = this[ctxSymbol];
|
|
87
|
+
if (ctx.transformListBefore) {
|
|
88
|
+
ctx.transformListBefore.push(cb);
|
|
89
|
+
} else {
|
|
90
|
+
ctx.transformListBefore = [cb];
|
|
91
|
+
}
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Assigns a unique identifier to the schema.
|
|
96
|
+
* This ID can be used to track or map validation errors back to specific fields
|
|
97
|
+
* in a form or other structures.
|
|
98
|
+
*
|
|
99
|
+
* @param {string} value - The unique identifier for the schema.
|
|
100
|
+
* @returns {this} The updated schema with the assigned ID.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* const schema = string().id('username');
|
|
104
|
+
*/
|
|
105
|
+
id(value) {
|
|
106
|
+
return this.meta("id", value);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Provides a description for the schema, offering additional context or information.
|
|
110
|
+
* The description can be used when displaying validation errors or for documentation purposes.
|
|
111
|
+
*
|
|
112
|
+
* @param {string} value - The description for the schema.
|
|
113
|
+
* @returns {this} The updated schema with the added description.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* const schema = string().description('The username of the account holder.');
|
|
117
|
+
*/
|
|
118
|
+
description(value) {
|
|
119
|
+
return this.meta("description", value);
|
|
120
|
+
}
|
|
121
|
+
meta(key, value) {
|
|
122
|
+
const ctx = this[ctxSymbol];
|
|
123
|
+
ctx.meta = { ...ctx.meta, [key]: value };
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
defaultValueCheck() {
|
|
127
|
+
if (this[ctxSymbol].defaultValue !== void 0) {
|
|
128
|
+
throw new BuildSchemaError("Default value must be the last method called in schema");
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
export {
|
|
134
|
+
CommonSchema
|
|
135
|
+
};
|
|
136
|
+
//# sourceMappingURL=chunk-7NBOVYSP.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/schemas/CommonSchema.ts"],"sourcesContent":["import { InferType } from '../InferType';\nimport { MetaContext, BaseType, RequiredValidation, WithNull, WithUndefined, TransformCallback } from '../commonTypes';\nimport { BuildSchemaError } from '../exceptions';\nimport { ctxSymbol } from '../helpers/core';\nimport { parseOrFail } from '../parseOrFail';\n\nexport type ObjectShapeSchemaType = Record<string, CommonSchema>;\n\nexport interface ValidatorContext {\n type: BaseType[];\n isNullable?: boolean;\n isOptional?: boolean;\n requiredValidations: RequiredValidation[];\n array?: CommonSchema;\n object?: ObjectShapeSchemaType;\n allowUnrecognizedObjectProps?: boolean;\n strictType?: boolean;\n strictTypeValue?: unknown;\n date?: boolean;\n defaultValue?: unknown;\n meta?: MetaContext;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n transformListBefore?: TransformCallback<any, any>[];\n}\n\nexport class CommonSchema {\n [ctxSymbol]: ValidatorContext;\n constructor(ctx: ValidatorContext) {\n this[ctxSymbol] = ctx;\n }\n\n /**\n * @param validators - One or more custom validation functions.\n * @returns {this} The schema instance with the added custom validation.\n */\n public custom(...validators: RequiredValidation[]): this {\n this.defaultValueCheck();\n this[ctxSymbol].requiredValidations.push(...validators);\n return this;\n }\n\n /**\n * Marks the schema as nullable, allowing the value to be `null`.\n *\n * @returns {WithNull<this>} The schema instance marked as nullable.\n */\n public nullable(): WithNull<this> {\n this.defaultValueCheck();\n this[ctxSymbol].isNullable = true;\n return this as WithNull<this>;\n }\n\n /**\n * Marks the schema as optional, allowing the value to be `undefined`.\n *\n * @returns {WithUndefined<this>} The schema instance marked as optional.\n */\n public optional(): WithUndefined<this> {\n this.defaultValueCheck();\n this[ctxSymbol].isOptional = true;\n return this as WithUndefined<this>;\n }\n\n /**\n * Marks the schema as optional, allowing the value to be `undefined`.\n *\n * @returns {this} The schema instance. This method should be used as a last one because it does the check of previous methods and\n */\n public default(defaultValue: InferType<this>): this {\n const ctx = this[ctxSymbol];\n if (ctx.isOptional) {\n throw new BuildSchemaError(`Cannot call method 'default' after method 'optional'`);\n }\n\n try {\n parseOrFail(this, defaultValue);\n } catch (e) {\n throw new BuildSchemaError((e as Error).message);\n }\n\n this[ctxSymbol].defaultValue = defaultValue;\n return this;\n }\n\n /**\n * Applies a transformation to the input value before any validation occurs.\n * The transformation should return a value of the same type as the inferred type of the schema,\n * ensuring that the overall type is not altered.\n *\n * @template In - The type of the input value before transformation (defaults to `unknown`).\n * @param {TransformCallback<In, InferType<this>>} cb - The callback function that performs the transformation.\n * @returns {this} The updated schema with the applied transformation.\n *\n * @example\n * const schema = string()\n * .nullable()\n * .transformBeforeValidation((val) => val + '') // Ensure the value is a string\n * .transformBeforeValidation((val: string) => (val === '' ? null : val)); // Convert empty strings to null\n *\n * // Parse 'test' will pass as 'test' is a valid string longer than 3 characters.\n * parseOrFail(schema, 'test');\n *\n * // Parsing '' will be transformed to null and will pass due to .nullable().\n * parseOrFail(schema, '');\n */\n public transformBeforeValidation<In>(cb: TransformCallback<In, InferType<this>>): this {\n const ctx = this[ctxSymbol];\n if (ctx.transformListBefore) {\n ctx.transformListBefore.push(cb);\n } else {\n ctx.transformListBefore = [cb];\n }\n\n return this;\n }\n\n /**\n * Assigns a unique identifier to the schema.\n * This ID can be used to track or map validation errors back to specific fields\n * in a form or other structures.\n *\n * @param {string} value - The unique identifier for the schema.\n * @returns {this} The updated schema with the assigned ID.\n *\n * @example\n * const schema = string().id('username');\n */\n public id(value: string): this {\n return this.meta('id', value);\n }\n\n /**\n * Provides a description for the schema, offering additional context or information.\n * The description can be used when displaying validation errors or for documentation purposes.\n *\n * @param {string} value - The description for the schema.\n * @returns {this} The updated schema with the added description.\n *\n * @example\n * const schema = string().description('The username of the account holder.');\n */\n public description(value: string): this {\n return this.meta('description', value);\n }\n\n private meta(key: string, value: string): this {\n const ctx = this[ctxSymbol];\n ctx.meta = { ...ctx.meta, [key]: value };\n return this;\n }\n\n protected defaultValueCheck() {\n if (this[ctxSymbol].defaultValue !== undefined) {\n throw new BuildSchemaError('Default value must be the last method called in schema');\n }\n }\n}\n\ntype TypeMapping = {\n number: number;\n string: string;\n boolean: boolean;\n undefined: undefined;\n object: object;\n // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n function: Function;\n symbol: symbol;\n bigint: bigint;\n};\n\nexport type MapMixTypes<T extends BaseType[]> = T extends (infer U)[]\n ? U extends keyof TypeMapping\n ? TypeMapping[U]\n : never\n : never;\n"],"mappings":";;;;;;;;;;;AAyBO,IAAM,eAAN,MAAmB;AAAA,EACxB,CAAC,SAAS;AAAA,EACV,YAAY,KAAuB;AACjC,SAAK,SAAS,IAAI;AAAA,EACpB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,UAAU,YAAwC;AACvD,SAAK,kBAAkB;AACvB,SAAK,SAAS,EAAE,oBAAoB,KAAK,GAAG,UAAU;AACtD,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,WAA2B;AAChC,SAAK,kBAAkB;AACvB,SAAK,SAAS,EAAE,aAAa;AAC7B,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,WAAgC;AACrC,SAAK,kBAAkB;AACvB,SAAK,SAAS,EAAE,aAAa;AAC7B,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,QAAQ,cAAqC;AAClD,UAAM,MAAM,KAAK,SAAS;AAC1B,QAAI,IAAI,YAAY;AAClB,YAAM,IAAI,iBAAiB,sDAAsD;AAAA,IACnF;AAEA,QAAI;AACF,kBAAY,MAAM,YAAY;AAAA,IAChC,SAAS,GAAG;AACV,YAAM,IAAI,iBAAkB,EAAY,OAAO;AAAA,IACjD;AAEA,SAAK,SAAS,EAAE,eAAe;AAC/B,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAuBO,0BAA8B,IAAkD;AACrF,UAAM,MAAM,KAAK,SAAS;AAC1B,QAAI,IAAI,qBAAqB;AAC3B,UAAI,oBAAoB,KAAK,EAAE;AAAA,IACjC,OAAO;AACL,UAAI,sBAAsB,CAAC,EAAE;AAAA,IAC/B;AAEA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAaO,GAAG,OAAqB;AAC7B,WAAO,KAAK,KAAK,MAAM,KAAK;AAAA,EAC9B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYO,YAAY,OAAqB;AACtC,WAAO,KAAK,KAAK,eAAe,KAAK;AAAA,EACvC;AAAA,EAEQ,KAAK,KAAa,OAAqB;AAC7C,UAAM,MAAM,KAAK,SAAS;AAC1B,QAAI,OAAO,EAAE,GAAG,IAAI,MAAM,CAAC,GAAG,GAAG,MAAM;AACvC,WAAO;AAAA,EACT;AAAA,EAEU,oBAAoB;AAC5B,QAAI,KAAK,SAAS,EAAE,iBAAiB,QAAW;AAC9C,YAAM,IAAI,iBAAiB,wDAAwD;AAAA,IACrF;AAAA,EACF;AACF;","names":[]}
|
|
@@ -3,13 +3,16 @@ import {
|
|
|
3
3
|
} from "./chunk-YBVMP4EI.mjs";
|
|
4
4
|
import {
|
|
5
5
|
guardException
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-FGZKNBDP.mjs";
|
|
7
7
|
|
|
8
8
|
// src/helpers/core.ts
|
|
9
9
|
var ctxSymbol = Symbol("contextSymbol");
|
|
10
10
|
function innerCheck(schema, receivedValue, exCtx) {
|
|
11
11
|
const commonTmap = exCtx.t;
|
|
12
12
|
const schemaData = schema[ctxSymbol];
|
|
13
|
+
schemaData.transformListBefore?.forEach((transformCallback) => {
|
|
14
|
+
receivedValue = transformCallback(receivedValue);
|
|
15
|
+
});
|
|
13
16
|
if (receivedValue === void 0) {
|
|
14
17
|
if (schemaData.defaultValue !== void 0) return schemaData.defaultValue;
|
|
15
18
|
if (!schemaData.isOptional) guardException("Required", receivedValue, exCtx, commonTmap["c:optional"]);
|
|
@@ -27,16 +30,20 @@ function innerCheck(schema, receivedValue, exCtx) {
|
|
|
27
30
|
if (!schemaData.type.includes(typeOfVal))
|
|
28
31
|
guardException(schemaData.type, typeOfVal, exCtx, commonTmap["c:invalidType"]);
|
|
29
32
|
}
|
|
30
|
-
schemaData.requiredValidations.forEach((requiredValidation) => {
|
|
31
|
-
requiredValidation(receivedValue, exCtx);
|
|
32
|
-
});
|
|
33
33
|
if (schemaData.array) {
|
|
34
34
|
if (!Array.isArray(receivedValue)) return guardException("Array", receivedValue, exCtx, commonTmap["c:array"]);
|
|
35
|
+
schemaData.requiredValidations.forEach((requiredValidation) => {
|
|
36
|
+
requiredValidation(receivedValue, exCtx);
|
|
37
|
+
});
|
|
35
38
|
const schema2 = schemaData.array;
|
|
36
39
|
const pathToError = exCtx.pathToError;
|
|
37
40
|
const parsedReceivedValue = [];
|
|
38
41
|
receivedValue.forEach((elem, i) => {
|
|
39
|
-
const parsedElement = innerCheck(schema2, elem, {
|
|
42
|
+
const parsedElement = innerCheck(schema2, elem, {
|
|
43
|
+
...exCtx,
|
|
44
|
+
pathToError: `${pathToError}[${i}]`,
|
|
45
|
+
meta: schemaData.meta
|
|
46
|
+
});
|
|
40
47
|
parsedReceivedValue.push(parsedElement);
|
|
41
48
|
});
|
|
42
49
|
return parsedReceivedValue;
|
|
@@ -44,6 +51,9 @@ function innerCheck(schema, receivedValue, exCtx) {
|
|
|
44
51
|
if (schemaData.object) {
|
|
45
52
|
if (typeOfVal !== "object") guardException("Object", receivedValue, exCtx, commonTmap["c:objectType"]);
|
|
46
53
|
if (Array.isArray(receivedValue)) guardException("Object", receivedValue, exCtx, commonTmap["c:objectTypeAsArray"]);
|
|
54
|
+
schemaData.requiredValidations.forEach((requiredValidation) => {
|
|
55
|
+
requiredValidation(receivedValue, exCtx);
|
|
56
|
+
});
|
|
47
57
|
const shapeSchema = schemaData.object;
|
|
48
58
|
const parsedReceivedValue = {};
|
|
49
59
|
if (!schemaData.allowUnrecognizedObjectProps) {
|
|
@@ -61,12 +71,16 @@ function innerCheck(schema, receivedValue, exCtx) {
|
|
|
61
71
|
}
|
|
62
72
|
const parsedReceivedObjectValuePropery = innerCheck(valueOfSchema, receivedObjectValuePropery, {
|
|
63
73
|
...exCtx,
|
|
64
|
-
pathToError: `${pathToError}.${keyOfSchema}
|
|
74
|
+
pathToError: `${pathToError}.${keyOfSchema}`,
|
|
75
|
+
meta: schemaData.meta
|
|
65
76
|
});
|
|
66
77
|
parsedReceivedValue[keyOfSchema] = parsedReceivedObjectValuePropery;
|
|
67
78
|
}
|
|
68
79
|
return parsedReceivedValue;
|
|
69
80
|
}
|
|
81
|
+
schemaData.requiredValidations.forEach((requiredValidation) => {
|
|
82
|
+
requiredValidation(receivedValue, exCtx);
|
|
83
|
+
});
|
|
70
84
|
return receivedValue;
|
|
71
85
|
}
|
|
72
86
|
|
|
@@ -74,4 +88,4 @@ export {
|
|
|
74
88
|
ctxSymbol,
|
|
75
89
|
innerCheck
|
|
76
90
|
};
|
|
77
|
-
//# sourceMappingURL=chunk-
|
|
91
|
+
//# sourceMappingURL=chunk-BNCT7NOR.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/helpers/core.ts"],"sourcesContent":["export const ctxSymbol = Symbol('contextSymbol');\n\nimport { ExceptionContext } from '../commonTypes';\nimport { guardException } from '../exceptions';\nimport type { CommonSchema } from '../schemas/CommonSchema';\nimport { isValidDateInner } from './isValidDateInner';\n\nexport function innerCheck(schema: CommonSchema, receivedValue: unknown, exCtx: ExceptionContext): unknown {\n const commonTmap = exCtx.t;\n const schemaData = schema[ctxSymbol];\n\n schemaData.transformListBefore?.forEach((transformCallback) => {\n receivedValue = transformCallback(receivedValue);\n });\n\n if (receivedValue === undefined) {\n if (schemaData.defaultValue !== undefined) return schemaData.defaultValue;\n if (!schemaData.isOptional) guardException('Required', receivedValue, exCtx, commonTmap['c:optional']);\n return receivedValue;\n }\n\n if (receivedValue === null) {\n if (!schemaData.isNullable) guardException('Not null', receivedValue, exCtx, commonTmap['c:nullable']);\n return receivedValue;\n }\n\n if (schemaData.date) {\n if (!isValidDateInner(receivedValue)) guardException('Date', receivedValue, exCtx, commonTmap['c:date']);\n }\n\n const typeOfVal = typeof receivedValue;\n\n if (schemaData.type.length) {\n if (!schemaData.type.includes(typeOfVal))\n guardException(schemaData.type, typeOfVal, exCtx, commonTmap['c:invalidType']);\n }\n\n if (schemaData.array) {\n if (!Array.isArray(receivedValue)) return guardException('Array', receivedValue, exCtx, commonTmap['c:array']);\n\n schemaData.requiredValidations.forEach((requiredValidation) => {\n requiredValidation(receivedValue, exCtx);\n });\n\n const schema = schemaData.array;\n const pathToError = exCtx.pathToError;\n const parsedReceivedValue: unknown[] = [];\n receivedValue.forEach((elem, i) => {\n const parsedElement = innerCheck(schema, elem, {\n ...exCtx,\n pathToError: `${pathToError}[${i}]`,\n meta: schemaData.meta,\n });\n parsedReceivedValue.push(parsedElement);\n });\n\n return parsedReceivedValue;\n }\n\n if (schemaData.object) {\n if (typeOfVal !== 'object') guardException('Object', receivedValue, exCtx, commonTmap['c:objectType']);\n if (Array.isArray(receivedValue)) guardException('Object', receivedValue, exCtx, commonTmap['c:objectTypeAsArray']);\n\n schemaData.requiredValidations.forEach((requiredValidation) => {\n requiredValidation(receivedValue, exCtx);\n });\n\n const shapeSchema = schemaData.object;\n const parsedReceivedValue: Record<string, unknown> = {};\n\n if (!schemaData.allowUnrecognizedObjectProps) {\n for (const keyPerReceivedValue of Object.keys(receivedValue)) {\n if (shapeSchema[keyPerReceivedValue] === undefined)\n guardException('Unrecognized property', keyPerReceivedValue, exCtx, commonTmap['c:unrecognizedProperty']);\n }\n }\n\n const pathToError = exCtx.pathToError;\n for (const [keyOfSchema, valueOfSchema] of Object.entries(shapeSchema)) {\n const receivedObjectValuePropery = (receivedValue as Record<string, unknown>)[keyOfSchema];\n if (receivedObjectValuePropery === undefined) {\n if (!valueOfSchema[ctxSymbol].isOptional)\n guardException('Required', receivedObjectValuePropery, exCtx, commonTmap['c:requiredProperty']);\n }\n\n const parsedReceivedObjectValuePropery = innerCheck(valueOfSchema, receivedObjectValuePropery, {\n ...exCtx,\n pathToError: `${pathToError}.${keyOfSchema}`,\n meta: schemaData.meta,\n });\n\n parsedReceivedValue[keyOfSchema] = parsedReceivedObjectValuePropery;\n }\n\n return parsedReceivedValue;\n }\n\n schemaData.requiredValidations.forEach((requiredValidation) => {\n requiredValidation(receivedValue, exCtx);\n });\n\n return receivedValue;\n}\n"],"mappings":";;;;;;;;AAAO,IAAM,YAAY,OAAO,eAAe;AAOxC,SAAS,WAAW,QAAsB,eAAwB,OAAkC;AACzG,QAAM,aAAa,MAAM;AACzB,QAAM,aAAa,OAAO,SAAS;AAEnC,aAAW,qBAAqB,QAAQ,CAAC,sBAAsB;AAC7D,oBAAgB,kBAAkB,aAAa;AAAA,EACjD,CAAC;AAED,MAAI,kBAAkB,QAAW;AAC/B,QAAI,WAAW,iBAAiB,OAAW,QAAO,WAAW;AAC7D,QAAI,CAAC,WAAW,WAAY,gBAAe,YAAY,eAAe,OAAO,WAAW,YAAY,CAAC;AACrG,WAAO;AAAA,EACT;AAEA,MAAI,kBAAkB,MAAM;AAC1B,QAAI,CAAC,WAAW,WAAY,gBAAe,YAAY,eAAe,OAAO,WAAW,YAAY,CAAC;AACrG,WAAO;AAAA,EACT;AAEA,MAAI,WAAW,MAAM;AACnB,QAAI,CAAC,iBAAiB,aAAa,EAAG,gBAAe,QAAQ,eAAe,OAAO,WAAW,QAAQ,CAAC;AAAA,EACzG;AAEA,QAAM,YAAY,OAAO;AAEzB,MAAI,WAAW,KAAK,QAAQ;AAC1B,QAAI,CAAC,WAAW,KAAK,SAAS,SAAS;AACrC,qBAAe,WAAW,MAAM,WAAW,OAAO,WAAW,eAAe,CAAC;AAAA,EACjF;AAEA,MAAI,WAAW,OAAO;AACpB,QAAI,CAAC,MAAM,QAAQ,aAAa,EAAG,QAAO,eAAe,SAAS,eAAe,OAAO,WAAW,SAAS,CAAC;AAE7G,eAAW,oBAAoB,QAAQ,CAAC,uBAAuB;AAC7D,yBAAmB,eAAe,KAAK;AAAA,IACzC,CAAC;AAED,UAAMA,UAAS,WAAW;AAC1B,UAAM,cAAc,MAAM;AAC1B,UAAM,sBAAiC,CAAC;AACxC,kBAAc,QAAQ,CAAC,MAAM,MAAM;AACjC,YAAM,gBAAgB,WAAWA,SAAQ,MAAM;AAAA,QAC7C,GAAG;AAAA,QACH,aAAa,GAAG,WAAW,IAAI,CAAC;AAAA,QAChC,MAAM,WAAW;AAAA,MACnB,CAAC;AACD,0BAAoB,KAAK,aAAa;AAAA,IACxC,CAAC;AAED,WAAO;AAAA,EACT;AAEA,MAAI,WAAW,QAAQ;AACrB,QAAI,cAAc,SAAU,gBAAe,UAAU,eAAe,OAAO,WAAW,cAAc,CAAC;AACrG,QAAI,MAAM,QAAQ,aAAa,EAAG,gBAAe,UAAU,eAAe,OAAO,WAAW,qBAAqB,CAAC;AAElH,eAAW,oBAAoB,QAAQ,CAAC,uBAAuB;AAC7D,yBAAmB,eAAe,KAAK;AAAA,IACzC,CAAC;AAED,UAAM,cAAc,WAAW;AAC/B,UAAM,sBAA+C,CAAC;AAEtD,QAAI,CAAC,WAAW,8BAA8B;AAC5C,iBAAW,uBAAuB,OAAO,KAAK,aAAa,GAAG;AAC5D,YAAI,YAAY,mBAAmB,MAAM;AACvC,yBAAe,yBAAyB,qBAAqB,OAAO,WAAW,wBAAwB,CAAC;AAAA,MAC5G;AAAA,IACF;AAEA,UAAM,cAAc,MAAM;AAC1B,eAAW,CAAC,aAAa,aAAa,KAAK,OAAO,QAAQ,WAAW,GAAG;AACtE,YAAM,6BAA8B,cAA0C,WAAW;AACzF,UAAI,+BAA+B,QAAW;AAC5C,YAAI,CAAC,cAAc,SAAS,EAAE;AAC5B,yBAAe,YAAY,4BAA4B,OAAO,WAAW,oBAAoB,CAAC;AAAA,MAClG;AAEA,YAAM,mCAAmC,WAAW,eAAe,4BAA4B;AAAA,QAC7F,GAAG;AAAA,QACH,aAAa,GAAG,WAAW,IAAI,WAAW;AAAA,QAC1C,MAAM,WAAW;AAAA,MACnB,CAAC;AAED,0BAAoB,WAAW,IAAI;AAAA,IACrC;AAEA,WAAO;AAAA,EACT;AAEA,aAAW,oBAAoB,QAAQ,CAAC,uBAAuB;AAC7D,uBAAmB,eAAe,KAAK;AAAA,EACzC,CAAC;AAED,SAAO;AACT;","names":["schema"]}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
setToDefaultLocale
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-H32B3NHW.mjs";
|
|
4
4
|
import {
|
|
5
5
|
guardException
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-FGZKNBDP.mjs";
|
|
7
7
|
|
|
8
8
|
// src/asserts/mix/equalTo.ts
|
|
9
9
|
var equalToErrorMessage = "The received value is not equal to expected";
|
|
@@ -18,4 +18,4 @@ setToDefaultLocale(equalTo);
|
|
|
18
18
|
export {
|
|
19
19
|
equalTo
|
|
20
20
|
};
|
|
21
|
-
//# sourceMappingURL=chunk-
|
|
21
|
+
//# sourceMappingURL=chunk-C3EDEQ3W.mjs.map
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
import {
|
|
2
2
|
equalTo
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-C3EDEQ3W.mjs";
|
|
4
4
|
import {
|
|
5
5
|
oneOfValues
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-K7X6DVM2.mjs";
|
|
7
7
|
import {
|
|
8
8
|
ONLY_ONCE
|
|
9
9
|
} from "./chunk-ASZW6XRM.mjs";
|
|
10
10
|
import {
|
|
11
11
|
_setStrictType
|
|
12
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-H6LHV3EA.mjs";
|
|
13
13
|
import {
|
|
14
14
|
CommonSchema
|
|
15
|
-
} from "./chunk-
|
|
15
|
+
} from "./chunk-7NBOVYSP.mjs";
|
|
16
16
|
import {
|
|
17
17
|
BuildSchemaError
|
|
18
|
-
} from "./chunk-
|
|
18
|
+
} from "./chunk-FGZKNBDP.mjs";
|
|
19
19
|
|
|
20
20
|
// src/schemas/NumberSchema.ts
|
|
21
21
|
var NumberSchema = class extends CommonSchema {
|
|
@@ -55,4 +55,4 @@ var NumberSchema = class extends CommonSchema {
|
|
|
55
55
|
export {
|
|
56
56
|
NumberSchema
|
|
57
57
|
};
|
|
58
|
-
//# sourceMappingURL=chunk-
|
|
58
|
+
//# sourceMappingURL=chunk-CHUPE6SJ.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/schemas/NumberSchema.ts"],"sourcesContent":["import { equalTo } from '../asserts/mix/equalTo';\nimport { oneOfValues } from '../asserts/mix/oneOfValues';\nimport { WithBGuardType } from '../commonTypes';\nimport { BuildSchemaError } from '../exceptions';\nimport { ONLY_ONCE } from '../helpers/constants';\nimport { _setStrictType } from '../helpers/setStrictType';\nimport { CommonSchema } from './CommonSchema';\n\nexport class NumberSchema extends CommonSchema {\n protected _number = 1;\n private limit: boolean | undefined;\n\n /**\n * Restricts the schema to exactly match the specified value and infers the literal value as the TypeScript type.\n *\n * @param expectedValue - The value that the schema must exactly match.\n * @returns - The schema instance restricted to the specified value, with the literal value inferred as the TypeScript type\n *\n * @example - number().equalTo(42); // Infers the type 42\n */\n public equalTo<Y extends number>(expectedValue: Y): WithBGuardType<this, Y> {\n if (this.limit) throw new BuildSchemaError(ONLY_ONCE);\n this.limit = true;\n _setStrictType(this, expectedValue);\n return this.custom(equalTo(expectedValue)) as WithBGuardType<this, Y>;\n }\n\n /**\n * Restricts the schema to match one of the specified values and infers the union of those values as the TypeScript type.\n *\n * @param expectedValues - An array of values that the schema can match.\n * @returns - The schema instance restricted to one of the specified values, with the union of those values inferred as the TypeScript type.\n *\n * @example\n * number().oneOfValues([5, 7]); // Infers the type 5 | 7\n */\n public oneOfValues<Y extends number>(expectedValue: Y[]): WithBGuardType<this, Y> {\n if (this.limit) throw new BuildSchemaError(ONLY_ONCE);\n this.limit = true;\n _setStrictType(this, expectedValue);\n return this.custom(oneOfValues(expectedValue)) as WithBGuardType<this, Y>;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAQO,IAAM,eAAN,cAA2B,aAAa;AAAA,EACnC,UAAU;AAAA,EACZ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUD,QAA0B,eAA2C;AAC1E,QAAI,KAAK,MAAO,OAAM,IAAI,iBAAiB,SAAS;AACpD,SAAK,QAAQ;AACb,mBAAe,MAAM,aAAa;AAClC,WAAO,KAAK,OAAO,QAAQ,aAAa,CAAC;AAAA,EAC3C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWO,YAA8B,eAA6C;AAChF,QAAI,KAAK,MAAO,OAAM,IAAI,iBAAiB,SAAS;AACpD,SAAK,QAAQ;AACb,mBAAe,MAAM,aAAa;AAClC,WAAO,KAAK,OAAO,YAAY,aAAa,CAAC;AAAA,EAC/C;AACF;","names":[]}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); var _class;
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunkQIDWASOCjs = require('./chunk-QIDWASOC.js');
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
var
|
|
6
|
+
var _chunkSHJVKSEZjs = require('./chunk-SHJVKSEZ.js');
|
|
7
7
|
|
|
8
8
|
|
|
9
|
-
var
|
|
9
|
+
var _chunk5P6AZJCNjs = require('./chunk-5P6AZJCN.js');
|
|
10
10
|
|
|
11
11
|
// src/schemas/BooleanSchema.ts
|
|
12
12
|
var isBoolean = (expected) => (received, ctx) => {
|
|
13
|
-
if (received !== expected)
|
|
13
|
+
if (received !== expected) _chunk5P6AZJCNjs.guardException.call(void 0, expected, received, ctx, "c:isBoolean");
|
|
14
14
|
};
|
|
15
|
-
var BooleanSchema = (_class = class extends
|
|
15
|
+
var BooleanSchema = (_class = class extends _chunkSHJVKSEZjs.CommonSchema {constructor(...args) { super(...args); _class.prototype.__init.call(this); }
|
|
16
16
|
__init() {this._boolean = 1}
|
|
17
17
|
/**
|
|
18
18
|
* Restricts the schema to exactly match the boolean value true and infers the true value as the TypeScript type.
|
|
@@ -22,7 +22,7 @@ var BooleanSchema = (_class = class extends _chunk3IOQH3K4js.CommonSchema {const
|
|
|
22
22
|
* @example - boolean().onlyTrue(); // Infers the type true
|
|
23
23
|
*/
|
|
24
24
|
onlyTrue() {
|
|
25
|
-
|
|
25
|
+
_chunkQIDWASOCjs._setStrictType.call(void 0, this, true);
|
|
26
26
|
return this.custom(isBoolean(true));
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
@@ -33,7 +33,7 @@ var BooleanSchema = (_class = class extends _chunk3IOQH3K4js.CommonSchema {const
|
|
|
33
33
|
* @example - boolean().onlyFalse(); // Infers the type false
|
|
34
34
|
*/
|
|
35
35
|
onlyFalse() {
|
|
36
|
-
|
|
36
|
+
_chunkQIDWASOCjs._setStrictType.call(void 0, this, false);
|
|
37
37
|
return this.custom(isBoolean(false));
|
|
38
38
|
}
|
|
39
39
|
}, _class);
|
|
@@ -41,4 +41,4 @@ var BooleanSchema = (_class = class extends _chunk3IOQH3K4js.CommonSchema {const
|
|
|
41
41
|
|
|
42
42
|
|
|
43
43
|
exports.BooleanSchema = BooleanSchema;
|
|
44
|
-
//# sourceMappingURL=chunk-
|
|
44
|
+
//# sourceMappingURL=chunk-ECBOZ3BM.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/bguard/bguard/lib/chunk-ECBOZ3BM.js","../src/schemas/BooleanSchema.ts"],"names":[],"mappings":"AAAA;AACE;AACF,sDAA4B;AAC5B;AACE;AACF,sDAA4B;AAC5B;AACE;AACF,sDAA4B;AAC5B;AACA;ACLA,IAAM,UAAA,EAAY,CAAC,QAAA,EAAA,GAAsB,CAAC,QAAA,EAAmB,GAAA,EAAA,GAA0B;AACrF,EAAA,GAAA,CAAI,SAAA,IAAa,QAAA,EAAU,6CAAA,QAAe,EAAU,QAAA,EAAU,GAAA,EAAK,aAAa,CAAA;AAClF,CAAA;AAEO,IAAM,cAAA,YAAN,MAAA,QAA4B,8BAAa;AAAA,iBACpC,SAAA,EAAW,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASd,QAAA,CAAA,EAAuC;AAC5C,IAAA,6CAAA,IAAe,EAAM,IAAI,CAAA;AACzB,IAAA,OAAO,IAAA,CAAK,MAAA,CAAO,SAAA,CAAU,IAAI,CAAC,CAAA;AAAA,EACpC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASO,SAAA,CAAA,EAAyC;AAC9C,IAAA,6CAAA,IAAe,EAAM,KAAK,CAAA;AAC1B,IAAA,OAAO,IAAA,CAAK,MAAA,CAAO,SAAA,CAAU,KAAK,CAAC,CAAA;AAAA,EACrC;AACF,UAAA;ADIA;AACA;AACE;AACF,sCAAC","file":"/home/runner/work/bguard/bguard/lib/chunk-ECBOZ3BM.js","sourcesContent":[null,"import { CommonSchema } from './CommonSchema';\nimport { guardException } from '../exceptions';\nimport { ExceptionContext, WithBGuardType } from '../commonTypes';\nimport { _setStrictType } from '../helpers/setStrictType';\n\nconst isBoolean = (expected: boolean) => (received: boolean, ctx: ExceptionContext) => {\n if (received !== expected) guardException(expected, received, ctx, 'c:isBoolean');\n};\n\nexport class BooleanSchema extends CommonSchema {\n protected _boolean = 1;\n\n /**\n * Restricts the schema to exactly match the boolean value true and infers the true value as the TypeScript type.\n *\n * @returns - The schema instance restricted to the value true, with the true value inferred as the TypeScript type\n *\n * @example - boolean().onlyTrue(); // Infers the type true\n */\n public onlyTrue(): WithBGuardType<this, true> {\n _setStrictType(this, true);\n return this.custom(isBoolean(true)) as WithBGuardType<this, true>;\n }\n\n /**\n * Restricts the schema to exactly match the boolean value false and infers the false value as the TypeScript type.\n *\n * @returns - The schema instance restricted to the value false, with the false value inferred as the TypeScript type\n *\n * @example - boolean().onlyFalse(); // Infers the type false\n */\n public onlyFalse(): WithBGuardType<this, false> {\n _setStrictType(this, false);\n return this.custom(isBoolean(false)) as WithBGuardType<this, false>;\n }\n}\n\n// export type WithBoolean<T extends BooleanSchema, Y = boolean> = T & { validation_boolean: Y };\n// export type ExtractFromBoolean<T> = T extends WithBoolean<BooleanSchema, infer Y> ? Y : never;\n"]}
|
|
@@ -4,12 +4,14 @@ var ValidationError = class extends Error {
|
|
|
4
4
|
received;
|
|
5
5
|
pathToError;
|
|
6
6
|
message;
|
|
7
|
-
|
|
7
|
+
meta;
|
|
8
|
+
constructor(expected, received, pathToError, message, meta) {
|
|
8
9
|
super();
|
|
9
10
|
this.expected = expected;
|
|
10
11
|
this.received = received;
|
|
11
12
|
this.pathToError = pathToError;
|
|
12
13
|
this.message = message;
|
|
14
|
+
this.meta = meta;
|
|
13
15
|
}
|
|
14
16
|
};
|
|
15
17
|
var BuildSchemaError = class extends Error {
|
|
@@ -32,7 +34,7 @@ function guardException(expected, received, ctx, messageKey) {
|
|
|
32
34
|
});
|
|
33
35
|
return;
|
|
34
36
|
}
|
|
35
|
-
throw new ValidationError(expected, received, ctx.pathToError, message);
|
|
37
|
+
throw new ValidationError(expected, received, ctx.pathToError, message, ctx.meta);
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
export {
|
|
@@ -40,4 +42,4 @@ export {
|
|
|
40
42
|
BuildSchemaError,
|
|
41
43
|
guardException
|
|
42
44
|
};
|
|
43
|
-
//# sourceMappingURL=chunk-
|
|
45
|
+
//# sourceMappingURL=chunk-FGZKNBDP.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/exceptions.ts"],"sourcesContent":["import type { ExceptionContext, MetaContext } from './commonTypes';\n\nexport class ValidationError extends Error {\n expected: unknown;\n received: unknown;\n pathToError: string;\n message: string;\n meta?: MetaContext;\n constructor(expected: unknown, received: unknown, pathToError: string, message: string, meta?: MetaContext) {\n super();\n this.expected = expected;\n this.received = received;\n this.pathToError = pathToError;\n this.message = message;\n this.meta = meta;\n }\n}\n\nexport class BuildSchemaError extends Error {}\n\nfunction replacePlaceholders(template: string, replacements: Record<string, unknown>): string {\n const regex = /{{(.*?)}}/g;\n return template.replace(regex, (_, key) => {\n return key in replacements ? `${replacements[key] as string}` : `{{${key}}}`;\n });\n}\n\nexport function guardException(\n expected: unknown,\n received: unknown,\n ctx: ExceptionContext,\n messageKey: string,\n): never | void {\n const rawMessage = ctx.t[messageKey] ?? messageKey;\n const message = replacePlaceholders(rawMessage, { e: expected, r: received, p: ctx.pathToError });\n\n if (ctx.getAllErrors) {\n ctx.errors.push({\n expected,\n received,\n pathToError: ctx.pathToError,\n message,\n });\n\n return;\n }\n throw new ValidationError(expected, received, ctx.pathToError, message, ctx.meta);\n}\n"],"mappings":";AAEO,IAAM,kBAAN,cAA8B,MAAM;AAAA,EACzC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,YAAY,UAAmB,UAAmB,aAAqB,SAAiB,MAAoB;AAC1G,UAAM;AACN,SAAK,WAAW;AAChB,SAAK,WAAW;AAChB,SAAK,cAAc;AACnB,SAAK,UAAU;AACf,SAAK,OAAO;AAAA,EACd;AACF;AAEO,IAAM,mBAAN,cAA+B,MAAM;AAAC;AAE7C,SAAS,oBAAoB,UAAkB,cAA+C;AAC5F,QAAM,QAAQ;AACd,SAAO,SAAS,QAAQ,OAAO,CAAC,GAAG,QAAQ;AACzC,WAAO,OAAO,eAAe,GAAG,aAAa,GAAG,CAAW,KAAK,KAAK,GAAG;AAAA,EAC1E,CAAC;AACH;AAEO,SAAS,eACd,UACA,UACA,KACA,YACc;AACd,QAAM,aAAa,IAAI,EAAE,UAAU,KAAK;AACxC,QAAM,UAAU,oBAAoB,YAAY,EAAE,GAAG,UAAU,GAAG,UAAU,GAAG,IAAI,YAAY,CAAC;AAEhG,MAAI,IAAI,cAAc;AACpB,QAAI,OAAO,KAAK;AAAA,MACd;AAAA,MACA;AAAA,MACA,aAAa,IAAI;AAAA,MACjB;AAAA,IACF,CAAC;AAED;AAAA,EACF;AACA,QAAM,IAAI,gBAAgB,UAAU,UAAU,IAAI,aAAa,SAAS,IAAI,IAAI;AAClF;","names":[]}
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
import {
|
|
2
2
|
equalTo
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-C3EDEQ3W.mjs";
|
|
4
4
|
import {
|
|
5
5
|
oneOfValues
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-K7X6DVM2.mjs";
|
|
7
7
|
import {
|
|
8
8
|
ONLY_ONCE
|
|
9
9
|
} from "./chunk-ASZW6XRM.mjs";
|
|
10
10
|
import {
|
|
11
11
|
_setStrictType
|
|
12
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-H6LHV3EA.mjs";
|
|
13
13
|
import {
|
|
14
14
|
CommonSchema
|
|
15
|
-
} from "./chunk-
|
|
15
|
+
} from "./chunk-7NBOVYSP.mjs";
|
|
16
16
|
import {
|
|
17
17
|
BuildSchemaError
|
|
18
|
-
} from "./chunk-
|
|
18
|
+
} from "./chunk-FGZKNBDP.mjs";
|
|
19
19
|
|
|
20
20
|
// src/schemas/StringSchema.ts
|
|
21
21
|
var StringSchema = class extends CommonSchema {
|
|
@@ -60,4 +60,4 @@ var StringSchema = class extends CommonSchema {
|
|
|
60
60
|
export {
|
|
61
61
|
StringSchema
|
|
62
62
|
};
|
|
63
|
-
//# sourceMappingURL=chunk-
|
|
63
|
+
//# sourceMappingURL=chunk-FP5QAEVQ.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/schemas/StringSchema.ts"],"sourcesContent":["import { equalTo } from '../asserts/mix/equalTo';\nimport { oneOfValues } from '../asserts/mix/oneOfValues';\nimport { WithBGuardType } from '../commonTypes';\nimport { BuildSchemaError } from '../exceptions';\nimport { ONLY_ONCE } from '../helpers/constants';\nimport { _setStrictType } from '../helpers/setStrictType';\nimport { CommonSchema } from './CommonSchema';\n\nexport class StringSchema extends CommonSchema {\n protected _string = 1;\n private limit: boolean | undefined;\n\n /**\n * Restricts the schema to exactly match the specified value and infers the literal value as the TypeScript type.\n *\n * @param expectedValue - The value that the schema must exactly match.\n * @returns - The schema instance restricted to the specified value, with the literal value inferred as the TypeScript type\n *\n * @example - string().equalTo('hello'); // Infers the type 'hello'\n */\n public equalTo<Y extends string>(expectedValue: Y): WithBGuardType<this, Y> {\n this.limitCheck();\n _setStrictType(this, `'${expectedValue}'`);\n\n return this.custom(equalTo(expectedValue)) as WithBGuardType<this, Y>;\n }\n\n /**\n * Restricts the schema to match one of the specified values and infers the union of those values as the TypeScript type.\n *\n * @param expectedValues - An array of values that the schema can match.\n * @returns - The schema instance restricted to one of the specified values, with the union of those values inferred as the TypeScript type.\n *\n * @example\n * string().oneOfValues(['foo', 'bar']); // Infers the type 'foo' | 'bar'\n */\n public oneOfValues<Y extends string>(expectedValue: Y[]): WithBGuardType<this, Y> {\n this.limitCheck();\n _setStrictType(\n this,\n expectedValue.map((el) => `'${el}'`),\n );\n return this.custom(oneOfValues(expectedValue)) as WithBGuardType<this, Y>;\n }\n\n private limitCheck() {\n if (this.limit) throw new BuildSchemaError(ONLY_ONCE);\n this.limit = true;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAQO,IAAM,eAAN,cAA2B,aAAa;AAAA,EACnC,UAAU;AAAA,EACZ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUD,QAA0B,eAA2C;AAC1E,SAAK,WAAW;AAChB,mBAAe,MAAM,IAAI,aAAa,GAAG;AAEzC,WAAO,KAAK,OAAO,QAAQ,aAAa,CAAC;AAAA,EAC3C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWO,YAA8B,eAA6C;AAChF,SAAK,WAAW;AAChB;AAAA,MACE;AAAA,MACA,cAAc,IAAI,CAAC,OAAO,IAAI,EAAE,GAAG;AAAA,IACrC;AACA,WAAO,KAAK,OAAO,YAAY,aAAa,CAAC;AAAA,EAC/C;AAAA,EAEQ,aAAa;AACnB,QAAI,KAAK,MAAO,OAAM,IAAI,iBAAiB,SAAS;AACpD,SAAK,QAAQ;AAAA,EACf;AACF;","names":[]}
|
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
2
|
|
|
3
|
-
var _chunkPIBDJJUUjs = require('./chunk-PIBDJJUU.js');
|
|
4
3
|
|
|
4
|
+
var _chunkX762EUSZjs = require('./chunk-X762EUSZ.js');
|
|
5
5
|
|
|
6
|
-
var _chunkUZ2PWO4Ojs = require('./chunk-UZ2PWO4O.js');
|
|
7
6
|
|
|
7
|
+
var _chunk5YYRBKDPjs = require('./chunk-5YYRBKDP.js');
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
|
|
10
|
+
var _chunk5P6AZJCNjs = require('./chunk-5P6AZJCN.js');
|
|
10
11
|
|
|
11
12
|
// src/parseOrFail.ts
|
|
12
13
|
function parseOrFail(schema, receivedValue, options) {
|
|
13
14
|
try {
|
|
14
|
-
return
|
|
15
|
-
t:
|
|
16
|
-
pathToError: ""
|
|
15
|
+
return _chunkX762EUSZjs.innerCheck.call(void 0, schema, receivedValue, {
|
|
16
|
+
t: _chunk5YYRBKDPjs.getTranslationByLocale.call(void 0, _optionalChain([options, 'optionalAccess', _ => _.lng])),
|
|
17
|
+
pathToError: "",
|
|
18
|
+
meta: schema[_chunkX762EUSZjs.ctxSymbol].meta
|
|
17
19
|
});
|
|
18
20
|
} catch (e) {
|
|
19
|
-
if (e instanceof
|
|
21
|
+
if (e instanceof _chunk5P6AZJCNjs.ValidationError) throw e;
|
|
20
22
|
throw new Error("Something unexpected happened");
|
|
21
23
|
}
|
|
22
24
|
}
|
|
@@ -24,4 +26,4 @@ function parseOrFail(schema, receivedValue, options) {
|
|
|
24
26
|
|
|
25
27
|
|
|
26
28
|
exports.parseOrFail = parseOrFail;
|
|
27
|
-
//# sourceMappingURL=chunk-
|
|
29
|
+
//# sourceMappingURL=chunk-G55SHHAR.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/bguard/bguard/lib/chunk-G55SHHAR.js","../src/parseOrFail.ts"],"names":[],"mappings":"AAAA;AACE;AACA;AACF,sDAA4B;AAC5B;AACE;AACF,sDAA4B;AAC5B;AACE;AACF,sDAA4B;AAC5B;AACA;AC+BO,SAAS,WAAA,CACd,MAAA,EACA,aAAA,EACA,OAAA,EACc;AACd,EAAA,IAAI;AACF,IAAA,OAAO,yCAAA,MAAW,EAAQ,aAAA,EAAe;AAAA,MACvC,CAAA,EAAG,qDAAA,gBAAuB,OAAA,2BAAS,KAAG,CAAA;AAAA,MACtC,WAAA,EAAa,EAAA;AAAA,MACb,IAAA,EAAM,MAAA,CAAO,0BAAS,CAAA,CAAE;AAAA,IAC1B,CAAC,CAAA;AAAA,EACH,EAAA,MAAA,CAAS,CAAA,EAAG;AACV,IAAA,GAAA,CAAI,EAAA,WAAa,gCAAA,EAAiB,MAAM,CAAA;AAExC,IAAA,MAAM,IAAI,KAAA,CAAM,+BAA+B,CAAA;AAAA,EACjD;AACF;ADlCA;AACA;AACE;AACF,kCAAC","file":"/home/runner/work/bguard/bguard/lib/chunk-G55SHHAR.js","sourcesContent":[null,"import type { InferType } from './InferType';\nimport type { CommonSchema } from './schemas/CommonSchema';\nimport { ctxSymbol, innerCheck } from './helpers/core';\nimport { getTranslationByLocale } from './translationMap';\nimport { ValidationError } from './exceptions';\ninterface ParseOptions {\n /**\n * Set language keyword to map error messages.\n * @default 'default'\n * @example 'sr' or 'Serbia' or any string to identify language\n */\n lng?: string;\n}\n\n/**\n * Parses and validates a value against the provided schema, returning a type-safe result.\n *\n * This function will throw a `ValidationError` if the value does not conform to the schema.\n * The inferred TypeScript type of the returned value will match the structure defined by the schema.\n *\n * @template T\n * @param {T} schema - The schema to validate the received value against. This schema dictates the expected structure and type of the value.\n * @param {unknown} receivedValue - The value to be validated and parsed according to the schema.\n * @param {ParseOptions} options - Options\n * @param {ParseOptions.lng} options.lng - Set language keyword to map Error message\n * @returns {InferType<T>} The validated value, with its TypeScript type inferred from the schema.\n *\n * @throws {ValidationError} If the received value does not match the schema, a `ValidationError` will be thrown.\n * @throws {Error} If an unexpected error occurs during validation, an error will be thrown with a generic message.\n *\n * @example\n * const schema = object({\n * name: string(),\n * age: number(),\n * });\n *\n * const result = parseOrFail(schema, { name: 'Alice', age: 30 });\n * // result will be inferred as { name: string; age: number }\n *\n * parseOrFail(schema, { name: 'Alice', age: '30' });\n * // Throws ValidationError because 'age' should be a number, not a string.\n */\nexport function parseOrFail<T extends CommonSchema>(\n schema: T,\n receivedValue: unknown,\n options?: ParseOptions,\n): InferType<T> {\n try {\n return innerCheck(schema, receivedValue, {\n t: getTranslationByLocale(options?.lng),\n pathToError: '',\n meta: schema[ctxSymbol].meta,\n }) as InferType<T>;\n } catch (e) {\n if (e instanceof ValidationError) throw e;\n /* istanbul ignore next */\n throw new Error('Something unexpected happened');\n }\n}\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
BuildSchemaError
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-FGZKNBDP.mjs";
|
|
4
4
|
|
|
5
5
|
// src/translationMap.ts
|
|
6
6
|
var defaultErrorMap = {
|
|
@@ -52,4 +52,4 @@ export {
|
|
|
52
52
|
clearLocales,
|
|
53
53
|
getTranslationByLocale
|
|
54
54
|
};
|
|
55
|
-
//# sourceMappingURL=chunk-
|
|
55
|
+
//# sourceMappingURL=chunk-H32B3NHW.mjs.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
ctxSymbol
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-BNCT7NOR.mjs";
|
|
4
4
|
|
|
5
5
|
// src/helpers/setStrictType.ts
|
|
6
6
|
function _setStrictType(that, val) {
|
|
@@ -11,4 +11,4 @@ function _setStrictType(that, val) {
|
|
|
11
11
|
export {
|
|
12
12
|
_setStrictType
|
|
13
13
|
};
|
|
14
|
-
//# sourceMappingURL=chunk-
|
|
14
|
+
//# sourceMappingURL=chunk-H6LHV3EA.mjs.map
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunk5YYRBKDPjs = require('./chunk-5YYRBKDP.js');
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
var
|
|
6
|
+
var _chunk5P6AZJCNjs = require('./chunk-5P6AZJCN.js');
|
|
7
7
|
|
|
8
8
|
// src/asserts/mix/equalTo.ts
|
|
9
9
|
var equalToErrorMessage = "The received value is not equal to expected";
|
|
10
10
|
var equalToErrorKey = "m:equalTo";
|
|
11
11
|
var equalTo = (expected) => (received, ctx) => {
|
|
12
|
-
if (expected !== received)
|
|
12
|
+
if (expected !== received) _chunk5P6AZJCNjs.guardException.call(void 0, expected, received, ctx, equalToErrorKey);
|
|
13
13
|
};
|
|
14
14
|
equalTo.key = equalToErrorKey;
|
|
15
15
|
equalTo.message = equalToErrorMessage;
|
|
16
|
-
|
|
16
|
+
_chunk5YYRBKDPjs.setToDefaultLocale.call(void 0, equalTo);
|
|
17
17
|
|
|
18
18
|
|
|
19
19
|
|
|
20
20
|
exports.equalTo = equalTo;
|
|
21
|
-
//# sourceMappingURL=chunk-
|
|
21
|
+
//# sourceMappingURL=chunk-HP3DRZWK.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/home/runner/work/bguard/bguard/lib/chunk-
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/bguard/bguard/lib/chunk-HP3DRZWK.js","../src/asserts/mix/equalTo.ts"],"names":[],"mappings":"AAAA;AACE;AACF,sDAA4B;AAC5B;AACE;AACF,sDAA4B;AAC5B;AACA;ACHA,IAAM,oBAAA,EAAsB,6CAAA;AAC5B,IAAM,gBAAA,EAAkB,WAAA;AAejB,IAAM,QAAA,EACX,CAAC,QAAA,EAAA,GACD,CAAC,QAAA,EAAmB,GAAA,EAAA,GAA0B;AAC5C,EAAA,GAAA,CAAI,SAAA,IAAa,QAAA,EAAU,6CAAA,QAAe,EAAU,QAAA,EAAU,GAAA,EAAK,eAAe,CAAA;AACpF,CAAA;AAEF,OAAA,CAAQ,IAAA,EAAM,eAAA;AACd,OAAA,CAAQ,QAAA,EAAU,mBAAA;AAClB,iDAAA,OAA0B,CAAA;ADZ1B;AACA;AACE;AACF,0BAAC","file":"/home/runner/work/bguard/bguard/lib/chunk-HP3DRZWK.js","sourcesContent":[null,"import { guardException } from '../../exceptions';\nimport { ExceptionContext, RequiredValidation } from '../../commonTypes';\nimport { setToDefaultLocale } from '../../translationMap';\n\nconst equalToErrorMessage = 'The received value is not equal to expected';\nconst equalToErrorKey = 'm:equalTo';\n\n/**\n * @description Creates a custom assertion that checks if a value is equal to the expected value.\n * @notice It has already been implemented in the number, bigint and string schema. There is no need to use it as a custom assert.\n * @param {unknown} expected The value that the received value is expected to match.\n * @returns {RequiredValidation} A validation function that takes a received value and an exception context.\n * @throws {ValidationError} If the received value does not match the expected value.\n * @example\n * const schema = number().custom(equalTo(5)); // Define a schema with a custom assertion\n * parseOrFail(schema, 5); // Valid\n * parseOrFail(schema, 3); // Throws an error: 'The received value is not equal to expected'\n *\n * @translation Error Translation Key = 'm:equalTo'\n */\nexport const equalTo =\n (expected: unknown): RequiredValidation =>\n (received: unknown, ctx: ExceptionContext) => {\n if (expected !== received) guardException(expected, received, ctx, equalToErrorKey);\n };\n\nequalTo.key = equalToErrorKey;\nequalTo.message = equalToErrorMessage;\nsetToDefaultLocale(equalTo);\n"]}
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunk5YYRBKDPjs = require('./chunk-5YYRBKDP.js');
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
var
|
|
6
|
+
var _chunk5P6AZJCNjs = require('./chunk-5P6AZJCN.js');
|
|
7
7
|
|
|
8
8
|
// src/asserts/mix/oneOfValues.ts
|
|
9
9
|
var oneOfValuesErrorMessage = "The received value is not equal to expected";
|
|
10
10
|
var oneOfValuesErrorKey = "m:oneOfValues";
|
|
11
11
|
var oneOfValues = (expected) => (received, ctx) => {
|
|
12
|
-
if (!expected.includes(received))
|
|
12
|
+
if (!expected.includes(received)) _chunk5P6AZJCNjs.guardException.call(void 0, expected, received, ctx, oneOfValuesErrorKey);
|
|
13
13
|
};
|
|
14
14
|
oneOfValues.key = oneOfValuesErrorKey;
|
|
15
15
|
oneOfValues.message = oneOfValuesErrorMessage;
|
|
16
|
-
|
|
16
|
+
_chunk5YYRBKDPjs.setToDefaultLocale.call(void 0, oneOfValues);
|
|
17
17
|
|
|
18
18
|
|
|
19
19
|
|
|
20
20
|
exports.oneOfValues = oneOfValues;
|
|
21
|
-
//# sourceMappingURL=chunk-
|
|
21
|
+
//# sourceMappingURL=chunk-IZ3CBVEE.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/home/runner/work/bguard/bguard/lib/chunk-
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/bguard/bguard/lib/chunk-IZ3CBVEE.js","../src/asserts/mix/oneOfValues.ts"],"names":[],"mappings":"AAAA;AACE;AACF,sDAA4B;AAC5B;AACE;AACF,sDAA4B;AAC5B;AACA;ACHA,IAAM,wBAAA,EAA0B,6CAAA;AAChC,IAAM,oBAAA,EAAsB,eAAA;AAgBrB,IAAM,YAAA,EACX,CAAC,QAAA,EAAA,GACD,CAAC,QAAA,EAAmB,GAAA,EAAA,GAA0B;AAC5C,EAAA,GAAA,CAAI,CAAC,QAAA,CAAS,QAAA,CAAS,QAAQ,CAAA,EAAG,6CAAA,QAAe,EAAU,QAAA,EAAU,GAAA,EAAK,mBAAmB,CAAA;AAC/F,CAAA;AAEF,WAAA,CAAY,IAAA,EAAM,mBAAA;AAClB,WAAA,CAAY,QAAA,EAAU,uBAAA;AACtB,iDAAA,WAA8B,CAAA;ADb9B;AACA;AACE;AACF,kCAAC","file":"/home/runner/work/bguard/bguard/lib/chunk-IZ3CBVEE.js","sourcesContent":[null,"import { guardException } from '../../exceptions';\nimport { ExceptionContext, RequiredValidation } from '../../commonTypes';\nimport { setToDefaultLocale } from '../../translationMap';\n\nconst oneOfValuesErrorMessage = 'The received value is not equal to expected';\nconst oneOfValuesErrorKey = 'm:oneOfValues';\n\n/**\n * @description Creates a custom assertion that checks if a value is equal to the one of expected values.\n * @notice It has already been implemented in the number, bigint and string schema. There is no need to use it as a custom assert.\n * @param {unknown} expected The value that the received value is expected to match.\n * @returns {RequiredValidation} A validation function that takes a received value and an exception context.\n * @throws {ValidationError} If the received value does not match at least one of the expected values.\n * @example\n * const schema = number().custom(oneOfValues([5, 4])); // Define a schema with a custom assertion\n * parseOrFail(schema, 5); // Valid\n * parseOrFail(schema, 4); // Valid\n * parseOrFail(schema, 3); // Throws an error: 'The received value is not equal to expected'\n *\n * @translation Error Translation Key = 'm:oneOfValues'\n */\nexport const oneOfValues =\n (expected: unknown[]): RequiredValidation =>\n (received: unknown, ctx: ExceptionContext) => {\n if (!expected.includes(received)) guardException(expected, received, ctx, oneOfValuesErrorKey);\n };\n\noneOfValues.key = oneOfValuesErrorKey;\noneOfValues.message = oneOfValuesErrorMessage;\nsetToDefaultLocale(oneOfValues);\n"]}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
setToDefaultLocale
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-H32B3NHW.mjs";
|
|
4
4
|
import {
|
|
5
5
|
guardException
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-FGZKNBDP.mjs";
|
|
7
7
|
|
|
8
8
|
// src/asserts/mix/oneOfValues.ts
|
|
9
9
|
var oneOfValuesErrorMessage = "The received value is not equal to expected";
|
|
@@ -18,4 +18,4 @@ setToDefaultLocale(oneOfValues);
|
|
|
18
18
|
export {
|
|
19
19
|
oneOfValues
|
|
20
20
|
};
|
|
21
|
-
//# sourceMappingURL=chunk-
|
|
21
|
+
//# sourceMappingURL=chunk-K7X6DVM2.mjs.map
|