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,156 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('context')) :
3
- typeof define === 'function' && define.amd ? define(['exports', 'context'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.n4s = {}, global.context));
5
- }(this, (function (exports, context) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('vest-utils'), require('context')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'vest-utils', 'context'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.n4s = {}, global["vest-utils"], global.context));
5
+ })(this, (function (exports, vestUtils, context) { 'use strict';
6
6
 
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);
7
+ const ctx = context.createCascade((ctxRef, parentContext) => {
8
+ const base = {
9
+ value: ctxRef.value,
10
+ meta: ctxRef.meta || {},
11
+ };
12
+ if (!parentContext) {
13
+ return vestUtils.assign(base, {
14
+ parent: emptyParent,
15
+ });
16
+ }
17
+ else if (ctxRef.set) {
18
+ return vestUtils.assign(base, {
19
+ parent: () => stripContext(parentContext),
20
+ });
21
+ }
22
+ return parentContext;
23
+ });
24
+ function stripContext(ctx) {
25
+ return {
26
+ value: ctx.value,
27
+ meta: ctx.meta,
28
+ parent: ctx.parent,
20
29
  };
21
30
  }
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;
31
+ function emptyParent() {
32
+ return null;
40
33
  }
41
34
 
42
35
  function endsWith(value, arg1) {
43
- return isStringValue(value) && isStringValue(arg1) && value.endsWith(arg1);
36
+ return vestUtils.isStringValue(value) && vestUtils.isStringValue(arg1) && value.endsWith(arg1);
44
37
  }
45
- var doesNotEndWith = bindNot(endsWith);
38
+ const doesNotEndWith = vestUtils.bindNot(endsWith);
46
39
 
47
40
  function equals(value, arg1) {
48
41
  return value === arg1;
49
42
  }
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
- }
43
+ const notEquals = vestUtils.bindNot(equals);
63
44
 
64
45
  function greaterThanOrEquals(value, gte) {
65
- return isNumeric(value) && isNumeric(gte) && Number(value) >= Number(gte);
46
+ return vestUtils.numberEquals(value, gte) || vestUtils.greaterThan(value, gte);
66
47
  }
67
48
 
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
49
  function inside(value, arg1) {
77
- if (isArray(arg1)) {
50
+ if (vestUtils.isArray(arg1)) {
78
51
  return arg1.indexOf(value) !== -1;
79
52
  }
80
53
  // both value and arg1 are strings
81
- if (isStringValue(arg1) && isStringValue(value)) {
54
+ if (vestUtils.isStringValue(arg1) && vestUtils.isStringValue(value)) {
82
55
  return arg1.indexOf(value) !== -1;
83
56
  }
84
57
  return false;
85
58
  }
86
- var notInside = bindNot(inside);
59
+ const notInside = vestUtils.bindNot(inside);
60
+
61
+ function lessThan(value, lt) {
62
+ return vestUtils.isNumeric(value) && vestUtils.isNumeric(lt) && Number(value) < Number(lt);
63
+ }
87
64
 
88
65
  function lessThanOrEquals(value, lte) {
89
- return isNumeric(value) && isNumeric(lte) && Number(value) <= Number(lte);
66
+ return vestUtils.numberEquals(value, lte) || lessThan(value, lte);
90
67
  }
91
68
 
92
69
  function isBetween(value, min, max) {
93
70
  return greaterThanOrEquals(value, min) && lessThanOrEquals(value, max);
94
71
  }
95
- var isNotBetween = bindNot(isBetween);
72
+ const isNotBetween = vestUtils.bindNot(isBetween);
96
73
 
97
74
  function isBlank(value) {
98
- return isNullish(value) || (isStringValue(value) && !value.trim());
75
+ return vestUtils.isNullish(value) || (vestUtils.isStringValue(value) && !value.trim());
99
76
  }
100
- var isNotBlank = bindNot(isBlank);
77
+ const isNotBlank = vestUtils.bindNot(isBlank);
101
78
 
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);
122
- }
123
- var lengthNotEquals = bindNot(lengthEquals);
124
-
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);
79
+ const isNotBoolean = vestUtils.bindNot(vestUtils.isBoolean);
141
80
 
142
81
  /**
143
82
  * Validates that a given value is an even number
144
83
  */
