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