valgen 4.0.0-alpha.6 → 4.0.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.
Files changed (57) hide show
  1. package/CHANGELOG.md +13 -2
  2. package/cjs/core/context.js +29 -23
  3. package/cjs/core/validator.js +9 -8
  4. package/cjs/helpers/omit-undefined.js +15 -0
  5. package/cjs/rules/is-array.js +12 -10
  6. package/cjs/rules/is-bigint.js +2 -2
  7. package/cjs/rules/is-boolean.js +2 -2
  8. package/cjs/rules/is-date.js +7 -13
  9. package/cjs/rules/is-empty.js +4 -4
  10. package/cjs/rules/is-enum.js +2 -5
  11. package/cjs/rules/is-integer.js +2 -2
  12. package/cjs/rules/is-matches.js +2 -6
  13. package/cjs/rules/is-null.js +6 -6
  14. package/cjs/rules/is-number.js +2 -2
  15. package/cjs/rules/is-object-id.js +2 -2
  16. package/cjs/rules/is-object.js +16 -18
  17. package/cjs/rules/is-record.js +13 -13
  18. package/cjs/rules/is-string.js +2 -2
  19. package/cjs/rules/is-tuple.js +12 -9
  20. package/cjs/rules/length.js +2 -2
  21. package/cjs/rules/optional.js +8 -8
  22. package/cjs/rules/pipe.js +2 -4
  23. package/cjs/rules/range.js +10 -10
  24. package/cjs/rules/string-formats.js +48 -48
  25. package/cjs/rules/union.js +10 -5
  26. package/cjs/rules/utilities.js +2 -2
  27. package/esm/core/context.js +29 -23
  28. package/esm/core/validator.js +9 -8
  29. package/esm/helpers/omit-undefined.js +11 -0
  30. package/esm/rules/is-array.js +13 -11
  31. package/esm/rules/is-bigint.js +2 -2
  32. package/esm/rules/is-boolean.js +2 -2
  33. package/esm/rules/is-date.js +7 -13
  34. package/esm/rules/is-empty.js +4 -4
  35. package/esm/rules/is-enum.js +2 -5
  36. package/esm/rules/is-integer.js +2 -2
  37. package/esm/rules/is-matches.js +2 -6
  38. package/esm/rules/is-null.js +6 -6
  39. package/esm/rules/is-number.js +2 -2
  40. package/esm/rules/is-object-id.js +2 -2
  41. package/esm/rules/is-object.js +17 -19
  42. package/esm/rules/is-record.js +14 -14
  43. package/esm/rules/is-string.js +2 -2
  44. package/esm/rules/is-tuple.js +13 -10
  45. package/esm/rules/length.js +2 -2
  46. package/esm/rules/optional.js +9 -9
  47. package/esm/rules/pipe.js +3 -5
  48. package/esm/rules/range.js +10 -10
  49. package/esm/rules/string-formats.js +48 -48
  50. package/esm/rules/union.js +10 -5
  51. package/esm/rules/utilities.js +3 -3
  52. package/package.json +4 -4
  53. package/typings/core/context.d.ts +11 -19
  54. package/typings/core/types.d.ts +4 -1
  55. package/typings/core/validator.d.ts +1 -1
  56. package/typings/helpers/omit-undefined.d.ts +1 -0
  57. package/typings/rules/is-object.d.ts +1 -1
@@ -10,7 +10,7 @@ function optional(nested, options) {
10
10
  return (0, index_js_1.validator)('optional', function (input, context) {
11
11
  if (input == null)
12
12
  return input;
13
- return nested[index_js_1.kValidatorFn](input, context, nested);
13
+ return nested(input, context);
14
14
  }, options);
15
15
  }
16
16
  exports.optional = optional;
@@ -19,12 +19,12 @@ exports.optional = optional;
19
19
  * @validator required
20
20
  */
