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