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