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