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