145
- var isEven = function (value) {
146
- if (isNumeric(value)) {
84
+ const isEven = (value) => {
85
+ if (vestUtils.isNumeric(value)) {
147
86
  return value % 2 === 0;
148
87
  }
149
88
  return false;
150
89
  };
151
90
 
152
- function isNaN$1(value) {
91
+ function isKeyOf(key, obj) {
92
+ return key in obj;
93
+ }
94
+ const isNotKeyOf = vestUtils.bindNot(isKeyOf);
95
+
96
+ function isNaN(value) {
153
97
  return Number.isNaN(value);
154
98
  }
155
- var isNotNaN = bindNot(isNaN$1);
99
+ const isNotNaN = vestUtils.bindNot(isNaN);
156
100
 
157
101
  function isNegative(value) {
158
- if (isNumeric(value)) {
159
- return Number(value) < 0;
160
- }
161
- return false;
102
+ return lessThan(value, 0);
162
103
  }
163
- var isPositive = bindNot(isNegative);
104
+
105
+ function isNumber(value) {
106
+ return Boolean(typeof value === 'number');
107
+ }
108
+ const isNotNumber = vestUtils.bindNot(isNumber);
164
109
 
165
110
  /**
166
111
  * Validates that a given value is an odd number
167
112
  */
168
- var isOdd = function (value) {
169
- if (isNumeric(value)) {
113
+ const isOdd = (value) => {
114
+ if (vestUtils.isNumeric(value)) {
170
115
  return value % 2 !== 0;
171
116
  }
172
117
  return false;
173
118
  };
174
119
 
175
- var isNotString = bindNot(isStringValue);
120
+ const isNotString = vestUtils.bindNot(vestUtils.isStringValue);
176
121
 
177
122
  function isTruthy(value) {
178
123
  return !!value;
179
124
  }
180
- var isFalsy = bindNot(isTruthy);
181
-
182
- function lessThan(value, lt) {
183
- return isNumeric(value) && isNumeric(lt) && Number(value) < Number(lt);
184
- }
125
+ const isFalsy = vestUtils.bindNot(isTruthy);
185
126
 
186
- function longerThan(value, arg1) {
187
- return value.length > Number(arg1);
127
+ function isValueOf(value, objectToCheck) {
128
+ if (vestUtils.isNullish(objectToCheck)) {
129
+ return false;
130
+ }
131
+ for (const key in objectToCheck) {
132
+ if (objectToCheck[key] === value) {
133
+ return true;
134
+ }
135
+ }
136
+ return false;
188
137
  }
138
+ const isNotValueOf = vestUtils.bindNot(isValueOf);
189
139
 
190
140
  function longerThanOrEquals(value, arg1) {
191
- return value.length >= Number(arg1);
141
+ return greaterThanOrEquals(value.length, arg1);
192
142
  }
193
143
 
194
144
  function matches(value, regex) {
195
145
  if (regex instanceof RegExp) {
196
146
  return regex.test(value);
197
147
  }
198
- else if (isStringValue(regex)) {
148
+ else if (vestUtils.isStringValue(regex)) {
199
149
  return new RegExp(regex).test(value);
200
150
  }
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);
151
+ return false;
209
152
  }
210
- var numberNotEquals = bindNot(numberEquals);
153
+ const notMatches = vestUtils.bindNot(matches);
211
154
 
212
155
  function condition(value, callback) {
213
156
  try {
@@ -219,182 +162,92 @@
219
162
  }
220
163
 
221
164
  function shorterThan(value, arg1) {
222
- return value.length < Number(arg1);
165
+ return lessThan(value.length, arg1);
223
166
  }
224
167
 
225
168
  function shorterThanOrEquals(value, arg1) {
226
- return value.length <= Number(arg1);
169
+ return lessThanOrEquals(value.length, arg1);
227
170
  }
228
171
 
229
172
  function startsWith(value, arg1) {
230
- return isStringValue(value) && isStringValue(arg1) && value.startsWith(arg1);
173
+ return vestUtils.isStringValue(value) && vestUtils.isStringValue(arg1) && value.startsWith(arg1);
231
174
  }
232
- var doesNotStartWith = bindNot(startsWith);
175
+ const doesNotStartWith = vestUtils.bindNot(startsWith);
233
176
 
234
177
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, max-lines-per-function
235
178
  function rules() {
236
179
  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,
180
+ condition,
181
+ doesNotEndWith,
182
+ doesNotStartWith,
183
+ endsWith,
184
+ equals,
185
+ greaterThan: vestUtils.greaterThan,
186
+ greaterThanOrEquals,
187
+ gt: vestUtils.greaterThan,
245
188
  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,
189
+ inside,
190
+ isArray: vestUtils.isArray,
191
+ isBetween,
192
+ isBlank,
193
+ isBoolean: vestUtils.isBoolean,
194
+ isEmpty: vestUtils.isEmpty,
195
+ isEven,
196
+ isFalsy,
197
+ isKeyOf,
198
+ isNaN,
199
+ isNegative,
200
+ isNotArray: vestUtils.isNotArray,
201
+ isNotBetween,
202
+ isNotBlank,
203
+ isNotBoolean,
204
+ isNotEmpty: vestUtils.isNotEmpty,
205
+ isNotKeyOf,
206
+ isNotNaN,
207
+ isNotNull: vestUtils.isNotNull,
208
+ isNotNullish: vestUtils.isNotNullish,
209
+ isNotNumber,
210
+ isNotNumeric: vestUtils.isNotNumeric,
211
+ isNotString,
212
+ isNotUndefined: vestUtils.isNotUndefined,
213
+ isNotValueOf,
214
+ isNull: vestUtils.isNull,
215
+ isNullish: vestUtils.isNullish,
216
+ isNumber,
217
+ isNumeric: vestUtils.isNumeric,
218
+ isOdd,
219
+ isPositive: vestUtils.isPositive,
220
+ isString: vestUtils.isStringValue,
221
+ isTruthy,
222
+ isUndefined: vestUtils.isUndefined,
223
+ isValueOf,
224
+ lengthEquals: vestUtils.lengthEquals,
225
+ lengthNotEquals: vestUtils.lengthNotEquals,
226
+ lessThan,
227
+ lessThanOrEquals,
228
+ longerThan: vestUtils.longerThan,
229
+ longerThanOrEquals,
283
230
  lt: lessThan,
284
231
  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
232
+ matches,
233
+ notEquals,
234
+ notInside,
235
+ notMatches,
236
+ numberEquals: vestUtils.numberEquals,
237
+ numberNotEquals: vestUtils.numberNotEquals,
238
+ shorterThan,
239
+ shorterThanOrEquals,
240
+ startsWith,
294
241
  };
295
242
  }
296
243
 
297
- var baseRules = rules();
244
+ const baseRules = rules();
298
245
  function getRule(ruleName) {
299
246
  return baseRules[ruleName];
300
247
  }
301
248
 
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
249
  function ruleReturn(pass, message) {
397
- var output = { pass: pass };
250
+ const output = { pass };
398
251
  if (message) {
399
252
  output.message = message;
400
253
  }
@@ -404,139 +257,112 @@
404
257
  return ruleReturn(true);
405
258
  }
406
259
  function defaultToPassing(callback) {
407
- return defaultTo(callback, passing());
260
+ return vestUtils.defaultTo(callback, passing());
408
261
  }
409
262
 
410
263
  /**
411
264
  * Transform the result of a rule into a standard format
412
265
  */
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
- }
266
+ function transformResult(result, ruleName, value, ...args) {
418
267
  validateResult(result);
419
268
  // if result is boolean
420
- if (isBoolean(result)) {
269
+ if (vestUtils.isBoolean(result)) {
421
270
  return ruleReturn(result);
422
271
  }
423
- else {
424
- return ruleReturn(result.pass, optionalFunctionValue.apply(void 0, __spreadArray([result.message, ruleName, value], args)));
425
- }
272
+ return ruleReturn(result.pass, vestUtils.optionalFunctionValue(result.message, ruleName, value, ...args));
426
273
  }
427
274
  function validateResult(result) {
428
275
  // 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));
276
+ vestUtils.invariant(vestUtils.isBoolean(result) || (result && vestUtils.isBoolean(result.pass)), 'Incorrect return value for rule: ' + JSON.stringify(result));
433
277
  }
434
278
 
279
+ // eslint-disable-next-line max-lines-per-function
435
280
  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);
281
+ const target = {
282
+ message,
283
+ };
284
+ let customMessage = undefined;
285
+ // We create a proxy intercepting access to the target object (which is empty).
286
+ const proxy = new Proxy(target, {
287
+ get: (_, key) => {
288
+ // On property access, we identify if it is a rule or not.
289
+ const rule = getRule(key);
290
+ // If it is a rule, we wrap it with `genRuleCall` that adds the base enforce behavior
446
291
  if (rule) {
447
- return genRuleCall(proxy, rule, ruleName);
292
+ return genRuleCall(proxy, rule, key);
448
293
  }
449
- }
294
+ return target[key];
295
+ },
450
296
  });
451
297
  return proxy;
298
+ // This function is used to wrap a rule with the base enforce behavior
299
+ // It takes the target object, the rule function, and the rule name
300
+ // It then returns the rule, in a manner that can be used by enforce
452
301
  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;
302
+ return function ruleCall(...args) {
303
+ // Order of operation:
304
+ // 1. Create a context with the value being enforced
305
+ // 2. Call the rule within the context, and pass over the arguments passed to it
306
+ // 3. Transform the result to the correct output format
307
+ const transformedResult = ctx.run({ value }, () => {
308
+ return transformResult(rule(value, ...args), ruleName, value, ...args);
309
+ });
310
+ function enforceMessage() {
311
+ if (!vestUtils.isNullish(customMessage))
312
+ return vestUtils.StringObject(customMessage);
313
+ if (vestUtils.isNullish(transformedResult.message)) {
314
+ return `enforce/${ruleName} failed with ${JSON.stringify(value)}`;
468
315
  }
316
+ return vestUtils.StringObject(transformedResult.message);
469
317
  }
318
+ // On rule failure (the result is false), we either throw an error
319
+ // or throw a string value if the rule has a message defined in it.
320
+ vestUtils.invariant(transformedResult.pass, enforceMessage());
470
321
  return target;
471
322
  };
472
323
  }
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;
324
+ function message(input) {
325
+ customMessage = input;
326
+ return proxy;
487
327
  }
488
328
  }
489
329
 
490
330
  // eslint-disable-next-line max-lines-per-function
491
331
  function genEnforceLazy(key) {
492
- var registeredRules = [];
493
- var lazyMessage;
332
+ const registeredRules = [];
333
+ let lazyMessage;
494
334
  return addLazyRule(key);
495
335
  // eslint-disable-next-line max-lines-per-function
496
336
  function addLazyRule(ruleName) {
497
337
  // 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) {
338
+ return (...args) => {
339
+ const rule = getRule(ruleName);
340
+ registeredRules.push((value) => transformResult(rule(value, ...args), ruleName, value, ...args));
341
+ let proxy = {
342
+ run: (value) => {
343
+ return defaultToPassing(vestUtils.mapFirst(registeredRules, (rule, breakout) => {
510
344
  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
- }
345
+ const res = ctx.run({ value }, () => rule(value));
346
+ breakout(!res.pass, ruleReturn(!!res.pass, (_a = vestUtils.optionalFunctionValue(lazyMessage, value, res.message)) !== null && _a !== void 0 ? _a : res.message));
515
347
  }));
516
348
  },
517
- test: function (value) { return proxy.run(value).pass; },
518
- message: function (message) {
349
+ test: (value) => proxy.run(value).pass,
350
+ message: (message) => {
519
351
  if (message) {
520
352
  lazyMessage = message;
521
353
  }
522
354
  return proxy;
523
- }
355
+ },
524
356
  };
525
- if (!isProxySupported()) {
526
- eachEnforceRule(function (ruleName) {
527
- proxy[ruleName] = addLazyRule(ruleName);
528
- });
529
- return proxy;
530
- }
531
357
  // reassigning the proxy here is not pretty
532
358
  // but it's a cleaner way of getting `run` and `test` for free
533
359
  proxy = new Proxy(proxy, {
534
- get: function (target, key) {
360
+ get: (target, key) => {
535
361
  if (getRule(key)) {
536
362
  return addLazyRule(key);
537
363
  }
538
364
  return target[key]; // already has `run` and `test` on it
539
- }
365
+ },
540
366
  });
541
367
  return proxy;
542
368
  };
@@ -570,21 +396,14 @@
570
396
  * while run will return an object with the validation result and an optional message created by the rule.
571
397
  */
572
398
  function genEnforce() {
573
- var target = {
574
- context: function () { return ctx.useX(); },
575
- extend: function (customRules) {
576
- assign(baseRules, customRules);
577
- }
399
+ const target = {
400
+ context: () => ctx.useX(),
401
+ extend: (customRules) => {
402
+ vestUtils.assign(baseRules, customRules);
403
+ },
578
404
  };
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) {
405
+ return new Proxy(vestUtils.assign(enforceEager, target), {
406
+ get: (target, key) => {
588
407
  if (key in target) {
589
408
  return target[key];
590
409
  }
@@ -593,14 +412,12 @@
593
412
  }
594
413
  // Only on the first rule access - start the chain of calls
595
414
  return genEnforceLazy(key);
596
- }
415
+ },
597
416
  });
598
417
  }
599
- var enforce = genEnforce();
418
+ const enforce = genEnforce();
600
419
 
601
420
  exports.ctx = ctx;
602
421
  exports.enforce = enforce;
603
422
 
604
- Object.defineProperty(exports, '__esModule', { value: true });
605
-
606
- })));
423
+ }));