vest 4.0.1 → 4.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/cjs/vest.development.js +36 -5
- package/dist/cjs/vest.production.js +1 -1
- package/dist/es/vest.development.js +36 -6
- package/dist/es/vest.production.js +1 -1
- package/dist/umd/enforce/compose.development.js +587 -5
- package/dist/umd/enforce/compose.production.js +1 -1
- package/dist/umd/enforce/compounds.development.js +625 -14
- package/dist/umd/enforce/compounds.production.js +1 -1
- package/dist/umd/enforce/schema.development.js +628 -27
- package/dist/umd/enforce/schema.production.js +1 -1
- package/dist/umd/vest.development.js +698 -129
- package/dist/umd/vest.production.js +1 -1
- package/package.json +2 -2
- package/testUtils/itWithContext.ts +1 -0
- package/testUtils/testDummy.ts +1 -0
- package/types/enforce/compose.d.ts +6 -0
- package/types/enforce/compounds.d.ts +6 -0
- package/types/enforce/schema.d.ts +6 -0
- package/types/vest.d.ts +6 -2
|
@@ -1,20 +1,333 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports'
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.vest = {}
|
|
5
|
-
}(this, (function (exports
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.vest = {}));
|
|
5
|
+
}(this, (function (exports) { 'use strict';
|
|
6
6
|
|
|
7
7
|
var assign = Object.assign;
|
|
8
8
|
|
|
9
|
+
function isFunction(value) {
|
|
10
|
+
return typeof value === 'function';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function bindNot(fn) {
|
|
14
|
+
return function () {
|
|
15
|
+
var args = [];
|
|
16
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
17
|
+
args[_i] = arguments[_i];
|
|
18
|
+
}
|
|
19
|
+
return !fn.apply(void 0, args);
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function isNull(value) {
|
|
24
|
+
return value === null;
|
|
25
|
+
}
|
|
26
|
+
var isNotNull = bindNot(isNull);
|
|
27
|
+
|
|
28
|
+
function isUndefined(value) {
|
|
29
|
+
return value === undefined;
|
|
30
|
+
}
|
|
31
|
+
var isNotUndefined = bindNot(isUndefined);
|
|
32
|
+
|
|
33
|
+
function isNullish(value) {
|
|
34
|
+
return isNull(value) || isUndefined(value);
|
|
35
|
+
}
|
|
36
|
+
var isNotNullish = bindNot(isNullish);
|
|
37
|
+
|
|
38
|
+
function isStringValue(v) {
|
|
39
|
+
return String(v) === v;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function endsWith(value, arg1) {
|
|
43
|
+
return isStringValue(value) && isStringValue(arg1) && value.endsWith(arg1);
|
|
44
|
+
}
|
|
45
|
+
var doesNotEndWith = bindNot(endsWith);
|
|
46
|
+
|
|
47
|
+
function equals(value, arg1) {
|
|
48
|
+
return value === arg1;
|
|
49
|
+
}
|
|
50
|
+
var notEquals = bindNot(equals);
|
|
51
|
+
|
|
52
|
+
function isNumeric(value) {
|
|
53
|
+
var str = String(value);
|
|
54
|
+
var num = Number(value);
|
|
55
|
+
var result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num);
|
|
56
|
+
return Boolean(result);
|
|
57
|
+
}
|
|
58
|
+
var isNotNumeric = bindNot(isNumeric);
|
|
59
|
+
|
|
60
|
+
function greaterThan(value, gt) {
|
|
61
|
+
return isNumeric(value) && isNumeric(gt) && Number(value) > Number(gt);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function greaterThanOrEquals(value, gte) {
|
|
65
|
+
return isNumeric(value) && isNumeric(gte) && Number(value) >= Number(gte);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// The module is named "isArrayValue" since it
|
|
69
|
+
// is conflicting with a nested npm dependency.
|
|
70
|
+
// We may need to revisit this in the future.
|
|
71
|
+
function isArray(value) {
|
|
72
|
+
return Boolean(Array.isArray(value));
|
|
73
|
+
}
|
|
74
|
+
var isNotArray = bindNot(isArray);
|
|
75
|
+
|
|
76
|
+
function inside(value, arg1) {
|
|
77
|
+
if (isArray(arg1)) {
|
|
78
|
+
return arg1.indexOf(value) !== -1;
|
|
79
|
+
}
|
|
80
|
+
// both value and arg1 are strings
|
|
81
|
+
if (isStringValue(arg1) && isStringValue(value)) {
|
|
82
|
+
return arg1.indexOf(value) !== -1;
|
|
83
|
+
}
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
var notInside = bindNot(inside);
|
|
87
|
+
|
|
88
|
+
function lessThanOrEquals(value, lte) {
|
|
89
|
+
return isNumeric(value) && isNumeric(lte) && Number(value) <= Number(lte);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function isBetween(value, min, max) {
|
|
93
|
+
return greaterThanOrEquals(value, min) && lessThanOrEquals(value, max);
|
|
94
|
+
}
|
|
95
|
+
var isNotBetween = bindNot(isBetween);
|
|
96
|
+
|
|
97
|
+
function isBlank(value) {
|
|
98
|
+
return isNullish(value) || (isStringValue(value) && !value.trim());
|
|
99
|
+
}
|
|
100
|
+
var isNotBlank = bindNot(isBlank);
|
|
101
|
+
|
|
102
|
+
function isBoolean(value) {
|
|
103
|
+
return !!value === value;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
var isNotBoolean = bindNot(isBoolean);
|
|
107
|
+
|
|
9
108
|
/**
|
|
10
|
-
*
|
|
109
|
+
* A safe hasOwnProperty access
|
|
11
110
|
*/
|
|
12
|
-
|
|
13
|
-
return
|
|
14
|
-
}
|
|
111
|
+
function hasOwnProperty(obj, key) {
|
|
112
|
+
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
113
|
+
}
|
|
15
114
|
|
|
16
|
-
function
|
|
17
|
-
return typeof value === '
|
|
115
|
+
function isNumber(value) {
|
|
116
|
+
return Boolean(typeof value === 'number');
|
|
117
|
+
}
|
|
118
|
+
var isNotNumber = bindNot(isNumber);
|
|
119
|
+
|
|
120
|
+
function lengthEquals(value, arg1) {
|
|
121
|
+
return value.length === Number(arg1);
|
|
122
|
+
}
|
|
123
|
+
var lengthNotEquals = bindNot(lengthEquals);
|
|
124
|
+
|
|
125
|
+
function isEmpty(value) {
|
|
126
|
+
if (!value) {
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
else if (isNumber(value)) {
|
|
130
|
+
return value === 0;
|
|
131
|
+
}
|
|
132
|
+
else if (hasOwnProperty(value, 'length')) {
|
|
133
|
+
return lengthEquals(value, 0);
|
|
134
|
+
}
|
|
135
|
+
else if (typeof value === 'object') {
|
|
136
|
+
return lengthEquals(Object.keys(value), 0);
|
|
137
|
+
}
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
var isNotEmpty = bindNot(isEmpty);
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Validates that a given value is an even number
|
|
144
|
+
*/
|
|
145
|
+
var isEven = function (value) {
|
|
146
|
+
if (isNumeric(value)) {
|
|
147
|
+
return value % 2 === 0;
|
|
148
|
+
}
|
|
149
|
+
return false;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
function isKeyOf(key, obj) {
|
|
153
|
+
return key in obj;
|
|
154
|
+
}
|
|
155
|
+
var isNotKeyOf = bindNot(isKeyOf);
|
|
156
|
+
|
|
157
|
+
function isNaN$1(value) {
|
|
158
|
+
return Number.isNaN(value);
|
|
159
|
+
}
|
|
160
|
+
var isNotNaN = bindNot(isNaN$1);
|
|
161
|
+
|
|
162
|
+
function isNegative(value) {
|
|
163
|
+
if (isNumeric(value)) {
|
|
164
|
+
return Number(value) < 0;
|
|
165
|
+
}
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
var isPositive = bindNot(isNegative);
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Validates that a given value is an odd number
|
|
172
|
+
*/
|
|
173
|
+
var isOdd = function (value) {
|
|
174
|
+
if (isNumeric(value)) {
|
|
175
|
+
return value % 2 !== 0;
|
|
176
|
+
}
|
|
177
|
+
return false;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
var isNotString = bindNot(isStringValue);
|
|
181
|
+
|
|
182
|
+
function isTruthy(value) {
|
|
183
|
+
return !!value;
|
|
184
|
+
}
|
|
185
|
+
var isFalsy = bindNot(isTruthy);
|
|
186
|
+
|
|
187
|
+
function isValueOf(value, objectToCheck) {
|
|
188
|
+
if (isNullish(objectToCheck)) {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
for (var key in objectToCheck) {
|
|
192
|
+
if (objectToCheck[key] === value) {
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
var isNotValueOf = bindNot(isValueOf);
|
|
199
|
+
|
|
200
|
+
function lessThan(value, lt) {
|
|
201
|
+
return isNumeric(value) && isNumeric(lt) && Number(value) < Number(lt);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function longerThan(value, arg1) {
|
|
205
|
+
return value.length > Number(arg1);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function longerThanOrEquals(value, arg1) {
|
|
209
|
+
return value.length >= Number(arg1);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function matches(value, regex) {
|
|
213
|
+
if (regex instanceof RegExp) {
|
|
214
|
+
return regex.test(value);
|
|
215
|
+
}
|
|
216
|
+
else if (isStringValue(regex)) {
|
|
217
|
+
return new RegExp(regex).test(value);
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
return false;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
var notMatches = bindNot(matches);
|
|
224
|
+
|
|
225
|
+
function numberEquals(value, eq) {
|
|
226
|
+
return isNumeric(value) && isNumeric(eq) && Number(value) === Number(eq);
|
|
227
|
+
}
|
|
228
|
+
var numberNotEquals = bindNot(numberEquals);
|
|
229
|
+
|
|
230
|
+
function condition(value, callback) {
|
|
231
|
+
try {
|
|
232
|
+
return callback(value);
|
|
233
|
+
}
|
|
234
|
+
catch (_a) {
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function shorterThan(value, arg1) {
|
|
240
|
+
return value.length < Number(arg1);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function shorterThanOrEquals(value, arg1) {
|
|
244
|
+
return value.length <= Number(arg1);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function startsWith(value, arg1) {
|
|
248
|
+
return isStringValue(value) && isStringValue(arg1) && value.startsWith(arg1);
|
|
249
|
+
}
|
|
250
|
+
var doesNotStartWith = bindNot(startsWith);
|
|
251
|
+
|
|
252
|
+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, max-lines-per-function
|
|
253
|
+
function rules() {
|
|
254
|
+
return {
|
|
255
|
+
condition: condition,
|
|
256
|
+
doesNotEndWith: doesNotEndWith,
|
|
257
|
+
doesNotStartWith: doesNotStartWith,
|
|
258
|
+
endsWith: endsWith,
|
|
259
|
+
equals: equals,
|
|
260
|
+
greaterThan: greaterThan,
|
|
261
|
+
greaterThanOrEquals: greaterThanOrEquals,
|
|
262
|
+
gt: greaterThan,
|
|
263
|
+
gte: greaterThanOrEquals,
|
|
264
|
+
inside: inside,
|
|
265
|
+
isArray: isArray,
|
|
266
|
+
isBetween: isBetween,
|
|
267
|
+
isBlank: isBlank,
|
|
268
|
+
isBoolean: isBoolean,
|
|
269
|
+
isEmpty: isEmpty,
|
|
270
|
+
isEven: isEven,
|
|
271
|
+
isFalsy: isFalsy,
|
|
272
|
+
isKeyOf: isKeyOf,
|
|
273
|
+
isNaN: isNaN$1,
|
|
274
|
+
isNegative: isNegative,
|
|
275
|
+
isNotArray: isNotArray,
|
|
276
|
+
isNotBetween: isNotBetween,
|
|
277
|
+
isNotBlank: isNotBlank,
|
|
278
|
+
isNotBoolean: isNotBoolean,
|
|
279
|
+
isNotEmpty: isNotEmpty,
|
|
280
|
+
isNotKeyOf: isNotKeyOf,
|
|
281
|
+
isNotNaN: isNotNaN,
|
|
282
|
+
isNotNull: isNotNull,
|
|
283
|
+
isNotNullish: isNotNullish,
|
|
284
|
+
isNotNumber: isNotNumber,
|
|
285
|
+
isNotNumeric: isNotNumeric,
|
|
286
|
+
isNotString: isNotString,
|
|
287
|
+
isNotUndefined: isNotUndefined,
|
|
288
|
+
isNotValueOf: isNotValueOf,
|
|
289
|
+
isNull: isNull,
|
|
290
|
+
isNullish: isNullish,
|
|
291
|
+
isNumber: isNumber,
|
|
292
|
+
isNumeric: isNumeric,
|
|
293
|
+
isOdd: isOdd,
|
|
294
|
+
isPositive: isPositive,
|
|
295
|
+
isString: isStringValue,
|
|
296
|
+
isTruthy: isTruthy,
|
|
297
|
+
isUndefined: isUndefined,
|
|
298
|
+
isValueOf: isValueOf,
|
|
299
|
+
lengthEquals: lengthEquals,
|
|
300
|
+
lengthNotEquals: lengthNotEquals,
|
|
301
|
+
lessThan: lessThan,
|
|
302
|
+
lessThanOrEquals: lessThanOrEquals,
|
|
303
|
+
longerThan: longerThan,
|
|
304
|
+
longerThanOrEquals: longerThanOrEquals,
|
|
305
|
+
lt: lessThan,
|
|
306
|
+
lte: lessThanOrEquals,
|
|
307
|
+
matches: matches,
|
|
308
|
+
notEquals: notEquals,
|
|
309
|
+
notInside: notInside,
|
|
310
|
+
notMatches: notMatches,
|
|
311
|
+
numberEquals: numberEquals,
|
|
312
|
+
numberNotEquals: numberNotEquals,
|
|
313
|
+
shorterThan: shorterThan,
|
|
314
|
+
shorterThanOrEquals: shorterThanOrEquals,
|
|
315
|
+
startsWith: startsWith
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
var baseRules = rules();
|
|
320
|
+
function getRule(ruleName) {
|
|
321
|
+
return baseRules[ruleName];
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function eachEnforceRule(action) {
|
|
325
|
+
for (var ruleName in baseRules) {
|
|
326
|
+
var ruleFn = getRule(ruleName);
|
|
327
|
+
if (isFunction(ruleFn)) {
|
|
328
|
+
action(ruleName, ruleFn);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
18
331
|
}
|
|
19
332
|
|
|
20
333
|
function optionalFunctionValue(value) {
|
|
@@ -42,6 +355,344 @@
|
|
|
42
355
|
}, 0);
|
|
43
356
|
}
|
|
44
357
|
|
|
358
|
+
// eslint-disable-next-line max-lines-per-function
|
|
359
|
+
function createContext(init) {
|
|
360
|
+
var storage = { ancestry: [] };
|
|
361
|
+
return {
|
|
362
|
+
bind: bind,
|
|
363
|
+
run: run,
|
|
364
|
+
use: use,
|
|
365
|
+
useX: useX
|
|
366
|
+
};
|
|
367
|
+
function useX(errorMessage) {
|
|
368
|
+
var _a;
|
|
369
|
+
return ((_a = storage.ctx) !== null && _a !== void 0 ? _a : throwError(defaultTo(errorMessage, 'Context was used after it was closed')));
|
|
370
|
+
}
|
|
371
|
+
function run(ctxRef, fn) {
|
|
372
|
+
var _a;
|
|
373
|
+
var parentContext = use();
|
|
374
|
+
var out = assign({}, parentContext ? parentContext : {}, (_a = optionalFunctionValue(init, ctxRef, parentContext)) !== null && _a !== void 0 ? _a : ctxRef);
|
|
375
|
+
var ctx = set(Object.freeze(out));
|
|
376
|
+
storage.ancestry.unshift(ctx);
|
|
377
|
+
var res = fn(ctx);
|
|
378
|
+
clear();
|
|
379
|
+
return res;
|
|
380
|
+
}
|
|
381
|
+
function bind(ctxRef, fn) {
|
|
382
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
383
|
+
// @ts-ignore - this one's pretty hard to get right
|
|
384
|
+
var returnedFn = function () {
|
|
385
|
+
var runTimeArgs = [];
|
|
386
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
387
|
+
runTimeArgs[_i] = arguments[_i];
|
|
388
|
+
}
|
|
389
|
+
return run(ctxRef, function () {
|
|
390
|
+
return fn.apply(void 0, runTimeArgs);
|
|
391
|
+
});
|
|
392
|
+
};
|
|
393
|
+
return returnedFn;
|
|
394
|
+
}
|
|
395
|
+
function use() {
|
|
396
|
+
return storage.ctx;
|
|
397
|
+
}
|
|
398
|
+
function set(value) {
|
|
399
|
+
return (storage.ctx = value);
|
|
400
|
+
}
|
|
401
|
+
function clear() {
|
|
402
|
+
var _a;
|
|
403
|
+
storage.ancestry.shift();
|
|
404
|
+
set((_a = storage.ancestry[0]) !== null && _a !== void 0 ? _a : null);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
var ctx = createContext(function (ctxRef, parentContext) {
|
|
409
|
+
var base = {
|
|
410
|
+
value: ctxRef.value,
|
|
411
|
+
meta: ctxRef.meta || {}
|
|
412
|
+
};
|
|
413
|
+
if (!parentContext) {
|
|
414
|
+
return assign(base, {
|
|
415
|
+
parent: emptyParent
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
else if (ctxRef.set) {
|
|
419
|
+
return assign(base, {
|
|
420
|
+
parent: function () { return stripContext(parentContext); }
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
return parentContext;
|
|
424
|
+
});
|
|
425
|
+
function stripContext(ctx) {
|
|
426
|
+
if (!ctx) {
|
|
427
|
+
return ctx;
|
|
428
|
+
}
|
|
429
|
+
return {
|
|
430
|
+
value: ctx.value,
|
|
431
|
+
meta: ctx.meta,
|
|
432
|
+
parent: ctx.parent
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
function emptyParent() {
|
|
436
|
+
return null;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/*! *****************************************************************************
|
|
440
|
+
Copyright (c) Microsoft Corporation.
|
|
441
|
+
|
|
442
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
443
|
+
purpose with or without fee is hereby granted.
|
|
444
|
+
|
|
445
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
446
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
447
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
448
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
449
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
450
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
451
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
452
|
+
***************************************************************************** */
|
|
453
|
+
|
|
454
|
+
var __assign = function() {
|
|
455
|
+
__assign = Object.assign || function __assign(t) {
|
|
456
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
457
|
+
s = arguments[i];
|
|
458
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
459
|
+
}
|
|
460
|
+
return t;
|
|
461
|
+
};
|
|
462
|
+
return __assign.apply(this, arguments);
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
function __spreadArray(to, from, pack) {
|
|
466
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
467
|
+
if (ar || !(i in from)) {
|
|
468
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
469
|
+
ar[i] = from[i];
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
function isProxySupported() {
|
|
476
|
+
try {
|
|
477
|
+
return isFunction(Proxy);
|
|
478
|
+
}
|
|
479
|
+
catch (_a) {
|
|
480
|
+
return false;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function ruleReturn(pass, message) {
|
|
485
|
+
var output = { pass: pass };
|
|
486
|
+
if (message) {
|
|
487
|
+
output.message = message;
|
|
488
|
+
}
|
|
489
|
+
return output;
|
|
490
|
+
}
|
|
491
|
+
function passing() {
|
|
492
|
+
return ruleReturn(true);
|
|
493
|
+
}
|
|
494
|
+
function defaultToPassing(callback) {
|
|
495
|
+
return defaultTo(callback, passing());
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Transform the result of a rule into a standard format
|
|
500
|
+
*/
|
|
501
|
+
function transformResult(result, ruleName, value) {
|
|
502
|
+
var args = [];
|
|
503
|
+
for (var _i = 3; _i < arguments.length; _i++) {
|
|
504
|
+
args[_i - 3] = arguments[_i];
|
|
505
|
+
}
|
|
506
|
+
validateResult(result);
|
|
507
|
+
// if result is boolean
|
|
508
|
+
if (isBoolean(result)) {
|
|
509
|
+
return ruleReturn(result);
|
|
510
|
+
}
|
|
511
|
+
else {
|
|
512
|
+
return ruleReturn(result.pass, optionalFunctionValue.apply(void 0, __spreadArray([result.message, ruleName, value], args)));
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
function validateResult(result) {
|
|
516
|
+
// if result is boolean, or if result.pass is boolean
|
|
517
|
+
if (isBoolean(result) || (result && isBoolean(result.pass))) {
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
throwError('Incorrect return value for rule: ' + JSON.stringify(result));
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
function enforceEager(value) {
|
|
524
|
+
var target = {};
|
|
525
|
+
if (!isProxySupported()) {
|
|
526
|
+
eachEnforceRule(function (ruleName, ruleFn) {
|
|
527
|
+
target[ruleName] = genRuleCall(target, ruleFn, ruleName);
|
|
528
|
+
});
|
|
529
|
+
return target;
|
|
530
|
+
}
|
|
531
|
+
var proxy = new Proxy(target, {
|
|
532
|
+
get: function (_, ruleName) {
|
|
533
|
+
var rule = getRule(ruleName);
|
|
534
|
+
if (rule) {
|
|
535
|
+
return genRuleCall(proxy, rule, ruleName);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
});
|
|
539
|
+
return proxy;
|
|
540
|
+
function genRuleCall(target, rule, ruleName) {
|
|
541
|
+
return function ruleCall() {
|
|
542
|
+
var args = [];
|
|
543
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
544
|
+
args[_i] = arguments[_i];
|
|
545
|
+
}
|
|
546
|
+
var transformedResult = transformResult.apply(void 0, __spreadArray([ctx.run({ value: value }, function () { return rule.apply(void 0, __spreadArray([value], args)); }),
|
|
547
|
+
ruleName,
|
|
548
|
+
value], args));
|
|
549
|
+
if (!transformedResult.pass) {
|
|
550
|
+
if (isEmpty(transformedResult.message)) {
|
|
551
|
+
throwError("enforce/" + ruleName + " failed with " + JSON.stringify(value));
|
|
552
|
+
}
|
|
553
|
+
else {
|
|
554
|
+
// Explicitly throw a string so that vest.test can pick it up as the validation error message
|
|
555
|
+
throw transformedResult.message;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
return target;
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
function mapFirst(array, callback) {
|
|
564
|
+
var broke = false;
|
|
565
|
+
var breakoutValue = null;
|
|
566
|
+
for (var i = 0; i < array.length; i++) {
|
|
567
|
+
callback(array[i], breakout, i);
|
|
568
|
+
if (broke) {
|
|
569
|
+
return breakoutValue;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
function breakout(value) {
|
|
573
|
+
broke = true;
|
|
574
|
+
breakoutValue = value;
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
// eslint-disable-next-line max-lines-per-function
|
|
579
|
+
function genEnforceLazy(key) {
|
|
580
|
+
var registeredRules = [];
|
|
581
|
+
var lazyMessage;
|
|
582
|
+
return addLazyRule(key);
|
|
583
|
+
// eslint-disable-next-line max-lines-per-function
|
|
584
|
+
function addLazyRule(ruleName) {
|
|
585
|
+
// eslint-disable-next-line max-lines-per-function
|
|
586
|
+
return function () {
|
|
587
|
+
var args = [];
|
|
588
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
589
|
+
args[_i] = arguments[_i];
|
|
590
|
+
}
|
|
591
|
+
var rule = getRule(ruleName);
|
|
592
|
+
registeredRules.push(function (value) {
|
|
593
|
+
return transformResult.apply(void 0, __spreadArray([rule.apply(void 0, __spreadArray([value], args)), ruleName, value], args));
|
|
594
|
+
});
|
|
595
|
+
var proxy = {
|
|
596
|
+
run: function (value) {
|
|
597
|
+
return defaultToPassing(mapFirst(registeredRules, function (rule, breakout) {
|
|
598
|
+
var _a;
|
|
599
|
+
var res = ctx.run({ value: value }, function () { return rule(value); });
|
|
600
|
+
if (!res.pass) {
|
|
601
|
+
breakout(ruleReturn(!!res.pass, (_a = optionalFunctionValue(lazyMessage, value, res.message)) !== null && _a !== void 0 ? _a : res.message));
|
|
602
|
+
}
|
|
603
|
+
}));
|
|
604
|
+
},
|
|
605
|
+
test: function (value) { return proxy.run(value).pass; },
|
|
606
|
+
message: function (message) {
|
|
607
|
+
if (message) {
|
|
608
|
+
lazyMessage = message;
|
|
609
|
+
}
|
|
610
|
+
return proxy;
|
|
611
|
+
}
|
|
612
|
+
};
|
|
613
|
+
if (!isProxySupported()) {
|
|
614
|
+
eachEnforceRule(function (ruleName) {
|
|
615
|
+
proxy[ruleName] = addLazyRule(ruleName);
|
|
616
|
+
});
|
|
617
|
+
return proxy;
|
|
618
|
+
}
|
|
619
|
+
// reassigning the proxy here is not pretty
|
|
620
|
+
// but it's a cleaner way of getting `run` and `test` for free
|
|
621
|
+
proxy = new Proxy(proxy, {
|
|
622
|
+
get: function (target, key) {
|
|
623
|
+
if (getRule(key)) {
|
|
624
|
+
return addLazyRule(key);
|
|
625
|
+
}
|
|
626
|
+
return target[key]; // already has `run` and `test` on it
|
|
627
|
+
}
|
|
628
|
+
});
|
|
629
|
+
return proxy;
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* Enforce is quite complicated, I want to explain it in detail.
|
|
636
|
+
* It is dynamic in nature, so a lot of proxy objects are involved.
|
|
637
|
+
*
|
|
638
|
+
* Enforce has two main interfaces
|
|
639
|
+
* 1. eager
|
|
640
|
+
* 2. lazy
|
|
641
|
+
*
|
|
642
|
+
* The eager interface is the most commonly used, and the easier to understand.
|
|
643
|
+
* It throws an error when a rule is not satisfied.
|
|
644
|
+
* The eager interface is declared in enforceEager.ts and it is quite simple to understand.
|
|
645
|
+
* enforce is called with a value, and the return value is a proxy object that points back to all the rules.
|
|
646
|
+
* When a rule is called, the value is mapped as its first argument, and if the rule passes, the same
|
|
647
|
+
* proxy object is returned. Otherwise, an error is thrown.
|
|
648
|
+
*
|
|
649
|
+
* The lazy interface works quite differently. It is declared in genEnforceLazy.ts.
|
|
650
|
+
* Rather than calling enforce directly, the lazy interface has all the rules as "methods" (only by proxy).
|
|
651
|
+
* Calling the first function in the chain will initialize an array of calls. It stores the different rule calls
|
|
652
|
+
* and the parameters passed to them. None of the rules are called yet.
|
|
653
|
+
* The rules are only invoked in sequence once either of these chained functions are called:
|
|
654
|
+
* 1. test(value)
|
|
655
|
+
* 2. run(value)
|
|
656
|
+
*
|
|
657
|
+
* Calling run or test will call all the rules in sequence, with the difference that test will only return a boolean value,
|
|
658
|
+
* while run will return an object with the validation result and an optional message created by the rule.
|
|
659
|
+
*/
|
|
660
|
+
function genEnforce() {
|
|
661
|
+
var target = {
|
|
662
|
+
context: function () { return ctx.useX(); },
|
|
663
|
+
extend: function (customRules) {
|
|
664
|
+
assign(baseRules, customRules);
|
|
665
|
+
}
|
|
666
|
+
};
|
|
667
|
+
if (!isProxySupported()) {
|
|
668
|
+
eachEnforceRule(function (ruleName) {
|
|
669
|
+
// Only on the first rule access - start the chain of calls
|
|
670
|
+
target[ruleName] = genEnforceLazy(ruleName);
|
|
671
|
+
});
|
|
672
|
+
return assign(enforceEager, target);
|
|
673
|
+
}
|
|
674
|
+
return new Proxy(assign(enforceEager, target), {
|
|
675
|
+
get: function (target, key) {
|
|
676
|
+
if (key in target) {
|
|
677
|
+
return target[key];
|
|
678
|
+
}
|
|
679
|
+
if (!getRule(key)) {
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
// Only on the first rule access - start the chain of calls
|
|
683
|
+
return genEnforceLazy(key);
|
|
684
|
+
}
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
var enforce = genEnforce();
|
|
688
|
+
|
|
689
|
+
/**
|
|
690
|
+
* @returns a unique numeric id.
|
|
691
|
+
*/
|
|
692
|
+
var genId = (function (n) { return function () {
|
|
693
|
+
return "" + n++;
|
|
694
|
+
}; })(0);
|
|
695
|
+
|
|
45
696
|
// eslint-disable-next-line max-lines-per-function
|
|
46
697
|
function createState(onStateChange) {
|
|
47
698
|
var state = {
|
|
@@ -178,7 +829,7 @@
|
|
|
178
829
|
};
|
|
179
830
|
}
|
|
180
831
|
|
|
181
|
-
var context =
|
|
832
|
+
var context = createContext(function (ctxRef, parentContext) {
|
|
182
833
|
return parentContext
|
|
183
834
|
? null
|
|
184
835
|
: assign({}, {
|
|
@@ -186,6 +837,7 @@
|
|
|
186
837
|
tests: {},
|
|
187
838
|
groups: {}
|
|
188
839
|
},
|
|
840
|
+
inclusion: {},
|
|
189
841
|
isolate: {
|
|
190
842
|
type: IsolateTypes.DEFAULT,
|
|
191
843
|
keys: {
|
|
@@ -197,28 +849,6 @@
|
|
|
197
849
|
}, ctxRef);
|
|
198
850
|
});
|
|
199
851
|
|
|
200
|
-
function bindNot(fn) {
|
|
201
|
-
return function () {
|
|
202
|
-
var args = [];
|
|
203
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
204
|
-
args[_i] = arguments[_i];
|
|
205
|
-
}
|
|
206
|
-
return !fn.apply(void 0, args);
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// The module is named "isArrayValue" since it
|
|
211
|
-
// is conflicting with a nested npm dependency.
|
|
212
|
-
// We may need to revisit this in the future.
|
|
213
|
-
function isArray(value) {
|
|
214
|
-
return Boolean(Array.isArray(value));
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
function isNull(value) {
|
|
218
|
-
return value === null;
|
|
219
|
-
}
|
|
220
|
-
var isNotNull = bindNot(isNull);
|
|
221
|
-
|
|
222
852
|
// This is sort of a map/filter in one function.
|
|
223
853
|
// Normally, behaves like a nested-array map
|
|
224
854
|
// Returning `null` will drop the element from the array
|
|
@@ -264,31 +894,11 @@
|
|
|
264
894
|
return current;
|
|
265
895
|
}
|
|
266
896
|
|
|
267
|
-
function isUndefined(value) {
|
|
268
|
-
return value === undefined;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
function isNullish(value) {
|
|
272
|
-
return isNull(value) || isUndefined(value);
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
function isStringValue(v) {
|
|
276
|
-
return String(v) === v;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
897
|
function shouldUseErrorAsMessage(message, error) {
|
|
280
898
|
// kind of cheating with this safe guard, but it does the job
|
|
281
899
|
return isUndefined(message) && isStringValue(error);
|
|
282
900
|
}
|
|
283
901
|
|
|
284
|
-
function lengthEquals(value, arg1) {
|
|
285
|
-
return value.length === Number(arg1);
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
function longerThan(value, arg1) {
|
|
289
|
-
return value.length > Number(arg1);
|
|
290
|
-
}
|
|
291
|
-
|
|
292
902
|
/**
|
|
293
903
|
* Creates a cache function
|
|
294
904
|
*/
|
|
@@ -604,34 +1214,6 @@
|
|
|
604
1214
|
return context.useX().isolate.type === IsolateTypes.EACH;
|
|
605
1215
|
}
|
|
606
1216
|
|
|
607
|
-
/**
|
|
608
|
-
* A safe hasOwnProperty access
|
|
609
|
-
*/
|
|
610
|
-
function hasOwnProperty(obj, key) {
|
|
611
|
-
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
function isNumber(value) {
|
|
615
|
-
return Boolean(typeof value === 'number');
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
function isEmpty(value) {
|
|
619
|
-
if (!value) {
|
|
620
|
-
return true;
|
|
621
|
-
}
|
|
622
|
-
else if (isNumber(value)) {
|
|
623
|
-
return value === 0;
|
|
624
|
-
}
|
|
625
|
-
else if (hasOwnProperty(value, 'length')) {
|
|
626
|
-
return lengthEquals(value, 0);
|
|
627
|
-
}
|
|
628
|
-
else if (typeof value === 'object') {
|
|
629
|
-
return lengthEquals(Object.keys(value), 0);
|
|
630
|
-
}
|
|
631
|
-
return true;
|
|
632
|
-
}
|
|
633
|
-
var isNotEmpty = bindNot(isEmpty);
|
|
634
|
-
|
|
635
1217
|
function nonMatchingFieldName(testObject, fieldName) {
|
|
636
1218
|
return !!fieldName && !matchingFieldName(testObject, fieldName);
|
|
637
1219
|
}
|
|
@@ -719,42 +1301,6 @@
|
|
|
719
1301
|
return testKey;
|
|
720
1302
|
}
|
|
721
1303
|
|
|
722
|
-
/*! *****************************************************************************
|
|
723
|
-
Copyright (c) Microsoft Corporation.
|
|
724
|
-
|
|
725
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
726
|
-
purpose with or without fee is hereby granted.
|
|
727
|
-
|
|
728
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
729
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
730
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
731
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
732
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
733
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
734
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
735
|
-
***************************************************************************** */
|
|
736
|
-
|
|
737
|
-
var __assign = function() {
|
|
738
|
-
__assign = Object.assign || function __assign(t) {
|
|
739
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
740
|
-
s = arguments[i];
|
|
741
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
742
|
-
}
|
|
743
|
-
return t;
|
|
744
|
-
};
|
|
745
|
-
return __assign.apply(this, arguments);
|
|
746
|
-
};
|
|
747
|
-
|
|
748
|
-
function __spreadArray(to, from, pack) {
|
|
749
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
750
|
-
if (ar || !(i in from)) {
|
|
751
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
752
|
-
ar[i] = from[i];
|
|
753
|
-
}
|
|
754
|
-
}
|
|
755
|
-
return to.concat(ar || from);
|
|
756
|
-
}
|
|
757
|
-
|
|
758
1304
|
function either(a, b) {
|
|
759
1305
|
return !!a !== !!b;
|
|
760
1306
|
}
|
|
@@ -1263,13 +1809,14 @@
|
|
|
1263
1809
|
return !!context.useX().skipped;
|
|
1264
1810
|
}
|
|
1265
1811
|
//Checks whether a certain test profile excluded by any of the exclusion groups.
|
|
1266
|
-
// eslint-disable-next-line complexity, max-statements
|
|
1812
|
+
// eslint-disable-next-line complexity, max-statements, max-lines-per-function
|
|
1267
1813
|
function isExcluded(testObject) {
|
|
1268
1814
|
var fieldName = testObject.fieldName, groupName = testObject.groupName;
|
|
1269
1815
|
if (isExcludedIndividually())
|
|
1270
1816
|
return true;
|
|
1271
1817
|
var context$1 = context.useX();
|
|
1272
1818
|
var exclusion = context$1.exclusion;
|
|
1819
|
+
var inclusion = context$1.inclusion;
|
|
1273
1820
|
var keyTests = exclusion.tests;
|
|
1274
1821
|
var testValue = keyTests[fieldName];
|
|
1275
1822
|
// if test is skipped
|
|
@@ -1299,8 +1846,12 @@
|
|
|
1299
1846
|
if (isTestIncluded)
|
|
1300
1847
|
return false;
|
|
1301
1848
|
// If there is _ANY_ `only`ed test (and we already know this one isn't) return true
|
|
1302
|
-
|
|
1303
|
-
|
|
1849
|
+
if (hasIncludedTests(keyTests)) {
|
|
1850
|
+
// Check if inclusion rules for this field (`include` hook)
|
|
1851
|
+
return !optionalFunctionValue(inclusion[fieldName]);
|
|
1852
|
+
}
|
|
1853
|
+
// We're done here. This field is not excluded
|
|
1854
|
+
return false;
|
|
1304
1855
|
}
|
|
1305
1856
|
// eslint-disable-next-line max-statements
|
|
1306
1857
|
function isMissingFromIncludedGroup(groupName) {
|
|
@@ -1405,6 +1956,29 @@
|
|
|
1405
1956
|
throwError("Wrong arguments passed to group. Group " + error + ".");
|
|
1406
1957
|
}
|
|
1407
1958
|
|
|
1959
|
+
function include(fieldName) {
|
|
1960
|
+
var context$1 = context.useX();
|
|
1961
|
+
var inclusion = context$1.inclusion, exclusion = context$1.exclusion;
|
|
1962
|
+
if (!fieldName) {
|
|
1963
|
+
return { when: when };
|
|
1964
|
+
}
|
|
1965
|
+
inclusion[fieldName] = defaultTo(exclusion.tests[fieldName], true);
|
|
1966
|
+
return { when: when };
|
|
1967
|
+
function when(condition) {
|
|
1968
|
+
var context$1 = context.useX();
|
|
1969
|
+
var inclusion = context$1.inclusion, exclusion = context$1.exclusion;
|
|
1970
|
+
inclusion[fieldName] = function () {
|
|
1971
|
+
if (hasOwnProperty(exclusion.tests, fieldName)) {
|
|
1972
|
+
return defaultTo(exclusion.tests[fieldName], true);
|
|
1973
|
+
}
|
|
1974
|
+
if (isStringValue(condition)) {
|
|
1975
|
+
return Boolean(exclusion.tests[condition]);
|
|
1976
|
+
}
|
|
1977
|
+
return optionalFunctionValue(condition, optionalFunctionValue(produceDraft));
|
|
1978
|
+
};
|
|
1979
|
+
}
|
|
1980
|
+
}
|
|
1981
|
+
|
|
1408
1982
|
/**
|
|
1409
1983
|
* Conditionally omits tests from the suite.
|
|
1410
1984
|
*
|
|
@@ -1472,8 +2046,6 @@
|
|
|
1472
2046
|
});
|
|
1473
2047
|
}
|
|
1474
2048
|
|
|
1475
|
-
var isNotString = bindNot(isStringValue);
|
|
1476
|
-
|
|
1477
2049
|
function isPromise(value) {
|
|
1478
2050
|
return value && isFunction(value.then);
|
|
1479
2051
|
}
|
|
@@ -1683,6 +2255,7 @@
|
|
|
1683
2255
|
}
|
|
1684
2256
|
}
|
|
1685
2257
|
|
|
2258
|
+
/* eslint-disable jest/valid-title */
|
|
1686
2259
|
// eslint-disable-next-line max-lines-per-function
|
|
1687
2260
|
function bindTestMemo(test) {
|
|
1688
2261
|
var cache = createCache(100); // arbitrary cache size
|
|
@@ -1760,19 +2333,15 @@
|
|
|
1760
2333
|
ctx.currentTest.warn();
|
|
1761
2334
|
}
|
|
1762
2335
|
|
|
1763
|
-
var VERSION = "4.0.
|
|
2336
|
+
var VERSION = "4.0.1";
|
|
1764
2337
|
|
|
1765
|
-
Object.defineProperty(exports, 'enforce', {
|
|
1766
|
-
enumerable: true,
|
|
1767
|
-
get: function () {
|
|
1768
|
-
return n4s.enforce;
|
|
1769
|
-
}
|
|
1770
|
-
});
|
|
1771
2338
|
exports.VERSION = VERSION;
|
|
1772
2339
|
exports.context = context;
|
|
1773
2340
|
exports.create = create;
|
|
1774
2341
|
exports.each = each;
|
|
2342
|
+
exports.enforce = enforce;
|
|
1775
2343
|
exports.group = group;
|
|
2344
|
+
exports.include = include;
|
|
1776
2345
|
exports.omitWhen = omitWhen;
|
|
1777
2346
|
exports.only = only;
|
|
1778
2347
|
exports.optional = optional;
|