n4s 4.1.9-dev-c786f7 → 4.2.0
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/n4s.development.js +17 -0
- package/dist/es/n4s.development.js +17 -0
- package/dist/umd/compose.development.js +8 -62
- package/dist/umd/compose.production.js +1 -1
- package/dist/umd/compounds.development.js +11 -55
- package/dist/umd/compounds.production.js +1 -1
- package/dist/umd/n4s.development.js +87 -267
- package/dist/umd/n4s.production.js +1 -1
- package/dist/umd/schema.development.js +8 -61
- package/dist/umd/schema.production.js +1 -1
- package/package.json +3 -3
- package/types/compose.d.ts +2 -1
- package/types/compounds.d.ts +8 -9
- package/types/n4s.d.ts +3 -3
- package/types/schema.d.ts +7 -5
|
@@ -333,30 +333,47 @@ function validateResult(result) {
|
|
|
333
333
|
|
|
334
334
|
function enforceEager(value) {
|
|
335
335
|
var target = {};
|
|
336
|
+
// This condition is for when we don't have proxy support (ES5).
|
|
337
|
+
// In this case, we need to manually assign the rules to the target object on runtime.
|
|
338
|
+
// The follow up proxy block is used in case we do have proxy support, and we can assign each rule upon invocation.
|
|
336
339
|
if (!isProxySupported()) {
|
|
340
|
+
// We iterate over each of the rules, and add them to the target object being return by enforce
|
|
337
341
|
eachEnforceRule(function (ruleName, ruleFn) {
|
|
342
|
+
// We then wrap the rule with `genRuleCall` that adds the base enforce behavior
|
|
338
343
|
target[ruleName] = genRuleCall(target, ruleFn, ruleName);
|
|
339
344
|
});
|
|
340
345
|
return target;
|
|
341
346
|
}
|
|
347
|
+
// We create a proxy intercepting access to the target object (which is empty).
|
|
342
348
|
var proxy = new Proxy(target, {
|
|
343
349
|
get: function (_, ruleName) {
|
|
350
|
+
// On property access, we identify if it is a rule or not.
|
|
344
351
|
var rule = getRule(ruleName);
|
|
352
|
+
// If it is a rule, we wrap it with `genRuleCall` that adds the base enforce behavior
|
|
345
353
|
if (rule) {
|
|
346
354
|
return genRuleCall(proxy, rule, ruleName);
|
|
347
355
|
}
|
|
348
356
|
}
|
|
349
357
|
});
|
|
350
358
|
return proxy;
|
|
359
|
+
// This function is used to wrap a rule with the base enforce behavior
|
|
360
|
+
// It takes the target object, the rule function, and the rule name
|
|
361
|
+
// It then returns the rule, in a manner that can be used by enforce
|
|
351
362
|
function genRuleCall(target, rule, ruleName) {
|
|
352
363
|
return function ruleCall() {
|
|
353
364
|
var args = [];
|
|
354
365
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
355
366
|
args[_i] = arguments[_i];
|
|
356
367
|
}
|
|
368
|
+
// Order of operation:
|
|
369
|
+
// 1. Create a context with the value being enforced
|
|
370
|
+
// 2. Call the rule within the context, and pass over the arguments passed to it
|
|
371
|
+
// 3. Transform the result to the correct output format
|
|
357
372
|
var transformedResult = ctx.run({ value: value }, function () {
|
|
358
373
|
return transformResult.apply(void 0, __spreadArray([rule.apply(void 0, __spreadArray([value], args, false)), ruleName, value], args, false));
|
|
359
374
|
});
|
|
375
|
+
// On rule failure (the result is false), we either throw an error
|
|
376
|
+
// or throw a string value if the rule has a message defined in it.
|
|
360
377
|
vestUtils.invariant(transformedResult.pass, vestUtils.isNullish(transformedResult.message)
|
|
361
378
|
? "enforce/".concat(ruleName, " failed with ").concat(JSON.stringify(value))
|
|
362
379
|
: vestUtils.StringObject(transformedResult.message));
|
|
@@ -329,30 +329,47 @@ function validateResult(result) {
|
|
|
329
329
|
|
|
330
330
|
function enforceEager(value) {
|
|
331
331
|
var target = {};
|
|
332
|
+
// This condition is for when we don't have proxy support (ES5).
|
|
333
|
+
// In this case, we need to manually assign the rules to the target object on runtime.
|
|
334
|
+
// The follow up proxy block is used in case we do have proxy support, and we can assign each rule upon invocation.
|
|
332
335
|
if (!isProxySupported()) {
|
|
336
|
+
// We iterate over each of the rules, and add them to the target object being return by enforce
|
|
333
337
|
eachEnforceRule(function (ruleName, ruleFn) {
|
|
338
|
+
// We then wrap the rule with `genRuleCall` that adds the base enforce behavior
|
|
334
339
|
target[ruleName] = genRuleCall(target, ruleFn, ruleName);
|
|
335
340
|
});
|
|
336
341
|
return target;
|
|
337
342
|
}
|
|
343
|
+
// We create a proxy intercepting access to the target object (which is empty).
|
|
338
344
|
var proxy = new Proxy(target, {
|
|
339
345
|
get: function (_, ruleName) {
|
|
346
|
+
// On property access, we identify if it is a rule or not.
|
|
340
347
|
var rule = getRule(ruleName);
|
|
348
|
+
// If it is a rule, we wrap it with `genRuleCall` that adds the base enforce behavior
|
|
341
349
|
if (rule) {
|
|
342
350
|
return genRuleCall(proxy, rule, ruleName);
|
|
343
351
|
}
|
|
344
352
|
}
|
|
345
353
|
});
|
|
346
354
|
return proxy;
|
|
355
|
+
// This function is used to wrap a rule with the base enforce behavior
|
|
356
|
+
// It takes the target object, the rule function, and the rule name
|
|
357
|
+
// It then returns the rule, in a manner that can be used by enforce
|
|
347
358
|
function genRuleCall(target, rule, ruleName) {
|
|
348
359
|
return function ruleCall() {
|
|
349
360
|
var args = [];
|
|
350
361
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
351
362
|
args[_i] = arguments[_i];
|
|
352
363
|
}
|
|
364
|
+
// Order of operation:
|
|
365
|
+
// 1. Create a context with the value being enforced
|
|
366
|
+
// 2. Call the rule within the context, and pass over the arguments passed to it
|
|
367
|
+
// 3. Transform the result to the correct output format
|
|
353
368
|
var transformedResult = ctx.run({ value: value }, function () {
|
|
354
369
|
return transformResult.apply(void 0, __spreadArray([rule.apply(void 0, __spreadArray([value], args, false)), ruleName, value], args, false));
|
|
355
370
|
});
|
|
371
|
+
// On rule failure (the result is false), we either throw an error
|
|
372
|
+
// or throw a string value if the rule has a message defined in it.
|
|
356
373
|
invariant(transformedResult.pass, isNullish(transformedResult.message)
|
|
357
374
|
? "enforce/".concat(ruleName, " failed with ").concat(JSON.stringify(value))
|
|
358
375
|
: StringObject(transformedResult.message));
|
|
@@ -1,62 +1,8 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('n4s')) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['n4s'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.compose = factory(global.n4s));
|
|
5
|
-
}(this, (function (n4s) { 'use strict';
|
|
6
|
-
|
|
7
|
-
function isFunction(value) {
|
|
8
|
-
return typeof value === 'function';
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function optionalFunctionValue(value) {
|
|
12
|
-
var args = [];
|
|
13
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
14
|
-
args[_i - 1] = arguments[_i];
|
|
15
|
-
}
|
|
16
|
-
return isFunction(value) ? value.apply(void 0, args) : value;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function defaultTo(value, defaultValue) {
|
|
20
|
-
var _a;
|
|
21
|
-
return (_a = optionalFunctionValue(value)) !== null && _a !== void 0 ? _a : optionalFunctionValue(defaultValue);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
var assign = Object.assign;
|
|
25
|
-
|
|
26
|
-
function invariant(condition,
|
|
27
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
28
|
-
message) {
|
|
29
|
-
if (condition) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
// If message is a string object (rather than string literal)
|
|
33
|
-
// Throw the value directly as a string
|
|
34
|
-
// Alternatively, throw an error with the message
|
|
35
|
-
throw message instanceof String
|
|
36
|
-
? message.valueOf()
|
|
37
|
-
: new Error(message ? optionalFunctionValue(message) : message);
|
|
38
|
-
}
|
|
39
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
40
|
-
function StringObject(value) {
|
|
41
|
-
return new String(optionalFunctionValue(value));
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function mapFirst(array, callback) {
|
|
45
|
-
var broke = false;
|
|
46
|
-
var breakoutValue = null;
|
|
47
|
-
for (var i = 0; i < array.length; i++) {
|
|
48
|
-
callback(array[i], breakout, i);
|
|
49
|
-
if (broke) {
|
|
50
|
-
return breakoutValue;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
function breakout(conditional, value) {
|
|
54
|
-
if (conditional) {
|
|
55
|
-
broke = true;
|
|
56
|
-
breakoutValue = value;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('n4s'), require('vest-utils')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['n4s', 'vest-utils'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.compose = factory(global.n4s, global['vest-utils']));
|
|
5
|
+
}(this, (function (n4s, vestUtils) { 'use strict';
|
|
60
6
|
|
|
61
7
|
function ruleReturn(pass, message) {
|
|
62
8
|
var output = { pass: pass };
|
|
@@ -72,7 +18,7 @@
|
|
|
72
18
|
return ruleReturn(true);
|
|
73
19
|
}
|
|
74
20
|
function defaultToPassing(callback) {
|
|
75
|
-
return defaultTo(callback, passing());
|
|
21
|
+
return vestUtils.defaultTo(callback, passing());
|
|
76
22
|
}
|
|
77
23
|
|
|
78
24
|
function runLazyRule(lazyRule, currentValue) {
|
|
@@ -90,16 +36,16 @@
|
|
|
90
36
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
91
37
|
composites[_i] = arguments[_i];
|
|
92
38
|
}
|
|
93
|
-
return assign(function (value) {
|
|
39
|
+
return vestUtils.assign(function (value) {
|
|
94
40
|
var res = run(value);
|
|
95
|
-
invariant(res.pass, StringObject(res.message));
|
|
41
|
+
vestUtils.invariant(res.pass, vestUtils.StringObject(res.message));
|
|
96
42
|
}, {
|
|
97
43
|
run: run,
|
|
98
44
|
test: function (value) { return run(value).pass; }
|
|
99
45
|
});
|
|
100
46
|
function run(value) {
|
|
101
47
|
return n4s.ctx.run({ value: value }, function () {
|
|
102
|
-
return defaultToPassing(mapFirst(composites, function (composite, breakout) {
|
|
48
|
+
return defaultToPassing(vestUtils.mapFirst(composites, function (composite, breakout) {
|
|
103
49
|
/* HACK: Just a small white lie. ~~HELP WANTED~~.
|
|
104
50
|
The ideal is that instead of `LazyRuleRunners` We would simply use `Lazy` to begin with.
|
|
105
51
|
The problem is that lazy rules can't really be passed to this function due to some generic hell
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";!function(n
|
|
1
|
+
"use strict";!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n(require("n4s"),require("vest-utils")):"function"==typeof define&&define.amd?define(["n4s","vest-utils"],n):(e="undefined"!=typeof globalThis?globalThis:e||self).compose=n(e.n4s,e["vest-utils"])}(this,(function(e,n){function t(e,n){return e={pass:e},n&&(e.message=n),e}return function(){function s(s){return e.ctx.run({value:s},(function(){return n.defaultTo(n.mapFirst(u,(function(e,n){try{var u=e.run(s)}catch(e){u=t(!1)}n(!u.pass,u)})),t(!0))}))}for(var u=[],i=0;i<arguments.length;i++)u[i]=arguments[i];return n.assign((function(e){e=s(e),n.invariant(e.pass,n.StringObject(e.message))}),{run:s,test:function(e){return s(e).pass}})}}));
|
|
@@ -1,53 +1,8 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('n4s')) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['n4s'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.n4s));
|
|
5
|
-
}(this, (function (n4s) { 'use strict';
|
|
6
|
-
|
|
7
|
-
function isNumeric(value) {
|
|
8
|
-
var str = String(value);
|
|
9
|
-
var num = Number(value);
|
|
10
|
-
var result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num);
|
|
11
|
-
return Boolean(result);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function greaterThan(value, gt) {
|
|
15
|
-
return isNumeric(value) && isNumeric(gt) && Number(value) > Number(gt);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function isFunction(value) {
|
|
19
|
-
return typeof value === 'function';
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function optionalFunctionValue(value) {
|
|
23
|
-
var args = [];
|
|
24
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
25
|
-
args[_i - 1] = arguments[_i];
|
|
26
|
-
}
|
|
27
|
-
return isFunction(value) ? value.apply(void 0, args) : value;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function defaultTo(value, defaultValue) {
|
|
31
|
-
var _a;
|
|
32
|
-
return (_a = optionalFunctionValue(value)) !== null && _a !== void 0 ? _a : optionalFunctionValue(defaultValue);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function mapFirst(array, callback) {
|
|
36
|
-
var broke = false;
|
|
37
|
-
var breakoutValue = null;
|
|
38
|
-
for (var i = 0; i < array.length; i++) {
|
|
39
|
-
callback(array[i], breakout, i);
|
|
40
|
-
if (broke) {
|
|
41
|
-
return breakoutValue;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
function breakout(conditional, value) {
|
|
45
|
-
if (conditional) {
|
|
46
|
-
broke = true;
|
|
47
|
-
breakoutValue = value;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('n4s'), require('vest-utils')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['n4s', 'vest-utils'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.n4s, global['vest-utils']));
|
|
5
|
+
}(this, (function (n4s, vestUtils) { 'use strict';
|
|
51
6
|
|
|
52
7
|
function ruleReturn(pass, message) {
|
|
53
8
|
var output = { pass: pass };
|
|
@@ -63,10 +18,10 @@
|
|
|
63
18
|
return ruleReturn(true);
|
|
64
19
|
}
|
|
65
20
|
function defaultToFailing(callback) {
|
|
66
|
-
return defaultTo(callback, failing());
|
|
21
|
+
return vestUtils.defaultTo(callback, failing());
|
|
67
22
|
}
|
|
68
23
|
function defaultToPassing(callback) {
|
|
69
|
-
return defaultTo(callback, passing());
|
|
24
|
+
return vestUtils.defaultTo(callback, passing());
|
|
70
25
|
}
|
|
71
26
|
|
|
72
27
|
function runLazyRule(lazyRule, currentValue) {
|
|
@@ -83,7 +38,7 @@
|
|
|
83
38
|
for (var _i = 1; _i < arguments.length; _i++) {
|
|
84
39
|
rules[_i - 1] = arguments[_i];
|
|
85
40
|
}
|
|
86
|
-
return defaultToPassing(mapFirst(rules, function (rule, breakout) {
|
|
41
|
+
return defaultToPassing(vestUtils.mapFirst(rules, function (rule, breakout) {
|
|
87
42
|
var res = runLazyRule(rule, value);
|
|
88
43
|
breakout(!res.pass, res);
|
|
89
44
|
}));
|
|
@@ -94,7 +49,7 @@
|
|
|
94
49
|
for (var _i = 1; _i < arguments.length; _i++) {
|
|
95
50
|
rules[_i - 1] = arguments[_i];
|
|
96
51
|
}
|
|
97
|
-
return defaultToFailing(mapFirst(rules, function (rule, breakout) {
|
|
52
|
+
return defaultToFailing(vestUtils.mapFirst(rules, function (rule, breakout) {
|
|
98
53
|
var res = runLazyRule(rule, value);
|
|
99
54
|
breakout(res.pass, res);
|
|
100
55
|
}));
|
|
@@ -105,7 +60,7 @@
|
|
|
105
60
|
for (var _i = 1; _i < arguments.length; _i++) {
|
|
106
61
|
rules[_i - 1] = arguments[_i];
|
|
107
62
|
}
|
|
108
|
-
return defaultToPassing(mapFirst(rules, function (rule, breakout) {
|
|
63
|
+
return defaultToPassing(vestUtils.mapFirst(rules, function (rule, breakout) {
|
|
109
64
|
var res = runLazyRule(rule, value);
|
|
110
65
|
breakout(res.pass, failing());
|
|
111
66
|
}));
|
|
@@ -114,6 +69,7 @@
|
|
|
114
69
|
function equals(value, arg1) {
|
|
115
70
|
return value === arg1;
|
|
116
71
|
}
|
|
72
|
+
vestUtils.bindNot(equals);
|
|
117
73
|
|
|
118
74
|
var REQUIRED_COUNT = 1;
|
|
119
75
|
function oneOf(value) {
|
|
@@ -127,7 +83,7 @@
|
|
|
127
83
|
if (res.pass) {
|
|
128
84
|
passingCount++;
|
|
129
85
|
}
|
|
130
|
-
if (greaterThan(passingCount, REQUIRED_COUNT)) {
|
|
86
|
+
if (vestUtils.greaterThan(passingCount, REQUIRED_COUNT)) {
|
|
131
87
|
return false;
|
|
132
88
|
}
|
|
133
89
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(require("n4s")):"function"==typeof define&&define.amd?define(["n4s"],e):e((n="undefined"!=typeof globalThis?globalThis:n||self).n4s
|
|
1
|
+
"use strict";!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(require("n4s"),require("vest-utils")):"function"==typeof define&&define.amd?define(["n4s","vest-utils"],e):e((n="undefined"!=typeof globalThis?globalThis:n||self).n4s,n["vest-utils"])}(this,(function(n,e){function t(n,e){return n={pass:n},e&&(n.message=e),n}function r(n,e){try{return n.run(e)}catch(n){return t(!1)}}e.bindNot((function(n,e){return n===e})),n.enforce.extend({allOf:function(n){for(var f=[],u=1;u<arguments.length;u++)f[u-1]=arguments[u];return e.defaultTo(e.mapFirst(f,(function(e,t){t(!(e=r(e,n)).pass,e)})),t(!0))},anyOf:function(n){for(var f=[],u=1;u<arguments.length;u++)f[u-1]=arguments[u];return e.defaultTo(e.mapFirst(f,(function(e,t){t((e=r(e,n)).pass,e)})),t(!1))},noneOf:function(n){for(var f=[],u=1;u<arguments.length;u++)f[u-1]=arguments[u];return e.defaultTo(e.mapFirst(f,(function(e,f){f((e=r(e,n)).pass,t(!1))})),t(!0))},oneOf:function(n){for(var f=[],u=1;u<arguments.length;u++)f[u-1]=arguments[u];var o=0;return f.some((function(t){if(r(t,n).pass&&o++,e.greaterThan(o,1))return!1})),t(1===o)}})}));
|
|
@@ -1,206 +1,60 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.n4s = {}));
|
|
5
|
-
}(this, (function (exports) { 'use strict';
|
|
6
|
-
|
|
7
|
-
function bindNot(fn) {
|
|
8
|
-
return function () {
|
|
9
|
-
var args = [];
|
|
10
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
11
|
-
args[_i] = arguments[_i];
|
|
12
|
-
}
|
|
13
|
-
return !fn.apply(void 0, args);
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function isNumeric(value) {
|
|
18
|
-
var str = String(value);
|
|
19
|
-
var num = Number(value);
|
|
20
|
-
var result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num);
|
|
21
|
-
return Boolean(result);
|
|
22
|
-
}
|
|
23
|
-
var isNotNumeric = bindNot(isNumeric);
|
|
24
|
-
|
|
25
|
-
function numberEquals(value, eq) {
|
|
26
|
-
return isNumeric(value) && isNumeric(eq) && Number(value) === Number(eq);
|
|
27
|
-
}
|
|
28
|
-
var numberNotEquals = bindNot(numberEquals);
|
|
29
|
-
|
|
30
|
-
function lengthEquals(value, arg1) {
|
|
31
|
-
return numberEquals(value.length, arg1);
|
|
32
|
-
}
|
|
33
|
-
var lengthNotEquals = bindNot(lengthEquals);
|
|
34
|
-
|
|
35
|
-
function greaterThan(value, gt) {
|
|
36
|
-
return isNumeric(value) && isNumeric(gt) && Number(value) > Number(gt);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function longerThan(value, arg1) {
|
|
40
|
-
return greaterThan(value.length, arg1);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function isNull(value) {
|
|
44
|
-
return value === null;
|
|
45
|
-
}
|
|
46
|
-
var isNotNull = bindNot(isNull);
|
|
47
|
-
|
|
48
|
-
function isUndefined(value) {
|
|
49
|
-
return value === undefined;
|
|
50
|
-
}
|
|
51
|
-
var isNotUndefined = bindNot(isUndefined);
|
|
52
|
-
|
|
53
|
-
function isNullish(value) {
|
|
54
|
-
return isNull(value) || isUndefined(value);
|
|
55
|
-
}
|
|
56
|
-
var isNotNullish = bindNot(isNullish);
|
|
57
|
-
|
|
58
|
-
function isFunction(value) {
|
|
59
|
-
return typeof value === 'function';
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function optionalFunctionValue(value) {
|
|
63
|
-
var args = [];
|
|
64
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
65
|
-
args[_i - 1] = arguments[_i];
|
|
66
|
-
}
|
|
67
|
-
return isFunction(value) ? value.apply(void 0, args) : value;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function defaultTo(value, defaultValue) {
|
|
71
|
-
var _a;
|
|
72
|
-
return (_a = optionalFunctionValue(value)) !== null && _a !== void 0 ? _a : optionalFunctionValue(defaultValue);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// The module is named "isArrayValue" since it
|
|
76
|
-
// is conflicting with a nested npm dependency.
|
|
77
|
-
// We may need to revisit this in the future.
|
|
78
|
-
function isArray(value) {
|
|
79
|
-
return Boolean(Array.isArray(value));
|
|
80
|
-
}
|
|
81
|
-
var isNotArray = bindNot(isArray);
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* A safe hasOwnProperty access
|
|
85
|
-
*/
|
|
86
|
-
function hasOwnProperty(obj, key) {
|
|
87
|
-
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
var assign = Object.assign;
|
|
91
|
-
|
|
92
|
-
function invariant(condition,
|
|
93
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
94
|
-
message) {
|
|
95
|
-
if (condition) {
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
// If message is a string object (rather than string literal)
|
|
99
|
-
// Throw the value directly as a string
|
|
100
|
-
// Alternatively, throw an error with the message
|
|
101
|
-
throw message instanceof String
|
|
102
|
-
? message.valueOf()
|
|
103
|
-
: new Error(message ? optionalFunctionValue(message) : message);
|
|
104
|
-
}
|
|
105
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
106
|
-
function StringObject(value) {
|
|
107
|
-
return new String(optionalFunctionValue(value));
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function isStringValue(v) {
|
|
111
|
-
return String(v) === v;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
function isBoolean(value) {
|
|
115
|
-
return !!value === value;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function mapFirst(array, callback) {
|
|
119
|
-
var broke = false;
|
|
120
|
-
var breakoutValue = null;
|
|
121
|
-
for (var i = 0; i < array.length; i++) {
|
|
122
|
-
callback(array[i], breakout, i);
|
|
123
|
-
if (broke) {
|
|
124
|
-
return breakoutValue;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
function breakout(conditional, value) {
|
|
128
|
-
if (conditional) {
|
|
129
|
-
broke = true;
|
|
130
|
-
breakoutValue = value;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function isEmpty(value) {
|
|
136
|
-
if (!value) {
|
|
137
|
-
return true;
|
|
138
|
-
}
|
|
139
|
-
else if (hasOwnProperty(value, 'length')) {
|
|
140
|
-
return lengthEquals(value, 0);
|
|
141
|
-
}
|
|
142
|
-
else if (typeof value === 'object') {
|
|
143
|
-
return lengthEquals(Object.keys(value), 0);
|
|
144
|
-
}
|
|
145
|
-
return false;
|
|
146
|
-
}
|
|
147
|
-
var isNotEmpty = bindNot(isEmpty);
|
|
148
|
-
|
|
149
|
-
function isPositive(value) {
|
|
150
|
-
return greaterThan(value, 0);
|
|
151
|
-
}
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('vest-utils'), require('context')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', 'vest-utils', 'context'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.n4s = {}, global['vest-utils'], global.context));
|
|
5
|
+
}(this, (function (exports, vestUtils, context) { 'use strict';
|
|
152
6
|
|
|
153
7
|
function endsWith(value, arg1) {
|
|
154
|
-
return isStringValue(value) && isStringValue(arg1) && value.endsWith(arg1);
|
|
8
|
+
return vestUtils.isStringValue(value) && vestUtils.isStringValue(arg1) && value.endsWith(arg1);
|
|
155
9
|
}
|
|
156
|
-
var doesNotEndWith = bindNot(endsWith);
|
|
10
|
+
var doesNotEndWith = vestUtils.bindNot(endsWith);
|
|
157
11
|
|
|
158
12
|
function equals(value, arg1) {
|
|
159
13
|
return value === arg1;
|
|
160
14
|
}
|
|
161
|
-
var notEquals = bindNot(equals);
|
|
15
|
+
var notEquals = vestUtils.bindNot(equals);
|
|
162
16
|
|
|
163
17
|
function greaterThanOrEquals(value, gte) {
|
|
164
|
-
return numberEquals(value, gte) || greaterThan(value, gte);
|
|
18
|
+
return vestUtils.numberEquals(value, gte) || vestUtils.greaterThan(value, gte);
|
|
165
19
|
}
|
|
166
20
|
|
|
167
21
|
function inside(value, arg1) {
|
|
168
|
-
if (isArray(arg1)) {
|
|
22
|
+
if (vestUtils.isArray(arg1)) {
|
|
169
23
|
return arg1.indexOf(value) !== -1;
|
|
170
24
|
}
|
|
171
25
|
// both value and arg1 are strings
|
|
172
|
-
if (isStringValue(arg1) && isStringValue(value)) {
|
|
26
|
+
if (vestUtils.isStringValue(arg1) && vestUtils.isStringValue(value)) {
|
|
173
27
|
return arg1.indexOf(value) !== -1;
|
|
174
28
|
}
|
|
175
29
|
return false;
|
|
176
30
|
}
|
|
177
|
-
var notInside = bindNot(inside);
|
|
31
|
+
var notInside = vestUtils.bindNot(inside);
|
|
178
32
|
|
|
179
33
|
function lessThan(value, lt) {
|
|
180
|
-
return isNumeric(value) && isNumeric(lt) && Number(value) < Number(lt);
|
|
34
|
+
return vestUtils.isNumeric(value) && vestUtils.isNumeric(lt) && Number(value) < Number(lt);
|
|
181
35
|
}
|
|
182
36
|
|
|
183
37
|
function lessThanOrEquals(value, lte) {
|
|
184
|
-
return numberEquals(value, lte) || lessThan(value, lte);
|
|
38
|
+
return vestUtils.numberEquals(value, lte) || lessThan(value, lte);
|
|
185
39
|
}
|
|
186
40
|
|
|
187
41
|
function isBetween(value, min, max) {
|
|
188
42
|
return greaterThanOrEquals(value, min) && lessThanOrEquals(value, max);
|
|
189
43
|
}
|
|
190
|
-
var isNotBetween = bindNot(isBetween);
|
|
44
|
+
var isNotBetween = vestUtils.bindNot(isBetween);
|
|
191
45
|
|
|
192
46
|
function isBlank(value) {
|
|
193
|
-
return isNullish(value) || (isStringValue(value) && !value.trim());
|
|
47
|
+
return vestUtils.isNullish(value) || (vestUtils.isStringValue(value) && !value.trim());
|
|
194
48
|
}
|
|
195
|
-
var isNotBlank = bindNot(isBlank);
|
|
49
|
+
var isNotBlank = vestUtils.bindNot(isBlank);
|
|
196
50
|
|
|
197
|
-
var isNotBoolean = bindNot(isBoolean);
|
|
51
|
+
var isNotBoolean = vestUtils.bindNot(vestUtils.isBoolean);
|
|
198
52
|
|
|
199
53
|
/**
|
|
200
54
|
* Validates that a given value is an even number
|
|
201
55
|
*/
|
|
202
56
|
var isEven = function (value) {
|
|
203
|
-
if (isNumeric(value)) {
|
|
57
|
+
if (vestUtils.isNumeric(value)) {
|
|
204
58
|
return value % 2 === 0;
|
|
205
59
|
}
|
|
206
60
|
return false;
|
|
@@ -209,12 +63,12 @@
|
|
|
209
63
|
function isKeyOf(key, obj) {
|
|
210
64
|
return key in obj;
|
|
211
65
|
}
|
|
212
|
-
var isNotKeyOf = bindNot(isKeyOf);
|
|
66
|
+
var isNotKeyOf = vestUtils.bindNot(isKeyOf);
|
|
213
67
|
|
|
214
|
-
function isNaN
|
|
68
|
+
function isNaN(value) {
|
|
215
69
|
return Number.isNaN(value);
|
|
216
70
|
}
|
|
217
|
-
var isNotNaN = bindNot(isNaN
|
|
71
|
+
var isNotNaN = vestUtils.bindNot(isNaN);
|
|
218
72
|
|
|
219
73
|
function isNegative(value) {
|
|
220
74
|
return lessThan(value, 0);
|
|
@@ -223,27 +77,27 @@
|
|
|
223
77
|
function isNumber(value) {
|
|
224
78
|
return Boolean(typeof value === 'number');
|
|
225
79
|
}
|
|
226
|
-
var isNotNumber = bindNot(isNumber);
|
|
80
|
+
var isNotNumber = vestUtils.bindNot(isNumber);
|
|
227
81
|
|
|
228
82
|
/**
|
|
229
83
|
* Validates that a given value is an odd number
|
|
230
84
|
*/
|
|
231
85
|
var isOdd = function (value) {
|
|
232
|
-
if (isNumeric(value)) {
|
|
86
|
+
if (vestUtils.isNumeric(value)) {
|
|
233
87
|
return value % 2 !== 0;
|
|
234
88
|
}
|
|
235
89
|
return false;
|
|
236
90
|
};
|
|
237
91
|
|
|
238
|
-
var isNotString = bindNot(isStringValue);
|
|
92
|
+
var isNotString = vestUtils.bindNot(vestUtils.isStringValue);
|
|
239
93
|
|
|
240
94
|
function isTruthy(value) {
|
|
241
95
|
return !!value;
|
|
242
96
|
}
|
|
243
|
-
var isFalsy = bindNot(isTruthy);
|
|
97
|
+
var isFalsy = vestUtils.bindNot(isTruthy);
|
|
244
98
|
|
|
245
99
|
function isValueOf(value, objectToCheck) {
|
|
246
|
-
if (isNullish(objectToCheck)) {
|
|
100
|
+
if (vestUtils.isNullish(objectToCheck)) {
|
|
247
101
|
return false;
|
|
248
102
|
}
|
|
249
103
|
for (var key in objectToCheck) {
|
|
@@ -253,7 +107,7 @@
|
|
|
253
107
|
}
|
|
254
108
|
return false;
|
|
255
109
|
}
|
|
256
|
-
var isNotValueOf = bindNot(isValueOf);
|
|
110
|
+
var isNotValueOf = vestUtils.bindNot(isValueOf);
|
|
257
111
|
|
|
258
112
|
function longerThanOrEquals(value, arg1) {
|
|
259
113
|
return greaterThanOrEquals(value.length, arg1);
|
|
@@ -263,14 +117,14 @@
|
|
|
263
117
|
if (regex instanceof RegExp) {
|
|
264
118
|
return regex.test(value);
|
|
265
119
|
}
|
|
266
|
-
else if (isStringValue(regex)) {
|
|
120
|
+
else if (vestUtils.isStringValue(regex)) {
|
|
267
121
|
return new RegExp(regex).test(value);
|
|
268
122
|
}
|
|
269
123
|
else {
|
|
270
124
|
return false;
|
|
271
125
|
}
|
|
272
126
|
}
|
|
273
|
-
var notMatches = bindNot(matches);
|
|
127
|
+
var notMatches = vestUtils.bindNot(matches);
|
|
274
128
|
|
|
275
129
|
function condition(value, callback) {
|
|
276
130
|
try {
|
|
@@ -290,9 +144,9 @@
|
|
|
290
144
|
}
|
|
291
145
|
|
|
292
146
|
function startsWith(value, arg1) {
|
|
293
|
-
return isStringValue(value) && isStringValue(arg1) && value.startsWith(arg1);
|
|
147
|
+
return vestUtils.isStringValue(value) && vestUtils.isStringValue(arg1) && value.startsWith(arg1);
|
|
294
148
|
}
|
|
295
|
-
var doesNotStartWith = bindNot(startsWith);
|
|
149
|
+
var doesNotStartWith = vestUtils.bindNot(startsWith);
|
|
296
150
|
|
|
297
151
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, max-lines-per-function
|
|
298
152
|
function rules() {
|
|
@@ -302,50 +156,50 @@
|
|
|
302
156
|
doesNotStartWith: doesNotStartWith,
|
|
303
157
|
endsWith: endsWith,
|
|
304
158
|
equals: equals,
|
|
305
|
-
greaterThan: greaterThan,
|
|
159
|
+
greaterThan: vestUtils.greaterThan,
|
|
306
160
|
greaterThanOrEquals: greaterThanOrEquals,
|
|
307
|
-
gt: greaterThan,
|
|
161
|
+
gt: vestUtils.greaterThan,
|
|
308
162
|
gte: greaterThanOrEquals,
|
|
309
163
|
inside: inside,
|
|
310
|
-
isArray: isArray,
|
|
164
|
+
isArray: vestUtils.isArray,
|
|
311
165
|
isBetween: isBetween,
|
|
312
166
|
isBlank: isBlank,
|
|
313
|
-
isBoolean: isBoolean,
|
|
314
|
-
isEmpty: isEmpty,
|
|
167
|
+
isBoolean: vestUtils.isBoolean,
|
|
168
|
+
isEmpty: vestUtils.isEmpty,
|
|
315
169
|
isEven: isEven,
|
|
316
170
|
isFalsy: isFalsy,
|
|
317
171
|
isKeyOf: isKeyOf,
|
|
318
|
-
isNaN: isNaN
|
|
172
|
+
isNaN: isNaN,
|
|
319
173
|
isNegative: isNegative,
|
|
320
|
-
isNotArray: isNotArray,
|
|
174
|
+
isNotArray: vestUtils.isNotArray,
|
|
321
175
|
isNotBetween: isNotBetween,
|
|
322
176
|
isNotBlank: isNotBlank,
|
|
323
177
|
isNotBoolean: isNotBoolean,
|
|
324
|
-
isNotEmpty: isNotEmpty,
|
|
178
|
+
isNotEmpty: vestUtils.isNotEmpty,
|
|
325
179
|
isNotKeyOf: isNotKeyOf,
|
|
326
180
|
isNotNaN: isNotNaN,
|
|
327
|
-
isNotNull: isNotNull,
|
|
328
|
-
isNotNullish: isNotNullish,
|
|
181
|
+
isNotNull: vestUtils.isNotNull,
|
|
182
|
+
isNotNullish: vestUtils.isNotNullish,
|
|
329
183
|
isNotNumber: isNotNumber,
|
|
330
|
-
isNotNumeric: isNotNumeric,
|
|
184
|
+
isNotNumeric: vestUtils.isNotNumeric,
|
|
331
185
|
isNotString: isNotString,
|
|
332
|
-
isNotUndefined: isNotUndefined,
|
|
186
|
+
isNotUndefined: vestUtils.isNotUndefined,
|
|
333
187
|
isNotValueOf: isNotValueOf,
|
|
334
|
-
isNull: isNull,
|
|
335
|
-
isNullish: isNullish,
|
|
188
|
+
isNull: vestUtils.isNull,
|
|
189
|
+
isNullish: vestUtils.isNullish,
|
|
336
190
|
isNumber: isNumber,
|
|
337
|
-
isNumeric: isNumeric,
|
|
191
|
+
isNumeric: vestUtils.isNumeric,
|
|
338
192
|
isOdd: isOdd,
|
|
339
|
-
isPositive: isPositive,
|
|
340
|
-
isString: isStringValue,
|
|
193
|
+
isPositive: vestUtils.isPositive,
|
|
194
|
+
isString: vestUtils.isStringValue,
|
|
341
195
|
isTruthy: isTruthy,
|
|
342
|
-
isUndefined: isUndefined,
|
|
196
|
+
isUndefined: vestUtils.isUndefined,
|
|
343
197
|
isValueOf: isValueOf,
|
|
344
|
-
lengthEquals: lengthEquals,
|
|
345
|
-
lengthNotEquals: lengthNotEquals,
|
|
198
|
+
lengthEquals: vestUtils.lengthEquals,
|
|
199
|
+
lengthNotEquals: vestUtils.lengthNotEquals,
|
|
346
200
|
lessThan: lessThan,
|
|
347
201
|
lessThanOrEquals: lessThanOrEquals,
|
|
348
|
-
longerThan: longerThan,
|
|
202
|
+
longerThan: vestUtils.longerThan,
|
|
349
203
|
longerThanOrEquals: longerThanOrEquals,
|
|
350
204
|
lt: lessThan,
|
|
351
205
|
lte: lessThanOrEquals,
|
|
@@ -353,8 +207,8 @@
|
|
|
353
207
|
notEquals: notEquals,
|
|
354
208
|
notInside: notInside,
|
|
355
209
|
notMatches: notMatches,
|
|
356
|
-
numberEquals: numberEquals,
|
|
357
|
-
numberNotEquals: numberNotEquals,
|
|
210
|
+
numberEquals: vestUtils.numberEquals,
|
|
211
|
+
numberNotEquals: vestUtils.numberNotEquals,
|
|
358
212
|
shorterThan: shorterThan,
|
|
359
213
|
shorterThanOrEquals: shorterThanOrEquals,
|
|
360
214
|
startsWith: startsWith
|
|
@@ -369,75 +223,24 @@
|
|
|
369
223
|
function eachEnforceRule(action) {
|
|
370
224
|
for (var ruleName in baseRules) {
|
|
371
225
|
var ruleFn = getRule(ruleName);
|
|
372
|
-
if (isFunction(ruleFn)) {
|
|
226
|
+
if (vestUtils.isFunction(ruleFn)) {
|
|
373
227
|
action(ruleName, ruleFn);
|
|
374
228
|
}
|
|
375
229
|
}
|
|
376
230
|
}
|
|
377
231
|
|
|
378
|
-
|
|
379
|
-
function createContext(init) {
|
|
380
|
-
var storage = { ancestry: [] };
|
|
381
|
-
return {
|
|
382
|
-
bind: bind,
|
|
383
|
-
run: run,
|
|
384
|
-
use: use,
|
|
385
|
-
useX: useX
|
|
386
|
-
};
|
|
387
|
-
function useX(errorMessage) {
|
|
388
|
-
var ctx = use();
|
|
389
|
-
invariant(ctx, defaultTo(errorMessage, 'Context was used after it was closed'));
|
|
390
|
-
return ctx;
|
|
391
|
-
}
|
|
392
|
-
function run(ctxRef, fn) {
|
|
393
|
-
var _a;
|
|
394
|
-
var parentContext = use();
|
|
395
|
-
var out = assign({}, parentContext ? parentContext : {}, (_a = optionalFunctionValue(init, ctxRef, parentContext)) !== null && _a !== void 0 ? _a : ctxRef);
|
|
396
|
-
var ctx = set(Object.freeze(out));
|
|
397
|
-
storage.ancestry.unshift(ctx);
|
|
398
|
-
var res = fn(ctx);
|
|
399
|
-
clear();
|
|
400
|
-
return res;
|
|
401
|
-
}
|
|
402
|
-
function bind(ctxRef, fn) {
|
|
403
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
404
|
-
// @ts-ignore - this one's pretty hard to get right
|
|
405
|
-
var returnedFn = function () {
|
|
406
|
-
var runTimeArgs = [];
|
|
407
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
408
|
-
runTimeArgs[_i] = arguments[_i];
|
|
409
|
-
}
|
|
410
|
-
return run(ctxRef, function () {
|
|
411
|
-
return fn.apply(void 0, runTimeArgs);
|
|
412
|
-
});
|
|
413
|
-
};
|
|
414
|
-
return returnedFn;
|
|
415
|
-
}
|
|
416
|
-
function use() {
|
|
417
|
-
return storage.ctx;
|
|
418
|
-
}
|
|
419
|
-
function set(value) {
|
|
420
|
-
return (storage.ctx = value);
|
|
421
|
-
}
|
|
422
|
-
function clear() {
|
|
423
|
-
var _a;
|
|
424
|
-
storage.ancestry.shift();
|
|
425
|
-
set((_a = storage.ancestry[0]) !== null && _a !== void 0 ? _a : null);
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
var ctx = createContext(function (ctxRef, parentContext) {
|
|
232
|
+
var ctx = context.createContext(function (ctxRef, parentContext) {
|
|
430
233
|
var base = {
|
|
431
234
|
value: ctxRef.value,
|
|
432
235
|
meta: ctxRef.meta || {}
|
|
433
236
|
};
|
|
434
237
|
if (!parentContext) {
|
|
435
|
-
return assign(base, {
|
|
238
|
+
return vestUtils.assign(base, {
|
|
436
239
|
parent: emptyParent
|
|
437
240
|
});
|
|
438
241
|
}
|
|
439
242
|
else if (ctxRef.set) {
|
|
440
|
-
return assign(base, {
|
|
243
|
+
return vestUtils.assign(base, {
|
|
441
244
|
parent: function () { return stripContext(parentContext); }
|
|
442
245
|
});
|
|
443
246
|
}
|
|
@@ -484,7 +287,7 @@
|
|
|
484
287
|
|
|
485
288
|
function isProxySupported() {
|
|
486
289
|
try {
|
|
487
|
-
return isFunction(Proxy);
|
|
290
|
+
return vestUtils.isFunction(Proxy);
|
|
488
291
|
}
|
|
489
292
|
catch (_a) {
|
|
490
293
|
return false;
|
|
@@ -502,7 +305,7 @@
|
|
|
502
305
|
return ruleReturn(true);
|
|
503
306
|
}
|
|
504
307
|
function defaultToPassing(callback) {
|
|
505
|
-
return defaultTo(callback, passing());
|
|
308
|
+
return vestUtils.defaultTo(callback, passing());
|
|
506
309
|
}
|
|
507
310
|
|
|
508
311
|
/**
|
|
@@ -515,47 +318,64 @@
|
|
|
515
318
|
}
|
|
516
319
|
validateResult(result);
|
|
517
320
|
// if result is boolean
|
|
518
|
-
if (isBoolean(result)) {
|
|
321
|
+
if (vestUtils.isBoolean(result)) {
|
|
519
322
|
return ruleReturn(result);
|
|
520
323
|
}
|
|
521
324
|
else {
|
|
522
|
-
return ruleReturn(result.pass, optionalFunctionValue.apply(void 0, __spreadArray([result.message, ruleName, value], args, false)));
|
|
325
|
+
return ruleReturn(result.pass, vestUtils.optionalFunctionValue.apply(void 0, __spreadArray([result.message, ruleName, value], args, false)));
|
|
523
326
|
}
|
|
524
327
|
}
|
|
525
328
|
function validateResult(result) {
|
|
526
329
|
// if result is boolean, or if result.pass is boolean
|
|
527
|
-
invariant(isBoolean(result) || (result && isBoolean(result.pass)), 'Incorrect return value for rule: ' + JSON.stringify(result));
|
|
330
|
+
vestUtils.invariant(vestUtils.isBoolean(result) || (result && vestUtils.isBoolean(result.pass)), 'Incorrect return value for rule: ' + JSON.stringify(result));
|
|
528
331
|
}
|
|
529
332
|
|
|
530
333
|
function enforceEager(value) {
|
|
531
334
|
var target = {};
|
|
335
|
+
// This condition is for when we don't have proxy support (ES5).
|
|
336
|
+
// In this case, we need to manually assign the rules to the target object on runtime.
|
|
337
|
+
// The follow up proxy block is used in case we do have proxy support, and we can assign each rule upon invocation.
|
|
532
338
|
if (!isProxySupported()) {
|
|
339
|
+
// We iterate over each of the rules, and add them to the target object being return by enforce
|
|
533
340
|
eachEnforceRule(function (ruleName, ruleFn) {
|
|
341
|
+
// We then wrap the rule with `genRuleCall` that adds the base enforce behavior
|
|
534
342
|
target[ruleName] = genRuleCall(target, ruleFn, ruleName);
|
|
535
343
|
});
|
|
536
344
|
return target;
|
|
537
345
|
}
|
|
346
|
+
// We create a proxy intercepting access to the target object (which is empty).
|
|
538
347
|
var proxy = new Proxy(target, {
|
|
539
348
|
get: function (_, ruleName) {
|
|
349
|
+
// On property access, we identify if it is a rule or not.
|
|
540
350
|
var rule = getRule(ruleName);
|
|
351
|
+
// If it is a rule, we wrap it with `genRuleCall` that adds the base enforce behavior
|
|
541
352
|
if (rule) {
|
|
542
353
|
return genRuleCall(proxy, rule, ruleName);
|
|
543
354
|
}
|
|
544
355
|
}
|
|
545
356
|
});
|
|
546
357
|
return proxy;
|
|
358
|
+
// This function is used to wrap a rule with the base enforce behavior
|
|
359
|
+
// It takes the target object, the rule function, and the rule name
|
|
360
|
+
// It then returns the rule, in a manner that can be used by enforce
|
|
547
361
|
function genRuleCall(target, rule, ruleName) {
|
|
548
362
|
return function ruleCall() {
|
|
549
363
|
var args = [];
|
|
550
364
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
551
365
|
args[_i] = arguments[_i];
|
|
552
366
|
}
|
|
367
|
+
// Order of operation:
|
|
368
|
+
// 1. Create a context with the value being enforced
|
|
369
|
+
// 2. Call the rule within the context, and pass over the arguments passed to it
|
|
370
|
+
// 3. Transform the result to the correct output format
|
|
553
371
|
var transformedResult = ctx.run({ value: value }, function () {
|
|
554
372
|
return transformResult.apply(void 0, __spreadArray([rule.apply(void 0, __spreadArray([value], args, false)), ruleName, value], args, false));
|
|
555
373
|
});
|
|
556
|
-
|
|
374
|
+
// On rule failure (the result is false), we either throw an error
|
|
375
|
+
// or throw a string value if the rule has a message defined in it.
|
|
376
|
+
vestUtils.invariant(transformedResult.pass, vestUtils.isNullish(transformedResult.message)
|
|
557
377
|
? "enforce/".concat(ruleName, " failed with ").concat(JSON.stringify(value))
|
|
558
|
-
: StringObject(transformedResult.message));
|
|
378
|
+
: vestUtils.StringObject(transformedResult.message));
|
|
559
379
|
return target;
|
|
560
380
|
};
|
|
561
381
|
}
|
|
@@ -580,10 +400,10 @@
|
|
|
580
400
|
});
|
|
581
401
|
var proxy = {
|
|
582
402
|
run: function (value) {
|
|
583
|
-
return defaultToPassing(mapFirst(registeredRules, function (rule, breakout) {
|
|
403
|
+
return defaultToPassing(vestUtils.mapFirst(registeredRules, function (rule, breakout) {
|
|
584
404
|
var _a;
|
|
585
405
|
var res = ctx.run({ value: value }, function () { return rule(value); });
|
|
586
|
-
breakout(!res.pass, ruleReturn(!!res.pass, (_a = optionalFunctionValue(lazyMessage, value, res.message)) !== null && _a !== void 0 ? _a : res.message));
|
|
406
|
+
breakout(!res.pass, ruleReturn(!!res.pass, (_a = vestUtils.optionalFunctionValue(lazyMessage, value, res.message)) !== null && _a !== void 0 ? _a : res.message));
|
|
587
407
|
}));
|
|
588
408
|
},
|
|
589
409
|
test: function (value) { return proxy.run(value).pass; },
|
|
@@ -645,12 +465,12 @@
|
|
|
645
465
|
var target = {
|
|
646
466
|
context: function () { return ctx.useX(); },
|
|
647
467
|
extend: function (customRules) {
|
|
648
|
-
assign(baseRules, customRules);
|
|
468
|
+
vestUtils.assign(baseRules, customRules);
|
|
649
469
|
handleNoProxy(); // TODO: REMOVE when we stop supporting ES5
|
|
650
470
|
}
|
|
651
471
|
};
|
|
652
472
|
handleNoProxy();
|
|
653
|
-
return new Proxy(assign(enforceEager, target), {
|
|
473
|
+
return new Proxy(vestUtils.assign(enforceEager, target), {
|
|
654
474
|
get: function (target, key) {
|
|
655
475
|
if (key in target) {
|
|
656
476
|
return target[key];
|
|
@@ -668,7 +488,7 @@
|
|
|
668
488
|
// Only on the first rule access - start the chain of calls
|
|
669
489
|
target[ruleName] = genEnforceLazy(ruleName);
|
|
670
490
|
});
|
|
671
|
-
return assign(enforceEager, target);
|
|
491
|
+
return vestUtils.assign(enforceEager, target);
|
|
672
492
|
}
|
|
673
493
|
}
|
|
674
494
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self).n4s={}
|
|
1
|
+
"use strict";!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){function i(n,e){return t.isStringValue(n)&&t.isStringValue(e)&&n.endsWith(e)}function r(n,t){return n===t}function u(n,e){return t.numberEquals(n,e)||t.greaterThan(n,e)}function s(n,e){return!!(t.isArray(e)||t.isStringValue(e)&&t.isStringValue(n))&&-1!==e.indexOf(n)}function o(n,e){return t.isNumeric(n)&&t.isNumeric(e)&&Number(n)<Number(e)}function a(n,e){return t.numberEquals(n,e)||o(n,e)}function l(n,t,e){return u(n,t)&&a(n,e)}function c(n){return t.isNullish(n)||t.isStringValue(n)&&!n.trim()}function f(n,t){return n in t}function N(n){return Number.isNaN(n)}function g(n){return"number"==typeof n}function d(n){return!!n}function h(n,e){if(t.isNullish(e))return!1;for(var i in e)if(e[i]===n)return!0;return!1}function p(n,e){return e instanceof RegExp?e.test(n):!!t.isStringValue(e)&&new RegExp(e).test(n)}function m(n,e){return t.isStringValue(n)&&t.isStringValue(e)&&n.startsWith(e)}function v(n){for(var e in K){var i=K[e];t.isFunction(i)&&n(e,i)}}function y(){return null}function b(n,t,e){if(e||2===arguments.length)for(var i,r=0,u=t.length;r<u;r++)!i&&r in t||(i||(i=Array.prototype.slice.call(t,0,r)),i[r]=t[r]);return n.concat(i||Array.prototype.slice.call(t))}function E(){try{return t.isFunction(Proxy)}catch(n){return!1}}function q(n,t){return n={pass:n},t&&(n.message=t),n}function x(n,e,i){for(var r=[],u=3;u<arguments.length;u++)r[u-3]=arguments[u];return t.invariant(t.isBoolean(n)||n&&t.isBoolean(n.pass),"Incorrect return value for rule: "+JSON.stringify(n)),t.isBoolean(n)?q(n):q(n.pass,t.optionalFunctionValue.apply(void 0,b([n.message,e,i],r,!1)))}function S(n){function e(e,i,r){return function(){for(var u=[],s=0;s<arguments.length;s++)u[s]=arguments[s];return s=M.run({value:n},(function(){return x.apply(void 0,b([i.apply(void 0,b([n],u,!1)),r,n],u,!1))})),t.invariant(s.pass,t.isNullish(s.message)?"enforce/".concat(r," failed with ").concat(JSON.stringify(n)):t.StringObject(s.message)),e}}var i={};if(!E())return v((function(n,t){i[n]=e(i,t,n)})),i;var r=new Proxy(i,{get:function(n,t){if(n=K[t])return e(r,n,t)}});return r}function T(n){var e,i=[];return function n(r){return function(){for(var u=[],s=0;s<arguments.length;s++)u[s]=arguments[s];var o=K[r];i.push((function(n){return x.apply(void 0,b([o.apply(void 0,b([n],u,!1)),r,n],u,!1))}));var a={run:function(n){return t.defaultTo(t.mapFirst(i,(function(i,r){var u,s=M.run({value:n},(function(){return i(n)}));r(!s.pass,q(!!s.pass,null!==(u=t.optionalFunctionValue(e,n,s.message))&&void 0!==u?u:s.message))})),q(!0))},test:function(n){return a.run(n).pass},message:function(n){return n&&(e=n),a}};return E()?a=new Proxy(a,{get:function(t,e){return K[e]?n(e):t[e]}}):(v((function(t){a[t]=n(t)})),a)}}(n)}var O=t.bindNot(i),V=t.bindNot(r),B=t.bindNot(s),w=t.bindNot(l),A=t.bindNot(c),P=t.bindNot(t.isBoolean),F=t.bindNot(f),W=t.bindNot(N),U=t.bindNot(g),j=t.bindNot(t.isStringValue),k=t.bindNot(d),I=t.bindNot(h),J=t.bindNot(p),K={condition:function(n,t){try{return t(n)}catch(n){return!1}},doesNotEndWith:O,doesNotStartWith:t.bindNot(m),endsWith:i,equals:r,greaterThan:t.greaterThan,greaterThanOrEquals:u,gt:t.greaterThan,gte:u,inside:s,isArray:t.isArray,isBetween:l,isBlank:c,isBoolean:t.isBoolean,isEmpty:t.isEmpty,isEven:function(n){return!!t.isNumeric(n)&&0==n%2},isFalsy:k,isKeyOf:f,isNaN:N,isNegative:function(n){return o(n,0)},isNotArray:t.isNotArray,isNotBetween:w,isNotBlank:A,isNotBoolean:P,isNotEmpty:t.isNotEmpty,isNotKeyOf:F,isNotNaN:W,isNotNull:t.isNotNull,isNotNullish:t.isNotNullish,isNotNumber:U,isNotNumeric:t.isNotNumeric,isNotString:j,isNotUndefined:t.isNotUndefined,isNotValueOf:I,isNull:t.isNull,isNullish:t.isNullish,isNumber:g,isNumeric:t.isNumeric,isOdd:function(n){return!!t.isNumeric(n)&&0!=n%2},isPositive:t.isPositive,isString:t.isStringValue,isTruthy:d,isUndefined:t.isUndefined,isValueOf:h,lengthEquals:t.lengthEquals,lengthNotEquals:t.lengthNotEquals,lessThan:o,lessThanOrEquals:a,longerThan:t.longerThan,longerThanOrEquals:function(n,t){return u(n.length,t)},lt:o,lte:a,matches:p,notEquals:V,notInside:B,notMatches:J,numberEquals:t.numberEquals,numberNotEquals:t.numberNotEquals,shorterThan:function(n,t){return o(n.length,t)},shorterThanOrEquals:function(n,t){return a(n.length,t)},startsWith:m},M=e.createContext((function(n,e){var i={value:n.value,meta:n.meta||{}};return e?n.set?t.assign(i,{parent:function(){return e?{value:e.value,meta:e.meta,parent:e.parent}:e}}):e:t.assign(i,{parent:y})}));e=function(){function n(){if(!E())return v((function(n){e[n]=T(n)})),t.assign(S,e)}var e={context:function(){return M.useX()},extend:function(e){t.assign(K,e),n()}};return n(),new Proxy(t.assign(S,e),{get:function(n,t){return t in n?n[t]:K[t]?T(t):void 0}})}(),n.ctx=M,n.enforce=e,Object.defineProperty(n,"__esModule",{value:!0})}));
|
|
@@ -1,61 +1,8 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('n4s')) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports', 'n4s'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.schema = {}, global.n4s));
|
|
5
|
-
}(this, (function (exports, n4s) { 'use strict';
|
|
6
|
-
|
|
7
|
-
function isNull(value) {
|
|
8
|
-
return value === null;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function isUndefined(value) {
|
|
12
|
-
return value === undefined;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function isNullish(value) {
|
|
16
|
-
return isNull(value) || isUndefined(value);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function isFunction(value) {
|
|
20
|
-
return typeof value === 'function';
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function optionalFunctionValue(value) {
|
|
24
|
-
var args = [];
|
|
25
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
26
|
-
args[_i - 1] = arguments[_i];
|
|
27
|
-
}
|
|
28
|
-
return isFunction(value) ? value.apply(void 0, args) : value;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function defaultTo(value, defaultValue) {
|
|
32
|
-
var _a;
|
|
33
|
-
return (_a = optionalFunctionValue(value)) !== null && _a !== void 0 ? _a : optionalFunctionValue(defaultValue);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* A safe hasOwnProperty access
|
|
38
|
-
*/
|
|
39
|
-
function hasOwnProperty(obj, key) {
|
|
40
|
-
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function mapFirst(array, callback) {
|
|
44
|
-
var broke = false;
|
|
45
|
-
var breakoutValue = null;
|
|
46
|
-
for (var i = 0; i < array.length; i++) {
|
|
47
|
-
callback(array[i], breakout, i);
|
|
48
|
-
if (broke) {
|
|
49
|
-
return breakoutValue;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
function breakout(conditional, value) {
|
|
53
|
-
if (conditional) {
|
|
54
|
-
broke = true;
|
|
55
|
-
breakoutValue = value;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('n4s'), require('vest-utils')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', 'n4s', 'vest-utils'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.schema = {}, global.n4s, global['vest-utils']));
|
|
5
|
+
}(this, (function (exports, n4s, vestUtils) { 'use strict';
|
|
59
6
|
|
|
60
7
|
function ruleReturn(pass, message) {
|
|
61
8
|
var output = { pass: pass };
|
|
@@ -71,7 +18,7 @@
|
|
|
71
18
|
return ruleReturn(true);
|
|
72
19
|
}
|
|
73
20
|
function defaultToPassing(callback) {
|
|
74
|
-
return defaultTo(callback, passing());
|
|
21
|
+
return vestUtils.defaultTo(callback, passing());
|
|
75
22
|
}
|
|
76
23
|
|
|
77
24
|
function runLazyRule(lazyRule, currentValue) {
|
|
@@ -84,7 +31,7 @@
|
|
|
84
31
|
}
|
|
85
32
|
|
|
86
33
|
function isArrayOf(inputArray, currentRule) {
|
|
87
|
-
return defaultToPassing(mapFirst(inputArray, function (currentValue, breakout, index) {
|
|
34
|
+
return defaultToPassing(vestUtils.mapFirst(inputArray, function (currentValue, breakout, index) {
|
|
88
35
|
var res = n4s.ctx.run({ value: currentValue, set: true, meta: { index: index } }, function () { return runLazyRule(currentRule, currentValue); });
|
|
89
36
|
breakout(!res.pass, res);
|
|
90
37
|
}));
|
|
@@ -110,7 +57,7 @@
|
|
|
110
57
|
}
|
|
111
58
|
|
|
112
59
|
function optional(value, ruleChain) {
|
|
113
|
-
if (isNullish(value)) {
|
|
60
|
+
if (vestUtils.isNullish(value)) {
|
|
114
61
|
return passing();
|
|
115
62
|
}
|
|
116
63
|
return runLazyRule(ruleChain, value);
|
|
@@ -122,7 +69,7 @@
|
|
|
122
69
|
return baseRes;
|
|
123
70
|
}
|
|
124
71
|
for (var key in inputObject) {
|
|
125
|
-
if (!hasOwnProperty(shapeObject, key)) {
|
|
72
|
+
if (!vestUtils.hasOwnProperty(shapeObject, key)) {
|
|
126
73
|
return failing();
|
|
127
74
|
}
|
|
128
75
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";!function(n
|
|
1
|
+
"use strict";!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("n4s"),require("vest-utils")):"function"==typeof define&&define.amd?define(["exports","n4s","vest-utils"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).schema={},e.n4s,e["vest-utils"])}(this,(function(e,n,t){function r(e,n){return e={pass:e},n&&(e.message=n),e}function u(e,n){try{return e.run(n)}catch(e){return r(!1)}}function i(e,t){var i,o=function(r){var i=e[r],o=t[r];if(!(r=n.ctx.run({value:i,set:!0,meta:{key:r}},(function(){return u(o,i)}))).pass)return{value:r}};for(i in t){var s=o(i);if("object"==typeof s)return s.value}return r(!0)}n.enforce.extend({isArrayOf:function(e,i){return t.defaultTo(t.mapFirst(e,(function(e,t,r){t(!(r=n.ctx.run({value:e,set:!0,meta:{index:r}},(function(){return u(i,e)}))).pass,r)})),r(!0))},loose:i,optional:function(e,n){return t.isNullish(e)?r(!0):u(n,e)},shape:function(e,n){var u=i(e,n);if(!u.pass)return u;for(var o in e)if(!t.hasOwnProperty(n,o))return r(!1);return r(!0)}}),e.partial=function(e){var t,r={};for(t in e)r[t]=n.enforce.optional(e[t]);return r},Object.defineProperty(e,"__esModule",{value:!0})}));
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "4.
|
|
2
|
+
"version": "4.2.0",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"main": "./dist/cjs/n4s.js",
|
|
5
5
|
"types": "./types/n4s.d.ts",
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
"release": "vx release"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"context": "^2.0.
|
|
14
|
-
"vest-utils": "^0.0.
|
|
13
|
+
"context": "^2.0.9",
|
|
14
|
+
"vest-utils": "^0.0.2"
|
|
15
15
|
},
|
|
16
16
|
"module": "./dist/es/n4s.production.js",
|
|
17
17
|
"exports": {
|
package/types/compose.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ type DropFirst<T extends unknown[]> = T extends [
|
|
|
3
3
|
...infer U
|
|
4
4
|
] ? U : never;
|
|
5
5
|
type Stringable = string | ((...args: any[]) => string);
|
|
6
|
+
type CB = (...args: any[]) => any;
|
|
6
7
|
type RuleReturn = boolean | {
|
|
7
8
|
pass: boolean;
|
|
8
9
|
message?: Stringable;
|
|
@@ -111,7 +112,7 @@ type LazyRules = n4s.IRules<LazyRuleMethods>;
|
|
|
111
112
|
type Lazy = LazyRules & LazyRuleMethods &
|
|
112
113
|
// This is a "catch all" hack to make TS happy while not
|
|
113
114
|
// losing type hints
|
|
114
|
-
Record<string,
|
|
115
|
+
Record<string, CB>;
|
|
115
116
|
type LazyRuleMethods = LazyRuleRunners & {
|
|
116
117
|
message: (message: LazyMessage) => Lazy;
|
|
117
118
|
};
|
package/types/compounds.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ type DropFirst<T extends unknown[]> = T extends [
|
|
|
3
3
|
...infer U
|
|
4
4
|
] ? U : never;
|
|
5
5
|
type Stringable = string | ((...args: any[]) => string);
|
|
6
|
+
type CB = (...args: any[]) => any;
|
|
7
|
+
type EnforceCustomMatcher<F extends CB, R> = (...args: DropFirst<Parameters<F>>) => R;
|
|
6
8
|
type RuleReturn = boolean | {
|
|
7
9
|
pass: boolean;
|
|
8
10
|
message?: Stringable;
|
|
@@ -111,7 +113,7 @@ type LazyRules = n4s.IRules<LazyRuleMethods>;
|
|
|
111
113
|
type Lazy = LazyRules & LazyRuleMethods &
|
|
112
114
|
// This is a "catch all" hack to make TS happy while not
|
|
113
115
|
// losing type hints
|
|
114
|
-
Record<string,
|
|
116
|
+
Record<string, CB>;
|
|
115
117
|
type LazyRuleMethods = LazyRuleRunners & {
|
|
116
118
|
message: (message: LazyMessage) => Lazy;
|
|
117
119
|
};
|
|
@@ -120,17 +122,14 @@ type LazyRuleRunners = {
|
|
|
120
122
|
run: (value: unknown) => RuleDetailedResult;
|
|
121
123
|
};
|
|
122
124
|
type LazyMessage = string | ((value: unknown, originalMessage?: Stringable) => string);
|
|
123
|
-
|
|
124
|
-
declare function anyOf(value: unknown, ...rules: Lazy[]): RuleDetailedResult;
|
|
125
|
-
declare function noneOf(value: unknown, ...rules: Lazy[]): RuleDetailedResult;
|
|
126
|
-
declare function oneOf(value: unknown, ...rules: Lazy[]): RuleDetailedResult;
|
|
125
|
+
type EnforceCompoundRule = (value: unknown, ...rules: Lazy[]) => RuleDetailedResult;
|
|
127
126
|
declare global {
|
|
128
127
|
namespace n4s {
|
|
129
128
|
interface EnforceCustomMatchers<R> {
|
|
130
|
-
allOf:
|
|
131
|
-
anyOf:
|
|
132
|
-
noneOf:
|
|
133
|
-
oneOf:
|
|
129
|
+
allOf: EnforceCustomMatcher<EnforceCompoundRule, R>;
|
|
130
|
+
anyOf: EnforceCustomMatcher<EnforceCompoundRule, R>;
|
|
131
|
+
noneOf: EnforceCustomMatcher<EnforceCompoundRule, R>;
|
|
132
|
+
oneOf: EnforceCustomMatcher<EnforceCompoundRule, R>;
|
|
134
133
|
}
|
|
135
134
|
}
|
|
136
135
|
}
|
package/types/n4s.d.ts
CHANGED
|
@@ -20,6 +20,7 @@ type DropFirst<T extends unknown[]> = T extends [
|
|
|
20
20
|
...infer U
|
|
21
21
|
] ? U : never;
|
|
22
22
|
type Stringable = string | ((...args: any[]) => string);
|
|
23
|
+
type CB = (...args: any[]) => any;
|
|
23
24
|
type RuleReturn = boolean | {
|
|
24
25
|
pass: boolean;
|
|
25
26
|
message?: Stringable;
|
|
@@ -128,13 +129,12 @@ declare global {
|
|
|
128
129
|
}
|
|
129
130
|
}
|
|
130
131
|
type IRules = n4s.IRules<Record<string, any>>;
|
|
131
|
-
|
|
132
|
-
type EnforceEager = typeof enforceEager;
|
|
132
|
+
type EnforceEager = (value: RuleValue) => IRules;
|
|
133
133
|
type LazyRules = n4s.IRules<LazyRuleMethods>;
|
|
134
134
|
type Lazy = LazyRules & LazyRuleMethods &
|
|
135
135
|
// This is a "catch all" hack to make TS happy while not
|
|
136
136
|
// losing type hints
|
|
137
|
-
Record<string,
|
|
137
|
+
Record<string, CB>;
|
|
138
138
|
type LazyRuleMethods = LazyRuleRunners & {
|
|
139
139
|
message: (message: LazyMessage) => Lazy;
|
|
140
140
|
};
|
package/types/schema.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ type DropFirst<T extends unknown[]> = T extends [
|
|
|
3
3
|
...infer U
|
|
4
4
|
] ? U : never;
|
|
5
5
|
type Stringable = string | ((...args: any[]) => string);
|
|
6
|
+
type CB = (...args: any[]) => any;
|
|
7
|
+
type EnforceCustomMatcher<F extends CB, R> = (...args: DropFirst<Parameters<F>>) => R;
|
|
6
8
|
type RuleReturn = boolean | {
|
|
7
9
|
pass: boolean;
|
|
8
10
|
message?: Stringable;
|
|
@@ -111,7 +113,7 @@ type LazyRules = n4s.IRules<LazyRuleMethods>;
|
|
|
111
113
|
type Lazy = LazyRules & LazyRuleMethods &
|
|
112
114
|
// This is a "catch all" hack to make TS happy while not
|
|
113
115
|
// losing type hints
|
|
114
|
-
Record<string,
|
|
116
|
+
Record<string, CB>;
|
|
115
117
|
type LazyRuleMethods = LazyRuleRunners & {
|
|
116
118
|
message: (message: LazyMessage) => Lazy;
|
|
117
119
|
};
|
|
@@ -132,10 +134,10 @@ declare function partial<T extends Record<any, any>>(shapeObject: T): T;
|
|
|
132
134
|
declare global {
|
|
133
135
|
namespace n4s {
|
|
134
136
|
interface EnforceCustomMatchers<R> {
|
|
135
|
-
isArrayOf:
|
|
136
|
-
loose:
|
|
137
|
-
shape:
|
|
138
|
-
optional:
|
|
137
|
+
isArrayOf: EnforceCustomMatcher<typeof isArrayOf, R>;
|
|
138
|
+
loose: EnforceCustomMatcher<typeof loose, R>;
|
|
139
|
+
shape: EnforceCustomMatcher<typeof shape, R>;
|
|
140
|
+
optional: EnforceCustomMatcher<typeof optional, R>;
|
|
139
141
|
}
|
|
140
142
|
}
|
|
141
143
|
}
|