valgen 7.0.0 → 7.0.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/constants.js +1 -1
- package/package.json +1 -1
- package/rules/utility-rules/one-of.js +30 -6
package/constants.js
CHANGED
package/package.json
CHANGED
|
@@ -13,8 +13,21 @@ export function oneOf(rules, options) {
|
|
|
13
13
|
let discriminator;
|
|
14
14
|
let v;
|
|
15
15
|
let passed = false;
|
|
16
|
-
//
|
|
17
|
-
|
|
16
|
+
// Every candidate failing is normal control flow here (that's how
|
|
17
|
+
// "try the next one" works), so a candidate's own context.fail must
|
|
18
|
+
// not throw or accumulate into the real error list. But swallowing it
|
|
19
|
+
// completely would hide the *reason* every candidate failed - including
|
|
20
|
+
// a genuine bug in a candidate rule, which would otherwise look
|
|
21
|
+
// identical to "the input just didn't match". So the mock still
|
|
22
|
+
// records the last failure's message, and it's reported directly as
|
|
23
|
+
// the final error (oneOf can only report one message anyway) instead
|
|
24
|
+
// of being discarded in favor of a generic one.
|
|
25
|
+
let lastFailMessage;
|
|
26
|
+
context.fail = (_rule, message) => {
|
|
27
|
+
passed = false;
|
|
28
|
+
lastFailMessage =
|
|
29
|
+
message instanceof Error ? message.message : String(message);
|
|
30
|
+
};
|
|
18
31
|
for (i = 0; i < l; i++) {
|
|
19
32
|
passed = true;
|
|
20
33
|
if (Array.isArray(rules[i])) {
|
|
@@ -39,7 +52,16 @@ export function oneOf(rules, options) {
|
|
|
39
52
|
if (!passed)
|
|
40
53
|
continue;
|
|
41
54
|
}
|
|
42
|
-
catch {
|
|
55
|
+
catch (e) {
|
|
56
|
+
// A discriminator/rule that throws directly (bypassing
|
|
57
|
+
// context.fail entirely, e.g. a plain function rather than one
|
|
58
|
+
// built with validator()) must still count as "this candidate
|
|
59
|
+
// failed" - otherwise `passed` is left at its top-of-loop `true`
|
|
60
|
+
// and, if this is the last candidate, oneOf would silently
|
|
61
|
+
// return an unvalidated value instead of failing.
|
|
62
|
+
passed = false;
|
|
63
|
+
lastFailMessage =
|
|
64
|
+
e?.message != null ? String(e.message) : String(e);
|
|
43
65
|
continue;
|
|
44
66
|
}
|
|
45
67
|
}
|
|
@@ -51,14 +73,16 @@ export function oneOf(rules, options) {
|
|
|
51
73
|
if (passed)
|
|
52
74
|
break;
|
|
53
75
|
}
|
|
54
|
-
catch {
|
|
55
|
-
|
|
76
|
+
catch (e) {
|
|
77
|
+
passed = false;
|
|
78
|
+
lastFailMessage =
|
|
79
|
+
e?.message != null ? String(e.message) : String(e);
|
|
56
80
|
}
|
|
57
81
|
}
|
|
58
82
|
// Restore fail method
|
|
59
83
|
delete context.fail;
|
|
60
84
|
if (passed)
|
|
61
85
|
return v;
|
|
62
|
-
context.fail(_this, `Value didn't match one of required rules`, input);
|
|
86
|
+
context.fail(_this, lastFailMessage || `Value didn't match one of required rules`, input);
|
|
63
87
|
}, options);
|
|
64
88
|
}
|