n4s 5.0.1 → 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/CHANGELOG.md +0 -4
- package/README.md +2 -0
- package/compose/package.json +2 -0
- package/compounds/package.json +2 -0
- package/date/package.json +9 -0
- package/dist/cjs/compose.development.js +14 -97
- package/dist/cjs/compose.js +0 -1
- package/dist/cjs/compose.production.js +1 -1
- package/dist/cjs/compounds.development.js +30 -86
- 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 +205 -387
- package/dist/cjs/n4s.js +0 -1
- package/dist/cjs/n4s.production.js +1 -1
- package/dist/cjs/schema.development.js +19 -80
- package/dist/cjs/schema.js +0 -1
- package/dist/cjs/schema.production.js +1 -1
- package/dist/es/compose.development.js +14 -97
- package/dist/es/compose.production.js +1 -1
- package/dist/es/compounds.development.js +28 -84
- 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 +192 -372
- package/dist/es/n4s.production.js +1 -1
- package/dist/es/schema.development.js +16 -75
- package/dist/es/schema.production.js +1 -1
- package/dist/umd/compose.development.js +18 -102
- package/dist/umd/compose.production.js +1 -1
- package/dist/umd/compounds.development.js +34 -91
- 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 +209 -392
- package/dist/umd/n4s.production.js +1 -1
- package/dist/umd/schema.development.js +23 -85
- package/dist/umd/schema.production.js +1 -1
- package/email/package.json +9 -0
- package/isURL/package.json +9 -0
- package/package.json +162 -55
- package/schema/package.json +2 -0
- package/testUtils/TEnforceMock.ts +3 -0
- package/types/compose.d.ts +46 -55
- package/types/compose.d.ts.map +1 -0
- package/types/compounds.d.ts +50 -62
- 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 +62 -73
- package/types/n4s.d.ts.map +1 -0
- package/types/schema.d.ts +55 -63
- package/types/schema.d.ts.map +1 -0
- package/docs/.nojekyll +0 -0
- package/docs/README.md +0 -44
- package/docs/_sidebar.md +0 -5
- package/docs/external.md +0 -27
- package/docs/index.html +0 -32
- package/docs/rules.md +0 -1282
- package/tsconfig.json +0 -8
|
@@ -1,74 +1,48 @@
|
|
|
1
|
-
import {
|
|
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
|
+
import { createCascade } from 'context';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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,
|
|
16
26
|
};
|
|
17
27
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return value === null;
|
|
21
|
-
}
|
|
22
|
-
var isNotNull = bindNot(isNull);
|
|
23
|
-
|
|
24
|
-
function isUndefined(value) {
|
|
25
|
-
return value === undefined;
|
|
26
|
-
}
|
|
27
|
-
var isNotUndefined = bindNot(isUndefined);
|
|
28
|
-
|
|
29
|
-
function isNullish(value) {
|
|
30
|
-
return isNull(value) || isUndefined(value);
|
|
31
|
-
}
|
|
32
|
-
var isNotNullish = bindNot(isNullish);
|
|
33
|
-
|
|
34
|
-
function isStringValue(v) {
|
|
35
|
-
return String(v) === v;
|
|
28
|
+
function emptyParent() {
|
|
29
|
+
return null;
|
|
36
30
|
}
|
|
37
31
|
|
|
38
32
|
function endsWith(value, arg1) {
|
|
39
33
|
return isStringValue(value) && isStringValue(arg1) && value.endsWith(arg1);
|
|
40
34
|
}
|
|
41
|
-
|
|
35
|
+
const doesNotEndWith = bindNot(endsWith);
|
|
42
36
|
|
|
43
37
|
function equals(value, arg1) {
|
|
44
38
|
return value === arg1;
|
|
45
39
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
function isNumeric(value) {
|
|
49
|
-
var str = String(value);
|
|
50
|
-
var num = Number(value);
|
|
51
|
-
var result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num);
|
|
52
|
-
return Boolean(result);
|
|
53
|
-
}
|
|
54
|
-
var isNotNumeric = bindNot(isNumeric);
|
|
55
|
-
|
|
56
|
-
function greaterThan(value, gt) {
|
|
57
|
-
return isNumeric(value) && isNumeric(gt) && Number(value) > Number(gt);
|
|
58
|
-
}
|
|
40
|
+
const notEquals = bindNot(equals);
|
|
59
41
|
|
|
60
42
|
function greaterThanOrEquals(value, gte) {
|
|
61
|
-
return
|
|
43
|
+
return numberEquals(value, gte) || greaterThan(value, gte);
|
|
62
44
|
}
|
|
63
45
|
|
|
64
|
-
// The module is named "isArrayValue" since it
|
|
65
|
-
// is conflicting with a nested npm dependency.
|
|
66
|
-
// We may need to revisit this in the future.
|
|
67
|
-
function isArray(value) {
|
|
68
|
-
return Boolean(Array.isArray(value));
|
|
69
|
-
}
|
|
70
|
-
var isNotArray = bindNot(isArray);
|
|
71
|
-
|
|
72
46
|
function inside(value, arg1) {
|
|
73
47
|
if (isArray(arg1)) {
|
|
74
48
|
return arg1.indexOf(value) !== -1;
|
|
@@ -79,112 +53,89 @@ function inside(value, arg1) {
|
|
|
79
53
|
}
|
|
80
54
|
return false;
|
|
81
55
|
}
|
|
82
|
-
|
|
56
|
+
const notInside = bindNot(inside);
|
|
57
|
+
|
|
58
|
+
function lessThan(value, lt) {
|
|
59
|
+
return isNumeric(value) && isNumeric(lt) && Number(value) < Number(lt);
|
|
60
|
+
}
|
|
83
61
|
|
|
84
62
|
function lessThanOrEquals(value, lte) {
|
|
85
|
-
return
|
|
63
|
+
return numberEquals(value, lte) || lessThan(value, lte);
|
|
86
64
|
}
|
|
87
65
|
|
|
88
66
|
function isBetween(value, min, max) {
|
|
89
67
|
return greaterThanOrEquals(value, min) && lessThanOrEquals(value, max);
|
|
90
68
|
}
|
|
91
|
-
|
|
69
|
+
const isNotBetween = bindNot(isBetween);
|
|
92
70
|
|
|
93
71
|
function isBlank(value) {
|
|
94
72
|
return isNullish(value) || (isStringValue(value) && !value.trim());
|
|
95
73
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
function isBoolean(value) {
|
|
99
|
-
return !!value === value;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
var isNotBoolean = bindNot(isBoolean);
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* A safe hasOwnProperty access
|
|
106
|
-
*/
|
|
107
|
-
function hasOwnProperty(obj, key) {
|
|
108
|
-
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
109
|
-
}
|
|
74
|
+
const isNotBlank = bindNot(isBlank);
|
|
110
75
|
|
|
111
|
-
|
|
112
|
-
return Boolean(typeof value === 'number');
|
|
113
|
-
}
|
|
114
|
-
var isNotNumber = bindNot(isNumber);
|
|
115
|
-
|
|
116
|
-
function lengthEquals(value, arg1) {
|
|
117
|
-
return value.length === Number(arg1);
|
|
118
|
-
}
|
|
119
|
-
var lengthNotEquals = bindNot(lengthEquals);
|
|
120
|
-
|
|
121
|
-
function isEmpty(value) {
|
|
122
|
-
if (!value) {
|
|
123
|
-
return true;
|
|
124
|
-
}
|
|
125
|
-
else if (isNumber(value)) {
|
|
126
|
-
return value === 0;
|
|
127
|
-
}
|
|
128
|
-
else if (hasOwnProperty(value, 'length')) {
|
|
129
|
-
return lengthEquals(value, 0);
|
|
130
|
-
}
|
|
131
|
-
else if (typeof value === 'object') {
|
|
132
|
-
return lengthEquals(Object.keys(value), 0);
|
|
133
|
-
}
|
|
134
|
-
return true;
|
|
135
|
-
}
|
|
136
|
-
var isNotEmpty = bindNot(isEmpty);
|
|
76
|
+
const isNotBoolean = bindNot(isBoolean);
|
|
137
77
|
|
|
138
78
|
/**
|
|
139
79
|
* Validates that a given value is an even number
|
|
140
80
|
*/
|
|
141
|
-
|
|
81
|
+
const isEven = (value) => {
|
|
142
82
|
if (isNumeric(value)) {
|
|
143
83
|
return value % 2 === 0;
|
|
144
84
|
}
|
|
145
85
|
return false;
|
|
146
86
|
};
|
|
147
87
|
|
|
148
|
-
function
|
|
88
|
+
function isKeyOf(key, obj) {
|
|
89
|
+
return key in obj;
|
|
90
|
+
}
|
|
91
|
+
const isNotKeyOf = bindNot(isKeyOf);
|
|
92
|
+
|
|
93
|
+
function isNaN(value) {
|
|
149
94
|
return Number.isNaN(value);
|
|
150
95
|
}
|
|
151
|
-
|
|
96
|
+
const isNotNaN = bindNot(isNaN);
|
|
152
97
|
|
|
153
98
|
function isNegative(value) {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
99
|
+
return lessThan(value, 0);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function isNumber(value) {
|
|
103
|
+
return Boolean(typeof value === 'number');
|
|
158
104
|
}
|
|
159
|
-
|
|
105
|
+
const isNotNumber = bindNot(isNumber);
|
|
160
106
|
|
|
161
107
|
/**
|
|
162
108
|
* Validates that a given value is an odd number
|
|
163
109
|
*/
|
|
164
|
-
|
|
110
|
+
const isOdd = (value) => {
|
|
165
111
|
if (isNumeric(value)) {
|
|
166
112
|
return value % 2 !== 0;
|
|
167
113
|
}
|
|
168
114
|
return false;
|
|
169
115
|
};
|
|
170
116
|
|
|
171
|
-
|
|
117
|
+
const isNotString = bindNot(isStringValue);
|
|
172
118
|
|
|
173
119
|
function isTruthy(value) {
|
|
174
120
|
return !!value;
|
|
175
121
|
}
|
|
176
|
-
|
|
122
|
+
const isFalsy = bindNot(isTruthy);
|
|
177
123
|
|
|
178
|
-
function
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
124
|
+
function isValueOf(value, objectToCheck) {
|
|
125
|
+
if (isNullish(objectToCheck)) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
for (const key in objectToCheck) {
|
|
129
|
+
if (objectToCheck[key] === value) {
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return false;
|
|
184
134
|
}
|
|
135
|
+
const isNotValueOf = bindNot(isValueOf);
|
|
185
136
|
|
|
186
137
|
function longerThanOrEquals(value, arg1) {
|
|
187
|
-
return value.length
|
|
138
|
+
return greaterThanOrEquals(value.length, arg1);
|
|
188
139
|
}
|
|
189
140
|
|
|
190
141
|
function matches(value, regex) {
|
|
@@ -194,16 +145,9 @@ function matches(value, regex) {
|
|
|
194
145
|
else if (isStringValue(regex)) {
|
|
195
146
|
return new RegExp(regex).test(value);
|
|
196
147
|
}
|
|
197
|
-
|
|
198
|
-
return false;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
var notMatches = bindNot(matches);
|
|
202
|
-
|
|
203
|
-
function numberEquals(value, eq) {
|
|
204
|
-
return isNumeric(value) && isNumeric(eq) && Number(value) === Number(eq);
|
|
148
|
+
return false;
|
|
205
149
|
}
|
|
206
|
-
|
|
150
|
+
const notMatches = bindNot(matches);
|
|
207
151
|
|
|
208
152
|
function condition(value, callback) {
|
|
209
153
|
try {
|
|
@@ -215,182 +159,92 @@ function condition(value, callback) {
|
|
|
215
159
|
}
|
|
216
160
|
|
|
217
161
|
function shorterThan(value, arg1) {
|
|
218
|
-
return value.length
|
|
162
|
+
return lessThan(value.length, arg1);
|
|
219
163
|
}
|
|
220
164
|
|
|
221
165
|
function shorterThanOrEquals(value, arg1) {
|
|
222
|
-
return value.length
|
|
166
|
+
return lessThanOrEquals(value.length, arg1);
|
|
223
167
|
}
|
|
224
168
|
|
|
225
169
|
function startsWith(value, arg1) {
|
|
226
170
|
return isStringValue(value) && isStringValue(arg1) && value.startsWith(arg1);
|
|
227
171
|
}
|
|
228
|
-
|
|
172
|
+
const doesNotStartWith = bindNot(startsWith);
|
|
229
173
|
|
|
230
174
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, max-lines-per-function
|
|
231
175
|
function rules() {
|
|
232
176
|
return {
|
|
233
|
-
condition
|
|
234
|
-
doesNotEndWith
|
|
235
|
-
doesNotStartWith
|
|
236
|
-
endsWith
|
|
237
|
-
equals
|
|
238
|
-
greaterThan
|
|
239
|
-
greaterThanOrEquals
|
|
177
|
+
condition,
|
|
178
|
+
doesNotEndWith,
|
|
179
|
+
doesNotStartWith,
|
|
180
|
+
endsWith,
|
|
181
|
+
equals,
|
|
182
|
+
greaterThan,
|
|
183
|
+
greaterThanOrEquals,
|
|
240
184
|
gt: greaterThan,
|
|
241
185
|
gte: greaterThanOrEquals,
|
|
242
|
-
inside
|
|
243
|
-
isArray
|
|
244
|
-
isBetween
|
|
245
|
-
isBlank
|
|
246
|
-
isBoolean
|
|
247
|
-
isEmpty
|
|
248
|
-
isEven
|
|
249
|
-
isFalsy
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
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,
|
|
270
217
|
isString: isStringValue,
|
|
271
|
-
isTruthy
|
|
272
|
-
isUndefined
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
218
|
+
isTruthy,
|
|
219
|
+
isUndefined,
|
|
220
|
+
isValueOf,
|
|
221
|
+
lengthEquals,
|
|
222
|
+
lengthNotEquals,
|
|
223
|
+
lessThan,
|
|
224
|
+
lessThanOrEquals,
|
|
225
|
+
longerThan,
|
|
226
|
+
longerThanOrEquals,
|
|
279
227
|
lt: lessThan,
|
|
280
228
|
lte: lessThanOrEquals,
|
|
281
|
-
matches
|
|
282
|
-
notEquals
|
|
283
|
-
notInside
|
|
284
|
-
notMatches
|
|
285
|
-
numberEquals
|
|
286
|
-
numberNotEquals
|
|
287
|
-
shorterThan
|
|
288
|
-
shorterThanOrEquals
|
|
289
|
-
startsWith
|
|
229
|
+
matches,
|
|
230
|
+
notEquals,
|
|
231
|
+
notInside,
|
|
232
|
+
notMatches,
|
|
233
|
+
numberEquals,
|
|
234
|
+
numberNotEquals,
|
|
235
|
+
shorterThan,
|
|
236
|
+
shorterThanOrEquals,
|
|
237
|
+
startsWith,
|
|
290
238
|
};
|
|
291
239
|
}
|
|
292
240
|
|
|
293
|
-
|
|
241
|
+
const baseRules = rules();
|
|
294
242
|
function getRule(ruleName) {
|
|
295
243
|
return baseRules[ruleName];
|
|
296
244
|
}
|
|
297
245
|
|
|
298
|
-
function eachEnforceRule(action) {
|
|
299
|
-
for (var ruleName in baseRules) {
|
|
300
|
-
var ruleFn = getRule(ruleName);
|
|
301
|
-
if (isFunction(ruleFn)) {
|
|
302
|
-
action(ruleName, ruleFn);
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
var ctx = createContext(function (ctxRef, parentContext) {
|
|
308
|
-
var base = {
|
|
309
|
-
value: ctxRef.value,
|
|
310
|
-
meta: ctxRef.meta || {}
|
|
311
|
-
};
|
|
312
|
-
if (!parentContext) {
|
|
313
|
-
return assign(base, {
|
|
314
|
-
parent: emptyParent
|
|
315
|
-
});
|
|
316
|
-
}
|
|
317
|
-
else if (ctxRef.set) {
|
|
318
|
-
return assign(base, {
|
|
319
|
-
parent: function () { return stripContext(parentContext); }
|
|
320
|
-
});
|
|
321
|
-
}
|
|
322
|
-
return parentContext;
|
|
323
|
-
});
|
|
324
|
-
function stripContext(ctx) {
|
|
325
|
-
if (!ctx) {
|
|
326
|
-
return ctx;
|
|
327
|
-
}
|
|
328
|
-
return {
|
|
329
|
-
value: ctx.value,
|
|
330
|
-
meta: ctx.meta,
|
|
331
|
-
parent: ctx.parent
|
|
332
|
-
};
|
|
333
|
-
}
|
|
334
|
-
function emptyParent() {
|
|
335
|
-
return null;
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
/*! *****************************************************************************
|
|
339
|
-
Copyright (c) Microsoft Corporation.
|
|
340
|
-
|
|
341
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
342
|
-
purpose with or without fee is hereby granted.
|
|
343
|
-
|
|
344
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
345
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
346
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
347
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
348
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
349
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
350
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
351
|
-
***************************************************************************** */
|
|
352
|
-
|
|
353
|
-
function __spreadArray(to, from, pack) {
|
|
354
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
355
|
-
if (ar || !(i in from)) {
|
|
356
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
357
|
-
ar[i] = from[i];
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
return to.concat(ar || from);
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
function optionalFunctionValue(value) {
|
|
364
|
-
var args = [];
|
|
365
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
366
|
-
args[_i - 1] = arguments[_i];
|
|
367
|
-
}
|
|
368
|
-
return isFunction(value) ? value.apply(void 0, args) : value;
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
function defaultTo(callback, defaultValue) {
|
|
372
|
-
var _a;
|
|
373
|
-
return (_a = optionalFunctionValue(callback)) !== null && _a !== void 0 ? _a : defaultValue;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
/**
|
|
377
|
-
* Throws a timed out error.
|
|
378
|
-
*/
|
|
379
|
-
function throwError(devMessage, productionMessage) {
|
|
380
|
-
throw new Error(devMessage );
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
function isProxySupported() {
|
|
384
|
-
try {
|
|
385
|
-
return isFunction(Proxy);
|
|
386
|
-
}
|
|
387
|
-
catch (_a) {
|
|
388
|
-
return false;
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
|
|
392
246
|
function ruleReturn(pass, message) {
|
|
393
|
-
|
|
247
|
+
const output = { pass };
|
|
394
248
|
if (message) {
|
|
395
249
|
output.message = message;
|
|
396
250
|
}
|
|
@@ -406,133 +260,106 @@ function defaultToPassing(callback) {
|
|
|
406
260
|
/**
|
|
407
261
|
* Transform the result of a rule into a standard format
|
|
408
262
|
*/
|
|
409
|
-
function transformResult(result, ruleName, value) {
|
|
410
|
-
var args = [];
|
|
411
|
-
for (var _i = 3; _i < arguments.length; _i++) {
|
|
412
|
-
args[_i - 3] = arguments[_i];
|
|
413
|
-
}
|
|
263
|
+
function transformResult(result, ruleName, value, ...args) {
|
|
414
264
|
validateResult(result);
|
|
415
265
|
// if result is boolean
|
|
416
266
|
if (isBoolean(result)) {
|
|
417
267
|
return ruleReturn(result);
|
|
418
268
|
}
|
|
419
|
-
|
|
420
|
-
return ruleReturn(result.pass, optionalFunctionValue.apply(void 0, __spreadArray([result.message, ruleName, value], args)));
|
|
421
|
-
}
|
|
269
|
+
return ruleReturn(result.pass, optionalFunctionValue(result.message, ruleName, value, ...args));
|
|
422
270
|
}
|
|
423
271
|
function validateResult(result) {
|
|
424
272
|
// if result is boolean, or if result.pass is boolean
|
|
425
|
-
|
|
426
|
-
return;
|
|
427
|
-
}
|
|
428
|
-
throwError('Incorrect return value for rule: ' + JSON.stringify(result));
|
|
273
|
+
invariant(isBoolean(result) || (result && isBoolean(result.pass)), 'Incorrect return value for rule: ' + JSON.stringify(result));
|
|
429
274
|
}
|
|
430
275
|
|
|
276
|
+
// eslint-disable-next-line max-lines-per-function
|
|
431
277
|
function enforceEager(value) {
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
278
|
+
const target = {
|
|
279
|
+
message,
|
|
280
|
+
};
|
|
281
|
+
let customMessage = undefined;
|
|
282
|
+
// We create a proxy intercepting access to the target object (which is empty).
|
|
283
|
+
const proxy = new Proxy(target, {
|
|
284
|
+
get: (_, key) => {
|
|
285
|
+
// On property access, we identify if it is a rule or not.
|
|
286
|
+
const rule = getRule(key);
|
|
287
|
+
// If it is a rule, we wrap it with `genRuleCall` that adds the base enforce behavior
|
|
442
288
|
if (rule) {
|
|
443
|
-
return genRuleCall(proxy, rule,
|
|
289
|
+
return genRuleCall(proxy, rule, key);
|
|
444
290
|
}
|
|
445
|
-
|
|
291
|
+
return target[key];
|
|
292
|
+
},
|
|
446
293
|
});
|
|
447
294
|
return proxy;
|
|
295
|
+
// This function is used to wrap a rule with the base enforce behavior
|
|
296
|
+
// It takes the target object, the rule function, and the rule name
|
|
297
|
+
// It then returns the rule, in a manner that can be used by enforce
|
|
448
298
|
function genRuleCall(target, rule, ruleName) {
|
|
449
|
-
return function ruleCall() {
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
ruleName,
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
if (
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
// Explicitly throw a string so that vest.test can pick it up as the validation error message
|
|
463
|
-
throw transformedResult.message;
|
|
299
|
+
return function ruleCall(...args) {
|
|
300
|
+
// Order of operation:
|
|
301
|
+
// 1. Create a context with the value being enforced
|
|
302
|
+
// 2. Call the rule within the context, and pass over the arguments passed to it
|
|
303
|
+
// 3. Transform the result to the correct output format
|
|
304
|
+
const transformedResult = ctx.run({ value }, () => {
|
|
305
|
+
return transformResult(rule(value, ...args), ruleName, value, ...args);
|
|
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)}`;
|
|
464
312
|
}
|
|
313
|
+
return StringObject(transformedResult.message);
|
|
465
314
|
}
|
|
315
|
+
// On rule failure (the result is false), we either throw an error
|
|
316
|
+
// or throw a string value if the rule has a message defined in it.
|
|
317
|
+
invariant(transformedResult.pass, enforceMessage());
|
|
466
318
|
return target;
|
|
467
319
|
};
|
|
468
320
|
}
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
var broke = false;
|
|
473
|
-
var breakoutValue = null;
|
|
474
|
-
for (var i = 0; i < array.length; i++) {
|
|
475
|
-
callback(array[i], breakout, i);
|
|
476
|
-
if (broke) {
|
|
477
|
-
return breakoutValue;
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
function breakout(value) {
|
|
481
|
-
broke = true;
|
|
482
|
-
breakoutValue = value;
|
|
321
|
+
function message(input) {
|
|
322
|
+
customMessage = input;
|
|
323
|
+
return proxy;
|
|
483
324
|
}
|
|
484
325
|
}
|
|
485
326
|
|
|
486
327
|
// eslint-disable-next-line max-lines-per-function
|
|
487
328
|
function genEnforceLazy(key) {
|
|
488
|
-
|
|
489
|
-
|
|
329
|
+
const registeredRules = [];
|
|
330
|
+
let lazyMessage;
|
|
490
331
|
return addLazyRule(key);
|
|
491
332
|
// eslint-disable-next-line max-lines-per-function
|
|
492
333
|
function addLazyRule(ruleName) {
|
|
493
334
|
// eslint-disable-next-line max-lines-per-function
|
|
494
|
-
return
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
registeredRules.push(function (value) {
|
|
501
|
-
return transformResult.apply(void 0, __spreadArray([rule.apply(void 0, __spreadArray([value], args)), ruleName, value], args));
|
|
502
|
-
});
|
|
503
|
-
var proxy = {
|
|
504
|
-
run: function (value) {
|
|
505
|
-
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) => {
|
|
506
341
|
var _a;
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
breakout(ruleReturn(!!res.pass, (_a = optionalFunctionValue(lazyMessage, value, res.message)) !== null && _a !== void 0 ? _a : res.message));
|
|
510
|
-
}
|
|
342
|
+
const res = ctx.run({ value }, () => rule(value));
|
|
343
|
+
breakout(!res.pass, ruleReturn(!!res.pass, (_a = optionalFunctionValue(lazyMessage, value, res.message)) !== null && _a !== void 0 ? _a : res.message));
|
|
511
344
|
}));
|
|
512
345
|
},
|
|
513
|
-
test:
|
|
514
|
-
message:
|
|
346
|
+
test: (value) => proxy.run(value).pass,
|
|
347
|
+
message: (message) => {
|
|
515
348
|
if (message) {
|
|
516
349
|
lazyMessage = message;
|
|
517
350
|
}
|
|
518
351
|
return proxy;
|
|
519
|
-
}
|
|
352
|
+
},
|
|
520
353
|
};
|
|
521
|
-
if (!isProxySupported()) {
|
|
522
|
-
eachEnforceRule(function (ruleName) {
|
|
523
|
-
proxy[ruleName] = addLazyRule(ruleName);
|
|
524
|
-
});
|
|
525
|
-
return proxy;
|
|
526
|
-
}
|
|
527
354
|
// reassigning the proxy here is not pretty
|
|
528
355
|
// but it's a cleaner way of getting `run` and `test` for free
|
|
529
356
|
proxy = new Proxy(proxy, {
|
|
530
|
-
get:
|
|
357
|
+
get: (target, key) => {
|
|
531
358
|
if (getRule(key)) {
|
|
532
359
|
return addLazyRule(key);
|
|
533
360
|
}
|
|
534
361
|
return target[key]; // already has `run` and `test` on it
|
|
535
|
-
}
|
|
362
|
+
},
|
|
536
363
|
});
|
|
537
364
|
return proxy;
|
|
538
365
|
};
|
|
@@ -566,21 +393,14 @@ function genEnforceLazy(key) {
|
|
|
566
393
|
* while run will return an object with the validation result and an optional message created by the rule.
|
|
567
394
|
*/
|
|
568
395
|
function genEnforce() {
|
|
569
|
-
|
|
570
|
-
context:
|
|
571
|
-
extend:
|
|
396
|
+
const target = {
|
|
397
|
+
context: () => ctx.useX(),
|
|
398
|
+
extend: (customRules) => {
|
|
572
399
|
assign(baseRules, customRules);
|
|
573
|
-
}
|
|
400
|
+
},
|
|
574
401
|
};
|
|
575
|
-
if (!isProxySupported()) {
|
|
576
|
-
eachEnforceRule(function (ruleName) {
|
|
577
|
-
// Only on the first rule access - start the chain of calls
|
|
578
|
-
target[ruleName] = genEnforceLazy(ruleName);
|
|
579
|
-
});
|
|
580
|
-
return target;
|
|
581
|
-
}
|
|
582
402
|
return new Proxy(assign(enforceEager, target), {
|
|
583
|
-
get:
|
|
403
|
+
get: (target, key) => {
|
|
584
404
|
if (key in target) {
|
|
585
405
|
return target[key];
|
|
586
406
|
}
|
|
@@ -589,9 +409,9 @@ function genEnforce() {
|
|
|
589
409
|
}
|
|
590
410
|
// Only on the first rule access - start the chain of calls
|
|
591
411
|
return genEnforceLazy(key);
|
|
592
|
-
}
|
|
412
|
+
},
|
|
593
413
|
});
|
|
594
414
|
}
|
|
595
|
-
|
|
415
|
+
const enforce = genEnforce();
|
|
596
416
|
|
|
597
417
|
export { ctx, enforce };
|