vest 4.0.0-dev-31f012 → 4.0.0-next-0370fc
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/CHANGELOG.md +63 -52
- package/README.md +12 -0
- package/dist/cjs/compose.js +7 -0
- package/dist/cjs/compounds.js +7 -0
- package/dist/cjs/enforce/compose.development.js +139 -0
- package/dist/cjs/enforce/compose.production.js +1 -0
- package/dist/cjs/enforce/compounds.development.js +147 -0
- package/dist/cjs/enforce/compounds.production.js +1 -0
- package/dist/cjs/enforce/package.json +1 -0
- package/dist/cjs/enforce/schema.development.js +111 -0
- package/dist/cjs/enforce/schema.production.js +1 -0
- package/dist/cjs/schema.js +7 -0
- package/dist/cjs/vest.development.js +233 -851
- package/dist/cjs/vest.production.js +1 -1
- package/dist/es/enforce/compose.development.js +137 -0
- package/dist/es/enforce/compose.production.js +1 -0
- package/dist/es/enforce/compounds.development.js +145 -0
- package/dist/es/enforce/compounds.production.js +1 -0
- package/dist/es/enforce/package.json +1 -0
- package/dist/es/enforce/schema.development.js +109 -0
- package/dist/es/enforce/schema.production.js +1 -0
- package/dist/es/vest.development.js +228 -851
- package/dist/es/vest.production.js +1 -1
- package/dist/umd/enforce/compose.development.js +143 -0
- package/dist/umd/enforce/compose.production.js +1 -0
- package/dist/umd/enforce/compounds.development.js +151 -0
- package/dist/umd/enforce/compounds.production.js +1 -0
- package/dist/umd/enforce/schema.development.js +115 -0
- package/dist/umd/enforce/schema.production.js +1 -0
- package/dist/umd/vest.development.js +1605 -2226
- package/dist/umd/vest.production.js +1 -1
- package/enforce/compose/package.json +7 -0
- package/enforce/compounds/package.json +7 -0
- package/enforce/schema/package.json +7 -0
- package/package.json +95 -1
- package/types/enforce/compose.d.ts +131 -0
- package/types/enforce/compounds.d.ts +145 -0
- package/types/enforce/schema.d.ts +143 -0
- package/types/vest.d.ts +24 -191
package/CHANGELOG.md
CHANGED
|
@@ -4,75 +4,86 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
|
|
5
5
|
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
|
6
6
|
|
|
7
|
-
## 4.0.0 - 2021-
|
|
7
|
+
## 4.0.0 - 2021-11-21
|
|
8
8
|
### Changed or removed
|
|
9
|
-
-
|
|
10
|
-
-
|
|
9
|
+
- 7c43eab major(context): used named export in context (ealush)
|
|
10
|
+
- e8652bc breaking(vest, enforce): prepare next major (ealush)
|
|
11
|
+
- dab8e00 breaking(vest, enforce): prepare next major (ealush)
|
|
11
12
|
|
|
12
13
|
### Added
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
18
|
-
-
|
|
19
|
-
-
|
|
14
|
+
- d3e7613 feat(vest): support custom optional logic (ealush)
|
|
15
|
+
- be1cbf6 feat(vest): Add a parser export (ealush)
|
|
16
|
+
- 2553748 feat(vest): isValid with field name (ealush)
|
|
17
|
+
- ec5ceb9 feat(vest): handle execution order state refill with memoized tests (ealush)
|
|
18
|
+
- 4032bd8 feat(vest): Make the state rely on execution order. Make skipWhen set skipped context. (undefined)
|
|
19
|
+
- 220127b added(n4s): partial rule modifier (undefined)
|
|
20
|
+
- b5ce72d feat(n4s): context propagation within enforce (undefined)
|
|
20
21
|
|
|
21
22
|
### Fixed and improved
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
-
|
|
25
|
-
-
|
|
26
|
-
-
|
|
27
|
-
-
|
|
28
|
-
-
|
|
29
|
-
-
|
|
30
|
-
-
|
|
31
|
-
-
|
|
32
|
-
-
|
|
33
|
-
-
|
|
34
|
-
-
|
|
35
|
-
-
|
|
36
|
-
-
|
|
37
|
-
-
|
|
38
|
-
-
|
|
39
|
-
-
|
|
40
|
-
-
|
|
41
|
-
-
|
|
42
|
-
-
|
|
43
|
-
-
|
|
23
|
+
- 0370fc1 n4s: extract non-required modules (ealush)
|
|
24
|
+
- 03ba92c vx: support namespaced exports (ealush)
|
|
25
|
+
- 38a54ae vx: support installing of local packages (ealush)
|
|
26
|
+
- 7baedf2 n4s: use named export in entry (ealush)
|
|
27
|
+
- 366001b patch(vest): better handling of skipped async tests (ealush)
|
|
28
|
+
- 2380679 types(vest): allow unprovided suite name in tests (ealush)
|
|
29
|
+
- f721b2d patch(vest): replace warns boolean flag with enum (ealush)
|
|
30
|
+
- 0acb24d patch(vest): Pass draft result to skipWhen conditional (ealush)
|
|
31
|
+
- e84cd9d patch(vest): add back optional suite name argument (ealush)
|
|
32
|
+
- 8b30b66 patch(vest): make compose an external export (ealush)
|
|
33
|
+
- ddfa9fd patch(vest): move creation functions to event bus (ealush)
|
|
34
|
+
- fe83e88 patch: Some cleanups (ealush)
|
|
35
|
+
- 9275707 fix(vest): prevent pending tests from being marked as skipped (ealush)
|
|
36
|
+
- 94e00a8 patch(vest): Add isolate module for containing re-orders (ealush)
|
|
37
|
+
- 5bcc14a patch(vest): refresh tests after cancel (ealush)
|
|
38
|
+
- fd049e8 patch: add cursor util (ealush)
|
|
39
|
+
- 9380c09 patch(vest): move cursor into context (ealush)
|
|
40
|
+
- 49b72f4 patch(vest): automatically purge not-found test results (ealush)
|
|
41
|
+
- 2dfedaf vx: correctly pass cli options to build script (ealush)
|
|
42
|
+
- aae250a patch(vest): Throw error when tests are called in the wrong order (ealush)
|
|
43
|
+
- a87824b vx: add types to exports (ealush)
|
|
44
|
+
- cc9b4b0 organize vest state hooks (ealush)
|
|
45
|
+
- df84261 patch(vest): infer omitted optional fields (ealush)
|
|
46
|
+
- 2534cf6 vx: add dev bundle to exports (ealush)
|
|
47
|
+
- 4d450aa fix(vest): prevent changing test status once it was finalized (ealush)
|
|
48
|
+
- 8730e25 patch(vest): add cache invalidation for canceled tests (ealush)
|
|
49
|
+
- 4cb2c6c types(vest): add safeguard around shouldUseErrorAsMessage (ealush)
|
|
50
|
+
- c214a12 patch(vest): some cleanups (ealush)
|
|
51
|
+
- 858458d patch(vest): remove duplicate code handling perv test registration (ealush)
|
|
52
|
+
- ec69173 vx: use context directly from published package (ealush)
|
|
53
|
+
- 962bc06 patch: run done callbacks via event (ealush)
|
|
54
|
+
- d96f428 patch(vest): removed state subscription (ealush)
|
|
44
55
|
- package.json
|
|
45
56
|
- packages/anyone/package.json
|
|
46
57
|
- packages/anyone/package.json
|
|
47
58
|
- packages/vast/types/vast.d.ts
|
|
48
|
-
-
|
|
49
|
-
-
|
|
50
|
-
-
|
|
51
|
-
-
|
|
52
|
-
-
|
|
53
|
-
-
|
|
54
|
-
-
|
|
59
|
+
- e4e1746 patch(vest): remove pending and lagging state (ealush)
|
|
60
|
+
- 9d8fd21 patch(vest): convert boolean flags to a single status key (ealush)
|
|
61
|
+
- dbb836e patch: simplify fieldname comparison (ealush)
|
|
62
|
+
- 5a78179 patch(vest): add useAllIncomplete hook (ealush)
|
|
63
|
+
- b5ae658 patch(vest): invalid accounts for warning tests as well (ealush)
|
|
64
|
+
- 4563b8d patch(vest): isValid counts all required tests per field (ealush)
|
|
65
|
+
- 3fe2f21 chore: cleanups (ealush)
|
|
55
66
|
- .eslintrc.js
|
|
56
|
-
-
|
|
67
|
+
- 149aab3 add(n4s): enforce.condition (ealush)
|
|
57
68
|
- package.json
|
|
58
69
|
- packages/anyone/package.json
|
|
59
|
-
-
|
|
60
|
-
-
|
|
70
|
+
- 40ef071 chore: cleanup residue (undefined)
|
|
71
|
+
- eeac20e chore: remove duplicate types (undefined)
|
|
61
72
|
- packages/anyone/package.json
|
|
62
|
-
-
|
|
63
|
-
-
|
|
73
|
+
- 4d88c04 patch: add nodejs exports (undefined)
|
|
74
|
+
- 6e57aa1 patch: remove unused exports (undefined)
|
|
64
75
|
- packages/anyone/package.json
|
|
65
|
-
-
|
|
76
|
+
- 26af06b chore: reduce complexity, remove all lint errors (undefined)
|
|
66
77
|
- packages/anyone/.npmignore
|
|
67
78
|
- .github/PULL_REQUEST_TEMPLATE.md
|
|
68
|
-
-
|
|
69
|
-
-
|
|
79
|
+
- 5be986d chore: cleanup unused code (ealush)
|
|
80
|
+
- ba68539 lint: handling lint of all packages (ealush)
|
|
70
81
|
- .gitignore
|
|
71
|
-
-
|
|
72
|
-
-
|
|
73
|
-
-
|
|
74
|
-
-
|
|
75
|
-
-
|
|
82
|
+
- cca5130 patch(n4s): add ruleReturn default values (ealush)
|
|
83
|
+
- 75306ff fix(n4s): make enforce compound runners fall back to correct response (ealush)
|
|
84
|
+
- 4751584 fix(n4s): make enforce chaining work (ealush)
|
|
85
|
+
- 73b28a1 chore: some lint fixes (ealush)
|
|
86
|
+
- 4135ce3 add cli options support (ealush)
|
|
76
87
|
|
|
77
88
|
## 3.2.7 - 2021-07-17
|
|
78
89
|
|
package/README.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|

