n4s 5.0.1 → 5.0.2

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 (80) hide show
  1. package/CHANGELOG.md +0 -4
  2. package/README.md +2 -0
  3. package/compose/package.json +2 -0
  4. package/compounds/package.json +2 -0
  5. package/date/package.json +9 -0
  6. package/dist/cjs/compose.development.js +14 -97
  7. package/dist/cjs/compose.js +0 -1
  8. package/dist/cjs/compose.production.js +1 -1
  9. package/dist/cjs/compounds.development.js +30 -86
  10. package/dist/cjs/compounds.js +0 -1
  11. package/dist/cjs/compounds.production.js +1 -1
  12. package/dist/cjs/date.development.js +186 -0
  13. package/dist/cjs/date.js +6 -0
  14. package/dist/cjs/date.production.js +1 -0
  15. package/dist/cjs/email.development.js +363 -0
  16. package/dist/cjs/email.js +6 -0
  17. package/dist/cjs/email.production.js +1 -0
  18. package/dist/cjs/isURL.development.js +353 -0
  19. package/dist/cjs/isURL.js +6 -0
  20. package/dist/cjs/isURL.production.js +1 -0
  21. package/dist/cjs/n4s.development.js +205 -387
  22. package/dist/cjs/n4s.js +0 -1
  23. package/dist/cjs/n4s.production.js +1 -1
  24. package/dist/cjs/schema.development.js +19 -80
  25. package/dist/cjs/schema.js +0 -1
  26. package/dist/cjs/schema.production.js +1 -1
  27. package/dist/es/compose.development.js +14 -97
  28. package/dist/es/compose.production.js +1 -1
  29. package/dist/es/compounds.development.js +28 -84
  30. package/dist/es/compounds.production.js +1 -1
  31. package/dist/es/date.development.js +184 -0
  32. package/dist/es/date.production.js +1 -0
  33. package/dist/es/email.development.js +361 -0
  34. package/dist/es/email.production.js +1 -0
  35. package/dist/es/isURL.development.js +351 -0
  36. package/dist/es/isURL.production.js +1 -0
  37. package/dist/es/n4s.development.js +192 -372
  38. package/dist/es/n4s.production.js +1 -1
  39. package/dist/es/schema.development.js +16 -75
  40. package/dist/es/schema.production.js +1 -1
  41. package/dist/umd/compose.development.js +18 -102
  42. package/dist/umd/compose.production.js +1 -1
  43. package/dist/umd/compounds.development.js +34 -91
  44. package/dist/umd/compounds.production.js +1 -1
  45. package/dist/umd/date.development.js +190 -0
  46. package/dist/umd/date.production.js +1 -0
  47. package/dist/umd/email.development.js +367 -0
  48. package/dist/umd/email.production.js +1 -0
  49. package/dist/umd/isURL.development.js +357 -0
  50. package/dist/umd/isURL.production.js +1 -0
  51. package/dist/umd/n4s.development.js +209 -392
  52. package/dist/umd/n4s.production.js +1 -1
  53. package/dist/umd/schema.development.js +23 -85
  54. package/dist/umd/schema.production.js +1 -1
  55. package/email/package.json +9 -0
  56. package/isURL/package.json +9 -0
  57. package/package.json +162 -55
  58. package/schema/package.json +2 -0
  59. package/testUtils/TEnforceMock.ts +3 -0
  60. package/types/compose.d.ts +46 -55
  61. package/types/compose.d.ts.map +1 -0
  62. package/types/compounds.d.ts +50 -62
  63. package/types/compounds.d.ts.map +1 -0
  64. package/types/date.d.ts +18 -0
  65. package/types/date.d.ts.map +1 -0
  66. package/types/email.d.ts +12 -0
  67. package/types/email.d.ts.map +1 -0
  68. package/types/isURL.d.ts +12 -0
  69. package/types/isURL.d.ts.map +1 -0
  70. package/types/n4s.d.ts +62 -73
  71. package/types/n4s.d.ts.map +1 -0
  72. package/types/schema.d.ts +55 -63
  73. package/types/schema.d.ts.map +1 -0
  74. package/docs/.nojekyll +0 -0
  75. package/docs/README.md +0 -44
  76. package/docs/_sidebar.md +0 -5
  77. package/docs/external.md +0 -27
  78. package/docs/index.html +0 -32
  79. package/docs/rules.md +0 -1282
  80. package/tsconfig.json +0 -8
