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