|
|
2
2
|
|
|
3
|
+
# Version 4 of Vest is coming soon!
|
|
4
|
+
|
|
5
|
+
If you want to try it out, you can already install it with the `next` tag.
|
|
6
|
+
|
|
7
|
+
```npm install vest@next```
|
|
8
|
+
|
|
9
|
+
This version brings many enhancements to Vest. This version is well tested, and can be used safely. If you do, however encounter any issues or have any thoughts on the next version, please report them on the issues page.
|
|
10
|
+
|
|
11
|
+
For the version 4 docs, you can reach out to: https://vest.vercel.app/ or specifically to the [migration guide](https://vest.vercel.app/docs/migration_guide).
|
|
12
|
+
|
|
13
|
+
A note on Typescript support: Version four uses Typescript 4 specific features, so for now only version 4 is supported.
|
|
14
|
+
|
|
3
15
|
# Vest 🦺 Declarative Validation Testing
|
|
4
16
|
|
|
5
17
|

|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var n4s = require('n4s');
|
|
4
|
+
|
|
5
|
+
function mapFirst(array, callback) {
|
|
6
|
+
var broke = false;
|
|
7
|
+
var breakoutValue = null;
|
|
8
|
+
for (var i = 0; i < array.length; i++) {
|
|
9
|
+
callback(array[i], breakout, i);
|
|
10
|
+
if (broke) {
|
|
11
|
+
return breakoutValue;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function breakout(value) {
|
|
15
|
+
broke = true;
|
|
16
|
+
breakoutValue = value;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function isFunction(value) {
|
|
21
|
+
return typeof value === 'function';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function optionalFunctionValue(value) {
|
|
25
|
+
var args = [];
|
|
26
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
27
|
+
args[_i - 1] = arguments[_i];
|
|
28
|
+
}
|
|
29
|
+
return isFunction(value) ? value.apply(void 0, args) : value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function defaultTo(callback, defaultValue) {
|
|
33
|
+
var _a;
|
|
34
|
+
return (_a = optionalFunctionValue(callback)) !== null && _a !== void 0 ? _a : defaultValue;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Throws a timed out error.
|
|
39
|
+
*/
|
|
40
|
+
function throwError(devMessage, productionMessage) {
|
|
41
|
+
throw new Error(devMessage );
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A safe hasOwnProperty access
|
|
46
|
+
*/
|
|
47
|
+
function hasOwnProperty(obj, key) {
|
|
48
|
+
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function isNumber(value) {
|
|
52
|
+
return Boolean(typeof value === 'number');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function lengthEquals(value, arg1) {
|
|
56
|
+
return value.length === Number(arg1);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function isEmpty(value) {
|
|
60
|
+
if (!value) {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
else if (isNumber(value)) {
|
|
64
|
+
return value === 0;
|
|
65
|
+
}
|
|
66
|
+
else if (hasOwnProperty(value, 'length')) {
|
|
67
|
+
return lengthEquals(value, 0);
|
|
68
|
+
}
|
|
69
|
+
else if (typeof value === 'object') {
|
|
70
|
+
return lengthEquals(Object.keys(value), 0);
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function ruleReturn(pass, message) {
|
|
76
|
+
var output = { pass: pass };
|
|
77
|
+
if (message) {
|
|
78
|
+
output.message = message;
|
|
79
|
+
}
|
|
80
|
+
return output;
|
|
81
|
+
}
|
|
82
|
+
function failing() {
|
|
83
|
+
return ruleReturn(false);
|
|
84
|
+
}
|
|
85
|
+
function passing() {
|
|
86
|
+
return ruleReturn(true);
|
|
87
|
+
}
|
|
88
|
+
function defaultToPassing(callback) {
|
|
89
|
+
return defaultTo(callback, passing());
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function runLazyRule(lazyRule, currentValue) {
|
|
93
|
+
try {
|
|
94
|
+
return lazyRule.run(currentValue);
|
|
95
|
+
}
|
|
96
|
+
catch (_a) {
|
|
97
|
+
return failing();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/* eslint-disable max-lines-per-function */
|
|
102
|
+
function compose() {
|
|
103
|
+
var composites = [];
|
|
104
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
105
|
+
composites[_i] = arguments[_i];
|
|
106
|
+
}
|
|
107
|
+
return Object.assign(function (value) {
|
|
108
|
+
var res = run(value);
|
|
109
|
+
if (!res.pass) {
|
|
110
|
+
if (isEmpty(res.message)) {
|
|
111
|
+
throwError();
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
// Explicitly throw a string so that vest.test can pick it up as the validation error message
|
|
115
|
+
throw res.message;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}, {
|
|
119
|
+
run: run,
|
|
120
|
+
test: function (value) { return run(value).pass; }
|
|
121
|
+
});
|
|
122
|
+
function run(value) {
|
|
123
|
+
return n4s.ctx.run({ value: value }, function () {
|
|
124
|
+
return defaultToPassing(mapFirst(composites, function (composite, breakout) {
|
|
125
|
+
/* HACK: Just a small white lie. ~~HELP WANTED~~.
|
|
126
|
+
The ideal is that instead of `TLazyRuleRunners` We would simply use `TLazy` to begin with.
|
|
127
|
+
The problem is that lazy rules can't really be passed to this function due to some generic hell
|
|
128
|
+
so we're limiting it to a small set of functions.
|
|
129
|
+
*/
|
|
130
|
+
var res = runLazyRule(composite, value);
|
|
131
|
+
if (!res.pass) {
|
|
132
|
+
breakout(res);
|
|
133
|
+
}
|
|
134
|
+
}));
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
module.exports = compose;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var n=require("n4s");function r(n,r){function t(n){u=!0,e=n}for(var u=!1,e=null,o=0;o<n.length;o++)if(r(n[o],t,o),u)return e}function t(n){return"function"==typeof n}function u(n,r){var u;return null!==(u=function(n){for(var r=[],u=1;u<arguments.length;u++)r[u-1]=arguments[u];return t(n)?n.apply(void 0,r):n}(n))&&void 0!==u?u:r}function e(n,r){throw Error(u(r,n))}function o(n,r){return n.length===Number(r)}function f(n){if(n){if(function(n){return"number"==typeof n}(n))return 0===n;if(function(n,r){return Object.prototype.hasOwnProperty.call(n,r)}(n,"length"))return o(n,0);if("object"==typeof n)return o(Object.keys(n),0)}return!0}function i(n,r){return n={pass:n},r&&(n.message=r),n}function c(n){return u(n,i(!0))}function s(n,r){try{return n.run(r)}catch(n){return i(!1)}}module.exports=function(){function t(t){return n.ctx.run({value:t},(function(){return c(r(u,(function(n,r){(n=s(n,t)).pass||r(n)})))}))}for(var u=[],o=0;o<arguments.length;o++)u[o]=arguments[o];return Object.assign((function(n){if(!(n=t(n)).pass){if(!f(n.message))throw n.message;e()}}),{run:t,test:function(n){return t(n).pass}})};
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var n4s = require('n4s');
|
|
4
|
+
|
|
5
|
+
function mapFirst(array, callback) {
|
|
6
|
+
var broke = false;
|
|
7
|
+
var breakoutValue = null;
|
|
8
|
+
for (var i = 0; i < array.length; i++) {
|
|
9
|
+
callback(array[i], breakout, i);
|
|
10
|
+
if (broke) {
|
|
11
|
+
return breakoutValue;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function breakout(value) {
|
|
15
|
+
broke = true;
|
|
16
|
+
breakoutValue = value;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function isFunction(value) {
|
|
21
|
+
return typeof value === 'function';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function optionalFunctionValue(value) {
|
|
25
|
+
var args = [];
|
|
26
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
27
|
+
args[_i - 1] = arguments[_i];
|
|
28
|
+
}
|
|
29
|
+
return isFunction(value) ? value.apply(void 0, args) : value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function defaultTo(callback, defaultValue) {
|
|
33
|
+
var _a;
|
|
34
|
+
return (_a = optionalFunctionValue(callback)) !== null && _a !== void 0 ? _a : defaultValue;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function ruleReturn(pass, message) {
|
|
38
|
+
var output = { pass: pass };
|
|
39
|
+
if (message) {
|
|
40
|
+
output.message = message;
|
|
41
|
+
}
|
|
42
|
+
return output;
|
|
43
|
+
}
|
|
44
|
+
function failing() {
|
|
45
|
+
return ruleReturn(false);
|
|
46
|
+
}
|
|
47
|
+
function passing() {
|
|
48
|
+
return ruleReturn(true);
|
|
49
|
+
}
|
|
50
|
+
function defaultToFailing(callback) {
|
|
51
|
+
return defaultTo(callback, failing());
|
|
52
|
+
}
|
|
53
|
+
function defaultToPassing(callback) {
|
|
54
|
+
return defaultTo(callback, passing());
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function runLazyRule(lazyRule, currentValue) {
|
|
58
|
+
try {
|
|
59
|
+
return lazyRule.run(currentValue);
|
|
60
|
+
}
|
|
61
|
+
catch (_a) {
|
|
62
|
+
return failing();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function allOf(value) {
|
|
67
|
+
var rules = [];
|
|
68
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
69
|
+
rules[_i - 1] = arguments[_i];
|
|
70
|
+
}
|
|
71
|
+
return defaultToPassing(mapFirst(rules, function (rule, breakout) {
|
|
72
|
+
var res = runLazyRule(rule, value);
|
|
73
|
+
if (!res.pass) {
|
|
74
|
+
breakout(res);
|
|
75
|
+
}
|
|
76
|
+
}));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function anyOf(value) {
|
|
80
|
+
var rules = [];
|
|
81
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
82
|
+
rules[_i - 1] = arguments[_i];
|
|
83
|
+
}
|
|
84
|
+
return defaultToFailing(mapFirst(rules, function (rule, breakout) {
|
|
85
|
+
var res = runLazyRule(rule, value);
|
|
86
|
+
if (res.pass) {
|
|
87
|
+
breakout(res);
|
|
88
|
+
}
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function noneOf(value) {
|
|
93
|
+
var rules = [];
|
|
94
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
95
|
+
rules[_i - 1] = arguments[_i];
|
|
96
|
+
}
|
|
97
|
+
return defaultToPassing(mapFirst(rules, function (rule, breakout) {
|
|
98
|
+
var res = runLazyRule(rule, value);
|
|
99
|
+
if (res.pass) {
|
|
100
|
+
breakout(failing());
|
|
101
|
+
}
|
|
102
|
+
}));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function lengthEquals(value, arg1) {
|
|
106
|
+
return value.length === Number(arg1);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function longerThan(value, arg1) {
|
|
110
|
+
return value.length > Number(arg1);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
var REQUIRED_COUNT = 1;
|
|
114
|
+
function oneOf(value) {
|
|
115
|
+
var rules = [];
|
|
116
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
117
|
+
rules[_i - 1] = arguments[_i];
|
|
118
|
+
}
|
|
119
|
+
var passing = [];
|
|
120
|
+
rules.some(function (rule) {
|
|
121
|
+
if (longerThan(passing, REQUIRED_COUNT)) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
var res = runLazyRule(rule, value);
|
|
125
|
+
if (res.pass) {
|
|
126
|
+
passing.push(res);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
return ruleReturn(lengthEquals(passing, REQUIRED_COUNT));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function isNull(value) {
|
|
133
|
+
return value === null;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function isUndefined(value) {
|
|
137
|
+
return value === undefined;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function optional(value, ruleChain) {
|
|
141
|
+
if (isUndefined(value) || isNull(value)) {
|
|
142
|
+
return passing();
|
|
143
|
+
}
|
|
144
|
+
return runLazyRule(ruleChain, value);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
n4s.enforce.extend({ allOf: allOf, anyOf: anyOf, noneOf: noneOf, oneOf: oneOf, optional: optional });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var n=require("n4s");function r(n,r){function t(n){u=!0,e=n}for(var u=!1,e=null,o=0;o<n.length;o++)if(r(n[o],t,o),u)return e}function t(n){return"function"==typeof n}function u(n,r){var u;return null!==(u=function(n){for(var r=[],u=1;u<arguments.length;u++)r[u-1]=arguments[u];return t(n)?n.apply(void 0,r):n}(n))&&void 0!==u?u:r}function e(n,r){return n={pass:n},r&&(n.message=r),n}function o(){return e(!1)}function f(){return e(!0)}function i(n){return u(n,o())}function c(n){return u(n,f())}function a(n,r){try{return n.run(r)}catch(n){return o()}}function l(n,r){return n.length===Number(r)}function s(n,r){return n.length>Number(r)}n.enforce.extend({allOf:function(n){for(var t=[],u=1;u<arguments.length;u++)t[u-1]=arguments[u];return c(r(t,(function(r,t){(r=a(r,n)).pass||t(r)})))},anyOf:function(n){for(var t=[],u=1;u<arguments.length;u++)t[u-1]=arguments[u];return i(r(t,(function(r,t){(r=a(r,n)).pass&&t(r)})))},noneOf:function(n){for(var t=[],u=1;u<arguments.length;u++)t[u-1]=arguments[u];return c(r(t,(function(r,t){a(r,n).pass&&t(o())})))},oneOf:function(n){for(var r=[],t=1;t<arguments.length;t++)r[t-1]=arguments[t];var u=[];return r.some((function(r){if(s(u,1))return!1;(r=a(r,n)).pass&&u.push(r)})),e(l(u,1))},optional:function(n,r){return function(n){return void 0===n}(n)||function(n){return null===n}(n)?f():a(r,n)}});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var n4s = require('n4s');
|
|
4
|
+
|
|
5
|
+
function mapFirst(array, callback) {
|
|
6
|
+
var broke = false;
|
|
7
|
+
var breakoutValue = null;
|
|
8
|
+
for (var i = 0; i < array.length; i++) {
|
|
9
|
+
callback(array[i], breakout, i);
|
|
10
|
+
if (broke) {
|
|
11
|
+
return breakoutValue;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function breakout(value) {
|
|
15
|
+
broke = true;
|
|
16
|
+
breakoutValue = value;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function isFunction(value) {
|
|
21
|
+
return typeof value === 'function';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function optionalFunctionValue(value) {
|
|
25
|
+
var args = [];
|
|
26
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
27
|
+
args[_i - 1] = arguments[_i];
|
|
28
|
+
}
|
|
29
|
+
return isFunction(value) ? value.apply(void 0, args) : value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function defaultTo(callback, defaultValue) {
|
|
33
|
+
var _a;
|
|
34
|
+
return (_a = optionalFunctionValue(callback)) !== null && _a !== void 0 ? _a : defaultValue;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function ruleReturn(pass, message) {
|
|
38
|
+
var output = { pass: pass };
|
|
39
|
+
if (message) {
|
|
40
|
+
output.message = message;
|
|
41
|
+
}
|
|
42
|
+
return output;
|
|
43
|
+
}
|
|
44
|
+
function failing() {
|
|
45
|
+
return ruleReturn(false);
|
|
46
|
+
}
|
|
47
|
+
function passing() {
|
|
48
|
+
return ruleReturn(true);
|
|
49
|
+
}
|
|
50
|
+
function defaultToPassing(callback) {
|
|
51
|
+
return defaultTo(callback, passing());
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function runLazyRule(lazyRule, currentValue) {
|
|
55
|
+
try {
|
|
56
|
+
return lazyRule.run(currentValue);
|
|
57
|
+
}
|
|
58
|
+
catch (_a) {
|
|
59
|
+
return failing();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function isArrayOf(inputArray, currentRule) {
|
|
64
|
+
return defaultToPassing(mapFirst(inputArray, function (currentValue, breakout, index) {
|
|
65
|
+
var res = n4s.ctx.run({ value: currentValue, set: true, meta: { index: index } }, function () { return runLazyRule(currentRule, currentValue); });
|
|
66
|
+
if (!res.pass) {
|
|
67
|
+
breakout(res);
|
|
68
|
+
}
|
|
69
|
+
}));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function loose(inputObject, shapeObject) {
|
|
73
|
+
var _loop_1 = function (key) {
|
|
74
|
+
var currentValue = inputObject[key];
|
|
75
|
+
var currentRule = shapeObject[key];
|
|
76
|
+
var res = n4s.ctx.run({ value: currentValue, set: true, meta: { key: key } }, function () {
|
|
77
|
+
return runLazyRule(currentRule, currentValue);
|
|
78
|
+
});
|
|
79
|
+
if (!res.pass) {
|
|
80
|
+
return { value: res };
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
for (var key in shapeObject) {
|
|
84
|
+
var state_1 = _loop_1(key);
|
|
85
|
+
if (typeof state_1 === "object")
|
|
86
|
+
return state_1.value;
|
|
87
|
+
}
|
|
88
|
+
return passing();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* A safe hasOwnProperty access
|
|
93
|
+
*/
|
|
94
|
+
function hasOwnProperty(obj, key) {
|
|
95
|
+
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function shape(inputObject, shapeObject) {
|
|
99
|
+
var baseRes = loose(inputObject, shapeObject);
|
|
100
|
+
if (!baseRes.pass) {
|
|
101
|
+
return baseRes;
|
|
102
|
+
}
|
|
103
|
+
for (var key in inputObject) {
|
|
104
|
+
if (!hasOwnProperty(shapeObject, key)) {
|
|
105
|
+
return failing();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return passing();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
n4s.enforce.extend({ shape: shape, loose: loose, isArrayOf: isArrayOf });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var n=require("n4s");function r(n){return"function"==typeof n}function t(n,t){var u;return null!==(u=function(n){for(var t=[],u=1;u<arguments.length;u++)t[u-1]=arguments[u];return r(n)?n.apply(void 0,t):n}(n))&&void 0!==u?u:t}function u(n,r){return n={pass:n},r&&(n.message=r),n}function e(){return u(!1)}function o(){return u(!0)}function f(n,r){try{return n.run(r)}catch(n){return e()}}function i(r,t){var u,e=function(u){var e=r[u],o=t[u];if(!(u=n.ctx.run({value:e,set:!0,meta:{key:u}},(function(){return f(o,e)}))).pass)return{value:u}};for(u in t){var i=e(u);if("object"==typeof i)return i.value}return o()}function c(n,r){return Object.prototype.hasOwnProperty.call(n,r)}n.enforce.extend({shape:function(n,r){var t=i(n,r);if(!t.pass)return t;for(var u in n)if(!c(r,u))return e();return o()},loose:i,isArrayOf:function(r,u){return function(n){return t(n,o())}(function(n,r){function t(n){u=!0,e=n}for(var u=!1,e=null,o=0;o<n.length;o++)if(r(n[o],t,o),u)return e}(r,(function(r,t,e){(e=n.ctx.run({value:r,set:!0,meta:{index:e}},(function(){return f(u,r)}))).pass||t(e)})))}});
|