n4s 5.0.0 → 5.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 +2 -0
- package/date/package.json +9 -0
- package/dist/cjs/compose.development.js +9 -13
- package/dist/cjs/compose.js +0 -1
- package/dist/cjs/compose.production.js +1 -1
- package/dist/cjs/compounds.development.js +16 -32
- package/dist/cjs/compounds.js +0 -1
- package/dist/cjs/compounds.production.js +1 -1
- package/dist/cjs/date.development.js +186 -0
- package/dist/cjs/date.js +6 -0
- package/dist/cjs/date.production.js +1 -0
- package/dist/cjs/email.development.js +363 -0
- package/dist/cjs/email.js +6 -0
- package/dist/cjs/email.production.js +1 -0
- package/dist/cjs/isURL.development.js +353 -0
- package/dist/cjs/isURL.js +6 -0
- package/dist/cjs/isURL.production.js +1 -0
- package/dist/cjs/n4s.development.js +134 -213
- package/dist/cjs/n4s.js +0 -1
- package/dist/cjs/n4s.production.js +1 -1
- package/dist/cjs/schema.development.js +13 -22
- package/dist/cjs/schema.js +0 -1
- package/dist/cjs/schema.production.js +1 -1
- package/dist/es/compose.development.js +11 -15
- package/dist/es/compose.production.js +1 -1
- package/dist/es/compounds.development.js +16 -32
- package/dist/es/compounds.production.js +1 -1
- package/dist/es/date.development.js +184 -0
- package/dist/es/date.production.js +1 -0
- package/dist/es/email.development.js +361 -0
- package/dist/es/email.production.js +1 -0
- package/dist/es/isURL.development.js +351 -0
- package/dist/es/isURL.production.js +1 -0
- package/dist/es/n4s.development.js +155 -232
- package/dist/es/n4s.production.js +1 -1
- package/dist/es/schema.development.js +13 -20
- package/dist/es/schema.production.js +1 -1
- package/dist/umd/compose.development.js +12 -16
- package/dist/umd/compose.production.js +1 -1
- package/dist/umd/compounds.development.js +19 -35
- package/dist/umd/compounds.production.js +1 -1
- package/dist/umd/date.development.js +190 -0
- package/dist/umd/date.production.js +1 -0
- package/dist/umd/email.development.js +367 -0
- package/dist/umd/email.production.js +1 -0
- package/dist/umd/isURL.development.js +357 -0
- package/dist/umd/isURL.production.js +1 -0
- package/dist/umd/n4s.development.js +137 -216
- package/dist/umd/n4s.production.js +1 -1
- package/dist/umd/schema.development.js +16 -25
- package/dist/umd/schema.production.js +1 -1
- package/email/package.json +9 -0
- package/isURL/package.json +9 -0
- package/package.json +159 -55
- package/testUtils/TEnforceMock.ts +3 -0
- package/types/compose.d.ts +22 -23
- package/types/compose.d.ts.map +1 -0
- package/types/compounds.d.ts +22 -24
- package/types/compounds.d.ts.map +1 -0
- package/types/date.d.ts +18 -0
- package/types/date.d.ts.map +1 -0
- package/types/email.d.ts +12 -0
- package/types/email.d.ts.map +1 -0
- package/types/isURL.d.ts +12 -0
- package/types/isURL.d.ts.map +1 -0
- package/types/n4s.d.ts +30 -27
- package/types/n4s.d.ts.map +1 -0
- package/types/schema.d.ts +22 -23
- package/types/schema.d.ts.map +1 -0
- package/tsconfig.json +0 -8
|
@@ -1,15 +1,43 @@
|
|
|
1
|
-
import { bindNot, isStringValue, numberEquals, greaterThan, isArray, isNumeric, isNullish, isBoolean, isEmpty, isNotArray, isNotEmpty, isNotNull, isNotNullish, isNotNumeric, isNotUndefined, isNull, isPositive, isUndefined, lengthEquals, lengthNotEquals, longerThan, numberNotEquals,
|
|
1
|
+
import { assign, bindNot, isStringValue, numberEquals, greaterThan, isArray, isNumeric, isNullish, isBoolean, isEmpty, isNotArray, isNotEmpty, isNotNull, isNotNullish, isNotNumeric, isNotUndefined, isNull, isPositive, isUndefined, lengthEquals, lengthNotEquals, longerThan, numberNotEquals, defaultTo, optionalFunctionValue, invariant, StringObject, mapFirst } from 'vest-utils';
|
|
2
2
|
import { createCascade } from 'context';
|
|
3
3
|
|
|
4
|
+
const ctx = createCascade((ctxRef, parentContext) => {
|
|
5
|
+
const base = {
|
|
6
|
+
value: ctxRef.value,
|
|
7
|
+
meta: ctxRef.meta || {},
|
|
8
|
+
};
|
|
9
|
+
if (!parentContext) {
|
|
10
|
+
return assign(base, {
|
|
11
|
+
parent: emptyParent,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
else if (ctxRef.set) {
|
|
15
|
+
return assign(base, {
|
|
16
|
+
parent: () => stripContext(parentContext),
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return parentContext;
|
|
20
|
+
});
|
|
21
|
+
function stripContext(ctx) {
|
|
22
|
+
return {
|
|
23
|
+
value: ctx.value,
|
|
24
|
+
meta: ctx.meta,
|
|
25
|
+
parent: ctx.parent,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function emptyParent() {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
|
|
4
32
|
function endsWith(value, arg1) {
|
|
5
33
|
return isStringValue(value) && isStringValue(arg1) && value.endsWith(arg1);
|
|
6
34
|
}
|
|
7
|
-
|
|
35
|
+
const doesNotEndWith = bindNot(endsWith);
|
|
8
36
|
|
|
9
37
|
function equals(value, arg1) {
|
|
10
38
|
return value === arg1;
|
|
11
39
|
}
|
|
12
|
-
|
|
40
|
+
const notEquals = bindNot(equals);
|
|
13
41
|
|
|
14
42
|
function greaterThanOrEquals(value, gte) {
|
|
15
43
|
return numberEquals(value, gte) || greaterThan(value, gte);
|
|
@@ -25,7 +53,7 @@ function inside(value, arg1) {
|
|
|
25
53
|
}
|
|
26
54
|
return false;
|
|
27
55
|
}
|
|
28
|
-
|
|
56
|
+
const notInside = bindNot(inside);
|
|
29
57
|
|
|
30
58
|
function lessThan(value, lt) {
|
|
31
59
|
return isNumeric(value) && isNumeric(lt) && Number(value) < Number(lt);
|
|
@@ -38,19 +66,19 @@ function lessThanOrEquals(value, lte) {
|
|
|
38
66
|
function isBetween(value, min, max) {
|
|
39
67
|
return greaterThanOrEquals(value, min) && lessThanOrEquals(value, max);
|
|
40
68
|
}
|
|
41
|
-
|
|
69
|
+
const isNotBetween = bindNot(isBetween);
|
|
42
70
|
|
|
43
71
|
function isBlank(value) {
|
|
44
72
|
return isNullish(value) || (isStringValue(value) && !value.trim());
|
|
45
73
|
}
|
|
46
|
-
|
|
74
|
+
const isNotBlank = bindNot(isBlank);
|
|
47
75
|
|
|
48
|
-
|
|
76
|
+
const isNotBoolean = bindNot(isBoolean);
|
|
49
77
|
|
|
50
78
|
/**
|
|
51
79
|
* Validates that a given value is an even number
|
|
52
80
|
*/
|
|
53
|
-
|
|
81
|
+
const isEven = (value) => {
|
|
54
82
|
if (isNumeric(value)) {
|
|
55
83
|
return value % 2 === 0;
|
|
56
84
|
}
|
|
@@ -60,12 +88,12 @@ var isEven = function (value) {
|
|
|
60
88
|
function isKeyOf(key, obj) {
|
|
61
89
|
return key in obj;
|
|
62
90
|
}
|
|
63
|
-
|
|
91
|
+
const isNotKeyOf = bindNot(isKeyOf);
|
|
64
92
|
|
|
65
93
|
function isNaN(value) {
|
|
66
94
|
return Number.isNaN(value);
|
|
67
95
|
}
|
|
68
|
-
|
|
96
|
+
const isNotNaN = bindNot(isNaN);
|
|
69
97
|
|
|
70
98
|
function isNegative(value) {
|
|
71
99
|
return lessThan(value, 0);
|
|
@@ -74,37 +102,37 @@ function isNegative(value) {
|
|
|
74
102
|
function isNumber(value) {
|
|
75
103
|
return Boolean(typeof value === 'number');
|
|
76
104
|
}
|
|
77
|
-
|
|
105
|
+
const isNotNumber = bindNot(isNumber);
|
|
78
106
|
|
|
79
107
|
/**
|
|
80
108
|
* Validates that a given value is an odd number
|
|
81
109
|
*/
|
|
82
|
-
|
|
110
|
+
const isOdd = (value) => {
|
|
83
111
|
if (isNumeric(value)) {
|
|
84
112
|
return value % 2 !== 0;
|
|
85
113
|
}
|
|
86
114
|
return false;
|
|
87
115
|
};
|
|
88
116
|
|
|
89
|
-
|
|
117
|
+
const isNotString = bindNot(isStringValue);
|
|
90
118
|
|
|
91
119
|
function isTruthy(value) {
|
|
92
120
|
return !!value;
|
|
93
121
|
}
|
|
94
|
-
|
|
122
|
+
const isFalsy = bindNot(isTruthy);
|
|
95
123
|
|
|
96
124
|
function isValueOf(value, objectToCheck) {
|
|
97
125
|
if (isNullish(objectToCheck)) {
|
|
98
126
|
return false;
|
|
99
127
|
}
|
|
100
|
-
for (
|
|
128
|
+
for (const key in objectToCheck) {
|
|
101
129
|
if (objectToCheck[key] === value) {
|
|
102
130
|
return true;
|
|
103
131
|
}
|
|
104
132
|
}
|
|
105
133
|
return false;
|
|
106
134
|
}
|
|
107
|
-
|
|
135
|
+
const isNotValueOf = bindNot(isValueOf);
|
|
108
136
|
|
|
109
137
|
function longerThanOrEquals(value, arg1) {
|
|
110
138
|
return greaterThanOrEquals(value.length, arg1);
|
|
@@ -117,11 +145,9 @@ function matches(value, regex) {
|
|
|
117
145
|
else if (isStringValue(regex)) {
|
|
118
146
|
return new RegExp(regex).test(value);
|
|
119
147
|
}
|
|
120
|
-
|
|
121
|
-
return false;
|
|
122
|
-
}
|
|
148
|
+
return false;
|
|
123
149
|
}
|
|
124
|
-
|
|
150
|
+
const notMatches = bindNot(matches);
|
|
125
151
|
|
|
126
152
|
function condition(value, callback) {
|
|
127
153
|
try {
|
|
@@ -143,156 +169,82 @@ function shorterThanOrEquals(value, arg1) {
|
|
|
143
169
|
function startsWith(value, arg1) {
|
|
144
170
|
return isStringValue(value) && isStringValue(arg1) && value.startsWith(arg1);
|
|
145
171
|
}
|
|
146
|
-
|
|
172
|
+
const doesNotStartWith = bindNot(startsWith);
|
|
147
173
|
|
|
148
174
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, max-lines-per-function
|
|
149
175
|
function rules() {
|
|
150
176
|
return {
|
|
151
|
-
condition
|
|
152
|
-
doesNotEndWith
|
|
153
|
-
doesNotStartWith
|
|
154
|
-
endsWith
|
|
155
|
-
equals
|
|
156
|
-
greaterThan
|
|
157
|
-
greaterThanOrEquals
|
|
177
|
+
condition,
|
|
178
|
+
doesNotEndWith,
|
|
179
|
+
doesNotStartWith,
|
|
180
|
+
endsWith,
|
|
181
|
+
equals,
|
|
182
|
+
greaterThan,
|
|
183
|
+
greaterThanOrEquals,
|
|
158
184
|
gt: greaterThan,
|
|
159
185
|
gte: greaterThanOrEquals,
|
|
160
|
-
inside
|
|
161
|
-
isArray
|
|
162
|
-
isBetween
|
|
163
|
-
isBlank
|
|
164
|
-
isBoolean
|
|
165
|
-
isEmpty
|
|
166
|
-
isEven
|
|
167
|
-
isFalsy
|
|
168
|
-
isKeyOf
|
|
169
|
-
isNaN
|
|
170
|
-
isNegative
|
|
171
|
-
isNotArray
|
|
172
|
-
isNotBetween
|
|
173
|
-
isNotBlank
|
|
174
|
-
isNotBoolean
|
|
175
|
-
isNotEmpty
|
|
176
|
-
isNotKeyOf
|
|
177
|
-
isNotNaN
|
|
178
|
-
isNotNull
|
|
179
|
-
isNotNullish
|
|
180
|
-
isNotNumber
|
|
181
|
-
isNotNumeric
|
|
182
|
-
isNotString
|
|
183
|
-
isNotUndefined
|
|
184
|
-
isNotValueOf
|
|
185
|
-
isNull
|
|
186
|
-
isNullish
|
|
187
|
-
isNumber
|
|
188
|
-
isNumeric
|
|
189
|
-
isOdd
|
|
190
|
-
isPositive
|
|
186
|
+
inside,
|
|
187
|
+
isArray,
|
|
188
|
+
isBetween,
|
|
189
|
+
isBlank,
|
|
190
|
+
isBoolean,
|
|
191
|
+
isEmpty,
|
|
192
|
+
isEven,
|
|
193
|
+
isFalsy,
|
|
194
|
+
isKeyOf,
|
|
195
|
+
isNaN,
|
|
196
|
+
isNegative,
|
|
197
|
+
isNotArray,
|
|
198
|
+
isNotBetween,
|
|
199
|
+
isNotBlank,
|
|
200
|
+
isNotBoolean,
|
|
201
|
+
isNotEmpty,
|
|
202
|
+
isNotKeyOf,
|
|
203
|
+
isNotNaN,
|
|
204
|
+
isNotNull,
|
|
205
|
+
isNotNullish,
|
|
206
|
+
isNotNumber,
|
|
207
|
+
isNotNumeric,
|
|
208
|
+
isNotString,
|
|
209
|
+
isNotUndefined,
|
|
210
|
+
isNotValueOf,
|
|
211
|
+
isNull,
|
|
212
|
+
isNullish,
|
|
213
|
+
isNumber,
|
|
214
|
+
isNumeric,
|
|
215
|
+
isOdd,
|
|
216
|
+
isPositive,
|
|
191
217
|
isString: isStringValue,
|
|
192
|
-
isTruthy
|
|
193
|
-
isUndefined
|
|
194
|
-
isValueOf
|
|
195
|
-
lengthEquals
|
|
196
|
-
lengthNotEquals
|
|
197
|
-
lessThan
|
|
198
|
-
lessThanOrEquals
|
|
199
|
-
longerThan
|
|
200
|
-
longerThanOrEquals
|
|
218
|
+
isTruthy,
|
|
219
|
+
isUndefined,
|
|
220
|
+
isValueOf,
|
|
221
|
+
lengthEquals,
|
|
222
|
+
lengthNotEquals,
|
|
223
|
+
lessThan,
|
|
224
|
+
lessThanOrEquals,
|
|
225
|
+
longerThan,
|
|
226
|
+
longerThanOrEquals,
|
|
201
227
|
lt: lessThan,
|
|
202
228
|
lte: lessThanOrEquals,
|
|
203
|
-
matches
|
|
204
|
-
notEquals
|
|
205
|
-
notInside
|
|
206
|
-
notMatches
|
|
207
|
-
numberEquals
|
|
208
|
-
numberNotEquals
|
|
209
|
-
shorterThan
|
|
210
|
-
shorterThanOrEquals
|
|
211
|
-
startsWith
|
|
229
|
+
matches,
|
|
230
|
+
notEquals,
|
|
231
|
+
notInside,
|
|
232
|
+
notMatches,
|
|
233
|
+
numberEquals,
|
|
234
|
+
numberNotEquals,
|
|
235
|
+
shorterThan,
|
|
236
|
+
shorterThanOrEquals,
|
|
237
|
+
startsWith,
|
|
212
238
|
};
|
|
213
239
|
}
|
|
214
240
|
|
|
215
|
-
|
|
241
|
+
const baseRules = rules();
|
|
216
242
|
function getRule(ruleName) {
|
|
217
243
|
return baseRules[ruleName];
|
|
218
244
|
}
|
|
219
245
|
|
|
220
|
-
function eachEnforceRule(action) {
|
|
221
|
-
for (var ruleName in baseRules) {
|
|
222
|
-
var ruleFn = getRule(ruleName);
|
|
223
|
-
if (isFunction(ruleFn)) {
|
|
224
|
-
action(ruleName, ruleFn);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
var ctx = createCascade(function (ctxRef, parentContext) {
|
|
230
|
-
var base = {
|
|
231
|
-
value: ctxRef.value,
|
|
232
|
-
meta: ctxRef.meta || {}
|
|
233
|
-
};
|
|
234
|
-
if (!parentContext) {
|
|
235
|
-
return assign(base, {
|
|
236
|
-
parent: emptyParent
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
else if (ctxRef.set) {
|
|
240
|
-
return assign(base, {
|
|
241
|
-
parent: function () { return stripContext(parentContext); }
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
return parentContext;
|
|
245
|
-
});
|
|
246
|
-
function stripContext(ctx) {
|
|
247
|
-
if (!ctx) {
|
|
248
|
-
return ctx;
|
|
249
|
-
}
|
|
250
|
-
return {
|
|
251
|
-
value: ctx.value,
|
|
252
|
-
meta: ctx.meta,
|
|
253
|
-
parent: ctx.parent
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
function emptyParent() {
|
|
257
|
-
return null;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/******************************************************************************
|
|
261
|
-
Copyright (c) Microsoft Corporation.
|
|
262
|
-
|
|
263
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
264
|
-
purpose with or without fee is hereby granted.
|
|
265
|
-
|
|
266
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
267
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
268
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
269
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
270
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
271
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
272
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
273
|
-
***************************************************************************** */
|
|
274
|
-
|
|
275
|
-
function __spreadArray(to, from, pack) {
|
|
276
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
277
|
-
if (ar || !(i in from)) {
|
|
278
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
279
|
-
ar[i] = from[i];
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
function isProxySupported() {
|
|
286
|
-
try {
|
|
287
|
-
return isFunction(Proxy);
|
|
288
|
-
}
|
|
289
|
-
catch (_a) {
|
|
290
|
-
return false;
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
246
|
function ruleReturn(pass, message) {
|
|
295
|
-
|
|
247
|
+
const output = { pass };
|
|
296
248
|
if (message) {
|
|
297
249
|
output.message = message;
|
|
298
250
|
}
|
|
@@ -308,124 +260,106 @@ function defaultToPassing(callback) {
|
|
|
308
260
|
/**
|
|
309
261
|
* Transform the result of a rule into a standard format
|
|
310
262
|
*/
|
|
311
|
-
function transformResult(result, ruleName, value) {
|
|
312
|
-
var args = [];
|
|
313
|
-
for (var _i = 3; _i < arguments.length; _i++) {
|
|
314
|
-
args[_i - 3] = arguments[_i];
|
|
315
|
-
}
|
|
263
|
+
function transformResult(result, ruleName, value, ...args) {
|
|
316
264
|
validateResult(result);
|
|
317
265
|
// if result is boolean
|
|
318
266
|
if (isBoolean(result)) {
|
|
319
267
|
return ruleReturn(result);
|
|
320
268
|
}
|
|
321
|
-
|
|
322
|
-
return ruleReturn(result.pass, optionalFunctionValue.apply(void 0, __spreadArray([result.message, ruleName, value], args, false)));
|
|
323
|
-
}
|
|
269
|
+
return ruleReturn(result.pass, optionalFunctionValue(result.message, ruleName, value, ...args));
|
|
324
270
|
}
|
|
325
271
|
function validateResult(result) {
|
|
326
272
|
// if result is boolean, or if result.pass is boolean
|
|
327
273
|
invariant(isBoolean(result) || (result && isBoolean(result.pass)), 'Incorrect return value for rule: ' + JSON.stringify(result));
|
|
328
274
|
}
|
|
329
275
|
|
|
276
|
+
// eslint-disable-next-line max-lines-per-function
|
|
330
277
|
function enforceEager(value) {
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
if (!isProxySupported()) {
|
|
336
|
-
// We iterate over each of the rules, and add them to the target object being return by enforce
|
|
337
|
-
eachEnforceRule(function (ruleName, ruleFn) {
|
|
338
|
-
// We then wrap the rule with `genRuleCall` that adds the base enforce behavior
|
|
339
|
-
target[ruleName] = genRuleCall(target, ruleFn, ruleName);
|
|
340
|
-
});
|
|
341
|
-
return target;
|
|
342
|
-
}
|
|
278
|
+
const target = {
|
|
279
|
+
message,
|
|
280
|
+
};
|
|
281
|
+
let customMessage = undefined;
|
|
343
282
|
// We create a proxy intercepting access to the target object (which is empty).
|
|
344
|
-
|
|
345
|
-
get:
|
|
283
|
+
const proxy = new Proxy(target, {
|
|
284
|
+
get: (_, key) => {
|
|
346
285
|
// On property access, we identify if it is a rule or not.
|
|
347
|
-
|
|
286
|
+
const rule = getRule(key);
|
|
348
287
|
// If it is a rule, we wrap it with `genRuleCall` that adds the base enforce behavior
|
|
349
288
|
if (rule) {
|
|
350
|
-
return genRuleCall(proxy, rule,
|
|
289
|
+
return genRuleCall(proxy, rule, key);
|
|
351
290
|
}
|
|
352
|
-
|
|
291
|
+
return target[key];
|
|
292
|
+
},
|
|
353
293
|
});
|
|
354
294
|
return proxy;
|
|
355
295
|
// This function is used to wrap a rule with the base enforce behavior
|
|
356
296
|
// It takes the target object, the rule function, and the rule name
|
|
357
297
|
// It then returns the rule, in a manner that can be used by enforce
|
|
358
298
|
function genRuleCall(target, rule, ruleName) {
|
|
359
|
-
return function ruleCall() {
|
|
360
|
-
var args = [];
|
|
361
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
362
|
-
args[_i] = arguments[_i];
|
|
363
|
-
}
|
|
299
|
+
return function ruleCall(...args) {
|
|
364
300
|
// Order of operation:
|
|
365
301
|
// 1. Create a context with the value being enforced
|
|
366
302
|
// 2. Call the rule within the context, and pass over the arguments passed to it
|
|
367
303
|
// 3. Transform the result to the correct output format
|
|
368
|
-
|
|
369
|
-
return transformResult
|
|
304
|
+
const transformedResult = ctx.run({ value }, () => {
|
|
305
|
+
return transformResult(rule(value, ...args), ruleName, value, ...args);
|
|
370
306
|
});
|
|
307
|
+
function enforceMessage() {
|
|
308
|
+
if (!isNullish(customMessage))
|
|
309
|
+
return StringObject(customMessage);
|
|
310
|
+
if (isNullish(transformedResult.message)) {
|
|
311
|
+
return `enforce/${ruleName} failed with ${JSON.stringify(value)}`;
|
|
312
|
+
}
|
|
313
|
+
return StringObject(transformedResult.message);
|
|
314
|
+
}
|
|
371
315
|
// On rule failure (the result is false), we either throw an error
|
|
372
316
|
// or throw a string value if the rule has a message defined in it.
|
|
373
|
-
invariant(transformedResult.pass,
|
|
374
|
-
? "enforce/".concat(ruleName, " failed with ").concat(JSON.stringify(value))
|
|
375
|
-
: StringObject(transformedResult.message));
|
|
317
|
+
invariant(transformedResult.pass, enforceMessage());
|
|
376
318
|
return target;
|
|
377
319
|
};
|
|
378
320
|
}
|
|
321
|
+
function message(input) {
|
|
322
|
+
customMessage = input;
|
|
323
|
+
return proxy;
|
|
324
|
+
}
|
|
379
325
|
}
|
|
380
326
|
|
|
381
327
|
// eslint-disable-next-line max-lines-per-function
|
|
382
328
|
function genEnforceLazy(key) {
|
|
383
|
-
|
|
384
|
-
|
|
329
|
+
const registeredRules = [];
|
|
330
|
+
let lazyMessage;
|
|
385
331
|
return addLazyRule(key);
|
|
386
332
|
// eslint-disable-next-line max-lines-per-function
|
|
387
333
|
function addLazyRule(ruleName) {
|
|
388
334
|
// eslint-disable-next-line max-lines-per-function
|
|
389
|
-
return
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
registeredRules.push(function (value) {
|
|
396
|
-
return transformResult.apply(void 0, __spreadArray([rule.apply(void 0, __spreadArray([value], args, false)), ruleName, value], args, false));
|
|
397
|
-
});
|
|
398
|
-
var proxy = {
|
|
399
|
-
run: function (value) {
|
|
400
|
-
return defaultToPassing(mapFirst(registeredRules, function (rule, breakout) {
|
|
335
|
+
return (...args) => {
|
|
336
|
+
const rule = getRule(ruleName);
|
|
337
|
+
registeredRules.push((value) => transformResult(rule(value, ...args), ruleName, value, ...args));
|
|
338
|
+
let proxy = {
|
|
339
|
+
run: (value) => {
|
|
340
|
+
return defaultToPassing(mapFirst(registeredRules, (rule, breakout) => {
|
|
401
341
|
var _a;
|
|
402
|
-
|
|
342
|
+
const res = ctx.run({ value }, () => rule(value));
|
|
403
343
|
breakout(!res.pass, ruleReturn(!!res.pass, (_a = optionalFunctionValue(lazyMessage, value, res.message)) !== null && _a !== void 0 ? _a : res.message));
|
|
404
344
|
}));
|
|
405
345
|
},
|
|
406
|
-
test:
|
|
407
|
-
message:
|
|
346
|
+
test: (value) => proxy.run(value).pass,
|
|
347
|
+
message: (message) => {
|
|
408
348
|
if (message) {
|
|
409
349
|
lazyMessage = message;
|
|
410
350
|
}
|
|
411
351
|
return proxy;
|
|
412
|
-
}
|
|
352
|
+
},
|
|
413
353
|
};
|
|
414
|
-
if (!isProxySupported()) {
|
|
415
|
-
eachEnforceRule(function (ruleName) {
|
|
416
|
-
proxy[ruleName] = addLazyRule(ruleName);
|
|
417
|
-
});
|
|
418
|
-
return proxy;
|
|
419
|
-
}
|
|
420
354
|
// reassigning the proxy here is not pretty
|
|
421
355
|
// but it's a cleaner way of getting `run` and `test` for free
|
|
422
356
|
proxy = new Proxy(proxy, {
|
|
423
|
-
get:
|
|
357
|
+
get: (target, key) => {
|
|
424
358
|
if (getRule(key)) {
|
|
425
359
|
return addLazyRule(key);
|
|
426
360
|
}
|
|
427
361
|
return target[key]; // already has `run` and `test` on it
|
|
428
|
-
}
|
|
362
|
+
},
|
|
429
363
|
});
|
|
430
364
|
return proxy;
|
|
431
365
|
};
|
|
@@ -459,16 +393,14 @@ function genEnforceLazy(key) {
|
|
|
459
393
|
* while run will return an object with the validation result and an optional message created by the rule.
|
|
460
394
|
*/
|
|
461
395
|
function genEnforce() {
|
|
462
|
-
|
|
463
|
-
context:
|
|
464
|
-
extend:
|
|
396
|
+
const target = {
|
|
397
|
+
context: () => ctx.useX(),
|
|
398
|
+
extend: (customRules) => {
|
|
465
399
|
assign(baseRules, customRules);
|
|
466
|
-
|
|
467
|
-
}
|
|
400
|
+
},
|
|
468
401
|
};
|
|
469
|
-
handleNoProxy();
|
|
470
402
|
return new Proxy(assign(enforceEager, target), {
|
|
471
|
-
get:
|
|
403
|
+
get: (target, key) => {
|
|
472
404
|
if (key in target) {
|
|
473
405
|
return target[key];
|
|
474
406
|
}
|
|
@@ -477,18 +409,9 @@ function genEnforce() {
|
|
|
477
409
|
}
|
|
478
410
|
// Only on the first rule access - start the chain of calls
|
|
479
411
|
return genEnforceLazy(key);
|
|
480
|
-
}
|
|
412
|
+
},
|
|
481
413
|
});
|
|
482
|
-
function handleNoProxy() {
|
|
483
|
-
if (!isProxySupported()) {
|
|
484
|
-
eachEnforceRule(function (ruleName) {
|
|
485
|
-
// Only on the first rule access - start the chain of calls
|
|
486
|
-
target[ruleName] = genEnforceLazy(ruleName);
|
|
487
|
-
});
|
|
488
|
-
return assign(enforceEager, target);
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
414
|
}
|
|
492
|
-
|
|
415
|
+
const enforce = genEnforce();
|
|
493
416
|
|
|
494
417
|
export { ctx, enforce };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{assign as n,bindNot as t,isStringValue as e,numberEquals as r,greaterThan as s,isArray as u,isNumeric as o,isNullish as i,isBoolean as c,isEmpty as a,isNotArray as f,isNotEmpty as l,isNotNull as N,isNotNullish as m,isNotNumeric as g,isNotUndefined as h,isNull as p,isPositive as d,isUndefined as y,lengthEquals as E,lengthNotEquals as v,longerThan as O,numberNotEquals as q,defaultTo as x,optionalFunctionValue as T,invariant as b,StringObject as w,mapFirst as B}from"vest-utils";import{createCascade as W}from"context";const S=W(((t,e)=>{const r={value:t.value,meta:t.meta||{}};return e?t.set?n(r,{parent:()=>function(n){return{value:n.value,meta:n.meta,parent:n.parent}}(e)}):e:n(r,{parent:P})}));function P(){return null}function k(n,t){return e(n)&&e(t)&&n.endsWith(t)}const A=t(k);function I(n,t){return n===t}const J=t(I);function K(n,t){return r(n,t)||s(n,t)}function R(n,t){return(u(t)||!(!e(t)||!e(n)))&&-1!==t.indexOf(n)}const U=t(R);function V(n,t){return o(n)&&o(t)&&Number(n)<Number(t)}function $(n,t){return r(n,t)||V(n,t)}function F(n,t,e){return K(n,t)&&$(n,e)}const M=t(F);function X(n){return i(n)||e(n)&&!n.trim()}const j=t(X),z=t(c),C=n=>!!o(n)&&n%2==0;function D(n,t){return n in t}const G=t(D);function H(n){return Number.isNaN(n)}const L=t(H);function Q(n){return V(n,0)}function Y(n){return Boolean("number"==typeof n)}const Z=t(Y),_=n=>!!o(n)&&n%2!=0,nn=t(e);function tn(n){return!!n}const en=t(tn);function rn(n,t){if(i(t))return!1;for(const e in t)if(t[e]===n)return!0;return!1}const sn=t(rn);function un(n,t){return K(n.length,t)}function on(n,t){return t instanceof RegExp?t.test(n):!!e(t)&&new RegExp(t).test(n)}const cn=t(on);function an(n,t){try{return t(n)}catch(n){return!1}}function fn(n,t){return V(n.length,t)}function ln(n,t){return $(n.length,t)}function Nn(n,t){return e(n)&&e(t)&&n.startsWith(t)}const mn=t(Nn);const gn={condition:an,doesNotEndWith:A,doesNotStartWith:mn,endsWith:k,equals:I,greaterThan:s,greaterThanOrEquals:K,gt:s,gte:K,inside:R,isArray:u,isBetween:F,isBlank:X,isBoolean:c,isEmpty:a,isEven:C,isFalsy:en,isKeyOf:D,isNaN:H,isNegative:Q,isNotArray:f,isNotBetween:M,isNotBlank:j,isNotBoolean:z,isNotEmpty:l,isNotKeyOf:G,isNotNaN:L,isNotNull:N,isNotNullish:m,isNotNumber:Z,isNotNumeric:g,isNotString:nn,isNotUndefined:h,isNotValueOf:sn,isNull:p,isNullish:i,isNumber:Y,isNumeric:o,isOdd:_,isPositive:d,isString:e,isTruthy:tn,isUndefined:y,isValueOf:rn,lengthEquals:E,lengthNotEquals:v,lessThan:V,lessThanOrEquals:$,longerThan:O,longerThanOrEquals:un,lt:V,lte:$,matches:on,notEquals:J,notInside:U,notMatches:cn,numberEquals:r,numberNotEquals:q,shorterThan:fn,shorterThanOrEquals:ln,startsWith:Nn};function hn(n){return gn[n]}function pn(n,t){const e={pass:n};return t&&(e.message=t),e}function dn(n){return x(n,pn(!0))}function yn(n,t,e,...r){return function(n){b(c(n)||n&&c(n.pass),"Incorrect return value for rule: "+JSON.stringify(n))}(n),c(n)?pn(n):pn(n.pass,T(n.message,t,e,...r))}function En(n){const t={message:function(n){return e=n,r}};let e;const r=new Proxy(t,{get:(s,u)=>{const o=hn(u);return o?function(t,r,s){return function(...u){const o=S.run({value:n},(()=>yn(r(n,...u),s,n,...u)));function c(){return i(e)?i(o.message)?`enforce/${s} failed with ${JSON.stringify(n)}`:w(o.message):w(e)}return b(o.pass,c()),t}}(r,o,u):t[u]}});return r}const vn=function(){const t={context:()=>S.useX(),extend:t=>{n(gn,t)}};return new Proxy(n(En,t),{get:(n,t)=>t in n?n[t]:hn(t)?function(n){const t=[];let e;return function n(r){return(...s)=>{const u=hn(r);t.push((n=>yn(u(n,...s),r,n,...s)));let o={run:n=>dn(B(t,((t,r)=>{var s;const u=S.run({value:n},(()=>t(n)));r(!u.pass,pn(!!u.pass,null!==(s=T(e,n,u.message))&&void 0!==s?s:u.message))}))),test:n=>o.run(n).pass,message:n=>(n&&(e=n),o)};return o=new Proxy(o,{get:(t,e)=>hn(e)?n(e):t[e]}),o}}(n)}(t):void 0})}();export{S as ctx,vn as enforce};
|