n4s 5.0.0-dev-781e21 → 5.0.0-dev-ec989a

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