vest 4.5.0 → 5.0.0-dev-781e21

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 (34) hide show
  1. package/dist/cjs/enforce/compose.development.js +1 -1
  2. package/dist/cjs/enforce/compose.production.js +1 -1
  3. package/dist/cjs/enforce/compounds.development.js +1 -1
  4. package/dist/cjs/enforce/compounds.production.js +1 -1
  5. package/dist/cjs/enforce/schema.development.js +1 -1
  6. package/dist/cjs/enforce/schema.production.js +1 -1
  7. package/dist/cjs/vest.development.js +146 -95
  8. package/dist/cjs/vest.production.js +1 -1
  9. package/dist/es/enforce/compose.development.js +1 -1
  10. package/dist/es/enforce/compose.production.js +1 -1
  11. package/dist/es/enforce/compounds.development.js +1 -1
  12. package/dist/es/enforce/compounds.production.js +1 -1
  13. package/dist/es/enforce/schema.development.js +1 -1
  14. package/dist/es/enforce/schema.production.js +1 -1
  15. package/dist/es/vest.development.js +148 -97
  16. package/dist/es/vest.production.js +1 -1
  17. package/dist/umd/classnames.development.js +11 -59
  18. package/dist/umd/classnames.production.js +1 -1
  19. package/dist/umd/enforce/compose.development.js +9 -682
  20. package/dist/umd/enforce/compose.production.js +1 -1
  21. package/dist/umd/enforce/compounds.development.js +17 -684
  22. package/dist/umd/enforce/compounds.production.js +1 -1
  23. package/dist/umd/enforce/schema.development.js +12 -685
  24. package/dist/umd/enforce/schema.production.js +1 -1
  25. package/dist/umd/parser.development.js +10 -58
  26. package/dist/umd/parser.production.js +1 -1
  27. package/dist/umd/promisify.development.js +5 -31
  28. package/dist/umd/promisify.production.js +1 -1
  29. package/dist/umd/vest.development.js +255 -997
  30. package/dist/umd/vest.production.js +1 -1
  31. package/package.json +3 -3
  32. package/types/enforce/compounds.d.ts +6 -8
  33. package/types/enforce/schema.d.ts +5 -4
  34. package/types/vest.d.ts +36 -28
