n4s 5.0.0 → 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 (70) hide show
  1. package/README.md +2 -0
  2. package/date/package.json +9 -0
  3. package/dist/cjs/compose.development.js +9 -13
  4. package/dist/cjs/compose.js +0 -1
  5. package/dist/cjs/compose.production.js +1 -1
  6. package/dist/cjs/compounds.development.js +16 -32
  7. package/dist/cjs/compounds.js +0 -1
  8. package/dist/cjs/compounds.production.js +1 -1
  9. package/dist/cjs/date.development.js +186 -0
  10. package/dist/cjs/date.js +6 -0
  11. package/dist/cjs/date.production.js +1 -0
  12. package/dist/cjs/email.development.js +363 -0
  13. package/dist/cjs/email.js +6 -0
  14. package/dist/cjs/email.production.js +1 -0
  15. package/dist/cjs/isURL.development.js +353 -0
  16. package/dist/cjs/isURL.js +6 -0
  17. package/dist/cjs/isURL.production.js +1 -0
  18. package/dist/cjs/n4s.development.js +134 -213
  19. package/dist/cjs/n4s.js +0 -1
  20. package/dist/cjs/n4s.production.js +1 -1
  21. package/dist/cjs/schema.development.js +13 -22
  22. package/dist/cjs/schema.js +0 -1
  23. package/dist/cjs/schema.production.js +1 -1
  24. package/dist/es/compose.development.js +11 -15
  25. package/dist/es/compose.production.js +1 -1
  26. package/dist/es/compounds.development.js +16 -32
  27. package/dist/es/compounds.production.js +1 -1
  28. package/dist/es/date.development.js +184 -0
  29. package/dist/es/date.production.js +1 -0
  30. package/dist/es/email.development.js +361 -0
  31. package/dist/es/email.production.js +1 -0
  32. package/dist/es/isURL.development.js +351 -0
  33. package/dist/es/isURL.production.js +1 -0
  34. package/dist/es/n4s.development.js +155 -232
  35. package/dist/es/n4s.production.js +1 -1
  36. package/dist/es/schema.development.js +13 -20
  37. package/dist/es/schema.production.js +1 -1
  38. package/dist/umd/compose.development.js +12 -16
  39. package/dist/umd/compose.production.js +1 -1
  40. package/dist/umd/compounds.development.js +19 -35
  41. package/dist/umd/compounds.production.js +1 -1
  42. package/dist/umd/date.development.js +190 -0
  43. package/dist/umd/date.production.js +1 -0
  44. package/dist/umd/email.development.js +367 -0
  45. package/dist/umd/email.production.js +1 -0
  46. package/dist/umd/isURL.development.js +357 -0
  47. package/dist/umd/isURL.production.js +1 -0
  48. package/dist/umd/n4s.development.js +137 -216
  49. package/dist/umd/n4s.production.js +1 -1
  50. package/dist/umd/schema.development.js +16 -25
  51. package/dist/umd/schema.production.js +1 -1
  52. package/email/package.json +9 -0
  53. package/isURL/package.json +9 -0
  54. package/package.json +159 -55
  55. package/testUtils/TEnforceMock.ts +3 -0
  56. package/types/compose.d.ts +22 -23
  57. package/types/compose.d.ts.map +1 -0
  58. package/types/compounds.d.ts +22 -24
  59. package/types/compounds.d.ts.map +1 -0
  60. package/types/date.d.ts +18 -0
  61. package/types/date.d.ts.map +1 -0
  62. package/types/email.d.ts +12 -0
  63. package/types/email.d.ts.map +1 -0
  64. package/types/isURL.d.ts +12 -0
  65. package/types/isURL.d.ts.map +1 -0
  66. package/types/n4s.d.ts +30 -27
  67. package/types/n4s.d.ts.map +1 -0
  68. package/types/schema.d.ts +22 -23
  69. package/types/schema.d.ts.map +1 -0
  70. package/tsconfig.json +0 -8
@@ -1,19 +1,45 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
3
  var vestUtils = require('vest-utils');
6
4
  var context = require('context');
7
5
 
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,
28
+ };
29
+ }
30
+ function emptyParent() {
31
+ return null;
32
+ }
33
+
8
34
  function endsWith(value, arg1) {
9
35
  return vestUtils.isStringValue(value) && vestUtils.isStringValue(arg1) && value.endsWith(arg1);
10
36
  }
11
- var doesNotEndWith = vestUtils.bindNot(endsWith);
37
+ const doesNotEndWith = vestUtils.bindNot(endsWith);
12
38
 
13
39
  function equals(value, arg1) {
14
40
  return value === arg1;
15
41
  }
16
- var notEquals = vestUtils.bindNot(equals);
42
+ const notEquals = vestUtils.bindNot(equals);
17
43
 
