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