@@ -1,153 +1,8 @@
1
- (function (factory) {
2
- typeof define === 'function' && define.amd ? define(factory) :
3
- factory();
4
- }((function () { 'use strict';
5
-
6
- function bindNot(fn) {
7
- return function () {
8
- var args = [];
9
- for (var _i = 0; _i < arguments.length; _i++) {
10
- args[_i] = arguments[_i];
11
- }
12
- return !fn.apply(void 0, args);
13
- };
14
- }
15
-
16
- function isNumeric(value) {
17
- var str = String(value);
18
- var num = Number(value);
19
- var result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num);
20
- return Boolean(result);
21
- }
22
- var isNotNumeric = bindNot(isNumeric);
23
-
24
- function numberEquals(value, eq) {
25
- return isNumeric(value) && isNumeric(eq) && Number(value) === Number(eq);
26
- }
27
- var numberNotEquals = bindNot(numberEquals);
28
-
29
- function lengthEquals(value, arg1) {
30
- return numberEquals(value.length, arg1);
31
- }
32
- var lengthNotEquals = bindNot(lengthEquals);
33
-
34
- function greaterThan(value, gt) {
35
- return isNumeric(value) && isNumeric(gt) && Number(value) > Number(gt);
36
- }
37
-
38
- function longerThan(value, arg1) {
39
- return greaterThan(value.length, arg1);
40
- }
41
-
42
- function isNull(value) {
43
- return value === null;
44
- }
45
- var isNotNull = bindNot(isNull);
46
-
47
- function isUndefined(value) {
48
- return value === undefined;
49
- }
50
- var isNotUndefined = bindNot(isUndefined);
51
-
52
- function isNullish(value) {
53
- return isNull(value) || isUndefined(value);
54
- }
55
- var isNotNullish = bindNot(isNullish);
56
-
57
- function isFunction(value) {
58
- return typeof value === 'function';
59
- }
60
-
61
- function optionalFunctionValue(value) {
62
- var args = [];
63
- for (var _i = 1; _i < arguments.length; _i++) {
64
- args[_i - 1] = arguments[_i];
65
- }
66
- return isFunction(value) ? value.apply(void 0, args) : value;
67
- }
68
-
69
- function defaultTo(value, defaultValue) {
70
- var _a;
71
- return (_a = optionalFunctionValue(value)) !== null && _a !== void 0 ? _a : optionalFunctionValue(defaultValue);
72
- }
73
-
74
- // The module is named "isArrayValue" since it
75
- // is conflicting with a nested npm dependency.
76
- // We may need to revisit this in the future.
77
- function isArray(value) {
78
- return Boolean(Array.isArray(value));
79
- }
80
- var isNotArray = bindNot(isArray);
81
-
82
- /**
83
- * A safe hasOwnProperty access
84
- */
85
- function hasOwnProperty(obj, key) {
86
- return Object.prototype.hasOwnProperty.call(obj, key);
87
- }
88
-
89
- var assign = Object.assign;
90
-
91
- function invariant(condition,
92
- // eslint-disable-next-line @typescript-eslint/ban-types
93
- message) {
94
- if (condition) {
95
- return;
96
- }
97
- // If message is a string object (rather than string literal)
98
- // Throw the value directly as a string
99
- // Alternatively, throw an error with the message
100
- throw message instanceof String
101
- ? message.valueOf()
102
- : new Error(message ? optionalFunctionValue(message) : message);
103
- }
104
- // eslint-disable-next-line @typescript-eslint/ban-types
105
- function StringObject(value) {
106
- return new String(optionalFunctionValue(value));
107
- }
108
-
109
- function isStringValue(v) {
110
- return String(v) === v;
111
- }
112
-
113
- function isBoolean(value) {
114
- return !!value === value;
115
- }
116
-
117
- function mapFirst(array, callback) {
118
- var broke = false;
119
- var breakoutValue = null;
120
- for (var i = 0; i < array.length; i++) {
121
- callback(array[i], breakout, i);
122
- if (broke) {
123
- return breakoutValue;
124
- }
125
- }
126
- function breakout(conditional, value) {
127
- if (conditional) {
128
- broke = true;
129
- breakoutValue = value;
130
- }
131
- }
132
- }
133
-
134
- function isEmpty(value) {
135
- if (!value) {
136
- return true;
137
- }
138
- else if (hasOwnProperty(value, 'length')) {
139
- return lengthEquals(value, 0);
140
- }
141
- else if (typeof value === 'object') {
142
- return lengthEquals(Object.keys(value), 0);
143
- }
144
- return false;
145
- }
146
- var isNotEmpty = bindNot(isEmpty);
147
-
148
- function isPositive(value) {
149
- return greaterThan(value, 0);
150
- }
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('n4s'), require('vest-utils')) :
3
+ typeof define === 'function' && define.amd ? define(['n4s', 'vest-utils'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.n4s, global['vest-utils']));
5
+ }(this, (function (n4s, vestUtils) { 'use strict';
151
6
 
152
7
  function ruleReturn(pass, message) {
153
8
  var output = { pass: pass };
@@ -163,10 +18,10 @@
163
18
  return ruleReturn(true);
164
19
  }
165
20
  function defaultToFailing(callback) {
166
- return defaultTo(callback, failing());
21
+ return vestUtils.defaultTo(callback, failing());
167
22
  }
168
23
  function defaultToPassing(callback) {
169
- return defaultTo(callback, passing());
24
+ return vestUtils.defaultTo(callback, passing());
170
25
  }
171
26
 
172
27
  function runLazyRule(lazyRule, currentValue) {
@@ -183,7 +38,7 @@
183
38
  for (var _i = 1; _i < arguments.length; _i++) {
184
39
  rules[_i - 1] = arguments[_i];
185
40
  }
186
- return defaultToPassing(mapFirst(rules, function (rule, breakout) {
41
+ return defaultToPassing(vestUtils.mapFirst(rules, function (rule, breakout) {
187
42
  var res = runLazyRule(rule, value);
188
43
  breakout(!res.pass, res);
189
44
  }));
@@ -194,550 +49,28 @@
194
49
  for (var _i = 1; _i < arguments.length; _i++) {
195
50
  rules[_i - 1] = arguments[_i];
196
51
  }
197
- return defaultToFailing(mapFirst(rules, function (rule, breakout) {
52
+ return defaultToFailing(vestUtils.mapFirst(rules, function (rule, breakout) {
198
53
  var res = runLazyRule(rule, value);
199
54
  breakout(res.pass, res);
200
55
  }));
201
56
  }
202
57
 
203
- function endsWith(value, arg1) {
204
- return isStringValue(value) && isStringValue(arg1) && value.endsWith(arg1);
205
- }
206
- var doesNotEndWith = bindNot(endsWith);
207
-
208
- function equals(value, arg1) {
209
- return value === arg1;
210
- }
211
- var notEquals = bindNot(equals);
212
-
213
- function greaterThanOrEquals(value, gte) {
214
- return numberEquals(value, gte) || greaterThan(value, gte);
215
- }
216
-
217
- function inside(value, arg1) {
218
- if (isArray(arg1)) {
219
- return arg1.indexOf(value) !== -1;
220
- }
221
- // both value and arg1 are strings
222
- if (isStringValue(arg1) && isStringValue(value)) {
223
- return arg1.indexOf(value) !== -1;
224
- }
225
- return false;
226
- }
227
- var notInside = bindNot(inside);
228
-
229
- function lessThan(value, lt) {
230
- return isNumeric(value) && isNumeric(lt) && Number(value) < Number(lt);
231
- }
232
-
233
- function lessThanOrEquals(value, lte) {
234
- return numberEquals(value, lte) || lessThan(value, lte);
235
- }
236
-
237
- function isBetween(value, min, max) {
238
- return greaterThanOrEquals(value, min) && lessThanOrEquals(value, max);
239
- }
240
- var isNotBetween = bindNot(isBetween);
241
-
242
- function isBlank(value) {
243
- return isNullish(value) || (isStringValue(value) && !value.trim());
244
- }
245
- var isNotBlank = bindNot(isBlank);
246
-
247
- var isNotBoolean = bindNot(isBoolean);
248
-
249
- /**
250
- * Validates that a given value is an even number
251
- */
252
- var isEven = function (value) {
253
- if (isNumeric(value)) {
254
- return value % 2 === 0;
255
- }
256
- return false;
257
- };
258
-
259
- function isKeyOf(key, obj) {
260
- return key in obj;
261
- }
262
- var isNotKeyOf = bindNot(isKeyOf);
263
-
264
- function isNaN$1(value) {
265
- return Number.isNaN(value);
266
- }
267
- var isNotNaN = bindNot(isNaN$1);
268
-
269
- function isNegative(value) {
270
- return lessThan(value, 0);
271
- }
272
-
273
- function isNumber(value) {
274
- return Boolean(typeof value === 'number');
275
- }
276
- var isNotNumber = bindNot(isNumber);
277
-
278
- /**
279
- * Validates that a given value is an odd number
280
- */
281
- var isOdd = function (value) {
282
- if (isNumeric(value)) {
283
- return value % 2 !== 0;
284
- }
285
- return false;
286
- };
287
-
288
- var isNotString = bindNot(isStringValue);
289
-
290
- function isTruthy(value) {
291
- return !!value;
292
- }
293
- var isFalsy = bindNot(isTruthy);
294
-
295
- function isValueOf(value, objectToCheck) {
296
- if (isNullish(objectToCheck)) {
297
- return false;
298
- }
299
- for (var key in objectToCheck) {
300
- if (objectToCheck[key] === value) {
301
- return true;
302
- }
303
- }
304
- return false;
305
- }
306
- var isNotValueOf = bindNot(isValueOf);
307
-
308
- function longerThanOrEquals(value, arg1) {
309
- return greaterThanOrEquals(value.length, arg1);
310
- }
311
-
312
- function matches(value, regex) {
313
- if (regex instanceof RegExp) {
314
- return regex.test(value);
315
- }
316
- else if (isStringValue(regex)) {
317
- return new RegExp(regex).test(value);
318
- }
319
- else {
320
- return false;
321
- }
322
- }
323
- var notMatches = bindNot(matches);
324
-
325
- function condition(value, callback) {
326
- try {
327
- return callback(value);
328
- }
329
- catch (_a) {
330
- return false;
331
- }
332
- }
333
-
334
- function shorterThan(value, arg1) {
335
- return lessThan(value.length, arg1);
336
- }
337
-
338
- function shorterThanOrEquals(value, arg1) {
339
- return lessThanOrEquals(value.length, arg1);
340
- }
341
-
342
- function startsWith(value, arg1) {
343
- return isStringValue(value) && isStringValue(arg1) && value.startsWith(arg1);
344
- }
345
- var doesNotStartWith = bindNot(startsWith);
346
-
347
- // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, max-lines-per-function
348
- function rules() {
349
- return {
350
- condition: condition,
351
- doesNotEndWith: doesNotEndWith,
352
- doesNotStartWith: doesNotStartWith,
353
- endsWith: endsWith,
354
- equals: equals,
355
- greaterThan: greaterThan,
356
- greaterThanOrEquals: greaterThanOrEquals,
357
- gt: greaterThan,
358
- gte: greaterThanOrEquals,
359
- inside: inside,
360
- isArray: isArray,
361
- isBetween: isBetween,
362
- isBlank: isBlank,
363
- isBoolean: isBoolean,
364
- isEmpty: isEmpty,
365
- isEven: isEven,
366
- isFalsy: isFalsy,
367
- isKeyOf: isKeyOf,
368
- isNaN: isNaN$1,
369
- isNegative: isNegative,
370
- isNotArray: isNotArray,
371
- isNotBetween: isNotBetween,
372
- isNotBlank: isNotBlank,
373
- isNotBoolean: isNotBoolean,
374
- isNotEmpty: isNotEmpty,
375
- isNotKeyOf: isNotKeyOf,
376
- isNotNaN: isNotNaN,
377
- isNotNull: isNotNull,
378
- isNotNullish: isNotNullish,
379
- isNotNumber: isNotNumber,
380
- isNotNumeric: isNotNumeric,
381
- isNotString: isNotString,
382
- isNotUndefined: isNotUndefined,
383
- isNotValueOf: isNotValueOf,
384
- isNull: isNull,
385
- isNullish: isNullish,
386
- isNumber: isNumber,
387
- isNumeric: isNumeric,
388
- isOdd: isOdd,
389
- isPositive: isPositive,
390
- isString: isStringValue,
391
- isTruthy: isTruthy,
392
- isUndefined: isUndefined,
393
- isValueOf: isValueOf,
394
- lengthEquals: lengthEquals,
395
- lengthNotEquals: lengthNotEquals,
396
- lessThan: lessThan,
397
- lessThanOrEquals: lessThanOrEquals,
398
- longerThan: longerThan,
399
- longerThanOrEquals: longerThanOrEquals,
400
- lt: lessThan,
401
- lte: lessThanOrEquals,
402
- matches: matches,
403
- notEquals: notEquals,
404
- notInside: notInside,
405
- notMatches: notMatches,
406
- numberEquals: numberEquals,
407
- numberNotEquals: numberNotEquals,
408
- shorterThan: shorterThan,
409
- shorterThanOrEquals: shorterThanOrEquals,
410
- startsWith: startsWith
411
- };
412
- }
413
-
414
- var baseRules = rules();
415
- function getRule(ruleName) {
416
- return baseRules[ruleName];
417
- }
418
-
419
- function eachEnforceRule(action) {
420
- for (var ruleName in baseRules) {
421
- var ruleFn = getRule(ruleName);
422
- if (isFunction(ruleFn)) {
423
- action(ruleName, ruleFn);
424
- }
425
- }
426
- }
427
-
428
- // eslint-disable-next-line max-lines-per-function
429
- function createContext(init) {
430
- var storage = { ancestry: [] };
431
- return {
432
- bind: bind,
433
- run: run,
434
- use: use,
435
- useX: useX
436
- };
437
- function useX(errorMessage) {
438
- var ctx = use();
439
- invariant(ctx, defaultTo(errorMessage, 'Context was used after it was closed'));
440
- return ctx;
441
- }
442
- function run(ctxRef, fn) {
443
- var _a;
444
- var parentContext = use();
445
- var out = assign({}, parentContext ? parentContext : {}, (_a = optionalFunctionValue(init, ctxRef, parentContext)) !== null && _a !== void 0 ? _a : ctxRef);
446
- var ctx = set(Object.freeze(out));
447
- storage.ancestry.unshift(ctx);
448
- var res = fn(ctx);
449
- clear();
450
- return res;
451
- }
452
- function bind(ctxRef, fn) {
453
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
454
- // @ts-ignore - this one's pretty hard to get right
455
- var returnedFn = function () {
456
- var runTimeArgs = [];
457
- for (var _i = 0; _i < arguments.length; _i++) {
458
- runTimeArgs[_i] = arguments[_i];
459
- }
460
- return run(ctxRef, function () {
461
- return fn.apply(void 0, runTimeArgs);
462
- });
463
- };
464
- return returnedFn;
465
- }
466
- function use() {
467
- return storage.ctx;
468
- }
469
- function set(value) {
470
- return (storage.ctx = value);
471
- }
472
- function clear() {
473
- var _a;
474
- storage.ancestry.shift();
475
- set((_a = storage.ancestry[0]) !== null && _a !== void 0 ? _a : null);
476
- }
477
- }
478
-
479
- var ctx = createContext(function (ctxRef, parentContext) {
480
- var base = {
481
- value: ctxRef.value,
482
- meta: ctxRef.meta || {}
483
- };
484
- if (!parentContext) {
485
- return assign(base, {
486
- parent: emptyParent
487
- });
488
- }
489
- else if (ctxRef.set) {
490
- return assign(base, {
491
- parent: function () { return stripContext(parentContext); }
492
- });
493
- }
494
- return parentContext;
495
- });
496
- function stripContext(ctx) {
497
- if (!ctx) {
498
- return ctx;
499
- }
500
- return {
501
- value: ctx.value,
502
- meta: ctx.meta,
503
- parent: ctx.parent
504
- };
505
- }
506
- function emptyParent() {
507
- return null;
508
- }
509
-
510
- /******************************************************************************
511
- Copyright (c) Microsoft Corporation.
512
-
513
- Permission to use, copy, modify, and/or distribute this software for any
514
- purpose with or without fee is hereby granted.
515
-
516
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
517
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
518
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
519
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
520
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
521
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
522
- PERFORMANCE OF THIS SOFTWARE.
523
- ***************************************************************************** */
524
-
525
- function __spreadArray(to, from, pack) {
526
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
527
- if (ar || !(i in from)) {
528
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
529
- ar[i] = from[i];
530
- }
531
- }
532
- return to.concat(ar || Array.prototype.slice.call(from));
533
- }
534
-
535
- function isProxySupported() {
536
- try {
537
- return isFunction(Proxy);
538
- }
539
- catch (_a) {
540
- return false;
541
- }
542
- }
543
-
544
- /**
545
- * Transform the result of a rule into a standard format
546
- */
547
- function transformResult(result, ruleName, value) {
548
- var args = [];
549
- for (var _i = 3; _i < arguments.length; _i++) {
550
- args[_i - 3] = arguments[_i];
551
- }
552
- validateResult(result);
553
- // if result is boolean
554
- if (isBoolean(result)) {
555
- return ruleReturn(result);
556
- }
557
- else {
558
- return ruleReturn(result.pass, optionalFunctionValue.apply(void 0, __spreadArray([result.message, ruleName, value], args, false)));
559
- }
560
- }
561
- function validateResult(result) {
562
- // if result is boolean, or if result.pass is boolean
563
- invariant(isBoolean(result) || (result && isBoolean(result.pass)), 'Incorrect return value for rule: ' + JSON.stringify(result));
564
- }
565
-
566
- function enforceEager(value) {
567
- var target = {};
568
- // This condition is for when we don't have proxy support (ES5).
569
- // In this case, we need to manually assign the rules to the target object on runtime.
570
- // The follow up proxy block is used in case we do have proxy support, and we can assign each rule upon invocation.
571
- if (!isProxySupported()) {
572
- // We iterate over each of the rules, and add them to the target object being return by enforce
573
- eachEnforceRule(function (ruleName, ruleFn) {
574
- // We then wrap the rule with `genRuleCall` that adds the base enforce behavior
575
- target[ruleName] = genRuleCall(target, ruleFn, ruleName);
576
- });
577
- return target;
578
- }
579
- // We create a proxy intercepting access to the target object (which is empty).
580
- var proxy = new Proxy(target, {
581
- get: function (_, ruleName) {
582
- // On property access, we identify if it is a rule or not.
583
- var rule = getRule(ruleName);
584
- // If it is a rule, we wrap it with `genRuleCall` that adds the base enforce behavior
585
- if (rule) {
586
- return genRuleCall(proxy, rule, ruleName);
587
- }
588
- }
589
- });
590
- return proxy;
591
- // This function is used to wrap a rule with the base enforce behavior
592
- // It takes the target object, the rule function, and the rule name
593
- // It then returns the rule, in a manner that can be used by enforce
594
- function genRuleCall(target, rule, ruleName) {
595
- return function ruleCall() {
596
- var args = [];
597
- for (var _i = 0; _i < arguments.length; _i++) {
598
- args[_i] = arguments[_i];
599
- }
600
- // Order of operation:
601
- // 1. Create a context with the value being enforced
602
- // 2. Call the rule within the context, and pass over the arguments passed to it
603
- // 3. Transform the result to the correct output format
604
- var transformedResult = ctx.run({ value: value }, function () {
605
- return transformResult.apply(void 0, __spreadArray([rule.apply(void 0, __spreadArray([value], args, false)), ruleName, value], args, false));
606
- });
607
- // On rule failure (the result is false), we either throw an error
608
- // or throw a string value if the rule has a message defined in it.
609
- invariant(transformedResult.pass, isNullish(transformedResult.message)
610
- ? "enforce/".concat(ruleName, " failed with ").concat(JSON.stringify(value))
611
- : StringObject(transformedResult.message));
612
- return target;
613
- };
614
- }
615
- }
616
-
617
- // eslint-disable-next-line max-lines-per-function
618
- function genEnforceLazy(key) {
619
- var registeredRules = [];
620
- var lazyMessage;
621
- return addLazyRule(key);
622
- // eslint-disable-next-line max-lines-per-function
623
- function addLazyRule(ruleName) {
624
- // eslint-disable-next-line max-lines-per-function
625
- return function () {
626
- var args = [];
627
- for (var _i = 0; _i < arguments.length; _i++) {
628
- args[_i] = arguments[_i];
629
- }
630
- var rule = getRule(ruleName);
631
- registeredRules.push(function (value) {
632
- return transformResult.apply(void 0, __spreadArray([rule.apply(void 0, __spreadArray([value], args, false)), ruleName, value], args, false));
633
- });
634
- var proxy = {
635
- run: function (value) {
636
- return defaultToPassing(mapFirst(registeredRules, function (rule, breakout) {
637
- var _a;
638
- var res = ctx.run({ value: value }, function () { return rule(value); });
639
- breakout(!res.pass, ruleReturn(!!res.pass, (_a = optionalFunctionValue(lazyMessage, value, res.message)) !== null && _a !== void 0 ? _a : res.message));
640
- }));
641
- },
642
- test: function (value) { return proxy.run(value).pass; },
643
- message: function (message) {
644
- if (message) {
645
- lazyMessage = message;
646
- }
647
- return proxy;
648
- }
649
- };
650
- if (!isProxySupported()) {
651
- eachEnforceRule(function (ruleName) {
652
- proxy[ruleName] = addLazyRule(ruleName);
653
- });
654
- return proxy;
655
- }
656
- // reassigning the proxy here is not pretty
657
- // but it's a cleaner way of getting `run` and `test` for free
658
- proxy = new Proxy(proxy, {
659
- get: function (target, key) {
660
- if (getRule(key)) {
661
- return addLazyRule(key);
662
- }
663
- return target[key]; // already has `run` and `test` on it
664
- }
665
- });
666
- return proxy;
667
- };
668
- }
669
- }
670
-
671
- /**
672
- * Enforce is quite complicated, I want to explain it in detail.
673
- * It is dynamic in nature, so a lot of proxy objects are involved.
674
- *
675
- * Enforce has two main interfaces
676
- * 1. eager
677
- * 2. lazy
678
- *
679
- * The eager interface is the most commonly used, and the easier to understand.
680
- * It throws an error when a rule is not satisfied.
681
- * The eager interface is declared in enforceEager.ts and it is quite simple to understand.
682
- * enforce is called with a value, and the return value is a proxy object that points back to all the rules.
683
- * When a rule is called, the value is mapped as its first argument, and if the rule passes, the same
684
- * proxy object is returned. Otherwise, an error is thrown.
685
- *
686
- * The lazy interface works quite differently. It is declared in genEnforceLazy.ts.
687
- * Rather than calling enforce directly, the lazy interface has all the rules as "methods" (only by proxy).
688
- * Calling the first function in the chain will initialize an array of calls. It stores the different rule calls
689
- * and the parameters passed to them. None of the rules are called yet.
690
- * The rules are only invoked in sequence once either of these chained functions are called:
691
- * 1. test(value)
692
- * 2. run(value)
693
- *
694
- * Calling run or test will call all the rules in sequence, with the difference that test will only return a boolean value,
695
- * while run will return an object with the validation result and an optional message created by the rule.
696
- */
697
- function genEnforce() {
698
- var target = {
699
- context: function () { return ctx.useX(); },
700
- extend: function (customRules) {
701
- assign(baseRules, customRules);
702
- handleNoProxy(); // TODO: REMOVE when we stop supporting ES5
703
- }
704
- };
705
- handleNoProxy();
706
- return new Proxy(assign(enforceEager, target), {
707
- get: function (target, key) {
708
- if (key in target) {
709
- return target[key];
710
- }
711
- if (!getRule(key)) {
712
- return;
713
- }
714
- // Only on the first rule access - start the chain of calls
715
- return genEnforceLazy(key);
716
- }
717
- });
718
- function handleNoProxy() {
719
- if (!isProxySupported()) {
720
- eachEnforceRule(function (ruleName) {
721
- // Only on the first rule access - start the chain of calls
722
- target[ruleName] = genEnforceLazy(ruleName);
723
- });
724
- return assign(enforceEager, target);
725
- }
726
- }
727
- }
728
- var enforce = genEnforce();
729
-
730
58
  function noneOf(value) {
731
59
  var rules = [];
732
60
  for (var _i = 1; _i < arguments.length; _i++) {
733
61
  rules[_i - 1] = arguments[_i];
734
62
  }
735
- return defaultToPassing(mapFirst(rules, function (rule, breakout) {
63
+ return defaultToPassing(vestUtils.mapFirst(rules, function (rule, breakout) {
736
64
  var res = runLazyRule(rule, value);
737
65
  breakout(res.pass, failing());
738
66
  }));
739
67
  }
740
68
 
69
+ function equals(value, arg1) {
70
+ return value === arg1;
71
+ }
72
+ vestUtils.bindNot(equals);
73
+
741
74
  var REQUIRED_COUNT = 1;
742
75
  function oneOf(value) {
743
76
  var rules = [];
@@ -750,13 +83,13 @@
750
83
  if (res.pass) {
751
84
  passingCount++;
752
85
  }
753
- if (greaterThan(passingCount, REQUIRED_COUNT)) {
86
+ if (vestUtils.greaterThan(passingCount, REQUIRED_COUNT)) {
754
87
  return false;
755
88
  }
756
89
  });
757
90
  return ruleReturn(equals(passingCount, REQUIRED_COUNT));
758
91
  }
759
92
 
760
- enforce.extend({ allOf: allOf, anyOf: anyOf, noneOf: noneOf, oneOf: oneOf });
93
+ n4s.enforce.extend({ allOf: allOf, anyOf: anyOf, noneOf: noneOf, oneOf: oneOf });
761
94
 
762
95
  })));