n4s 4.3.5-dev-d315d9 → 4.3.5
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 +14 -10
- package/dist/cjs/compose.production.js +1 -1
- package/dist/cjs/compounds.development.js +33 -17
- package/dist/cjs/compounds.production.js +1 -1
- package/dist/cjs/n4s.development.js +139 -100
- package/dist/cjs/n4s.production.js +1 -1
- package/dist/cjs/schema.development.js +21 -14
- package/dist/cjs/schema.production.js +1 -1
- package/dist/es/compose.development.js +14 -10
- package/dist/es/compose.production.js +1 -1
- package/dist/es/compounds.development.js +33 -17
- package/dist/es/compounds.production.js +1 -1
- package/dist/es/n4s.development.js +159 -120
- package/dist/es/n4s.production.js +1 -1
- package/dist/es/schema.development.js +21 -14
- package/dist/es/schema.production.js +1 -1
- package/dist/umd/compose.development.js +17 -13
- package/dist/umd/compose.production.js +1 -1
- package/dist/umd/compounds.development.js +36 -20
- package/dist/umd/compounds.production.js +1 -1
- package/dist/umd/n4s.development.js +139 -100
- package/dist/umd/n4s.production.js +1 -1
- package/dist/umd/schema.development.js +24 -17
- package/dist/umd/schema.production.js +1 -1
- package/package.json +3 -3
- package/types/compounds.d.ts.map +1 -1
- package/types/schema.d.ts.map +1 -1
- package/testUtils/TEnforceMock.ts +0 -3
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var n4s = require('n4s');
|
|
4
3
|
var vestUtils = require('vest-utils');
|
|
4
|
+
var n4s = require('n4s');
|
|
5
5
|
|
|
6
6
|
function ruleReturn(pass, message) {
|
|
7
|
-
|
|
7
|
+
var output = { pass: pass };
|
|
8
8
|
if (message) {
|
|
9
9
|
output.message = message;
|
|
10
10
|
}
|
|
@@ -30,23 +30,27 @@ function runLazyRule(lazyRule, currentValue) {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
/* eslint-disable max-lines-per-function */
|
|
33
|
-
function compose(
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
function compose() {
|
|
34
|
+
var composites = [];
|
|
35
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
36
|
+
composites[_i] = arguments[_i];
|
|
37
|
+
}
|
|
38
|
+
return vestUtils.assign(function (value) {
|
|
39
|
+
var res = run(value);
|
|
36
40
|
vestUtils.invariant(res.pass, vestUtils.StringObject(res.message));
|
|
37
41
|
}, {
|
|
38
|
-
run,
|
|
39
|
-
test: (value)
|
|
42
|
+
run: run,
|
|
43
|
+
test: function (value) { return run(value).pass; }
|
|
40
44
|
});
|
|
41
45
|
function run(value) {
|
|
42
|
-
return n4s.ctx.run({ value }, ()
|
|
43
|
-
return defaultToPassing(vestUtils.mapFirst(composites, (composite, breakout)
|
|
46
|
+
return n4s.ctx.run({ value: value }, function () {
|
|
47
|
+
return defaultToPassing(vestUtils.mapFirst(composites, function (composite, breakout) {
|
|
44
48
|
/* HACK: Just a small white lie. ~~HELP WANTED~~.
|
|
45
49
|
The ideal is that instead of `LazyRuleRunners` We would simply use `Lazy` to begin with.
|
|
46
50
|
The problem is that lazy rules can't really be passed to this function due to some generic hell
|
|
47
51
|
so we're limiting it to a small set of functions.
|
|
48
52
|
*/
|
|
49
|
-
|
|
53
|
+
var res = runLazyRule(composite, value);
|
|
50
54
|
breakout(!res.pass, res);
|
|
51
55
|
}));
|
|
52
56
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var n=require("vest-utils"),r=require("n4s");function t(n,r){var t={pass:n};return r&&(t.message=r),t}function u(r){return n.defaultTo(r,t(!0))}function e(n,r){try{return n.run(r)}catch(n){return t(!1)}}module.exports=function(){for(var t=[],s=0;s<arguments.length;s++)t[s]=arguments[s];return n.assign((function(r){var t=a(r);n.invariant(t.pass,n.StringObject(t.message))}),{run:a,test:function(n){return a(n).pass}});function a(s){return r.ctx.run({value:s},(function(){return u(n.mapFirst(t,(function(n,r){var t=e(n,s);r(!t.pass,t)})))}))}};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var n4s = require('n4s');
|
|
4
3
|
var vestUtils = require('vest-utils');
|
|
4
|
+
var n4s = require('n4s');
|
|
5
5
|
|
|
6
6
|
function ruleReturn(pass, message) {
|
|
7
|
-
|
|
7
|
+
var output = { pass: pass };
|
|
8
8
|
if (message) {
|
|
9
9
|
output.message = message;
|
|
10
10
|
}
|
|
@@ -32,23 +32,35 @@ function runLazyRule(lazyRule, currentValue) {
|
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
function allOf(value
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
function allOf(value) {
|
|
36
|
+
var rules = [];
|
|
37
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
38
|
+
rules[_i - 1] = arguments[_i];
|
|
39
|
+
}
|
|
40
|
+
return defaultToPassing(vestUtils.mapFirst(rules, function (rule, breakout) {
|
|
41
|
+
var res = runLazyRule(rule, value);
|
|
38
42
|
breakout(!res.pass, res);
|
|
39
43
|
}));
|
|
40
44
|
}
|
|
41
45
|
|
|
42
|
-
function anyOf(value
|
|
43
|
-
|
|
44
|
-
|
|
46
|
+
function anyOf(value) {
|
|
47
|
+
var rules = [];
|
|
48
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
49
|
+
rules[_i - 1] = arguments[_i];
|
|
50
|
+
}
|
|
51
|
+
return defaultToFailing(vestUtils.mapFirst(rules, function (rule, breakout) {
|
|
52
|
+
var res = runLazyRule(rule, value);
|
|
45
53
|
breakout(res.pass, res);
|
|
46
54
|
}));
|
|
47
55
|
}
|
|
48
56
|
|
|
49
|
-
function noneOf(value
|
|
50
|
-
|
|
51
|
-
|
|
57
|
+
function noneOf(value) {
|
|
58
|
+
var rules = [];
|
|
59
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
60
|
+
rules[_i - 1] = arguments[_i];
|
|
61
|
+
}
|
|
62
|
+
return defaultToPassing(vestUtils.mapFirst(rules, function (rule, breakout) {
|
|
63
|
+
var res = runLazyRule(rule, value);
|
|
52
64
|
breakout(res.pass, failing());
|
|
53
65
|
}));
|
|
54
66
|
}
|
|
@@ -58,11 +70,15 @@ function equals(value, arg1) {
|
|
|
58
70
|
}
|
|
59
71
|
vestUtils.bindNot(equals);
|
|
60
72
|
|
|
61
|
-
|
|
62
|
-
function oneOf(value
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
73
|
+
var REQUIRED_COUNT = 1;
|
|
74
|
+
function oneOf(value) {
|
|
75
|
+
var rules = [];
|
|
76
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
77
|
+
rules[_i - 1] = arguments[_i];
|
|
78
|
+
}
|
|
79
|
+
var passingCount = 0;
|
|
80
|
+
rules.some(function (rule) {
|
|
81
|
+
var res = runLazyRule(rule, value);
|
|
66
82
|
if (res.pass) {
|
|
67
83
|
passingCount++;
|
|
68
84
|
}
|
|
@@ -73,4 +89,4 @@ function oneOf(value, ...rules) {
|
|
|
73
89
|
return ruleReturn(equals(passingCount, REQUIRED_COUNT));
|
|
74
90
|
}
|
|
75
91
|
|
|
76
|
-
n4s.enforce.extend({ allOf, anyOf, noneOf, oneOf });
|
|
92
|
+
n4s.enforce.extend({ allOf: allOf, anyOf: anyOf, noneOf: noneOf, oneOf: oneOf });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var n=require("
|
|
1
|
+
"use strict";var n=require("vest-utils"),r=require("n4s");function t(n,r){var t={pass:n};return r&&(t.message=r),t}function e(){return t(!1)}function u(r){return n.defaultTo(r,e())}function f(r){return n.defaultTo(r,t(!0))}function a(n,r){try{return n.run(r)}catch(n){return e()}}function o(n,r){return n===r}n.bindNot(o);r.enforce.extend({allOf:function(r){for(var t=[],e=1;e<arguments.length;e++)t[e-1]=arguments[e];return f(n.mapFirst(t,(function(n,t){var e=a(n,r);t(!e.pass,e)})))},anyOf:function(r){for(var t=[],e=1;e<arguments.length;e++)t[e-1]=arguments[e];return u(n.mapFirst(t,(function(n,t){var e=a(n,r);t(e.pass,e)})))},noneOf:function(r){for(var t=[],u=1;u<arguments.length;u++)t[u-1]=arguments[u];return f(n.mapFirst(t,(function(n,t){t(a(n,r).pass,e())})))},oneOf:function(r){for(var e=[],u=1;u<arguments.length;u++)e[u-1]=arguments[u];var f=0;return e.some((function(t){if(a(t,r).pass&&f++,n.greaterThan(f,1))return!1})),t(o(f,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
|
+
var doesNotEndWith = vestUtils.bindNot(endsWith);
|
|
12
12
|
|
|
13
13
|
function equals(value, arg1) {
|
|
14
14
|
return value === arg1;
|
|
15
15
|
}
|
|
16
|
-
|
|
16
|
+
var 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
|
+
var 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
|
+
var 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
|
+
var isNotBlank = vestUtils.bindNot(isBlank);
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
var isNotBoolean = vestUtils.bindNot(vestUtils.isBoolean);
|
|
53
53
|
|
|
54
54
|
/**
|
|
55
55
|
* Validates that a given value is an even number
|
|
56
56
|
*/
|
|
57
|
-
|
|
57
|
+
var isEven = function (value) {
|
|
58
58
|
if (vestUtils.isNumeric(value)) {
|
|
59
59
|
return value % 2 === 0;
|
|
60
60
|
}
|
|
@@ -64,12 +64,12 @@ const isEven = (value) => {
|
|
|
64
64
|
function isKeyOf(key, obj) {
|
|
65
65
|
return key in obj;
|
|
66
66
|
}
|
|
67
|
-
|
|
67
|
+
var isNotKeyOf = vestUtils.bindNot(isKeyOf);
|
|
68
68
|
|
|
69
69
|
function isNaN(value) {
|
|
70
70
|
return Number.isNaN(value);
|
|
71
71
|
}
|
|
72
|
-
|
|
72
|
+
var 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
|
+
var isNotNumber = vestUtils.bindNot(isNumber);
|
|
82
82
|
|
|
83
83
|
/**
|
|
84
84
|
* Validates that a given value is an odd number
|
|
85
85
|
*/
|
|
86
|
-
|
|
86
|
+
var isOdd = function (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
|
+
var isNotString = vestUtils.bindNot(vestUtils.isStringValue);
|
|
94
94
|
|
|
95
95
|
function isTruthy(value) {
|
|
96
96
|
return !!value;
|
|
97
97
|
}
|
|
98
|
-
|
|
98
|
+
var 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 (var 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
|
+
var 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
|
+
var 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
|
+
var 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: condition,
|
|
154
|
+
doesNotEndWith: doesNotEndWith,
|
|
155
|
+
doesNotStartWith: doesNotStartWith,
|
|
156
|
+
endsWith: endsWith,
|
|
157
|
+
equals: equals,
|
|
158
158
|
greaterThan: vestUtils.greaterThan,
|
|
159
|
-
greaterThanOrEquals,
|
|
159
|
+
greaterThanOrEquals: greaterThanOrEquals,
|
|
160
160
|
gt: vestUtils.greaterThan,
|
|
161
161
|
gte: greaterThanOrEquals,
|
|
162
|
-
inside,
|
|
162
|
+
inside: inside,
|
|
163
163
|
isArray: vestUtils.isArray,
|
|
164
|
-
isBetween,
|
|
165
|
-
isBlank,
|
|
164
|
+
isBetween: isBetween,
|
|
165
|
+
isBlank: isBlank,
|
|
166
166
|
isBoolean: vestUtils.isBoolean,
|
|
167
167
|
isEmpty: vestUtils.isEmpty,
|
|
168
|
-
isEven,
|
|
169
|
-
isFalsy,
|
|
170
|
-
isKeyOf,
|
|
171
|
-
isNaN,
|
|
172
|
-
isNegative,
|
|
168
|
+
isEven: isEven,
|
|
169
|
+
isFalsy: isFalsy,
|
|
170
|
+
isKeyOf: isKeyOf,
|
|
171
|
+
isNaN: isNaN,
|
|
172
|
+
isNegative: isNegative,
|
|
173
173
|
isNotArray: vestUtils.isNotArray,
|
|
174
|
-
isNotBetween,
|
|
175
|
-
isNotBlank,
|
|
176
|
-
isNotBoolean,
|
|
174
|
+
isNotBetween: isNotBetween,
|
|
175
|
+
isNotBlank: isNotBlank,
|
|
176
|
+
isNotBoolean: isNotBoolean,
|
|
177
177
|
isNotEmpty: vestUtils.isNotEmpty,
|
|
178
|
-
isNotKeyOf,
|
|
179
|
-
isNotNaN,
|
|
178
|
+
isNotKeyOf: isNotKeyOf,
|
|
179
|
+
isNotNaN: isNotNaN,
|
|
180
180
|
isNotNull: vestUtils.isNotNull,
|
|
181
181
|
isNotNullish: vestUtils.isNotNullish,
|
|
182
|
-
isNotNumber,
|
|
182
|
+
isNotNumber: isNotNumber,
|
|
183
183
|
isNotNumeric: vestUtils.isNotNumeric,
|
|
184
|
-
isNotString,
|
|
184
|
+
isNotString: isNotString,
|
|
185
185
|
isNotUndefined: vestUtils.isNotUndefined,
|
|
186
|
-
isNotValueOf,
|
|
186
|
+
isNotValueOf: isNotValueOf,
|
|
187
187
|
isNull: vestUtils.isNull,
|
|
188
188
|
isNullish: vestUtils.isNullish,
|
|
189
|
-
isNumber,
|
|
189
|
+
isNumber: isNumber,
|
|
190
190
|
isNumeric: vestUtils.isNumeric,
|
|
191
|
-
isOdd,
|
|
191
|
+
isOdd: isOdd,
|
|
192
192
|
isPositive: vestUtils.isPositive,
|
|
193
193
|
isString: vestUtils.isStringValue,
|
|
194
|
-
isTruthy,
|
|
194
|
+
isTruthy: isTruthy,
|
|
195
195
|
isUndefined: vestUtils.isUndefined,
|
|
196
|
-
isValueOf,
|
|
196
|
+
isValueOf: isValueOf,
|
|
197
197
|
lengthEquals: vestUtils.lengthEquals,
|
|
198
198
|
lengthNotEquals: vestUtils.lengthNotEquals,
|
|
199
|
-
lessThan,
|
|
200
|
-
lessThanOrEquals,
|
|
199
|
+
lessThan: lessThan,
|
|
200
|
+
lessThanOrEquals: lessThanOrEquals,
|
|
201
201
|
longerThan: vestUtils.longerThan,
|
|
202
|
-
longerThanOrEquals,
|
|
202
|
+
longerThanOrEquals: longerThanOrEquals,
|
|
203
203
|
lt: lessThan,
|
|
204
204
|
lte: lessThanOrEquals,
|
|
205
|
-
matches,
|
|
206
|
-
notEquals,
|
|
207
|
-
notInside,
|
|
208
|
-
notMatches,
|
|
205
|
+
matches: matches,
|
|
206
|
+
notEquals: notEquals,
|
|
207
|
+
notInside: notInside,
|
|
208
|
+
notMatches: notMatches,
|
|
209
209
|
numberEquals: vestUtils.numberEquals,
|
|
210
210
|
numberNotEquals: vestUtils.numberNotEquals,
|
|
211
|
-
shorterThan,
|
|
212
|
-
shorterThanOrEquals,
|
|
213
|
-
startsWith
|
|
211
|
+
shorterThan: shorterThan,
|
|
212
|
+
shorterThanOrEquals: shorterThanOrEquals,
|
|
213
|
+
startsWith: startsWith
|
|
214
214
|
};
|
|
215
215
|
}
|
|
216
216
|
|
|
217
|
-
|
|
217
|
+
var 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 (var ruleName in baseRules) {
|
|
224
|
+
var 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
|
+
var ctx = context.createCascade(function (ctxRef, parentContext) {
|
|
232
|
+
var 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: function () { return stripContext(parentContext); }
|
|
244
244
|
});
|
|
245
245
|
}
|
|
246
246
|
return parentContext;
|
|
@@ -252,13 +252,38 @@ 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
|
+
|
|
262
287
|
function isProxySupported() {
|
|
263
288
|
try {
|
|
264
289
|
return vestUtils.isFunction(Proxy);
|
|
@@ -269,7 +294,7 @@ function isProxySupported() {
|
|
|
269
294
|
}
|
|
270
295
|
|
|
271
296
|
function ruleReturn(pass, message) {
|
|
272
|
-
|
|
297
|
+
var output = { pass: pass };
|
|
273
298
|
if (message) {
|
|
274
299
|
output.message = message;
|
|
275
300
|
}
|
|
@@ -285,13 +310,17 @@ function defaultToPassing(callback) {
|
|
|
285
310
|
/**
|
|
286
311
|
* Transform the result of a rule into a standard format
|
|
287
312
|
*/
|
|
288
|
-
function transformResult(result, ruleName, value
|
|
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
|
+
}
|
|
289
318
|
validateResult(result);
|
|
290
319
|
// if result is boolean
|
|
291
320
|
if (vestUtils.isBoolean(result)) {
|
|
292
321
|
return ruleReturn(result);
|
|
293
322
|
}
|
|
294
|
-
return ruleReturn(result.pass, vestUtils.optionalFunctionValue(result.message, ruleName, value,
|
|
323
|
+
return ruleReturn(result.pass, vestUtils.optionalFunctionValue.apply(void 0, __spreadArray([result.message, ruleName, value], args, false)));
|
|
295
324
|
}
|
|
296
325
|
function validateResult(result) {
|
|
297
326
|
// if result is boolean, or if result.pass is boolean
|
|
@@ -300,51 +329,55 @@ function validateResult(result) {
|
|
|
300
329
|
|
|
301
330
|
// eslint-disable-next-line max-lines-per-function
|
|
302
331
|
function enforceEager(value) {
|
|
303
|
-
|
|
304
|
-
message
|
|
332
|
+
var target = {
|
|
333
|
+
message: message
|
|
305
334
|
};
|
|
306
|
-
|
|
335
|
+
var customMessage = undefined;
|
|
307
336
|
// This condition is for when we don't have proxy support (ES5).
|
|
308
337
|
// In this case, we need to manually assign the rules to the target object on runtime.
|
|
309
338
|
// The follow up proxy block is used in case we do have proxy support, and we can assign each rule upon invocation.
|
|
310
339
|
if (!isProxySupported()) {
|
|
311
340
|
// We iterate over each of the rules, and add them to the target object being return by enforce
|
|
312
|
-
eachEnforceRule((ruleName, ruleFn)
|
|
341
|
+
eachEnforceRule(function (ruleName, ruleFn) {
|
|
313
342
|
// We then wrap the rule with `genRuleCall` that adds the base enforce behavior
|
|
314
343
|
target[ruleName] = genRuleCall(target, ruleFn, ruleName);
|
|
315
344
|
});
|
|
316
345
|
return target;
|
|
317
346
|
}
|
|
318
347
|
// We create a proxy intercepting access to the target object (which is empty).
|
|
319
|
-
|
|
320
|
-
get: (_, key)
|
|
348
|
+
var proxy = new Proxy(target, {
|
|
349
|
+
get: function (_, key) {
|
|
321
350
|
// On property access, we identify if it is a rule or not.
|
|
322
|
-
|
|
351
|
+
var rule = getRule(key);
|
|
323
352
|
// If it is a rule, we wrap it with `genRuleCall` that adds the base enforce behavior
|
|
324
353
|
if (rule) {
|
|
325
354
|
return genRuleCall(proxy, rule, key);
|
|
326
355
|
}
|
|
327
356
|
return target[key];
|
|
328
|
-
}
|
|
357
|
+
}
|
|
329
358
|
});
|
|
330
359
|
return proxy;
|
|
331
360
|
// This function is used to wrap a rule with the base enforce behavior
|
|
332
361
|
// It takes the target object, the rule function, and the rule name
|
|
333
362
|
// It then returns the rule, in a manner that can be used by enforce
|
|
334
363
|
function genRuleCall(target, rule, ruleName) {
|
|
335
|
-
return function ruleCall(
|
|
364
|
+
return function ruleCall() {
|
|
365
|
+
var args = [];
|
|
366
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
367
|
+
args[_i] = arguments[_i];
|
|
368
|
+
}
|
|
336
369
|
// Order of operation:
|
|
337
370
|
// 1. Create a context with the value being enforced
|
|
338
371
|
// 2. Call the rule within the context, and pass over the arguments passed to it
|
|
339
372
|
// 3. Transform the result to the correct output format
|
|
340
|
-
|
|
341
|
-
return transformResult(rule(value,
|
|
373
|
+
var transformedResult = ctx.run({ value: value }, function () {
|
|
374
|
+
return transformResult.apply(void 0, __spreadArray([rule.apply(void 0, __spreadArray([value], args, false)), ruleName, value], args, false));
|
|
342
375
|
});
|
|
343
376
|
function enforceMessage() {
|
|
344
377
|
if (!vestUtils.isNullish(customMessage))
|
|
345
378
|
return vestUtils.StringObject(customMessage);
|
|
346
379
|
if (vestUtils.isNullish(transformedResult.message)) {
|
|
347
|
-
return
|
|
380
|
+
return "enforce/".concat(ruleName, " failed with ").concat(JSON.stringify(value));
|
|
348
381
|
}
|
|
349
382
|
return vestUtils.StringObject(transformedResult.message);
|
|
350
383
|
}
|
|
@@ -362,33 +395,39 @@ function enforceEager(value) {
|
|
|
362
395
|
|
|
363
396
|
// eslint-disable-next-line max-lines-per-function
|
|
364
397
|
function genEnforceLazy(key) {
|
|
365
|
-
|
|
366
|
-
|
|
398
|
+
var registeredRules = [];
|
|
399
|
+
var lazyMessage;
|
|
367
400
|
return addLazyRule(key);
|
|
368
401
|
// eslint-disable-next-line max-lines-per-function
|
|
369
402
|
function addLazyRule(ruleName) {
|
|
370
403
|
// eslint-disable-next-line max-lines-per-function
|
|
371
|
-
return (
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
404
|
+
return function () {
|
|
405
|
+
var args = [];
|
|
406
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
407
|
+
args[_i] = arguments[_i];
|
|
408
|
+
}
|
|
409
|
+
var rule = getRule(ruleName);
|
|
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) {
|
|
377
416
|
var _a;
|
|
378
|
-
|
|
417
|
+
var res = ctx.run({ value: value }, function () { return rule(value); });
|
|
379
418
|
breakout(!res.pass, ruleReturn(!!res.pass, (_a = vestUtils.optionalFunctionValue(lazyMessage, value, res.message)) !== null && _a !== void 0 ? _a : res.message));
|
|
380
419
|
}));
|
|
381
420
|
},
|
|
382
|
-
test: (value)
|
|
383
|
-
message: (message)
|
|
421
|
+
test: function (value) { return proxy.run(value).pass; },
|
|
422
|
+
message: function (message) {
|
|
384
423
|
if (message) {
|
|
385
424
|
lazyMessage = message;
|
|
386
425
|
}
|
|
387
426
|
return proxy;
|
|
388
|
-
}
|
|
427
|
+
}
|
|
389
428
|
};
|
|
390
429
|
if (!isProxySupported()) {
|
|
391
|
-
eachEnforceRule((ruleName)
|
|
430
|
+
eachEnforceRule(function (ruleName) {
|
|
392
431
|
proxy[ruleName] = addLazyRule(ruleName);
|
|
393
432
|
});
|
|
394
433
|
return proxy;
|
|
@@ -396,12 +435,12 @@ function genEnforceLazy(key) {
|
|
|
396
435
|
// reassigning the proxy here is not pretty
|
|
397
436
|
// but it's a cleaner way of getting `run` and `test` for free
|
|
398
437
|
proxy = new Proxy(proxy, {
|
|
399
|
-
get: (target, key)
|
|
438
|
+
get: function (target, key) {
|
|
400
439
|
if (getRule(key)) {
|
|
401
440
|
return addLazyRule(key);
|
|
402
441
|
}
|
|
403
442
|
return target[key]; // already has `run` and `test` on it
|
|
404
|
-
}
|
|
443
|
+
}
|
|
405
444
|
});
|
|
406
445
|
return proxy;
|
|
407
446
|
};
|
|
@@ -435,16 +474,16 @@ function genEnforceLazy(key) {
|
|
|
435
474
|
* while run will return an object with the validation result and an optional message created by the rule.
|
|
436
475
|
*/
|
|
437
476
|
function genEnforce() {
|
|
438
|
-
|
|
439
|
-
context: ()
|
|
440
|
-
extend: (customRules)
|
|
477
|
+
var target = {
|
|
478
|
+
context: function () { return ctx.useX(); },
|
|
479
|
+
extend: function (customRules) {
|
|
441
480
|
vestUtils.assign(baseRules, customRules);
|
|
442
481
|
handleNoProxy(); // TODO: REMOVE when we stop supporting ES5
|
|
443
|
-
}
|
|
482
|
+
}
|
|
444
483
|
};
|
|
445
484
|
handleNoProxy();
|
|
446
485
|
return new Proxy(vestUtils.assign(enforceEager, target), {
|
|
447
|
-
get: (target, key)
|
|
486
|
+
get: function (target, key) {
|
|
448
487
|
if (key in target) {
|
|
449
488
|
return target[key];
|
|
450
489
|
}
|
|
@@ -453,11 +492,11 @@ function genEnforce() {
|
|
|
453
492
|
}
|
|
454
493
|
// Only on the first rule access - start the chain of calls
|
|
455
494
|
return genEnforceLazy(key);
|
|
456
|
-
}
|
|
495
|
+
}
|
|
457
496
|
});
|
|
458
497
|
function handleNoProxy() {
|
|
459
498
|
if (!isProxySupported()) {
|
|
460
|
-
eachEnforceRule((ruleName)
|
|
499
|
+
eachEnforceRule(function (ruleName) {
|
|
461
500
|
// Only on the first rule access - start the chain of calls
|
|
462
501
|
target[ruleName] = genEnforceLazy(ruleName);
|
|
463
502
|
});
|
|
@@ -465,7 +504,7 @@ function genEnforce() {
|
|
|
465
504
|
}
|
|
466
505
|
}
|
|
467
506
|
}
|
|
468
|
-
|
|
507
|
+
var enforce = genEnforce();
|
|
469
508
|
|
|
470
509
|
exports.ctx = ctx;
|
|
471
510
|
exports.enforce = enforce;
|