18
44
  function greaterThanOrEquals(value, gte) {
19
45
  return vestUtils.numberEquals(value, gte) || vestUtils.greaterThan(value, gte);
@@ -29,7 +55,7 @@ function inside(value, arg1) {
29
55
  }
30
56
  return false;
31
57
  }
32
- var notInside = vestUtils.bindNot(inside);
58
+ const notInside = vestUtils.bindNot(inside);
33
59
 
34
60
  function lessThan(value, lt) {
35
61
  return vestUtils.isNumeric(value) && vestUtils.isNumeric(lt) && Number(value) < Number(lt);
@@ -42,19 +68,19 @@ function lessThanOrEquals(value, lte) {
42
68
  function isBetween(value, min, max) {
43
69
  return greaterThanOrEquals(value, min) && lessThanOrEquals(value, max);
44
70
  }
45
- var isNotBetween = vestUtils.bindNot(isBetween);
71
+ const isNotBetween = vestUtils.bindNot(isBetween);
46
72
 
47
73
  function isBlank(value) {
48
74
  return vestUtils.isNullish(value) || (vestUtils.isStringValue(value) && !value.trim());
49
75
  }
50
- var isNotBlank = vestUtils.bindNot(isBlank);
76
+ const isNotBlank = vestUtils.bindNot(isBlank);
51
77
 
52
- var isNotBoolean = vestUtils.bindNot(vestUtils.isBoolean);
78
+ const isNotBoolean = vestUtils.bindNot(vestUtils.isBoolean);
53
79
 
54
80
  /**
55
81
  * Validates that a given value is an even number
56
82
  */
57
- var isEven = function (value) {
83
+ const isEven = (value) => {
58
84
  if (vestUtils.isNumeric(value)) {
59
85
  return value % 2 === 0;
60
86
  }
@@ -64,12 +90,12 @@ var isEven = function (value) {
64
90
  function isKeyOf(key, obj) {
65
91
  return key in obj;
66
92
  }
67
- var isNotKeyOf = vestUtils.bindNot(isKeyOf);
93
+ const isNotKeyOf = vestUtils.bindNot(isKeyOf);
68
94
 
69
95
  function isNaN(value) {
70
96
  return Number.isNaN(value);
71
97
  }
72
- var isNotNaN = vestUtils.bindNot(isNaN);
98
+ const isNotNaN = vestUtils.bindNot(isNaN);
73
99
 
74
100
  function isNegative(value) {
75
101
  return lessThan(value, 0);
@@ -78,37 +104,37 @@ function isNegative(value) {
78
104
  function isNumber(value) {
79
105
  return Boolean(typeof value === 'number');
80
106
  }
81
- var isNotNumber = vestUtils.bindNot(isNumber);
107
+ const isNotNumber = vestUtils.bindNot(isNumber);
82
108
 
83
109
  /**
84
110
  * Validates that a given value is an odd number
85
111
  */
86
- var isOdd = function (value) {
112
+ const isOdd = (value) => {
87
113
  if (vestUtils.isNumeric(value)) {
88
114
  return value % 2 !== 0;
89
115
  }
90
116
  return false;
91
117
  };
92
118
 
93
- var isNotString = vestUtils.bindNot(vestUtils.isStringValue);
119
+ const isNotString = vestUtils.bindNot(vestUtils.isStringValue);
94
120
 
95
121
  function isTruthy(value) {
96
122
  return !!value;
97
123
  }
98
- var isFalsy = vestUtils.bindNot(isTruthy);
124
+ const isFalsy = vestUtils.bindNot(isTruthy);
99
125
 
100
126
  function isValueOf(value, objectToCheck) {
101
127
  if (vestUtils.isNullish(objectToCheck)) {
102
128
  return false;
103
129
  }
104
- for (var key in objectToCheck) {
130
+ for (const key in objectToCheck) {
105
131
  if (objectToCheck[key] === value) {
106
132
  return true;
107
133
  }
108
134
  }
109
135
  return false;
110
136
  }
111
- var isNotValueOf = vestUtils.bindNot(isValueOf);
137
+ const isNotValueOf = vestUtils.bindNot(isValueOf);
112
138
 
113
139
  function longerThanOrEquals(value, arg1) {
114
140
  return greaterThanOrEquals(value.length, arg1);
@@ -121,11 +147,9 @@ function matches(value, regex) {
121
147
  else if (vestUtils.isStringValue(regex)) {
122
148
  return new RegExp(regex).test(value);
123
149
  }
124
- else {
125
- return false;
126
- }
150
+ return false;
127
151
  }
128
- var notMatches = vestUtils.bindNot(matches);
152
+ const notMatches = vestUtils.bindNot(matches);
129
153
 
130
154
  function condition(value, callback) {
131
155
  try {
@@ -147,156 +171,82 @@ function shorterThanOrEquals(value, arg1) {
147
171
  function startsWith(value, arg1) {
148
172
  return vestUtils.isStringValue(value) && vestUtils.isStringValue(arg1) && value.startsWith(arg1);
149
173
  }
150
- var doesNotStartWith = vestUtils.bindNot(startsWith);
174
+ const doesNotStartWith = vestUtils.bindNot(startsWith);
151
175
 
152
176
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, max-lines-per-function
153
177
  function rules() {
154
178
  return {
155
- condition: condition,
156
- doesNotEndWith: doesNotEndWith,
157
- doesNotStartWith: doesNotStartWith,
158
- endsWith: endsWith,
159
- equals: equals,
179
+ condition,
180
+ doesNotEndWith,
181
+ doesNotStartWith,
182
+ endsWith,
183
+ equals,
160
184
  greaterThan: vestUtils.greaterThan,
161
- greaterThanOrEquals: greaterThanOrEquals,
185
+ greaterThanOrEquals,
162
186
  gt: vestUtils.greaterThan,
163
187
  gte: greaterThanOrEquals,
164
- inside: inside,
188
+ inside,
165
189
  isArray: vestUtils.isArray,
166
- isBetween: isBetween,
167
- isBlank: isBlank,
190
+ isBetween,
191
+ isBlank,
168
192
  isBoolean: vestUtils.isBoolean,
169
193
  isEmpty: vestUtils.isEmpty,
170
- isEven: isEven,
171
- isFalsy: isFalsy,
172
- isKeyOf: isKeyOf,
173
- isNaN: isNaN,
174
- isNegative: isNegative,
194
+ isEven,
195
+ isFalsy,
196
+ isKeyOf,
197
+ isNaN,
198
+ isNegative,
175
199
  isNotArray: vestUtils.isNotArray,
176
- isNotBetween: isNotBetween,
177
- isNotBlank: isNotBlank,
178
- isNotBoolean: isNotBoolean,
200
+ isNotBetween,
201
+ isNotBlank,
202
+ isNotBoolean,
179
203
  isNotEmpty: vestUtils.isNotEmpty,
180
- isNotKeyOf: isNotKeyOf,
181
- isNotNaN: isNotNaN,
204
+ isNotKeyOf,
205
+ isNotNaN,
182
206
  isNotNull: vestUtils.isNotNull,
183
207
  isNotNullish: vestUtils.isNotNullish,
184
- isNotNumber: isNotNumber,
208
+ isNotNumber,
185
209
  isNotNumeric: vestUtils.isNotNumeric,
186
- isNotString: isNotString,
210
+ isNotString,
187
211
  isNotUndefined: vestUtils.isNotUndefined,
188
- isNotValueOf: isNotValueOf,
212
+ isNotValueOf,
189
213
  isNull: vestUtils.isNull,
190
214
  isNullish: vestUtils.isNullish,
191
- isNumber: isNumber,
215
+ isNumber,
192
216
  isNumeric: vestUtils.isNumeric,
193
- isOdd: isOdd,
217
+ isOdd,
194
218
  isPositive: vestUtils.isPositive,
195
219
  isString: vestUtils.isStringValue,
196
- isTruthy: isTruthy,
220
+ isTruthy,
197
221
  isUndefined: vestUtils.isUndefined,
198
- isValueOf: isValueOf,
222
+ isValueOf,
199
223
  lengthEquals: vestUtils.lengthEquals,
200
224
  lengthNotEquals: vestUtils.lengthNotEquals,
201
- lessThan: lessThan,
202
- lessThanOrEquals: lessThanOrEquals,
225
+ lessThan,
226
+ lessThanOrEquals,
203
227
  longerThan: vestUtils.longerThan,
204
- longerThanOrEquals: longerThanOrEquals,
228
+ longerThanOrEquals,
205
229
  lt: lessThan,
206
230
  lte: lessThanOrEquals,
207
- matches: matches,
208
- notEquals: notEquals,
209
- notInside: notInside,
210
- notMatches: notMatches,
231
+ matches,
232
+ notEquals,
233
+ notInside,
234
+ notMatches,
211
235
  numberEquals: vestUtils.numberEquals,
212
236
  numberNotEquals: vestUtils.numberNotEquals,
213
- shorterThan: shorterThan,
214
- shorterThanOrEquals: shorterThanOrEquals,
215
- startsWith: startsWith
237
+ shorterThan,
238
+ shorterThanOrEquals,
239
+ startsWith,
216
240
  };
217
241
  }
218
242
 
219
- var baseRules = rules();
243
+ const baseRules = rules();
220
244
  function getRule(ruleName) {
221
245
  return baseRules[ruleName];
222
246
  }
223
247
 
224
- function eachEnforceRule(action) {
225
- for (var ruleName in baseRules) {
226
- var ruleFn = getRule(ruleName);
227
- if (vestUtils.isFunction(ruleFn)) {
228
- action(ruleName, ruleFn);
229
- }
230
- }
231
- }
232
-
233
- var ctx = context.createCascade(function (ctxRef, parentContext) {
234
- var base = {
235
- value: ctxRef.value,
236
- meta: ctxRef.meta || {}
237
- };
238
- if (!parentContext) {
239
- return vestUtils.assign(base, {
240
- parent: emptyParent
241
- });
242
- }
243
- else if (ctxRef.set) {
244
- return vestUtils.assign(base, {
245
- parent: function () { return stripContext(parentContext); }
246
- });
247
- }
248
- return parentContext;
249
- });
250
- function stripContext(ctx) {
251
- if (!ctx) {
252
- return ctx;
253
- }
254
- return {
255
- value: ctx.value,
256
- meta: ctx.meta,
257
- parent: ctx.parent
258
- };
259
- }
260
- function emptyParent() {
261
- return null;
262
- }
263
-
264
- /******************************************************************************
265
- Copyright (c) Microsoft Corporation.
266
-
267
- Permission to use, copy, modify, and/or distribute this software for any
268
- purpose with or without fee is hereby granted.
269
-
270
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
271
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
272
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
273
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
274
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
275
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
276
- PERFORMANCE OF THIS SOFTWARE.
277
- ***************************************************************************** */
278
-
279
- function __spreadArray(to, from, pack) {
280
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
281
- if (ar || !(i in from)) {
282
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
283
- ar[i] = from[i];
284
- }
285
- }
286
- return to.concat(ar || Array.prototype.slice.call(from));
287
- }
288
-
289
- function isProxySupported() {
290
- try {
291
- return vestUtils.isFunction(Proxy);
292
- }
293
- catch (_a) {
294
- return false;
295
- }
296
- }
297
-
298
248
  function ruleReturn(pass, message) {
299
- var output = { pass: pass };
249
+ const output = { pass };
300
250
  if (message) {
301
251
  output.message = message;
302
252
  }
@@ -312,124 +262,106 @@ function defaultToPassing(callback) {
312
262
  /**
313
263
  * Transform the result of a rule into a standard format
314
264
  */
315
- function transformResult(result, ruleName, value) {
316
- var args = [];
317
- for (var _i = 3; _i < arguments.length; _i++) {
318
- args[_i - 3] = arguments[_i];
319
- }
265
+ function transformResult(result, ruleName, value, ...args) {
320
266
  validateResult(result);
321
267
  // if result is boolean
322
268
  if (vestUtils.isBoolean(result)) {
323
269
  return ruleReturn(result);
324
270
  }
325
- else {
326
- return ruleReturn(result.pass, vestUtils.optionalFunctionValue.apply(void 0, __spreadArray([result.message, ruleName, value], args, false)));
327
- }
271
+ return ruleReturn(result.pass, vestUtils.optionalFunctionValue(result.message, ruleName, value, ...args));
328
272
  }
329
273
  function validateResult(result) {
330
274
  // if result is boolean, or if result.pass is boolean
331
275
  vestUtils.invariant(vestUtils.isBoolean(result) || (result && vestUtils.isBoolean(result.pass)), 'Incorrect return value for rule: ' + JSON.stringify(result));
332
276
  }
333
277
 
278
+ // eslint-disable-next-line max-lines-per-function
334
279
  function enforceEager(value) {
335
- var target = {};
336
- // This condition is for when we don't have proxy support (ES5).
337
- // In this case, we need to manually assign the rules to the target object on runtime.
338
- // The follow up proxy block is used in case we do have proxy support, and we can assign each rule upon invocation.
339
- if (!isProxySupported()) {
340
- // We iterate over each of the rules, and add them to the target object being return by enforce
341
- eachEnforceRule(function (ruleName, ruleFn) {
342
- // We then wrap the rule with `genRuleCall` that adds the base enforce behavior
343
- target[ruleName] = genRuleCall(target, ruleFn, ruleName);
344
- });
345
- return target;
346
- }
280
+ const target = {
281
+ message,
282
+ };
283
+ let customMessage = undefined;
347
284
  // We create a proxy intercepting access to the target object (which is empty).
348
- var proxy = new Proxy(target, {
349
- get: function (_, ruleName) {
285
+ const proxy = new Proxy(target, {
286
+ get: (_, key) => {
350
287
  // On property access, we identify if it is a rule or not.
351
- var rule = getRule(ruleName);
288
+ const rule = getRule(key);
352
289
  // If it is a rule, we wrap it with `genRuleCall` that adds the base enforce behavior
353
290
  if (rule) {
354
- return genRuleCall(proxy, rule, ruleName);
291
+ return genRuleCall(proxy, rule, key);
355
292
  }
356
- }
293
+ return target[key];
294
+ },
357
295
  });
358
296
  return proxy;
359
297
  // This function is used to wrap a rule with the base enforce behavior
360
298
  // It takes the target object, the rule function, and the rule name
361
299
  // It then returns the rule, in a manner that can be used by enforce
362
300
  function genRuleCall(target, rule, ruleName) {
363
- return function ruleCall() {
364
- var args = [];
365
- for (var _i = 0; _i < arguments.length; _i++) {
366
- args[_i] = arguments[_i];
367
- }
301
+ return function ruleCall(...args) {
368
302
  // Order of operation:
369
303
  // 1. Create a context with the value being enforced
370
304
  // 2. Call the rule within the context, and pass over the arguments passed to it
371
305
  // 3. Transform the result to the correct output format
372
- var transformedResult = ctx.run({ value: value }, function () {
373
- return transformResult.apply(void 0, __spreadArray([rule.apply(void 0, __spreadArray([value], args, false)), ruleName, value], args, false));
306
+ const transformedResult = ctx.run({ value }, () => {
307
+ return transformResult(rule(value, ...args), ruleName, value, ...args);
374
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)}`;
314
+ }
315
+ return vestUtils.StringObject(transformedResult.message);
316
+ }
375
317
  // On rule failure (the result is false), we either throw an error
376
318
  // or throw a string value if the rule has a message defined in it.
377
- vestUtils.invariant(transformedResult.pass, vestUtils.isNullish(transformedResult.message)
378
- ? "enforce/".concat(ruleName, " failed with ").concat(JSON.stringify(value))
379
- : vestUtils.StringObject(transformedResult.message));
319
+ vestUtils.invariant(transformedResult.pass, enforceMessage());
380
320
  return target;
381
321
  };
382
322
  }
323
+ function message(input) {
324
+ customMessage = input;
325
+ return proxy;
326
+ }
383
327
  }
384
328
 
385
329
  // eslint-disable-next-line max-lines-per-function
386
330
  function genEnforceLazy(key) {
387
- var registeredRules = [];
388
- var lazyMessage;
331
+ const registeredRules = [];
332
+ let lazyMessage;
389
333
  return addLazyRule(key);
390
334
  // eslint-disable-next-line max-lines-per-function
391
335
  function addLazyRule(ruleName) {
392
336
  // eslint-disable-next-line max-lines-per-function
393
- return function () {
394
- var args = [];
395
- for (var _i = 0; _i < arguments.length; _i++) {
396
- args[_i] = arguments[_i];
397
- }
398
- var rule = getRule(ruleName);
399
- registeredRules.push(function (value) {
400
- return transformResult.apply(void 0, __spreadArray([rule.apply(void 0, __spreadArray([value], args, false)), ruleName, value], args, false));
401
- });
402
- var proxy = {
403
- run: function (value) {
404
- return defaultToPassing(vestUtils.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) => {
405
343
  var _a;
406
- var res = ctx.run({ value: value }, function () { return rule(value); });
344
+ const res = ctx.run({ value }, () => rule(value));
407
345
  breakout(!res.pass, ruleReturn(!!res.pass, (_a = vestUtils.optionalFunctionValue(lazyMessage, value, res.message)) !== null && _a !== void 0 ? _a : res.message));
408
346
  }));
409
347
  },
410
- test: function (value) { return proxy.run(value).pass; },
411
- message: function (message) {
348
+ test: (value) => proxy.run(value).pass,
349
+ message: (message) => {
412
350
  if (message) {
413
351
  lazyMessage = message;
414
352
  }
415
353
  return proxy;
416
- }
354
+ },
417
355
  };
418
- if (!isProxySupported()) {
419
- eachEnforceRule(function (ruleName) {
420
- proxy[ruleName] = addLazyRule(ruleName);
421
- });
422
- return proxy;
423
- }
424
356
  // reassigning the proxy here is not pretty
425
357
  // but it's a cleaner way of getting `run` and `test` for free
426
358
  proxy = new Proxy(proxy, {
427
- get: function (target, key) {
359
+ get: (target, key) => {
428
360
  if (getRule(key)) {
429
361
  return addLazyRule(key);
430
362
  }
431
363
  return target[key]; // already has `run` and `test` on it
432
- }
364
+ },
433
365
  });
434
366
  return proxy;
435
367
  };
@@ -463,16 +395,14 @@ function genEnforceLazy(key) {
463
395
  * while run will return an object with the validation result and an optional message created by the rule.
464
396
  */
465
397
  function genEnforce() {
466
- var target = {
467
- context: function () { return ctx.useX(); },
468
- extend: function (customRules) {
398
+ const target = {
399
+ context: () => ctx.useX(),
400
+ extend: (customRules) => {
469
401
  vestUtils.assign(baseRules, customRules);
470
- handleNoProxy(); // TODO: REMOVE when we stop supporting ES5
471
- }
402
+ },
472
403
  };
473
- handleNoProxy();
474
404
  return new Proxy(vestUtils.assign(enforceEager, target), {
475
- get: function (target, key) {
405
+ get: (target, key) => {
476
406
  if (key in target) {
477
407
  return target[key];
478
408
  }
@@ -481,19 +411,10 @@ function genEnforce() {
481
411
  }
482
412
  // Only on the first rule access - start the chain of calls
483
413
  return genEnforceLazy(key);
484
- }
414
+ },
485
415
  });
486
- function handleNoProxy() {
487
- if (!isProxySupported()) {
488
- eachEnforceRule(function (ruleName) {
489
- // Only on the first rule access - start the chain of calls
490
- target[ruleName] = genEnforceLazy(ruleName);
491
- });
492
- return vestUtils.assign(enforceEager, target);
493
- }
494
- }
495
416
  }
496
- var enforce = genEnforce();
417
+ const enforce = genEnforce();
497
418
 
498
419
  exports.ctx = ctx;
499
420
  exports.enforce = enforce;
package/dist/cjs/n4s.js CHANGED
@@ -1,5 +1,4 @@
1
1
  'use strict'
2
-
3
2
  if (process.env.NODE_ENV === 'production') {
4
3
  module.exports = require('./n4s.production.js');
5
4
  } else {
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var n=require("vest-utils"),t=require("context");function r(t,r){return n.isStringValue(t)&&n.isStringValue(r)&&t.endsWith(r)}var i=n.bindNot(r);function e(n,t){return n===t}var u=n.bindNot(e);function s(t,r){return n.numberEquals(t,r)||n.greaterThan(t,r)}function o(t,r){return!!(n.isArray(r)||n.isStringValue(r)&&n.isStringValue(t))&&-1!==r.indexOf(t)}var a=n.bindNot(o);function l(t,r){return n.isNumeric(t)&&n.isNumeric(r)&&Number(t)<Number(r)}function c(t,r){return n.numberEquals(t,r)||l(t,r)}function f(n,t,r){return s(n,t)&&c(n,r)}var N=n.bindNot(f);function g(t){return n.isNullish(t)||n.isStringValue(t)&&!t.trim()}var v=n.bindNot(g),d=n.bindNot(n.isBoolean),h=function(t){return!!n.isNumeric(t)&&0==t%2};function p(n,t){return n in t}var m=n.bindNot(p);function y(n){return Number.isNaN(n)}var b=n.bindNot(y);function E(n){return l(n,0)}function q(n){return"number"==typeof n}var S=n.bindNot(q),x=function(t){return!!n.isNumeric(t)&&0!=t%2},O=n.bindNot(n.isStringValue);function T(n){return!!n}var V=n.bindNot(T);function B(t,r){if(n.isNullish(r))return!1;for(var i in r)if(r[i]===t)return!0;return!1}var w=n.bindNot(B);function A(n,t){return s(n.length,t)}function P(t,r){return r instanceof RegExp?r.test(t):!!n.isStringValue(r)&&new RegExp(r).test(t)}var F=n.bindNot(P);function W(n,t){try{return t(n)}catch(n){return!1}}function U(n,t){return l(n.length,t)}function j(n,t){return c(n.length,t)}function k(t,r){return n.isStringValue(t)&&n.isStringValue(r)&&t.startsWith(r)}var I=n.bindNot(k);var J={condition:W,doesNotEndWith:i,doesNotStartWith:I,endsWith:r,equals:e,greaterThan:n.greaterThan,greaterThanOrEquals:s,gt:n.greaterThan,gte:s,inside:o,isArray:n.isArray,isBetween:f,isBlank:g,isBoolean:n.isBoolean,isEmpty:n.isEmpty,isEven:h,isFalsy:V,isKeyOf:p,isNaN:y,isNegative:E,isNotArray:n.isNotArray,isNotBetween:N,isNotBlank:v,isNotBoolean:d,isNotEmpty:n.isNotEmpty,isNotKeyOf:m,isNotNaN:b,isNotNull:n.isNotNull,isNotNullish:n.isNotNullish,isNotNumber:S,isNotNumeric:n.isNotNumeric,isNotString:O,isNotUndefined:n.isNotUndefined,isNotValueOf:w,isNull:n.isNull,isNullish:n.isNullish,isNumber:q,isNumeric:n.isNumeric,isOdd:x,isPositive:n.isPositive,isString:n.isStringValue,isTruthy:T,isUndefined:n.isUndefined,isValueOf:B,lengthEquals:n.lengthEquals,lengthNotEquals:n.lengthNotEquals,lessThan:l,lessThanOrEquals:c,longerThan:n.longerThan,longerThanOrEquals:A,lt:l,lte:c,matches:P,notEquals:u,notInside:a,notMatches:F,numberEquals:n.numberEquals,numberNotEquals:n.numberNotEquals,shorterThan:U,shorterThanOrEquals:j,startsWith:k};function K(n){return J[n]}function M(t){for(var r in J){var i=K(r);n.isFunction(i)&&t(r,i)}}var R=t.createCascade((function(t,r){var i={value:t.value,meta:t.meta||{}};return r?t.set?n.assign(i,{parent:function(){return function(n){return n?{value:n.value,meta:n.meta,parent:n.parent}:n}(r)}}):r:n.assign(i,{parent:_})}));function _(){return null}function C(n,t,r){if(r||2===arguments.length)for(var i,e=0,u=t.length;e<u;e++)!i&&e in t||(i||(i=Array.prototype.slice.call(t,0,e)),i[e]=t[e]);return n.concat(i||Array.prototype.slice.call(t))}function X(){try{return n.isFunction(Proxy)}catch(n){return!1}}function z(n,t){return n={pass:n},t&&(n.message=t),n}function D(t){return n.defaultTo(t,z(!0))}function G(t,r,i){for(var e=[],u=3;u<arguments.length;u++)e[u-3]=arguments[u];return H(t),n.isBoolean(t)?z(t):z(t.pass,n.optionalFunctionValue.apply(void 0,C([t.message,r,i],e,!1)))}function H(t){n.invariant(n.isBoolean(t)||t&&n.isBoolean(t.pass),"Incorrect return value for rule: "+JSON.stringify(t))}function L(t){function r(r,i,e){return function(){for(var u=[],s=0;s<arguments.length;s++)u[s]=arguments[s];return s=R.run({value:t},(function(){return G.apply(void 0,C([i.apply(void 0,C([t],u,!1)),e,t],u,!1))})),n.invariant(s.pass,n.isNullish(s.message)?"enforce/".concat(e," failed with ").concat(JSON.stringify(t)):n.StringObject(s.message)),r}}var i={};if(!X())return M((function(n,t){i[n]=r(i,t,n)})),i;var e=new Proxy(i,{get:function(n,t){if(n=K(t))return r(e,n,t)}});return e}function Q(t){var r,i=[];return function t(e){return function(){for(var u=[],s=0;s<arguments.length;s++)u[s]=arguments[s];var o=K(e);i.push((function(n){return G.apply(void 0,C([o.apply(void 0,C([n],u,!1)),e,n],u,!1))}));var a={run:function(t){return D(n.mapFirst(i,(function(i,e){var u,s=R.run({value:t},(function(){return i(t)}));e(!s.pass,z(!!s.pass,null!==(u=n.optionalFunctionValue(r,t,s.message))&&void 0!==u?u:s.message))})))},test:function(n){return a.run(n).pass},message:function(n){return n&&(r=n),a}};return X()?a=new Proxy(a,{get:function(n,r){return K(r)?t(r):n[r]}}):(M((function(n){a[n]=t(n)})),a)}}(t)}var Y=function(){function t(){if(!X())return M((function(n){r[n]=Q(n)})),n.assign(L,r)}var r={context:function(){return R.useX()},extend:function(r){n.assign(J,r),t()}};return t(),new Proxy(n.assign(L,r),{get:function(n,t){return t in n?n[t]:K(t)?Q(t):void 0}})}();exports.ctx=R,exports.enforce=Y;
1
+ "use strict";var n=require("vest-utils");const t=require("context").createCascade(((t,i)=>{const s={value:t.value,meta:t.meta||{}};return i?t.set?n.assign(s,{parent:()=>function(n){return{value:n.value,meta:n.meta,parent:n.parent}}(i)}):i:n.assign(s,{parent:e})}));function e(){return null}function i(t,e){return n.isStringValue(t)&&n.isStringValue(e)&&t.endsWith(e)}const s=n.bindNot(i);function r(n,t){return n===t}const u=n.bindNot(r);function o(t,e){return n.numberEquals(t,e)||n.greaterThan(t,e)}function a(t,e){return(n.isArray(e)||!(!n.isStringValue(e)||!n.isStringValue(t)))&&-1!==e.indexOf(t)}const l=n.bindNot(a);function c(t,e){return n.isNumeric(t)&&n.isNumeric(e)&&Number(t)<Number(e)}function N(t,e){return n.numberEquals(t,e)||c(t,e)}function f(n,t,e){return o(n,t)&&N(n,e)}const g=n.bindNot(f);function h(t){return n.isNullish(t)||n.isStringValue(t)&&!t.trim()}const d=n.bindNot(h),m=n.bindNot(n.isBoolean),b=t=>!!n.isNumeric(t)&&t%2==0;function p(n,t){return n in t}const E=n.bindNot(p);function y(n){return Number.isNaN(n)}const q=n.bindNot(y);function v(n){return c(n,0)}function S(n){return Boolean("number"==typeof n)}const O=n.bindNot(S),T=t=>!!n.isNumeric(t)&&t%2!=0,V=n.bindNot(n.isStringValue);function x(n){return!!n}const B=n.bindNot(x);function w(t,e){if(n.isNullish(e))return!1;for(const n in e)if(e[n]===t)return!0;return!1}const W=n.bindNot(w);function A(n,t){return o(n.length,t)}function P(t,e){return e instanceof RegExp?e.test(t):!!n.isStringValue(e)&&new RegExp(e).test(t)}const F=n.bindNot(P);function U(n,t){try{return t(n)}catch(n){return!1}}function j(n,t){return c(n.length,t)}function k(n,t){return N(n.length,t)}function I(t,e){return n.isStringValue(t)&&n.isStringValue(e)&&t.startsWith(e)}const J=n.bindNot(I);const K={condition:U,doesNotEndWith:s,doesNotStartWith:J,endsWith:i,equals:r,greaterThan:n.greaterThan,greaterThanOrEquals:o,gt:n.greaterThan,gte:o,inside:a,isArray:n.isArray,isBetween:f,isBlank:h,isBoolean:n.isBoolean,isEmpty:n.isEmpty,isEven:b,isFalsy:B,isKeyOf:p,isNaN:y,isNegative:v,isNotArray:n.isNotArray,isNotBetween:g,isNotBlank:d,isNotBoolean:m,isNotEmpty:n.isNotEmpty,isNotKeyOf:E,isNotNaN:q,isNotNull:n.isNotNull,isNotNullish:n.isNotNullish,isNotNumber:O,isNotNumeric:n.isNotNumeric,isNotString:V,isNotUndefined:n.isNotUndefined,isNotValueOf:W,isNull:n.isNull,isNullish:n.isNullish,isNumber:S,isNumeric:n.isNumeric,isOdd:T,isPositive:n.isPositive,isString:n.isStringValue,isTruthy:x,isUndefined:n.isUndefined,isValueOf:w,lengthEquals:n.lengthEquals,lengthNotEquals:n.lengthNotEquals,lessThan:c,lessThanOrEquals:N,longerThan:n.longerThan,longerThanOrEquals:A,lt:c,lte:N,matches:P,notEquals:u,notInside:l,notMatches:F,numberEquals:n.numberEquals,numberNotEquals:n.numberNotEquals,shorterThan:j,shorterThanOrEquals:k,startsWith:I};function R(n){return K[n]}function $(n,t){const e={pass:n};return t&&(e.message=t),e}function C(t){return n.defaultTo(t,$(!0))}function M(t,e,i,...s){return function(t){n.invariant(n.isBoolean(t)||t&&n.isBoolean(t.pass),"Incorrect return value for rule: "+JSON.stringify(t))}(t),n.isBoolean(t)?$(t):$(t.pass,n.optionalFunctionValue(t.message,e,i,...s))}function X(e){const i={message:function(n){return s=n,r}};let s;const r=new Proxy(i,{get:(u,o)=>{const a=R(o);return a?function(i,r,u){return function(...o){const a=t.run({value:e},(()=>M(r(e,...o),u,e,...o)));function l(){return n.isNullish(s)?n.isNullish(a.message)?`enforce/${u} failed with ${JSON.stringify(e)}`:n.StringObject(a.message):n.StringObject(s)}return n.invariant(a.pass,l()),i}}(r,a,o):i[o]}});return r}const z=function(){const e={context:()=>t.useX(),extend:t=>{n.assign(K,t)}};return new Proxy(n.assign(X,e),{get:(e,i)=>i in e?e[i]:R(i)?function(e){const i=[];let s;return function e(r){return(...u)=>{const o=R(r);i.push((n=>M(o(n,...u),r,n,...u)));let a={run:e=>C(n.mapFirst(i,((i,r)=>{var u;const o=t.run({value:e},(()=>i(e)));r(!o.pass,$(!!o.pass,null!==(u=n.optionalFunctionValue(s,e,o.message))&&void 0!==u?u:o.message))}))),test:n=>a.run(n).pass,message:n=>(n&&(s=n),a)};return a=new Proxy(a,{get:(n,t)=>R(t)?e(t):n[t]}),a}}(e)}(i):void 0})}();exports.ctx=t,exports.enforce=z;