n4s 5.0.0-dev-781e21 → 5.0.0-next-56be98

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