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