21
21
  function required(nested, options) {
22
- return (0, index_js_1.validator)('required', function (input, context) {
22
+ return (0, index_js_1.validator)('required', function (input, context, _this) {
23
23
  if (input == null) {
24
- context.failure(`{{label}} is required`);
24
+ context.fail(_this, `{{label}} is required`, input);
25
25
  return;
26
26
  }
27
- return nested[index_js_1.kValidatorFn](input, context, nested);
27
+ return nested(input, context);
28
28
  }, options);
29
29
  }
30
30
  exports.required = required;
@@ -33,12 +33,12 @@ exports.required = required;
33
33
  * @validator exists
34
34
  */
35
35
  function exists(options) {
36
- return (0, index_js_1.validator)('exists', function (input, context) {
36
+ return (0, index_js_1.validator)('exists', function (input, context, _this) {
37
37
  if (input !== undefined ||
38
- (context.input.value && context.parent &&
39
- Object.getOwnPropertyDescriptor(context.input.value, context.input.property)))
38
+ (context.scope &&
39
+ Object.getOwnPropertyDescriptor(context.scope, input)))
40
40
  return input;
41
- context.failure(`{{label}} must exist`);
41
+ context.fail(_this, `{{label}} must exist`, input);
42
42
  }, options);
43
43
  }
44
44
  exports.exists = exists;
package/cjs/rules/pipe.js CHANGED
@@ -14,8 +14,7 @@ function pipe(...rules) {
14
14
  let v = input;
15
15
  for (i = 0; i < l; i++) {
16
16
  c = rules[i];
17
- const subContext = context.extend(c);
18
- v = c[index_js_1.kValidatorFn](v, subContext, c);
17
+ v = c(v, context);
19
18
  if (context.errors.length)
20
19
  return;
21
20
  }
@@ -34,8 +33,7 @@ function allOf(...rules) {
34
33
  const l = rules.length;
35
34
  for (i = 0; i < l; i++) {
36
35
  c = rules[i];
37
- const subContext = context.extend(c);
38
- c[index_js_1.kValidatorFn](input, subContext, c);
36
+ c(input, context);
39
37
  }
40
38
  return input;
41
39
  });
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.range = exports.isLte = exports.isLt = exports.isGte = exports.isGt = void 0;
4
4
  const index_js_1 = require("../core/index.js");
5
5
  function isGt(minValue, options) {
6
- return (0, index_js_1.validator)('isGt', function (input, context) {
6
+ return (0, index_js_1.validator)('isGt', function (input, context, _this) {
7
7
  if ((typeof minValue === 'number' || typeof minValue === 'bigint') &&
8
8
  (typeof input === 'number' || typeof input === 'bigint') &&
9
9
  input > minValue)
@@ -14,12 +14,12 @@ function isGt(minValue, options) {
14
14
  (input > minValue ||
15
15
  (options?.caseInsensitive && input.toLowerCase() > minValue.toLowerCase())))
16
16
  return input;
17
- context.failure(`{{label}} must be greater than ${(typeof minValue === 'string' ? `"${minValue}"` : minValue)}`);
17
+ context.fail(_this, `{{label}} must be greater than ${(typeof minValue === 'string' ? `"${minValue}"` : minValue)}`, input);
18
18
  }, options);
19
19
  }
20
20
  exports.isGt = isGt;
21
21
  function isGte(minValue, options) {
22
- return (0, index_js_1.validator)('isGte', function (input, context) {
22
+ return (0, index_js_1.validator)('isGte', function (input, context, _this) {
23
23
  if ((typeof minValue === 'number' || typeof minValue === 'bigint') &&
24
24
  (typeof input === 'number' || typeof input === 'bigint') &&
25
25
  input >= minValue)
@@ -30,12 +30,12 @@ function isGte(minValue, options) {
30
30
  (input >= minValue ||
31
31
  (options?.caseInsensitive && input.toLowerCase() >= minValue.toLowerCase())))
32
32
  return input;
33
- context.failure(`{{label}} must be greater than or equal to ${(typeof minValue === 'string' ? `"${minValue}"` : minValue)}`);
33
+ context.fail(_this, `{{label}} must be greater than or equal to ${(typeof minValue === 'string' ? `"${minValue}"` : minValue)}`, input);
34
34
  }, options);
35
35
  }
36
36
  exports.isGte = isGte;
37
37
  function isLt(maxValue, options) {
38
- return (0, index_js_1.validator)('isLt', function (input, context) {
38
+ return (0, index_js_1.validator)('isLt', function (input, context, _this) {
39
39
  if ((typeof maxValue === 'number' || typeof maxValue === 'bigint') &&
40
40
  (typeof input === 'number' || typeof input === 'bigint') &&
41
41
  input < maxValue)
@@ -46,12 +46,12 @@ function isLt(maxValue, options) {
46
46
  (input < maxValue ||
47
47
  (options?.caseInsensitive && input.toLowerCase() < maxValue.toLowerCase())))
48
48
  return input;
49
- context.failure(`{{label}} must be lover than ${(typeof maxValue === 'string' ? `"${maxValue}"` : maxValue)}`);
49
+ context.fail(_this, `{{label}} must be lover than ${(typeof maxValue === 'string' ? `"${maxValue}"` : maxValue)}`, input);
50
50
  }, options);
51
51
  }
52
52
  exports.isLt = isLt;
53
53
  function isLte(maxValue, options) {
54
- return (0, index_js_1.validator)('isLt', function (input, context) {
54
+ return (0, index_js_1.validator)('isLt', function (input, context, _this) {
55
55
  if ((typeof maxValue === 'number' || typeof maxValue === 'bigint') &&
56
56
  (typeof input === 'number' || typeof input === 'bigint') &&
57
57
  input <= maxValue)
@@ -62,7 +62,7 @@ function isLte(maxValue, options) {
62
62
  (input <= maxValue ||
63
63
  (options?.caseInsensitive && input.toLowerCase() <= maxValue.toLowerCase())))
64
64
  return input;
65
- context.failure(`{{label}} must be lover than or equal to ${(typeof maxValue === 'string' ? `"${maxValue}"` : maxValue)}`);
65
+ context.fail(_this, `{{label}} must be lover than or equal to ${(typeof maxValue === 'string' ? `"${maxValue}"` : maxValue)}`, input);
66
66
  }, options);
67
67
  }
68
68
  exports.isLte = isLte;
@@ -71,7 +71,7 @@ exports.isLte = isLte;
71
71
  * @validator range
72
72
  */
73
73
  function range(minValue, maxValue, options) {
74
- return (0, index_js_1.validator)('range', function (input, context) {
74
+ return (0, index_js_1.validator)('range', function (input, context, _this) {
75
75
  if ((typeof minValue === 'number' || typeof minValue === 'bigint') &&
76
76
  (typeof maxValue === 'number' || typeof maxValue === 'bigint') &&
77
77
  (typeof input === 'number' || typeof input === 'bigint') &&
@@ -83,7 +83,7 @@ function range(minValue, maxValue, options) {
83
83
  if (typeof minValue === 'string' && typeof maxValue === 'string' &&
84
84
  typeof input === 'string' && input >= minValue && input <= maxValue)
85
85
  return input;
86
- context.failure(`{{label}} must be between ${minValue} and ${maxValue}`);
86
+ context.fail(_this, `{{label}} must be between ${minValue} and ${maxValue}`, input);
87
87
  }, options);
88
88
  }
89
89
  exports.range = range;
@@ -11,10 +11,10 @@ const index_js_1 = require("../core/index.js");
11
11
  * @validator isUUID
12
12
  */
13
13
  function isUUID(version, options) {
14
- return (0, index_js_1.validator)('isUUID', function (input, context) {
14
+ return (0, index_js_1.validator)('isUUID', function (input, context, _this) {
15
15
  if (input != null && typeof input === 'string' && validator_1.default.isUUID(input, version))
16
16
  return input;
17
- context.failure(`{{label}} is not a valid UUID${version ? ' v' + version : ''}`);
17
+ context.fail(_this, `{{label}} is not a valid UUID${version ? ' v' + version : ''}`, input);
18
18
  }, options);
19
19
  }
20
20
  exports.isUUID = isUUID;
@@ -23,10 +23,10 @@ exports.isUUID = isUUID;
23
23
  * @validator isEmail
24
24
  */
25
25
  function isEmail(options) {
26
- return (0, index_js_1.validator)('isEmail', function (input, context) {
26
+ return (0, index_js_1.validator)('isEmail', function (input, context, _this) {
27
27
  if (typeof input === 'string' && validator_1.default.isEmail(input, options))
28
28
  return input;
29
- context.failure(`{{label}} is not a valid a EMail`);
29
+ context.fail(_this, `{{label}} is not a valid a EMail`, input);
30
30
  }, options);
31
31
  }
32
32
  exports.isEmail = isEmail;
@@ -35,10 +35,10 @@ exports.isEmail = isEmail;
35
35
  * @validator isMobilePhone
36
36
  */
37
37
  function isMobilePhone(options) {
38
- return (0, index_js_1.validator)('isMobilePhone', function (input, context) {
38
+ return (0, index_js_1.validator)('isMobilePhone', function (input, context, _this) {
39
39
  if (typeof input === 'string' && validator_1.default.isMobilePhone(input, options?.locale, options))
40
40
  return input;
41
- context.failure(`{{label}} is not a valid a Mobile Phone Number`);
41
+ context.fail(_this, `{{label}} is not a valid a Mobile Phone Number`, input);
42
42
  }, options);
43
43
  }
44
44
  exports.isMobilePhone = isMobilePhone;
@@ -47,10 +47,10 @@ exports.isMobilePhone = isMobilePhone;
47
47
  * @validator isIP
48
48
  */
49
49
  function isIP(version, options) {
50
- return (0, index_js_1.validator)('isIP', function (input, context) {
50
+ return (0, index_js_1.validator)('isIP', function (input, context, _this) {
51
51
  if (input != null && typeof input === 'string' && validator_1.default.isIP(input, version))
52
52
  return input;
53
- context.failure(`{{label}} is not a valid IP${version ? ' v' + version : ''}`);
53
+ context.fail(_this, `{{label}} is not a valid IP${version ? ' v' + version : ''}`, input);
54
54
  }, options);
55
55
  }
56
56
  exports.isIP = isIP;
@@ -59,10 +59,10 @@ exports.isIP = isIP;
59
59
  * @validator isUUID
60
60
  */
61
61
  function isIPRange(version, options) {
62
- return (0, index_js_1.validator)('isIPRange', function (input, context) {
62
+ return (0, index_js_1.validator)('isIPRange', function (input, context, _this) {
63
63
  if (input != null && typeof input === 'string' && validator_1.default.isIPRange(input, version))
64
64
  return input;
65
- context.failure(`{{label}} is not a valid IP${version ? ' v' + version : ''} range`);
65
+ context.fail(_this, `{{label}} is not a valid IP${version ? ' v' + version : ''} range`, input);
66
66
  }, options);
67
67
  }
68
68
  exports.isIPRange = isIPRange;
@@ -71,10 +71,10 @@ exports.isIPRange = isIPRange;
71
71
  * @validator isMACAddress
72
72
  */
73
73
  function isMACAddress(options) {
74
- return (0, index_js_1.validator)('isMACAddress', function (input, context) {
74
+ return (0, index_js_1.validator)('isMACAddress', function (input, context, _this) {
75
75
  if (input != null && typeof input === 'string' && validator_1.default.isMACAddress(input, options))
76
76
  return input;
77
- context.failure(`{{label}} is not a valid MAC address`);
77
+ context.fail(_this, `{{label}} is not a valid MAC address`, input);
78
78
  }, options);
79
79
  }
80
80
  exports.isMACAddress = isMACAddress;
@@ -83,10 +83,10 @@ exports.isMACAddress = isMACAddress;
83
83
  * @validator isPort
84
84
  */
85
85
  function isPort(options) {
86
- return (0, index_js_1.validator)('isPort', function (input, context) {
86
+ return (0, index_js_1.validator)('isPort', function (input, context, _this) {
87
87
  if (input != null && typeof input === 'string' && validator_1.default.isPort(input))
88
88
  return input;
89
- context.failure(`{{label}} is not a valid port number`);
89
+ context.fail(_this, `{{label}} is not a valid port number`, input);
90
90
  }, options);
91
91
  }
92
92
  exports.isPort = isPort;
@@ -95,10 +95,10 @@ exports.isPort = isPort;
95
95
  * @validator isURL
96
96
  */
97
97
  function isURL(options) {
98
- return (0, index_js_1.validator)('isURL', function (input, context) {
98
+ return (0, index_js_1.validator)('isURL', function (input, context, _this) {
99
99
  if (input != null && typeof input === 'string' && validator_1.default.isURL(input, options))
100
100
  return input;
101
- context.failure(`{{label}} is not a valid URL`);
101
+ context.fail(_this, `{{label}} is not a valid URL`, input);
102
102
  }, options);
103
103
  }
104
104
  exports.isURL = isURL;
@@ -107,10 +107,10 @@ exports.isURL = isURL;
107
107
  * @validator isBase64
108
108
  */
109
109
  function isBase64(options) {
110
- return (0, index_js_1.validator)('isBase64', function (input, context) {
110
+ return (0, index_js_1.validator)('isBase64', function (input, context, _this) {
111
111
  if (typeof input === 'string' && validator_1.default.isBase64(input, options))
112
112
  return input;
113
- context.failure(`{{label}} is not a valid a Base64 string`);
113
+ context.fail(_this, `{{label}} is not a valid a Base64 string`, input);
114
114
  }, options);
115
115
  }
116
116
  exports.isBase64 = isBase64;
@@ -119,10 +119,10 @@ exports.isBase64 = isBase64;
119
119
  * @validator isBIC
120
120
  */
121
121
  function isBIC(options) {
122
- return (0, index_js_1.validator)('isBIC', function (input, context) {
122
+ return (0, index_js_1.validator)('isBIC', function (input, context, _this) {
123
123
  if (typeof input === 'string' && validator_1.default.isBIC(input))
124
124
  return input;
125
- context.failure(`{{label}}is not a valid a BIC (Bank Identification Code) or SWIFT code`);
125
+ context.fail(_this, `{{label}}is not a valid a BIC (Bank Identification Code) or SWIFT code`, input);
126
126
  }, options);
127
127
  }
128
128
  exports.isBIC = isBIC;
@@ -131,10 +131,10 @@ exports.isBIC = isBIC;
131
131
  * @validator isCreditCard
132
132
  */
133
133
  function isCreditCard(options) {
134
- return (0, index_js_1.validator)('isCreditCard', function (input, context) {
134
+ return (0, index_js_1.validator)('isCreditCard', function (input, context, _this) {
135
135
  if (typeof input === 'string' && validator_1.default.isCreditCard(input, options))
136
136
  return input;
137
- context.failure(`{{label}} is not a valid Credit Card number`);
137
+ context.fail(_this, `{{label}} is not a valid Credit Card number`, input);
138
138
  }, options);
139
139
  }
140
140
  exports.isCreditCard = isCreditCard;
@@ -143,10 +143,10 @@ exports.isCreditCard = isCreditCard;
143
143
  * @validator isEAN
144
144
  */
145
145
  function isIBAN(options) {
146
- return (0, index_js_1.validator)('isIBAN', function (input, context) {
146
+ return (0, index_js_1.validator)('isIBAN', function (input, context, _this) {
147
147
  if (typeof input === 'string' && validator_1.default.isIBAN(input))
148
148
  return input;
149
- context.failure(`{{label}} is not a valid IBAN (International Bank Account Number)`);
149
+ context.fail(_this, `{{label}} is not a valid IBAN (International Bank Account Number)`, input);
150
150
  }, options);
151
151
  }
152
152
  exports.isIBAN = isIBAN;
@@ -155,10 +155,10 @@ exports.isIBAN = isIBAN;
155
155
  * @validator isPassportNumber
156
156
  */
157
157
  function isPassportNumber(countryCode, options) {
158
- return (0, index_js_1.validator)('isPassportNumber', function (input, context) {
158
+ return (0, index_js_1.validator)('isPassportNumber', function (input, context, _this) {
159
159
  if (typeof input === 'string' && validator_1.default.isPassportNumber(input, countryCode))
160
160
  return input;
161
- context.failure(`{{label}} is not a valid ${countryCode} PassportNumber)`);
161
+ context.fail(_this, `{{label}} is not a valid ${countryCode} PassportNumber)`, input);
162
162
  }, options);
163
163
  }
164
164
  exports.isPassportNumber = isPassportNumber;
@@ -167,10 +167,10 @@ exports.isPassportNumber = isPassportNumber;
167
167
  * @validator isEAN
168
168
  */
169
169
  function isEAN(options) {
170
- return (0, index_js_1.validator)('isEAN', function (input, context) {
170
+ return (0, index_js_1.validator)('isEAN', function (input, context, _this) {
171
171
  if (typeof input === 'string' && validator_1.default.isEAN(input))
172
172
  return input;
173
- context.failure(`{{label}} is not a valid EAN (European Article Number)`);
173
+ context.fail(_this, `{{label}} is not a valid EAN (European Article Number)`, input);
174
174
  }, options);
175
175
  }
176
176
  exports.isEAN = isEAN;
@@ -179,10 +179,10 @@ exports.isEAN = isEAN;
179
179
  * @validator isFQDN
180
180
  */
181
181
  function isFQDN(options) {
182
- return (0, index_js_1.validator)('isFQDN', function (input, context) {
182
+ return (0, index_js_1.validator)('isFQDN', function (input, context, _this) {
183
183
  if (typeof input === 'string' && validator_1.default.isFQDN(input, options))
184
184
  return input;
185
- context.failure(`{{label}} is not valid FQDN`);
185
+ context.fail(_this, `{{label}} is not valid FQDN`, input);
186
186
  }, options);
187
187
  }
188
188
  exports.isFQDN = isFQDN;
@@ -191,10 +191,10 @@ exports.isFQDN = isFQDN;
191
191
  * @validator isISSN
192
192
  */
193
193
  function isISSN(options) {
194
- return (0, index_js_1.validator)('isISSN', function (input, context) {
194
+ return (0, index_js_1.validator)('isISSN', function (input, context, _this) {
195
195
  if (typeof input === 'string' && validator_1.default.isISSN(input, options))
196
196
  return input;
197
- context.failure(`{{label}} is not a valid ISSN`);
197
+ context.fail(_this, `{{label}} is not a valid ISSN`, input);
198
198
  }, options);
199
199
  }
200
200
  exports.isISSN = isISSN;
@@ -203,10 +203,10 @@ exports.isISSN = isISSN;
203
203
  * @validator isVAT
204
204
  */
205
205
  function isVAT(countryCode, options) {
206
- return (0, index_js_1.validator)('isVAT', function (input, context) {
206
+ return (0, index_js_1.validator)('isVAT', function (input, context, _this) {
207
207
  if (typeof input === 'string' && validator_1.default.isVAT(input, countryCode))
208
208
  return input;
209
- context.failure(`{{label}} is not a valid VAT number`);
209
+ context.fail(_this, `{{label}} is not a valid VAT number`, input);
210
210
  }, options);
211
211
  }
212
212
  exports.isVAT = isVAT;
@@ -215,10 +215,10 @@ exports.isVAT = isVAT;
215
215
  * @validator isBtcAddress
216
216
  */
217
217
  function isBtcAddress(options) {
218
- return (0, index_js_1.validator)('isBtcAddress', function (input, context) {
218
+ return (0, index_js_1.validator)('isBtcAddress', function (input, context, _this) {
219
219
  if (typeof input === 'string' && validator_1.default.isBtcAddress(input))
220
220
  return input;
221
- context.failure(`{{label}} is not a valid a BTC address`);
221
+ context.fail(_this, `{{label}} is not a valid a BTC address`, input);
222
222
  }, options);
223
223
  }
224
224
  exports.isBtcAddress = isBtcAddress;
@@ -227,10 +227,10 @@ exports.isBtcAddress = isBtcAddress;
227
227
  * @validator isBtcAddress
228
228
  */
229
229
  function isETHAddress(options) {
230
- return (0, index_js_1.validator)('isETHAddress', function (input, context) {
230
+ return (0, index_js_1.validator)('isETHAddress', function (input, context, _this) {
231
231
  if (typeof input === 'string' && validator_1.default.isEthereumAddress(input))
232
232
  return input;
233
- context.failure(`{{label}} is not a valid a ETH (Ethereum) address`);
233
+ context.fail(_this, `{{label}} is not a valid a ETH (Ethereum) address`, input);
234
234
  }, options);
235
235
  }
236
236
  exports.isETHAddress = isETHAddress;
@@ -239,10 +239,10 @@ exports.isETHAddress = isETHAddress;
239
239
  * @validator isHash
240
240
  */
241
241
  function isHash(algorithm, options) {
242
- return (0, index_js_1.validator)('isHash', function (input, context) {
242
+ return (0, index_js_1.validator)('isHash', function (input, context, _this) {
243
243
  if (typeof input === 'string' && validator_1.default.isHash(input, algorithm))
244
244
  return input;
245
- context.failure(`{{label}} is not a valid ${algorithm} hash`);
245
+ context.fail(_this, `{{label}} is not a valid ${algorithm} hash`, input);
246
246
  }, options);
247
247
  }
248
248
  exports.isHash = isHash;
@@ -251,10 +251,10 @@ exports.isHash = isHash;
251
251
  * @validator isHexColor
252
252
  */
253
253
  function isHexColor(options) {
254
- return (0, index_js_1.validator)('isHexColor', function (input, context) {
254
+ return (0, index_js_1.validator)('isHexColor', function (input, context, _this) {
255
255
  if (typeof input === 'string' && validator_1.default.isHexColor(input))
256
256
  return input;
257
- context.failure(`{{label}} is not a valid Hex Color`);
257
+ context.fail(_this, `{{label}} is not a valid Hex Color`, input);
258
258
  }, options);
259
259
  }
260
260
  exports.isHexColor = isHexColor;
@@ -263,10 +263,10 @@ exports.isHexColor = isHexColor;
263
263
  * @validator isHash
264
264
  */
265
265
  function isJWT(options) {
266
- return (0, index_js_1.validator)('isJWT', function (input, context) {
266
+ return (0, index_js_1.validator)('isJWT', function (input, context, _this) {
267
267
  if (typeof input === 'string' && validator_1.default.isJWT(input))
268
268
  return input;
269
- context.failure(`{{label}} is not a valid JWT token`);
269
+ context.fail(_this, `{{label}} is not a valid JWT token`, input);
270
270
  }, options);
271
271
  }
272
272
  exports.isJWT = isJWT;
@@ -275,10 +275,10 @@ exports.isJWT = isJWT;
275
275
  * @validator isLowercase
276
276
  */
277
277
  function isLowercase(options) {
278
- return (0, index_js_1.validator)('isLowercase', function (input, context) {
278
+ return (0, index_js_1.validator)('isLowercase', function (input, context, _this) {
279
279
  if (typeof input === 'string' && validator_1.default.isLowercase(input))
280
280
  return input;
281
- context.failure(`{{label}} is not a lowercase string`);
281
+ context.fail(_this, `{{label}} is not a lowercase string`, input);
282
282
  }, options);
283
283
  }
284
284
  exports.isLowercase = isLowercase;
@@ -287,10 +287,10 @@ exports.isLowercase = isLowercase;
287
287
  * @validator isUppercase
288
288
  */
289
289
  function isUppercase(options) {
290
- return (0, index_js_1.validator)('isLowercase', function (input, context) {
290
+ return (0, index_js_1.validator)('isLowercase', function (input, context, _this) {
291
291
  if (typeof input === 'string' && validator_1.default.isUppercase(input))
292
292
  return input;
293
- context.failure(`{{label}} is not an uppercase string`);
293
+ context.fail(_this, `{{label}} is not an uppercase string`, input);
294
294
  }, options);
295
295
  }
296
296
  exports.isUppercase = isUppercase;
@@ -7,25 +7,30 @@ const index_js_1 = require("../core/index.js");
7
7
  */
8
8
  function union(codecs) {
9
9
  const l = codecs.length;
10
- return (0, index_js_1.validator)('union', function (input, context) {
10
+ return (0, index_js_1.validator)('union', function (input, context, _this) {
11
11
  let i;
12
12
  let c;
13
13
  let v;
14
14
  let passed = false;
15
+ // Mock fail method to prevent errors
16
+ context.fail = () => passed = false;
15
17
  for (i = 0; i < l; i++) {
16
18
  c = codecs[i];
17
19
  try {
18
- v = c(input, context);
19
20
  passed = true;
20
- break;
21
+ v = c(input, context);
22
+ if (passed)
23
+ break;
21
24
  }
22
- catch (e) {
25
+ catch {
23
26
  //
24
27
  }
25
28
  }
29
+ // Restore fail method
30
+ delete context.fail;
26
31
  if (passed)
27
32
  return v;
28
- context.failure(`{{location}} didn't match any of required format`);
33
+ context.fail(_this, `{{location}} didn't match any of required format`, input);
29
34
  });
30
35
  }
31
36
  exports.union = union;
@@ -9,7 +9,7 @@ const index_js_1 = require("../core/index.js");
9
9
  function forwardRef(fn) {
10
10
  return (0, index_js_1.validator)('forwardRef', function (input, context) {
11
11
  const nested = fn(context);
12
- return nested[index_js_1.kValidatorFn](input, context, nested);
12
+ return nested(input, context);
13
13
  });
14
14
  }
15
15
  exports.forwardRef = forwardRef;
@@ -24,7 +24,7 @@ function iif(check, _then, _else) {
24
24
  // ignored
25
25
  }
26
26
  if ((0, index_js_1.isValidator)(c))
27
- return c[index_js_1.kValidatorFn](input, context, c);
27
+ return c(input, context);
28
28
  return c;
29
29
  });
30
30
  }
@@ -1,12 +1,11 @@
1
+ import { omitUndefined } from '../helpers/omit-undefined.js';
1
2
  import { kOptions } from './constants.js';
2
3
  import { ValidationError } from './validation-error.js';
3
4
  const VARIABLE_REPLACE_PATTERN = /{{([^}]*)}}/g;
4
5
  const OPTIONAL_VAR_PATTERN = /^([^?]+)(?:\||(.*))?$/;
5
6
  export class Context {
6
- constructor(rule, options) {
7
+ constructor(options) {
7
8
  this.errors = [];
8
- this.input = {};
9
- this.rule = rule;
10
9
  if (options?.maxErrors != null)
11
10
  this.maxErrors = options.maxErrors;
12
11
  if (typeof options?.onFail === 'function')
@@ -14,34 +13,43 @@ export class Context {
14
13
  if (options?.coerce != null)
15
14
  this.coerce = options?.coerce;
16
15
  }
17
- failure(message) {
18
- const issue = (typeof message === 'object'
19
- ? { ...message, message: message.message }
20
- : { message: '' + message });
21
- issue.rule = this.rule.id;
22
- Object.assign(issue, this.input);
23
- if (this.onFail) {
16
+ fail(rule, message, value, details) {
17
+ const issue = omitUndefined({
18
+ message: message instanceof Error ? message.message : String(message),
19
+ rule: rule.id,
20
+ root: this.root,
21
+ location: this.location,
22
+ context: this.context,
23
+ property: this.property,
24
+ index: this.index,
25
+ label: this.label,
26
+ value,
27
+ ...details
28
+ });
29
+ issue.value = value;
30
+ const onFail = this.onFail || rule[kOptions].onFail;
31
+ if (onFail) {
24
32
  const proto = Object.getPrototypeOf(this);
25
- const x = this.onFail(issue, this, proto.onFail);
33
+ const superOnFail = proto.onFail !== onFail ? proto.onFail : undefined;
34
+ const x = onFail(issue, this, superOnFail);
26
35
  if (!x)
27
36
  return;
28
- if (typeof x === 'object') {
29
- delete x.id;
30
- delete x.input;
37
+ if (typeof x === 'object')
31
38
  Object.assign(issue, x);
32
- }
33
39
  else
34
40
  issue.message = String(x);
35
41
  }
36
- issue.message = ('' + issue.message).replace(VARIABLE_REPLACE_PATTERN, (x, g) => {
42
+ issue.message = ('' + issue.message)
43
+ .replace(VARIABLE_REPLACE_PATTERN, (x, g) => {
37
44
  const m = OPTIONAL_VAR_PATTERN.exec(g);
38
45
  if (!m)
39
46
  return x;
40
47
  const k = m[1];
41
- const v = this.input[k] ??
42
- (m[1] === 'label' ? this.input.property : undefined);
48
+ let v = issue[k];
49
+ if (!v && m[1] === 'label' && (this.label || this.property))
50
+ v = '`' + (this.label || this.property) + '`';
43
51
  if (v != null)
44
- return '`' + v + '`';
52
+ return v;
45
53
  if (m[2])
46
54
  return m[2];
47
55
  return m[1] === 'label' ? 'Value' : x;
@@ -50,12 +58,10 @@ export class Context {
50
58
  if (this.errors.length >= (this.maxErrors ?? Infinity))
51
59
  throw new ValidationError(this.errors);
52
60
  }
53
- extend(rule, options) {
54
- const extended = new Context(rule, { onFail: undefined, ...rule[kOptions], ...options });
61
+ extend(options) {
62
+ const extended = new Context({ onFail: undefined, ...options });
55
63
  Object.setPrototypeOf(extended, this);
56
64
  delete extended.errors;
57
- extended.parent = this;
58
- extended.root = this.root || this;
59
65
  return extended;
60
66
  }
61
67
  }
@@ -20,20 +20,21 @@ export function validator(arg0, arg1, arg2) {
20
20
  id = id || fn.name || 'unnamed-rule';
21
21
  const name = fn.name || camelCase(id);
22
22
  const _rule = ({
23
- [name](input, options, parent) {
24
- const ctx = parent ? parent.extend(_rule, options) : new Context(_rule, options);
23
+ [name](input, options) {
24
+ const ctx = options instanceof Context ? options : undefined;
25
+ const opts = (ctx ? undefined : options);
26
+ const context = ctx || new Context(opts);
25
27
  let value;
26
28
  try {
27
- ctx.input.value = input;
28
- value = fn(input, ctx, _rule);
29
+ value = fn(input, context, _rule);
29
30
  }
30
31
  catch (e) {
31
- if (!parent && e instanceof ValidationError)
32
+ if (e instanceof ValidationError)
32
33
  throw e;
33
- ctx.failure(e);
34
+ context.fail(_rule, e, input);
34
35
  }
35
- if (!parent && ctx.errors.length)
36
- throw new ValidationError(ctx.errors);
36
+ if (!ctx && context.errors.length)
37
+ throw new ValidationError(context.errors);
37
38
  return value;
38
39
  }
39
40
  })[name];
@@ -0,0 +1,11 @@
1
+ export function omitUndefined(obj, recursive) {
2
+ if (!(obj && typeof obj === 'object'))
3
+ return obj;
4
+ for (const k of Object.keys(obj)) {
5
+ if (obj[k] === undefined)
6
+ delete obj[k];
7
+ else if (recursive && typeof obj[k] === 'object')
8
+ omitUndefined(obj[k]);
9
+ }
10
+ return obj;
11
+ }