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