@@ -1,74 +1,48 @@
1
- import { createContext } from 'context';
1
+ import { assign, bindNot, isStringValue, numberEquals, greaterThan, isArray, isNumeric, isNullish, isBoolean, isEmpty, isNotArray, isNotEmpty, isNotNull, isNotNullish, isNotNumeric, isNotUndefined, isNull, isPositive, isUndefined, lengthEquals, lengthNotEquals, longerThan, numberNotEquals, defaultTo, optionalFunctionValue, invariant, StringObject, mapFirst } from 'vest-utils';
2
+ import { createCascade } from 'context';
2
3
 
3
- var assign = Object.assign;
4
-
5
- function isFunction(value) {
6
- return typeof value === 'function';
7
- }
8
-
9
- function bindNot(fn) {
10
- return function () {
11
- var args = [];
12
- for (var _i = 0; _i < arguments.length; _i++) {
13
- args[_i] = arguments[_i];
14
- }
15
- return !fn.apply(void 0, args);
4
+ const ctx = createCascade((ctxRef, parentContext) => {
5
+ const base = {
6
+ value: ctxRef.value,
7
+ meta: ctxRef.meta || {},
8
+ };
9
+ if (!parentContext) {
10
+ return assign(base, {
11
+ parent: emptyParent,
12
+ });
13
+ }
14
+ else if (ctxRef.set) {
15
+ return assign(base, {
16
+ parent: () => stripContext(parentContext),
17
+ });
18
+ }
19
+ return parentContext;
20
+ });
21
+ function stripContext(ctx) {
22
+ return {
23
+ value: ctx.value,
24
+ meta: ctx.meta,
25
+ parent: ctx.parent,
16
26
  };
17
27
  }
18
-
19
- function isNull(value) {
20
- return value === null;
21
- }
22
- var isNotNull = bindNot(isNull);
23
-
24
- function isUndefined(value) {
25
- return value === undefined;
26
- }
27
- var isNotUndefined = bindNot(isUndefined);
28
-
29
- function isNullish(value) {
30
- return isNull(value) || isUndefined(value);
31
- }
32
- var isNotNullish = bindNot(isNullish);
33
-
34
- function isStringValue(v) {
35
- return String(v) === v;
28
+ function emptyParent() {
29
+ return null;
36
30
  }
37
31
 
38
32
  function endsWith(value, arg1) {
39
33
  return isStringValue(value) && isStringValue(arg1) && value.endsWith(arg1);
40
34
  }
41
- var doesNotEndWith = bindNot(endsWith);
35
+ const doesNotEndWith = bindNot(endsWith);
42
36
 
43
37
  function equals(value, arg1) {
44
38
  return value === arg1;
45
39
  }
46
- var notEquals = bindNot(equals);
47
-
48
- function isNumeric(value) {
49
- var str = String(value);
50
- var num = Number(value);
51
- var result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num);
52
- return Boolean(result);
53
- }
54
- var isNotNumeric = bindNot(isNumeric);
55
-
56
- function greaterThan(value, gt) {
57
- return isNumeric(value) && isNumeric(gt) && Number(value) > Number(gt);
58
- }
40
+ const notEquals = bindNot(equals);
59
41
 
60
42
  function greaterThanOrEquals(value, gte) {
61
- return isNumeric(value) && isNumeric(gte) && Number(value) >= Number(gte);
43
+ return numberEquals(value, gte) || greaterThan(value, gte);
62
44
  }
63
45
 
64
- // The module is named "isArrayValue" since it
65
- // is conflicting with a nested npm dependency.
66
- // We may need to revisit this in the future.
67
- function isArray(value) {
68
- return Boolean(Array.isArray(value));
69
- }
70
- var isNotArray = bindNot(isArray);
71
-
72
46
  function inside(value, arg1) {
73
47
  if (isArray(arg1)) {
74
48
  return arg1.indexOf(value) !== -1;
@@ -79,112 +53,89 @@ function inside(value, arg1) {
79
53
  }
80
54
  return false;
81
55
  }
82
- var notInside = bindNot(inside);
56
+ const notInside = bindNot(inside);
57
+
58
+ function lessThan(value, lt) {
59
+ return isNumeric(value) && isNumeric(lt) && Number(value) < Number(lt);
60
+ }
83
61
 
84
62
  function lessThanOrEquals(value, lte) {
85
- return isNumeric(value) && isNumeric(lte) && Number(value) <= Number(lte);
63
+ return numberEquals(value, lte) || lessThan(value, lte);
86
64
  }
87
65
 
88
66
  function isBetween(value, min, max) {
89
67
  return greaterThanOrEquals(value, min) && lessThanOrEquals(value, max);
90
68
  }
91
- var isNotBetween = bindNot(isBetween);
69
+ const isNotBetween = bindNot(isBetween);
92
70
 
93
71
  function isBlank(value) {
94
72
  return isNullish(value) || (isStringValue(value) && !value.trim());
95
73
  }
96
- var isNotBlank = bindNot(isBlank);
97
-
98
- function isBoolean(value) {
99
- return !!value === value;
100
- }
101
-
102
- var isNotBoolean = bindNot(isBoolean);
103
-
104
- /**
105
- * A safe hasOwnProperty access
106
- */
107
- function hasOwnProperty(obj, key) {
108
- return Object.prototype.hasOwnProperty.call(obj, key);
109
- }
74
+ const isNotBlank = bindNot(isBlank);
110
75
 
111
- function isNumber(value) {
112
- return Boolean(typeof value === 'number');
113
- }
114
- var isNotNumber = bindNot(isNumber);
115
-
116
- function lengthEquals(value, arg1) {
117
- return value.length === Number(arg1);
118
- }
119
- var lengthNotEquals = bindNot(lengthEquals);
120
-
121
- function isEmpty(value) {
122
- if (!value) {
123
- return true;
124
- }
125
- else if (isNumber(value)) {
126
- return value === 0;
127
- }
128
- else if (hasOwnProperty(value, 'length')) {
129
- return lengthEquals(value, 0);
130
- }
131
- else if (typeof value === 'object') {
132
- return lengthEquals(Object.keys(value), 0);
133
- }
134
- return true;
135
- }
136
- var isNotEmpty = bindNot(isEmpty);
76
+ const isNotBoolean = bindNot(isBoolean);
137
77
 
138
78
  /**
139
79
  * Validates that a given value is an even number
140
80
  */
141
- var isEven = function (value) {
81
+ const isEven = (value) => {
142
82
  if (isNumeric(value)) {
143
83
  return value % 2 === 0;
144
84
  }
145
85
  return false;
146
86
  };
147
87
 
148
- function isNaN$1(value) {
88
+ function isKeyOf(key, obj) {
89
+ return key in obj;
90
+ }
91
+ const isNotKeyOf = bindNot(isKeyOf);
92
+
93
+ function isNaN(value) {
149
94
  return Number.isNaN(value);
150
95
  }
151
- var isNotNaN = bindNot(isNaN$1);
96
+ const isNotNaN = bindNot(isNaN);
152
97
 
153
98
  function isNegative(value) {
154
- if (isNumeric(value)) {
155
- return Number(value) < 0;
156
- }
157
- return false;
99
+ return lessThan(value, 0);
100
+ }
101
+
102
+ function isNumber(value) {
103
+ return Boolean(typeof value === 'number');
158
104
  }
159
- var isPositive = bindNot(isNegative);
105
+ const isNotNumber = bindNot(isNumber);
160
106
 
161
107
  /**
162
108
  * Validates that a given value is an odd number
163
109
  */
164
- var isOdd = function (value) {
110
+ const isOdd = (value) => {
165
111
  if (isNumeric(value)) {
166
112
  return value % 2 !== 0;
167
113
  }
168
114
  return false;
169
115
  };
170
116
 
171
- var isNotString = bindNot(isStringValue);
117
+ const isNotString = bindNot(isStringValue);
172
118
 
173
119
  function isTruthy(value) {
174
120
  return !!value;
175
121
  }
176
- var isFalsy = bindNot(isTruthy);
122
+ const isFalsy = bindNot(isTruthy);
177
123
 
178
- function lessThan(value, lt) {
179
- return isNumeric(value) && isNumeric(lt) && Number(value) < Number(lt);
180
- }
181
-
182
- function longerThan(value, arg1) {
183
- return value.length > Number(arg1);
124
+ function isValueOf(value, objectToCheck) {
125
+ if (isNullish(objectToCheck)) {
126
+ return false;
127
+ }
128
+ for (const key in objectToCheck) {
129
+ if (objectToCheck[key] === value) {
130
+ return true;
131
+ }
132
+ }
133
+ return false;
184
134
  }
135
+ const isNotValueOf = bindNot(isValueOf);
185
136
 
186
137
  function longerThanOrEquals(value, arg1) {
187
- return value.length >= Number(arg1);
138
+ return greaterThanOrEquals(value.length, arg1);
188
139
  }
189
140
 
190
141
  function matches(value, regex) {
@@ -194,16 +145,9 @@ function matches(value, regex) {
194
145
  else if (isStringValue(regex)) {
195
146
  return new RegExp(regex).test(value);
196
147
  }
197
- else {
198
- return false;
199
- }
200
- }
201
- var notMatches = bindNot(matches);
202
-
203
- function numberEquals(value, eq) {
204
- return isNumeric(value) && isNumeric(eq) && Number(value) === Number(eq);
148
+ return false;
205
149
  }
206
- var numberNotEquals = bindNot(numberEquals);
150
+ const notMatches = bindNot(matches);
207
151
 
208
152
  function condition(value, callback) {
209
153
  try {
@@ -215,182 +159,92 @@ function condition(value, callback) {
215
159
  }
216
160
 
217
161
  function shorterThan(value, arg1) {
218
- return value.length < Number(arg1);
162
+ return lessThan(value.length, arg1);
219
163
  }
220
164
 
221
165
  function shorterThanOrEquals(value, arg1) {
222
- return value.length <= Number(arg1);
166
+ return lessThanOrEquals(value.length, arg1);
223
167
  }
224
168
 
225
169
  function startsWith(value, arg1) {
226
170
  return isStringValue(value) && isStringValue(arg1) && value.startsWith(arg1);
227
171
  }
228
- var doesNotStartWith = bindNot(startsWith);
172
+ const doesNotStartWith = bindNot(startsWith);
229
173
 
230
174
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, max-lines-per-function
231
175
  function rules() {
232
176
  return {
233
- condition: condition,
234
- doesNotEndWith: doesNotEndWith,
235
- doesNotStartWith: doesNotStartWith,
236
- endsWith: endsWith,
237
- equals: equals,
238
- greaterThan: greaterThan,
239
- greaterThanOrEquals: greaterThanOrEquals,
177
+ condition,
178
+ doesNotEndWith,
179
+ doesNotStartWith,
180
+ endsWith,
181
+ equals,
182
+ greaterThan,
183
+ greaterThanOrEquals,
240
184
  gt: greaterThan,
241
185
  gte: greaterThanOrEquals,
242
- inside: inside,
243
- isArray: isArray,
244
- isBetween: isBetween,
245
- isBlank: isBlank,
246
- isBoolean: isBoolean,
247
- isEmpty: isEmpty,
248
- isEven: isEven,
249
- isFalsy: isFalsy,
250
- isNaN: isNaN$1,
251
- isNegative: isNegative,
252
- isNotArray: isNotArray,
253
- isNotBetween: isNotBetween,
254
- isNotBlank: isNotBlank,
255
- isNotBoolean: isNotBoolean,
256
- isNotEmpty: isNotEmpty,
257
- isNotNaN: isNotNaN,
258
- isNotNull: isNotNull,
259
- isNotNullish: isNotNullish,
260
- isNotNumber: isNotNumber,
261
- isNotNumeric: isNotNumeric,
262
- isNotString: isNotString,
263
- isNotUndefined: isNotUndefined,
264
- isNull: isNull,
265
- isNullish: isNullish,
266
- isNumber: isNumber,
267
- isNumeric: isNumeric,
268
- isOdd: isOdd,
269
- isPositive: isPositive,
186
+ inside,
187
+ isArray,
188
+ isBetween,
189
+ isBlank,
190
+ isBoolean,
191
+ isEmpty,
192
+ isEven,
193
+ isFalsy,
194
+ isKeyOf,
195
+ isNaN,
196
+ isNegative,
197
+ isNotArray,
198
+ isNotBetween,
199
+ isNotBlank,
200
+ isNotBoolean,
201
+ isNotEmpty,
202
+ isNotKeyOf,
203
+ isNotNaN,
204
+ isNotNull,
205
+ isNotNullish,
206
+ isNotNumber,
207
+ isNotNumeric,
208
+ isNotString,
209
+ isNotUndefined,
210
+ isNotValueOf,
211
+ isNull,
212
+ isNullish,
213
+ isNumber,
214
+ isNumeric,
215
+ isOdd,
216
+ isPositive,
270
217
  isString: isStringValue,
271
- isTruthy: isTruthy,
272
- isUndefined: isUndefined,
273
- lengthEquals: lengthEquals,
274
- lengthNotEquals: lengthNotEquals,
275
- lessThan: lessThan,
276
- lessThanOrEquals: lessThanOrEquals,
277
- longerThan: longerThan,
278
- longerThanOrEquals: longerThanOrEquals,
218
+ isTruthy,
219
+ isUndefined,
220
+ isValueOf,
221
+ lengthEquals,
222
+ lengthNotEquals,
223
+ lessThan,
224
+ lessThanOrEquals,
225
+ longerThan,
226
+ longerThanOrEquals,
279
227
  lt: lessThan,
280
228
  lte: lessThanOrEquals,
281
- matches: matches,
282
- notEquals: notEquals,
283
- notInside: notInside,
284
- notMatches: notMatches,
285
- numberEquals: numberEquals,
286
- numberNotEquals: numberNotEquals,
287
- shorterThan: shorterThan,
288
- shorterThanOrEquals: shorterThanOrEquals,
289
- startsWith: startsWith
229
+ matches,
230
+ notEquals,
231
+ notInside,
232
+ notMatches,
233
+ numberEquals,
234
+ numberNotEquals,
235
+ shorterThan,
236
+ shorterThanOrEquals,
237
+ startsWith,
290
238
  };
291
239
  }
292
240
 
293
- var baseRules = rules();
241
+ const baseRules = rules();
294
242
  function getRule(ruleName) {
295
243
  return baseRules[ruleName];
296
244
  }
297
245
 
298
- function eachEnforceRule(action) {
299
- for (var ruleName in baseRules) {
300
- var ruleFn = getRule(ruleName);
301
- if (isFunction(ruleFn)) {
302
- action(ruleName, ruleFn);
303
- }
304
- }
305
- }
306
-
307
- var ctx = createContext(function (ctxRef, parentContext) {
308
- var base = {
309
- value: ctxRef.value,
310
- meta: ctxRef.meta || {}
311
- };
312
- if (!parentContext) {
313
- return assign(base, {
314
- parent: emptyParent
315
- });
316
- }
317
- else if (ctxRef.set) {
318
- return assign(base, {
319
- parent: function () { return stripContext(parentContext); }
320
- });
321
- }
322
- return parentContext;
323
- });
324
- function stripContext(ctx) {
325
- if (!ctx) {
326
- return ctx;
327
- }
328
- return {
329
- value: ctx.value,
330
- meta: ctx.meta,
331
- parent: ctx.parent
332
- };
333
- }
334
- function emptyParent() {
335
- return null;
336
- }
337
-
338
- /*! *****************************************************************************
339
- Copyright (c) Microsoft Corporation.
340
-
341
- Permission to use, copy, modify, and/or distribute this software for any
342
- purpose with or without fee is hereby granted.
343
-
344
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
345
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
346
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
347
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
348
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
349
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
350
- PERFORMANCE OF THIS SOFTWARE.
351
- ***************************************************************************** */
352
-
353
- function __spreadArray(to, from, pack) {
354
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
355
- if (ar || !(i in from)) {
356
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
357
- ar[i] = from[i];
358
- }
359
- }
360
- return to.concat(ar || from);
361
- }
362
-
363
- function optionalFunctionValue(value) {
364
- var args = [];
365
- for (var _i = 1; _i < arguments.length; _i++) {
366
- args[_i - 1] = arguments[_i];
367
- }
368
- return isFunction(value) ? value.apply(void 0, args) : value;
369
- }
370
-
371
- function defaultTo(callback, defaultValue) {
372
- var _a;
373
- return (_a = optionalFunctionValue(callback)) !== null && _a !== void 0 ? _a : defaultValue;
374
- }
375
-
376
- /**
377
- * Throws a timed out error.
378
- */
379
- function throwError(devMessage, productionMessage) {
380
- throw new Error(devMessage );
381
- }
382
-
383
- function isProxySupported() {
384
- try {
385
- return isFunction(Proxy);
386
- }
387
- catch (_a) {
388
- return false;
389
- }
390
- }
391
-
392
246
  function ruleReturn(pass, message) {
393
- var output = { pass: pass };
247
+ const output = { pass };
394
248
  if (message) {
395
249
  output.message = message;
396
250
  }
@@ -406,133 +260,106 @@ function defaultToPassing(callback) {
406
260
  /**
407
261
  * Transform the result of a rule into a standard format
408
262
  */
409
- function transformResult(result, ruleName, value) {
410
- var args = [];
411
- for (var _i = 3; _i < arguments.length; _i++) {
412
- args[_i - 3] = arguments[_i];
413
- }
263
+ function transformResult(result, ruleName, value, ...args) {
414
264
  validateResult(result);
415
265
  // if result is boolean
416
266
  if (isBoolean(result)) {
417
267
  return ruleReturn(result);
418
268
  }
419
- else {
420
- return ruleReturn(result.pass, optionalFunctionValue.apply(void 0, __spreadArray([result.message, ruleName, value], args)));
421
- }
269
+ return ruleReturn(result.pass, optionalFunctionValue(result.message, ruleName, value, ...args));
422
270
  }
423
271
  function validateResult(result) {
424
272
  // if result is boolean, or if result.pass is boolean
425
- if (isBoolean(result) || (result && isBoolean(result.pass))) {
426
- return;
427
- }
428
- throwError('Incorrect return value for rule: ' + JSON.stringify(result));
273
+ invariant(isBoolean(result) || (result && isBoolean(result.pass)), 'Incorrect return value for rule: ' + JSON.stringify(result));
429
274
  }
430
275
 
276
+ // eslint-disable-next-line max-lines-per-function
431
277
  function enforceEager(value) {
432
- var target = {};
433
- if (!isProxySupported()) {
434
- eachEnforceRule(function (ruleName, ruleFn) {
435
- target[ruleName] = genRuleCall(target, ruleFn, ruleName);
436
- });
437
- return target;
438
- }
439
- var proxy = new Proxy(target, {
440
- get: function (_, ruleName) {
441
- var rule = getRule(ruleName);
278
+ const target = {
279
+ message,
280
+ };
281
+ let customMessage = undefined;
282
+ // We create a proxy intercepting access to the target object (which is empty).
283
+ const proxy = new Proxy(target, {
284
+ get: (_, key) => {
285
+ // On property access, we identify if it is a rule or not.
286
+ const rule = getRule(key);
287
+ // If it is a rule, we wrap it with `genRuleCall` that adds the base enforce behavior
442
288
  if (rule) {
443
- return genRuleCall(proxy, rule, ruleName);
289
+ return genRuleCall(proxy, rule, key);
444
290
  }
445
- }
291
+ return target[key];
292
+ },
446
293
  });
447
294
  return proxy;
295
+ // This function is used to wrap a rule with the base enforce behavior
296
+ // It takes the target object, the rule function, and the rule name
297
+ // It then returns the rule, in a manner that can be used by enforce
448
298
  function genRuleCall(target, rule, ruleName) {
449
- return function ruleCall() {
450
- var args = [];
451
- for (var _i = 0; _i < arguments.length; _i++) {
452
- args[_i] = arguments[_i];
453
- }
454
- var transformedResult = transformResult.apply(void 0, __spreadArray([ctx.run({ value: value }, function () { return rule.apply(void 0, __spreadArray([value], args)); }),
455
- ruleName,
456
- value], args));
457
- if (!transformedResult.pass) {
458
- if (isEmpty(transformedResult.message)) {
459
- throwError("enforce/" + ruleName + " failed with " + JSON.stringify(value));
460
- }
461
- else {
462
- // Explicitly throw a string so that vest.test can pick it up as the validation error message
463
- throw transformedResult.message;
299
+ return function ruleCall(...args) {
300
+ // Order of operation:
301
+ // 1. Create a context with the value being enforced
302
+ // 2. Call the rule within the context, and pass over the arguments passed to it
303
+ // 3. Transform the result to the correct output format
304
+ const transformedResult = ctx.run({ value }, () => {
305
+ return transformResult(rule(value, ...args), ruleName, value, ...args);
306
+ });
307
+ function enforceMessage() {
308
+ if (!isNullish(customMessage))
309
+ return StringObject(customMessage);
310
+ if (isNullish(transformedResult.message)) {
311
+ return `enforce/${ruleName} failed with ${JSON.stringify(value)}`;
464
312
  }
313
+ return StringObject(transformedResult.message);
465
314
  }
315
+ // On rule failure (the result is false), we either throw an error
316
+ // or throw a string value if the rule has a message defined in it.
317
+ invariant(transformedResult.pass, enforceMessage());
466
318
  return target;
467
319
  };
468
320
  }
469
- }
470
-
471
- function mapFirst(array, callback) {
472
- var broke = false;
473
- var breakoutValue = null;
474
- for (var i = 0; i < array.length; i++) {
475
- callback(array[i], breakout, i);
476
- if (broke) {
477
- return breakoutValue;
478
- }
479
- }
480
- function breakout(value) {
481
- broke = true;
482
- breakoutValue = value;
321
+ function message(input) {
322
+ customMessage = input;
323
+ return proxy;
483
324
  }
484
325
  }
485
326
 
486
327
  // eslint-disable-next-line max-lines-per-function
487
328
  function genEnforceLazy(key) {
488
- var registeredRules = [];
489
- var lazyMessage;
329
+ const registeredRules = [];
330
+ let lazyMessage;
490
331
  return addLazyRule(key);
491
332
  // eslint-disable-next-line max-lines-per-function
492
333
  function addLazyRule(ruleName) {
493
334
  // eslint-disable-next-line max-lines-per-function
494
- return function () {
495
- var args = [];
496
- for (var _i = 0; _i < arguments.length; _i++) {
497
- args[_i] = arguments[_i];
498
- }
499
- var rule = getRule(ruleName);
500
- registeredRules.push(function (value) {
501
- return transformResult.apply(void 0, __spreadArray([rule.apply(void 0, __spreadArray([value], args)), ruleName, value], args));
502
- });
503
- var proxy = {
504
- run: function (value) {
505
- return defaultToPassing(mapFirst(registeredRules, function (rule, breakout) {
335
+ return (...args) => {
336
+ const rule = getRule(ruleName);
337
+ registeredRules.push((value) => transformResult(rule(value, ...args), ruleName, value, ...args));
338
+ let proxy = {
339
+ run: (value) => {
340
+ return defaultToPassing(mapFirst(registeredRules, (rule, breakout) => {
506
341
  var _a;
507
- var res = ctx.run({ value: value }, function () { return rule(value); });
508
- if (!res.pass) {
509
- breakout(ruleReturn(!!res.pass, (_a = optionalFunctionValue(lazyMessage, value, res.message)) !== null && _a !== void 0 ? _a : res.message));
510
- }
342
+ const res = ctx.run({ value }, () => rule(value));
343
+ breakout(!res.pass, ruleReturn(!!res.pass, (_a = optionalFunctionValue(lazyMessage, value, res.message)) !== null && _a !== void 0 ? _a : res.message));
511
344
  }));
512
345
  },
513
- test: function (value) { return proxy.run(value).pass; },
514
- message: function (message) {
346
+ test: (value) => proxy.run(value).pass,
347
+ message: (message) => {
515
348
  if (message) {
516
349
  lazyMessage = message;
517
350
  }
518
351
  return proxy;
519
- }
352
+ },
520
353
  };
521
- if (!isProxySupported()) {
522
- eachEnforceRule(function (ruleName) {
523
- proxy[ruleName] = addLazyRule(ruleName);
524
- });
525
- return proxy;
526
- }
527
354
  // reassigning the proxy here is not pretty
528
355
  // but it's a cleaner way of getting `run` and `test` for free
529
356
  proxy = new Proxy(proxy, {
530
- get: function (target, key) {
357
+ get: (target, key) => {
531
358
  if (getRule(key)) {
532
359
  return addLazyRule(key);
533
360
  }
534
361
  return target[key]; // already has `run` and `test` on it
535
- }
362
+ },
536
363
  });
537
364
  return proxy;
538
365
  };
@@ -566,21 +393,14 @@ function genEnforceLazy(key) {
566
393
  * while run will return an object with the validation result and an optional message created by the rule.
567
394
  */
568
395
  function genEnforce() {
569
- var target = {
570
- context: function () { return ctx.useX(); },
571
- extend: function (customRules) {
396
+ const target = {
397
+ context: () => ctx.useX(),
398
+ extend: (customRules) => {
572
399
  assign(baseRules, customRules);
573
- }
400
+ },
574
401
  };
575
- if (!isProxySupported()) {
576
- eachEnforceRule(function (ruleName) {
577
- // Only on the first rule access - start the chain of calls
578
- target[ruleName] = genEnforceLazy(ruleName);
579
- });
580
- return target;
581
- }
582
402
  return new Proxy(assign(enforceEager, target), {
583
- get: function (target, key) {
403
+ get: (target, key) => {
584
404
  if (key in target) {
585
405
  return target[key];
586
406
  }
@@ -589,9 +409,9 @@ function genEnforce() {
589
409
  }
590
410
  // Only on the first rule access - start the chain of calls
591
411
  return genEnforceLazy(key);
592
- }
412
+ },
593
413
  });
594
414
  }
595
- var enforce = genEnforce();
415
+ const enforce = genEnforce();
596
416
 
597
417
  export { ctx, enforce };