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