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