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,8 +1,8 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(
|
|
3
|
-
typeof define === 'function' && define.amd ? define(
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.compose = factory(
|
|
5
|
-
}(this, (function (
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.compose = factory());
|
|
5
|
+
}(this, (function () { 'use strict';
|
|
6
6
|
|
|
7
7
|
function mapFirst(array, callback) {
|
|
8
8
|
var broke = false;
|
|
@@ -43,6 +43,16 @@
|
|
|
43
43
|
throw new Error(devMessage );
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
function bindNot(fn) {
|
|
47
|
+
return function () {
|
|
48
|
+
var args = [];
|
|
49
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
50
|
+
args[_i] = arguments[_i];
|
|
51
|
+
}
|
|
52
|
+
return !fn.apply(void 0, args);
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
46
56
|
/**
|
|
47
57
|
* A safe hasOwnProperty access
|
|
48
58
|
*/
|
|
@@ -53,10 +63,12 @@
|
|
|
53
63
|
function isNumber(value) {
|
|
54
64
|
return Boolean(typeof value === 'number');
|
|
55
65
|
}
|
|
66
|
+
var isNotNumber = bindNot(isNumber);
|
|
56
67
|
|
|
57
68
|
function lengthEquals(value, arg1) {
|
|
58
69
|
return value.length === Number(arg1);
|
|
59
70
|
}
|
|
71
|
+
var lengthNotEquals = bindNot(lengthEquals);
|
|
60
72
|
|
|
61
73
|
function isEmpty(value) {
|
|
62
74
|
if (!value) {
|
|
@@ -73,6 +85,400 @@
|
|
|
73
85
|
}
|
|
74
86
|
return true;
|
|
75
87
|
}
|
|
88
|
+
var isNotEmpty = bindNot(isEmpty);
|
|
89
|
+
|
|
90
|
+
var assign = Object.assign;
|
|
91
|
+
|
|
92
|
+
function isNull(value) {
|
|
93
|
+
return value === null;
|
|
94
|
+
}
|
|
95
|
+
var isNotNull = bindNot(isNull);
|
|
96
|
+
|
|
97
|
+
function isUndefined(value) {
|
|
98
|
+
return value === undefined;
|
|
99
|
+
}
|
|
100
|
+
var isNotUndefined = bindNot(isUndefined);
|
|
101
|
+
|
|
102
|
+
function isNullish(value) {
|
|
103
|
+
return isNull(value) || isUndefined(value);
|
|
104
|
+
}
|
|
105
|
+
var isNotNullish = bindNot(isNullish);
|
|
106
|
+
|
|
107
|
+
function isStringValue(v) {
|
|
108
|
+
return String(v) === v;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function endsWith(value, arg1) {
|
|
112
|
+
return isStringValue(value) && isStringValue(arg1) && value.endsWith(arg1);
|
|
113
|
+
}
|
|
114
|
+
var doesNotEndWith = bindNot(endsWith);
|
|
115
|
+
|
|
116
|
+
function equals(value, arg1) {
|
|
117
|
+
return value === arg1;
|
|
118
|
+
}
|
|
119
|
+
var notEquals = bindNot(equals);
|
|
120
|
+
|
|
121
|
+
function isNumeric(value) {
|
|
122
|
+
var str = String(value);
|
|
123
|
+
var num = Number(value);
|
|
124
|
+
var result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num);
|
|
125
|
+
return Boolean(result);
|
|
126
|
+
}
|
|
127
|
+
var isNotNumeric = bindNot(isNumeric);
|
|
128
|
+
|
|
129
|
+
function greaterThan(value, gt) {
|
|
130
|
+
return isNumeric(value) && isNumeric(gt) && Number(value) > Number(gt);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function greaterThanOrEquals(value, gte) {
|
|
134
|
+
return isNumeric(value) && isNumeric(gte) && Number(value) >= Number(gte);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// The module is named "isArrayValue" since it
|
|
138
|
+
// is conflicting with a nested npm dependency.
|
|
139
|
+
// We may need to revisit this in the future.
|
|
140
|
+
function isArray(value) {
|
|
141
|
+
return Boolean(Array.isArray(value));
|
|
142
|
+
}
|
|
143
|
+
var isNotArray = bindNot(isArray);
|
|
144
|
+
|
|
145
|
+
function inside(value, arg1) {
|
|
146
|
+
if (isArray(arg1)) {
|
|
147
|
+
return arg1.indexOf(value) !== -1;
|
|
148
|
+
}
|
|
149
|
+
// both value and arg1 are strings
|
|
150
|
+
if (isStringValue(arg1) && isStringValue(value)) {
|
|
151
|
+
return arg1.indexOf(value) !== -1;
|
|
152
|
+
}
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
var notInside = bindNot(inside);
|
|
156
|
+
|
|
157
|
+
function lessThanOrEquals(value, lte) {
|
|
158
|
+
return isNumeric(value) && isNumeric(lte) && Number(value) <= Number(lte);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function isBetween(value, min, max) {
|
|
162
|
+
return greaterThanOrEquals(value, min) && lessThanOrEquals(value, max);
|
|
163
|
+
}
|
|
164
|
+
var isNotBetween = bindNot(isBetween);
|
|
165
|
+
|
|
166
|
+
function isBlank(value) {
|
|
167
|
+
return isNullish(value) || (isStringValue(value) && !value.trim());
|
|
168
|
+
}
|
|
169
|
+
var isNotBlank = bindNot(isBlank);
|
|
170
|
+
|
|
171
|
+
function isBoolean(value) {
|
|
172
|
+
return !!value === value;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
var isNotBoolean = bindNot(isBoolean);
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Validates that a given value is an even number
|
|
179
|
+
*/
|
|
180
|
+
var isEven = function (value) {
|
|
181
|
+
if (isNumeric(value)) {
|
|
182
|
+
return value % 2 === 0;
|
|
183
|
+
}
|
|
184
|
+
return false;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
function isKeyOf(key, obj) {
|
|
188
|
+
return key in obj;
|
|
189
|
+
}
|
|
190
|
+
var isNotKeyOf = bindNot(isKeyOf);
|
|
191
|
+
|
|
192
|
+
function isNaN$1(value) {
|
|
193
|
+
return Number.isNaN(value);
|
|
194
|
+
}
|
|
195
|
+
var isNotNaN = bindNot(isNaN$1);
|
|
196
|
+
|
|
197
|
+
function isNegative(value) {
|
|
198
|
+
if (isNumeric(value)) {
|
|
199
|
+
return Number(value) < 0;
|
|
200
|
+
}
|
|
201
|
+
return false;
|
|
202
|
+
}
|
|
203
|
+
var isPositive = bindNot(isNegative);
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Validates that a given value is an odd number
|
|
207
|
+
*/
|
|
208
|
+
var isOdd = function (value) {
|
|
209
|
+
if (isNumeric(value)) {
|
|
210
|
+
return value % 2 !== 0;
|
|
211
|
+
}
|
|
212
|
+
return false;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
var isNotString = bindNot(isStringValue);
|
|
216
|
+
|
|
217
|
+
function isTruthy(value) {
|
|
218
|
+
return !!value;
|
|
219
|
+
}
|
|
220
|
+
var isFalsy = bindNot(isTruthy);
|
|
221
|
+
|
|
222
|
+
function isValueOf(value, objectToCheck) {
|
|
223
|
+
if (isNullish(objectToCheck)) {
|
|
224
|
+
return false;
|
|
225
|
+
}
|
|
226
|
+
for (var key in objectToCheck) {
|
|
227
|
+
if (objectToCheck[key] === value) {
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
var isNotValueOf = bindNot(isValueOf);
|
|
234
|
+
|
|
235
|
+
function lessThan(value, lt) {
|
|
236
|
+
return isNumeric(value) && isNumeric(lt) && Number(value) < Number(lt);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function longerThan(value, arg1) {
|
|
240
|
+
return value.length > Number(arg1);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function longerThanOrEquals(value, arg1) {
|
|
244
|
+
return value.length >= Number(arg1);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function matches(value, regex) {
|
|
248
|
+
if (regex instanceof RegExp) {
|
|
249
|
+
return regex.test(value);
|
|
250
|
+
}
|
|
251
|
+
else if (isStringValue(regex)) {
|
|
252
|
+
return new RegExp(regex).test(value);
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
var notMatches = bindNot(matches);
|
|
259
|
+
|
|
260
|
+
function numberEquals(value, eq) {
|
|
261
|
+
return isNumeric(value) && isNumeric(eq) && Number(value) === Number(eq);
|
|
262
|
+
}
|
|
263
|
+
var numberNotEquals = bindNot(numberEquals);
|
|
264
|
+
|
|
265
|
+
function condition(value, callback) {
|
|
266
|
+
try {
|
|
267
|
+
return callback(value);
|
|
268
|
+
}
|
|
269
|
+
catch (_a) {
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function shorterThan(value, arg1) {
|
|
275
|
+
return value.length < Number(arg1);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function shorterThanOrEquals(value, arg1) {
|
|
279
|
+
return value.length <= Number(arg1);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function startsWith(value, arg1) {
|
|
283
|
+
return isStringValue(value) && isStringValue(arg1) && value.startsWith(arg1);
|
|
284
|
+
}
|
|
285
|
+
var doesNotStartWith = bindNot(startsWith);
|
|
286
|
+
|
|
287
|
+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, max-lines-per-function
|
|
288
|
+
function rules() {
|
|
289
|
+
return {
|
|
290
|
+
condition: condition,
|
|
291
|
+
doesNotEndWith: doesNotEndWith,
|
|
292
|
+
doesNotStartWith: doesNotStartWith,
|
|
293
|
+
endsWith: endsWith,
|
|
294
|
+
equals: equals,
|
|
295
|
+
greaterThan: greaterThan,
|
|
296
|
+
greaterThanOrEquals: greaterThanOrEquals,
|
|
297
|
+
gt: greaterThan,
|
|
298
|
+
gte: greaterThanOrEquals,
|
|
299
|
+
inside: inside,
|
|
300
|
+
isArray: isArray,
|
|
301
|
+
isBetween: isBetween,
|
|
302
|
+
isBlank: isBlank,
|
|
303
|
+
isBoolean: isBoolean,
|
|
304
|
+
isEmpty: isEmpty,
|
|
305
|
+
isEven: isEven,
|
|
306
|
+
isFalsy: isFalsy,
|
|
307
|
+
isKeyOf: isKeyOf,
|
|
308
|
+
isNaN: isNaN$1,
|
|
309
|
+
isNegative: isNegative,
|
|
310
|
+
isNotArray: isNotArray,
|
|
311
|
+
isNotBetween: isNotBetween,
|
|
312
|
+
isNotBlank: isNotBlank,
|
|
313
|
+
isNotBoolean: isNotBoolean,
|
|
314
|
+
isNotEmpty: isNotEmpty,
|
|
315
|
+
isNotKeyOf: isNotKeyOf,
|
|
316
|
+
isNotNaN: isNotNaN,
|
|
317
|
+
isNotNull: isNotNull,
|
|
318
|
+
isNotNullish: isNotNullish,
|
|
319
|
+
isNotNumber: isNotNumber,
|
|
320
|
+
isNotNumeric: isNotNumeric,
|
|
321
|
+
isNotString: isNotString,
|
|
322
|
+
isNotUndefined: isNotUndefined,
|
|
323
|
+
isNotValueOf: isNotValueOf,
|
|
324
|
+
isNull: isNull,
|
|
325
|
+
isNullish: isNullish,
|
|
326
|
+
isNumber: isNumber,
|
|
327
|
+
isNumeric: isNumeric,
|
|
328
|
+
isOdd: isOdd,
|
|
329
|
+
isPositive: isPositive,
|
|
330
|
+
isString: isStringValue,
|
|
331
|
+
isTruthy: isTruthy,
|
|
332
|
+
isUndefined: isUndefined,
|
|
333
|
+
isValueOf: isValueOf,
|
|
334
|
+
lengthEquals: lengthEquals,
|
|
335
|
+
lengthNotEquals: lengthNotEquals,
|
|
336
|
+
lessThan: lessThan,
|
|
337
|
+
lessThanOrEquals: lessThanOrEquals,
|
|
338
|
+
longerThan: longerThan,
|
|
339
|
+
longerThanOrEquals: longerThanOrEquals,
|
|
340
|
+
lt: lessThan,
|
|
341
|
+
lte: lessThanOrEquals,
|
|
342
|
+
matches: matches,
|
|
343
|
+
notEquals: notEquals,
|
|
344
|
+
notInside: notInside,
|
|
345
|
+
notMatches: notMatches,
|
|
346
|
+
numberEquals: numberEquals,
|
|
347
|
+
numberNotEquals: numberNotEquals,
|
|
348
|
+
shorterThan: shorterThan,
|
|
349
|
+
shorterThanOrEquals: shorterThanOrEquals,
|
|
350
|
+
startsWith: startsWith
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
var baseRules = rules();
|
|
355
|
+
function getRule(ruleName) {
|
|
356
|
+
return baseRules[ruleName];
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function eachEnforceRule(action) {
|
|
360
|
+
for (var ruleName in baseRules) {
|
|
361
|
+
var ruleFn = getRule(ruleName);
|
|
362
|
+
if (isFunction(ruleFn)) {
|
|
363
|
+
action(ruleName, ruleFn);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// eslint-disable-next-line max-lines-per-function
|
|
369
|
+
function createContext(init) {
|
|
370
|
+
var storage = { ancestry: [] };
|
|
371
|
+
return {
|
|
372
|
+
bind: bind,
|
|
373
|
+
run: run,
|
|
374
|
+
use: use,
|
|
375
|
+
useX: useX
|
|
376
|
+
};
|
|
377
|
+
function useX(errorMessage) {
|
|
378
|
+
var _a;
|
|
379
|
+
return ((_a = storage.ctx) !== null && _a !== void 0 ? _a : throwError(defaultTo(errorMessage, 'Context was used after it was closed')));
|
|
380
|
+
}
|
|
381
|
+
function run(ctxRef, fn) {
|
|
382
|
+
var _a;
|
|
383
|
+
var parentContext = use();
|
|
384
|
+
var out = assign({}, parentContext ? parentContext : {}, (_a = optionalFunctionValue(init, ctxRef, parentContext)) !== null && _a !== void 0 ? _a : ctxRef);
|
|
385
|
+
var ctx = set(Object.freeze(out));
|
|
386
|
+
storage.ancestry.unshift(ctx);
|
|
387
|
+
var res = fn(ctx);
|
|
388
|
+
clear();
|
|
389
|
+
return res;
|
|
390
|
+
}
|
|
391
|
+
function bind(ctxRef, fn) {
|
|
392
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
393
|
+
// @ts-ignore - this one's pretty hard to get right
|
|
394
|
+
var returnedFn = function () {
|
|
395
|
+
var runTimeArgs = [];
|
|
396
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
397
|
+
runTimeArgs[_i] = arguments[_i];
|
|
398
|
+
}
|
|
399
|
+
return run(ctxRef, function () {
|
|
400
|
+
return fn.apply(void 0, runTimeArgs);
|
|
401
|
+
});
|
|
402
|
+
};
|
|
403
|
+
return returnedFn;
|
|
404
|
+
}
|
|
405
|
+
function use() {
|
|
406
|
+
return storage.ctx;
|
|
407
|
+
}
|
|
408
|
+
function set(value) {
|
|
409
|
+
return (storage.ctx = value);
|
|
410
|
+
}
|
|
411
|
+
function clear() {
|
|
412
|
+
var _a;
|
|
413
|
+
storage.ancestry.shift();
|
|
414
|
+
set((_a = storage.ancestry[0]) !== null && _a !== void 0 ? _a : null);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
var ctx = createContext(function (ctxRef, parentContext) {
|
|
419
|
+
var base = {
|
|
420
|
+
value: ctxRef.value,
|
|
421
|
+
meta: ctxRef.meta || {}
|
|
422
|
+
};
|
|
423
|
+
if (!parentContext) {
|
|
424
|
+
return assign(base, {
|
|
425
|
+
parent: emptyParent
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
else if (ctxRef.set) {
|
|
429
|
+
return assign(base, {
|
|
430
|
+
parent: function () { return stripContext(parentContext); }
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
return parentContext;
|
|
434
|
+
});
|
|
435
|
+
function stripContext(ctx) {
|
|
436
|
+
if (!ctx) {
|
|
437
|
+
return ctx;
|
|
438
|
+
}
|
|
439
|
+
return {
|
|
440
|
+
value: ctx.value,
|
|
441
|
+
meta: ctx.meta,
|
|
442
|
+
parent: ctx.parent
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
function emptyParent() {
|
|
446
|
+
return null;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/*! *****************************************************************************
|
|
450
|
+
Copyright (c) Microsoft Corporation.
|
|
451
|
+
|
|
452
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
453
|
+
purpose with or without fee is hereby granted.
|
|
454
|
+
|
|
455
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
456
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
457
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
458
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
459
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
460
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
461
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
462
|
+
***************************************************************************** */
|
|
463
|
+
|
|
464
|
+
function __spreadArray(to, from, pack) {
|
|
465
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
466
|
+
if (ar || !(i in from)) {
|
|
467
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
468
|
+
ar[i] = from[i];
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
function isProxySupported() {
|
|
475
|
+
try {
|
|
476
|
+
return isFunction(Proxy);
|
|
477
|
+
}
|
|
478
|
+
catch (_a) {
|
|
479
|
+
return false;
|
|
480
|
+
}
|
|
481
|
+
}
|
|
76
482
|
|
|
77
483
|
function ruleReturn(pass, message) {
|
|
78
484
|
var output = { pass: pass };
|
|
@@ -91,6 +497,182 @@
|
|
|
91
497
|
return defaultTo(callback, passing());
|
|
92
498
|
}
|
|
93
499
|
|
|
500
|
+
/**
|
|
501
|
+
* Transform the result of a rule into a standard format
|
|
502
|
+
*/
|
|
503
|
+
function transformResult(result, ruleName, value) {
|
|
504
|
+
var args = [];
|
|
505
|
+
for (var _i = 3; _i < arguments.length; _i++) {
|
|
506
|
+
args[_i - 3] = arguments[_i];
|
|
507
|
+
}
|
|
508
|
+
validateResult(result);
|
|
509
|
+
// if result is boolean
|
|
510
|
+
if (isBoolean(result)) {
|
|
511
|
+
return ruleReturn(result);
|
|
512
|
+
}
|
|
513
|
+
else {
|
|
514
|
+
return ruleReturn(result.pass, optionalFunctionValue.apply(void 0, __spreadArray([result.message, ruleName, value], args)));
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
function validateResult(result) {
|
|
518
|
+
// if result is boolean, or if result.pass is boolean
|
|
519
|
+
if (isBoolean(result) || (result && isBoolean(result.pass))) {
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
throwError('Incorrect return value for rule: ' + JSON.stringify(result));
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
function enforceEager(value) {
|
|
526
|
+
var target = {};
|
|
527
|
+
if (!isProxySupported()) {
|
|
528
|
+
eachEnforceRule(function (ruleName, ruleFn) {
|
|
529
|
+
target[ruleName] = genRuleCall(target, ruleFn, ruleName);
|
|
530
|
+
});
|
|
531
|
+
return target;
|
|
532
|
+
}
|
|
533
|
+
var proxy = new Proxy(target, {
|
|
534
|
+
get: function (_, ruleName) {
|
|
535
|
+
var rule = getRule(ruleName);
|
|
536
|
+
if (rule) {
|
|
537
|
+
return genRuleCall(proxy, rule, ruleName);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
});
|
|
541
|
+
return proxy;
|
|
542
|
+
function genRuleCall(target, rule, ruleName) {
|
|
543
|
+
return function ruleCall() {
|
|
544
|
+
var args = [];
|
|
545
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
546
|
+
args[_i] = arguments[_i];
|
|
547
|
+
}
|
|
548
|
+
var transformedResult = transformResult.apply(void 0, __spreadArray([ctx.run({ value: value }, function () { return rule.apply(void 0, __spreadArray([value], args)); }),
|
|
549
|
+
ruleName,
|
|
550
|
+
value], args));
|
|
551
|
+
if (!transformedResult.pass) {
|
|
552
|
+
if (isEmpty(transformedResult.message)) {
|
|
553
|
+
throwError("enforce/" + ruleName + " failed with " + JSON.stringify(value));
|
|
554
|
+
}
|
|
555
|
+
else {
|
|
556
|
+
// Explicitly throw a string so that vest.test can pick it up as the validation error message
|
|
557
|
+
throw transformedResult.message;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
return target;
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// eslint-disable-next-line max-lines-per-function
|
|
566
|
+
function genEnforceLazy(key) {
|
|
567
|
+
var registeredRules = [];
|
|
568
|
+
var lazyMessage;
|
|
569
|
+
return addLazyRule(key);
|
|
570
|
+
// eslint-disable-next-line max-lines-per-function
|
|
571
|
+
function addLazyRule(ruleName) {
|
|
572
|
+
// eslint-disable-next-line max-lines-per-function
|
|
573
|
+
return function () {
|
|
574
|
+
var args = [];
|
|
575
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
576
|
+
args[_i] = arguments[_i];
|
|
577
|
+
}
|
|
578
|
+
var rule = getRule(ruleName);
|
|
579
|
+
registeredRules.push(function (value) {
|
|
580
|
+
return transformResult.apply(void 0, __spreadArray([rule.apply(void 0, __spreadArray([value], args)), ruleName, value], args));
|
|
581
|
+
});
|
|
582
|
+
var proxy = {
|
|
583
|
+
run: function (value) {
|
|
584
|
+
return defaultToPassing(mapFirst(registeredRules, function (rule, breakout) {
|
|
585
|
+
var _a;
|
|
586
|
+
var res = ctx.run({ value: value }, function () { return rule(value); });
|
|
587
|
+
if (!res.pass) {
|
|
588
|
+
breakout(ruleReturn(!!res.pass, (_a = optionalFunctionValue(lazyMessage, value, res.message)) !== null && _a !== void 0 ? _a : res.message));
|
|
589
|
+
}
|
|
590
|
+
}));
|
|
591
|
+
},
|
|
592
|
+
test: function (value) { return proxy.run(value).pass; },
|
|
593
|
+
message: function (message) {
|
|
594
|
+
if (message) {
|
|
595
|
+
lazyMessage = message;
|
|
596
|
+
}
|
|
597
|
+
return proxy;
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
if (!isProxySupported()) {
|
|
601
|
+
eachEnforceRule(function (ruleName) {
|
|
602
|
+
proxy[ruleName] = addLazyRule(ruleName);
|
|
603
|
+
});
|
|
604
|
+
return proxy;
|
|
605
|
+
}
|
|
606
|
+
// reassigning the proxy here is not pretty
|
|
607
|
+
// but it's a cleaner way of getting `run` and `test` for free
|
|
608
|
+
proxy = new Proxy(proxy, {
|
|
609
|
+
get: function (target, key) {
|
|
610
|
+
if (getRule(key)) {
|
|
611
|
+
return addLazyRule(key);
|
|
612
|
+
}
|
|
613
|
+
return target[key]; // already has `run` and `test` on it
|
|
614
|
+
}
|
|
615
|
+
});
|
|
616
|
+
return proxy;
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Enforce is quite complicated, I want to explain it in detail.
|
|
623
|
+
* It is dynamic in nature, so a lot of proxy objects are involved.
|
|
624
|
+
*
|
|
625
|
+
* Enforce has two main interfaces
|
|
626
|
+
* 1. eager
|
|
627
|
+
* 2. lazy
|
|
628
|
+
*
|
|
629
|
+
* The eager interface is the most commonly used, and the easier to understand.
|
|
630
|
+
* It throws an error when a rule is not satisfied.
|
|
631
|
+
* The eager interface is declared in enforceEager.ts and it is quite simple to understand.
|
|
632
|
+
* enforce is called with a value, and the return value is a proxy object that points back to all the rules.
|
|
633
|
+
* When a rule is called, the value is mapped as its first argument, and if the rule passes, the same
|
|
634
|
+
* proxy object is returned. Otherwise, an error is thrown.
|
|
635
|
+
*
|
|
636
|
+
* The lazy interface works quite differently. It is declared in genEnforceLazy.ts.
|
|
637
|
+
* Rather than calling enforce directly, the lazy interface has all the rules as "methods" (only by proxy).
|
|
638
|
+
* Calling the first function in the chain will initialize an array of calls. It stores the different rule calls
|
|
639
|
+
* and the parameters passed to them. None of the rules are called yet.
|
|
640
|
+
* The rules are only invoked in sequence once either of these chained functions are called:
|
|
641
|
+
* 1. test(value)
|
|
642
|
+
* 2. run(value)
|
|
643
|
+
*
|
|
644
|
+
* Calling run or test will call all the rules in sequence, with the difference that test will only return a boolean value,
|
|
645
|
+
* while run will return an object with the validation result and an optional message created by the rule.
|
|
646
|
+
*/
|
|
647
|
+
function genEnforce() {
|
|
648
|
+
var target = {
|
|
649
|
+
context: function () { return ctx.useX(); },
|
|
650
|
+
extend: function (customRules) {
|
|
651
|
+
assign(baseRules, customRules);
|
|
652
|
+
}
|
|
653
|
+
};
|
|
654
|
+
if (!isProxySupported()) {
|
|
655
|
+
eachEnforceRule(function (ruleName) {
|
|
656
|
+
// Only on the first rule access - start the chain of calls
|
|
657
|
+
target[ruleName] = genEnforceLazy(ruleName);
|
|
658
|
+
});
|
|
659
|
+
return assign(enforceEager, target);
|
|
660
|
+
}
|
|
661
|
+
return new Proxy(assign(enforceEager, target), {
|
|
662
|
+
get: function (target, key) {
|
|
663
|
+
if (key in target) {
|
|
664
|
+
return target[key];
|
|
665
|
+
}
|
|
666
|
+
if (!getRule(key)) {
|
|
667
|
+
return;
|
|
668
|
+
}
|
|
669
|
+
// Only on the first rule access - start the chain of calls
|
|
670
|
+
return genEnforceLazy(key);
|
|
671
|
+
}
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
genEnforce();
|
|
675
|
+
|
|
94
676
|
function runLazyRule(lazyRule, currentValue) {
|
|
95
677
|
try {
|
|
96
678
|
return lazyRule.run(currentValue);
|
|
@@ -122,7 +704,7 @@
|
|
|
122
704
|
test: function (value) { return run(value).pass; }
|
|
123
705
|
});
|
|
124
706
|
function run(value) {
|
|
125
|
-
return
|
|
707
|
+
return ctx.run({ value: value }, function () {
|
|
126
708
|
return defaultToPassing(mapFirst(composites, function (composite, breakout) {
|
|
127
709
|
/* HACK: Just a small white lie. ~~HELP WANTED~~.
|
|
128
710
|
The ideal is that instead of `TLazyRuleRunners` We would simply use `TLazy` to begin with.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";!function(n,
|
|
1
|
+
"use strict";!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(n="undefined"!=typeof globalThis?globalThis:n||self).compose=t()}(this,(function(){function n(n,t){function r(n){e=!0,u=n}for(var e=!1,u=null,i=0;i<n.length;i++)if(t(n[i],r,i),e)return u}function t(n){return"function"==typeof n}function r(n){for(var r=[],e=1;e<arguments.length;e++)r[e-1]=arguments[e];return t(n)?n.apply(void 0,r):n}function e(n,t){var e;return null!==(e=r(n))&&void 0!==e?e:t}function u(n,t){throw Error(e(t,n))}function i(n){return function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return!n.apply(void 0,t)}}function o(n){return"number"==typeof n}function s(n,t){return n.length===Number(t)}function f(n){if(n){if(o(n))return 0===n;if(Object.prototype.hasOwnProperty.call(n,"length"))return s(n,0);if("object"==typeof n)return s(Object.keys(n),0)}return!0}function a(n){return null===n}function c(n){return void 0===n}function l(n){return null===n||c(n)}function N(n){return String(n)===n}function h(n,t){return N(n)&&N(t)&&n.endsWith(t)}function p(n,t){return n===t}function v(n){var t=Number(n);return!(isNaN(parseFloat(String(n)))||isNaN(Number(n))||!isFinite(t))}function g(n,t){return v(n)&&v(t)&&Number(n)>Number(t)}function m(n,t){return v(n)&&v(t)&&Number(n)>=Number(t)}function d(n){return!!Array.isArray(n)}function y(n,t){return!!(d(t)||N(t)&&N(n))&&-1!==t.indexOf(n)}function b(n,t){return v(n)&&v(t)&&Number(n)<=Number(t)}function O(n,t,r){return m(n,t)&&b(n,r)}function x(n){return l(n)||N(n)&&!n.trim()}function E(n){return!!n===n}function w(n,t){return n in t}function T(n){return Number.isNaN(n)}function q(n){return!!v(n)&&0>Number(n)}function j(n){return!!n}function S(n,t){if(l(t))return!1;for(var r in t)if(t[r]===n)return!0;return!1}function A(n,t){return v(n)&&v(t)&&Number(n)<Number(t)}function B(n,t){return t instanceof RegExp?t.test(n):!!N(t)&&new RegExp(t).test(n)}function P(n,t){return v(n)&&v(t)&&Number(n)===Number(t)}function W(n,t){return N(n)&&N(t)&&n.startsWith(t)}function k(n){for(var r in Nn){var e=Nn[r];t(e)&&n(r,e)}}function F(){return null}function I(n,t,r){if(r||2===arguments.length)for(var e,u=0,i=t.length;u<i;u++)!e&&u in t||(e||(e=Array.prototype.slice.call(t,0,u)),e[u]=t[u]);return n.concat(e||Array.prototype.slice.call(t))}function J(){try{return t(Proxy)}catch(n){return!1}}function K(n,t){return n={pass:n},t&&(n.message=t),n}function R(n,t,e){for(var i=[],o=3;o<arguments.length;o++)i[o-3]=arguments[o];return E(n)||n&&E(n.pass)||u("Incorrect return value for rule: "+JSON.stringify(n)),E(n)?K(n):K(n.pass,r.apply(void 0,I([n.message,t,e],i)))}function U(n){function t(t,r,e){return function(){for(var i=[],o=0;o<arguments.length;o++)i[o]=arguments[o];if(!(o=R.apply(void 0,I([hn.run({value:n},(function(){return r.apply(void 0,I([n],i))})),e,n],i))).pass){if(!f(o.message))throw o.message;u("enforce/"+e+" failed with "+JSON.stringify(n))}return t}}var r={};if(!J())return k((function(n,e){r[n]=t(r,e,n)})),r;var e=new Proxy(r,{get:function(n,r){if(n=Nn[r])return t(e,n,r)}});return e}function V(t){var u,i=[];return function t(o){return function(){for(var s=[],f=0;f<arguments.length;f++)s[f]=arguments[f];var a=Nn[o];i.push((function(n){return R.apply(void 0,I([a.apply(void 0,I([n],s)),o,n],s))}));var c={run:function(t){return e(n(i,(function(n,e){var i,o=hn.run({value:t},(function(){return n(t)}));o.pass||e(K(!!o.pass,null!==(i=r(u,t,o.message))&&void 0!==i?i:o.message))})),K(!0))},test:function(n){return c.run(n).pass},message:function(n){return n&&(u=n),c}};return J()?c=new Proxy(c,{get:function(n,r){return Nn[r]?t(r):n[r]}}):(k((function(n){c[n]=t(n)})),c)}}(t)}var X,z=i(o),C=i(s),M=i(f),D=Object.assign,G=i(a),H=i(c),L=i(l),Q=i(h),Y=i(p),Z=i(v),$=i(d),_=i(y),nn=i(O),tn=i(x),rn=i(E),en=i(w),un=i(T),on=i(q),sn=i(N),fn=i(j),an=i(S),cn=i(B),ln=i(P),Nn={condition:function(n,t){try{return t(n)}catch(n){return!1}},doesNotEndWith:Q,doesNotStartWith:i(W),endsWith:h,equals:p,greaterThan:g,greaterThanOrEquals:m,gt:g,gte:m,inside:y,isArray:d,isBetween:O,isBlank:x,isBoolean:E,isEmpty:f,isEven:function(n){return!!v(n)&&0==n%2},isFalsy:fn,isKeyOf:w,isNaN:T,isNegative:q,isNotArray:$,isNotBetween:nn,isNotBlank:tn,isNotBoolean:rn,isNotEmpty:M,isNotKeyOf:en,isNotNaN:un,isNotNull:G,isNotNullish:L,isNotNumber:z,isNotNumeric:Z,isNotString:sn,isNotUndefined:H,isNotValueOf:an,isNull:a,isNullish:l,isNumber:o,isNumeric:v,isOdd:function(n){return!!v(n)&&0!=n%2},isPositive:on,isString:N,isTruthy:j,isUndefined:c,isValueOf:S,lengthEquals:s,lengthNotEquals:C,lessThan:A,lessThanOrEquals:b,longerThan:function(n,t){return n.length>Number(t)},longerThanOrEquals:function(n,t){return n.length>=Number(t)},lt:A,lte:b,matches:B,notEquals:Y,notInside:_,notMatches:cn,numberEquals:P,numberNotEquals:ln,shorterThan:function(n,t){return n.length<Number(t)},shorterThanOrEquals:function(n,t){return n.length<=Number(t)},startsWith:W},hn=function(n){function t(t,e){var u,s,f=i();return t=D({},f||{},null!==(u=r(n,t,f))&&void 0!==u?u:t),u=o.ctx=Object.freeze(t),o.ancestry.unshift(u),e=e(u),o.ancestry.shift(),o.ctx=null!==(s=o.ancestry[0])&&void 0!==s?s:null,e}function i(){return o.ctx}var o={ancestry:[]};return{bind:function(n,r){return function(){for(var e=[],u=0;u<arguments.length;u++)e[u]=arguments[u];return t(n,(function(){return r.apply(void 0,e)}))}},run:t,use:i,useX:function(n){var t;return null!==(t=o.ctx)&&void 0!==t?t:u(e(n,"Context was used after it was closed"))}}}((function(n,t){var r={value:n.value,meta:n.meta||{}};return t?n.set?D(r,{parent:function(){return t?{value:t.value,meta:t.meta,parent:t.parent}:t}}):t:D(r,{parent:F})}));return X={context:function(){return hn.useX()},extend:function(n){D(Nn,n)}},J()?new Proxy(D(U,X),{get:function(n,t){return t in n?n[t]:Nn[t]?V(t):void 0}}):(k((function(n){X[n]=V(n)})),D(U,X)),function(){function t(t){return hn.run({value:t},(function(){return e(n(r,(function(n,r){try{var e=n.run(t)}catch(n){e=K(!1)}e.pass||r(e)})),K(!0))}))}for(var r=[],i=0;i<arguments.length;i++)r[i]=arguments[i];return Object.assign((function(n){if(!(n=t(n)).pass){if(!f(n.message))throw n.message;u()}}),{run:t,test:function(n){return t(n).pass}})}}));
|