vest 4.0.0-dev-366a8b → 4.0.0-dev-e266d9
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 +70 -49
- package/README.md +2 -112
- package/dist/cjs/classnames.development.js +3 -3
- package/dist/cjs/classnames.production.js +1 -1
- 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 +132 -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 +144 -0
- package/dist/cjs/enforce/schema.production.js +1 -0
- package/dist/cjs/promisify.development.js +1 -1
- package/dist/cjs/promisify.production.js +1 -1
- package/dist/cjs/schema.js +7 -0
- package/dist/cjs/vest.development.js +608 -1097
- package/dist/cjs/vest.production.js +1 -1
- package/dist/es/classnames.development.js +3 -3
- package/dist/es/classnames.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 +130 -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 +140 -0
- package/dist/es/enforce/schema.production.js +1 -0
- package/dist/es/promisify.development.js +1 -1
- package/dist/es/promisify.production.js +1 -1
- package/dist/es/vest.development.js +602 -1097
- package/dist/es/vest.production.js +1 -1
- package/dist/umd/classnames.development.js +3 -3
- package/dist/umd/classnames.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 +136 -0
- package/dist/umd/enforce/compounds.production.js +1 -0
- package/dist/umd/enforce/schema.development.js +148 -0
- package/dist/umd/enforce/schema.production.js +1 -0
- package/dist/umd/promisify.development.js +1 -1
- package/dist/umd/promisify.production.js +1 -1
- package/dist/umd/vest.development.js +1693 -2185
- 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 +107 -13
- package/testUtils/mockThrowError.ts +16 -0
- package/types/classnames.d.ts +2 -2
- package/types/enforce/compose.d.ts +134 -0
- package/types/enforce/compounds.d.ts +146 -0
- package/types/enforce/schema.d.ts +151 -0
- package/types/vest.d.ts +31 -196
- package/docs/.nojekyll +0 -0
- package/docs/README.md +0 -113
- package/docs/_assets/favicon.ico +0 -0
- package/docs/_assets/vest-logo.png +0 -0
- package/docs/_sidebar.md +0 -14
- package/docs/cross_field_validations.md +0 -33
- package/docs/enforce.md +0 -11
- package/docs/exclusion.md +0 -129
- package/docs/getting_started.md +0 -72
- package/docs/group.md +0 -142
- package/docs/index.html +0 -41
- package/docs/migration.md +0 -202
- package/docs/n4s/rules.md +0 -1282
- package/docs/node.md +0 -36
- package/docs/optional.md +0 -103
- package/docs/result.md +0 -249
- package/docs/state.md +0 -102
- package/docs/test.md +0 -172
- package/docs/utilities.md +0 -109
- package/docs/warn.md +0 -82
package/CHANGELOG.md
CHANGED
|
@@ -4,72 +4,93 @@ 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-12-12
|
|
8
8
|
### Changed or removed
|
|
9
|
-
-
|
|
10
|
-
-
|
|
9
|
+
- b5a0108 breaking(vest): remove test.each (ealush)
|
|
10
|
+
- 7c43eab major(context): used named export in context (ealush)
|
|
11
|
+
- e8652bc breaking(vest, enforce): prepare next major (ealush)
|
|
12
|
+
- dab8e00 breaking(vest, enforce): prepare next major (ealush)
|
|
11
13
|
|
|
12
14
|
### Added
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
18
|
-
-
|
|
19
|
-
-
|
|
15
|
+
- b0a9a14 feat(vest): use key prop to retain test state after reorder (#732) (Evyatar)
|
|
16
|
+
- d3e7613 feat(vest): support custom optional logic (ealush)
|
|
17
|
+
- be1cbf6 feat(vest): Add a parser export (ealush)
|
|
18
|
+
- 2553748 feat(vest): isValid with field name (ealush)
|
|
19
|
+
- ec5ceb9 feat(vest): handle execution order state refill with memoized tests (ealush)
|
|
20
|
+
- 4032bd8 feat(vest): Make the state rely on execution order. Make skipWhen set skipped context. (undefined)
|
|
21
|
+
- 220127b added(n4s): partial rule modifier (undefined)
|
|
22
|
+
- b5ce72d feat(n4s): context propagation within enforce (undefined)
|
|
20
23
|
|
|
21
24
|
### Fixed and improved
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
-
|
|
25
|
-
-
|
|
26
|
-
-
|
|
27
|
-
-
|
|
28
|
-
-
|
|
29
|
-
-
|
|
30
|
-
-
|
|
31
|
-
-
|
|
32
|
-
-
|
|
33
|
-
-
|
|
34
|
-
-
|
|
35
|
-
-
|
|
36
|
-
-
|
|
37
|
-
-
|
|
38
|
-
-
|
|
39
|
-
-
|
|
40
|
-
-
|
|
25
|
+
- 1384c08 fix(vest): only.group works in conjunction with only (#734) (Evyatar)
|
|
26
|
+
- README.md
|
|
27
|
+
- 7daf6d2 rule(n4s): add isNullish rule, improve isBlank (ealush)
|
|
28
|
+
- c4a4140 chore: organize imports (ealush)
|
|
29
|
+
- 9f9b970 vx: improve package.json generation (ealush)
|
|
30
|
+
- 0370fc1 n4s: extract non-required modules (ealush)
|
|
31
|
+
- 03ba92c vx: support namespaced exports (ealush)
|
|
32
|
+
- 38a54ae vx: support installing of local packages (ealush)
|
|
33
|
+
- 7baedf2 n4s: use named export in entry (ealush)
|
|
34
|
+
- 366001b patch(vest): better handling of skipped async tests (ealush)
|
|
35
|
+
- 2380679 types(vest): allow unprovided suite name in tests (ealush)
|
|
36
|
+
- f721b2d patch(vest): replace warns boolean flag with enum (ealush)
|
|
37
|
+
- 0acb24d patch(vest): Pass draft result to skipWhen conditional (ealush)
|
|
38
|
+
- e84cd9d patch(vest): add back optional suite name argument (ealush)
|
|
39
|
+
- 8b30b66 patch(vest): make compose an external export (ealush)
|
|
40
|
+
- ddfa9fd patch(vest): move creation functions to event bus (ealush)
|
|
41
|
+
- fe83e88 patch: Some cleanups (ealush)
|
|
42
|
+
- 9275707 fix(vest): prevent pending tests from being marked as skipped (ealush)
|
|
43
|
+
- 94e00a8 patch(vest): Add isolate module for containing re-orders (ealush)
|
|
44
|
+
- 5bcc14a patch(vest): refresh tests after cancel (ealush)
|
|
45
|
+
- fd049e8 patch: add cursor util (ealush)
|
|
46
|
+
- 9380c09 patch(vest): move cursor into context (ealush)
|
|
47
|
+
- 49b72f4 patch(vest): automatically purge not-found test results (ealush)
|
|
48
|
+
- 2dfedaf vx: correctly pass cli options to build script (ealush)
|
|
49
|
+
- aae250a patch(vest): Throw error when tests are called in the wrong order (ealush)
|
|
50
|
+
- a87824b vx: add types to exports (ealush)
|
|
51
|
+
- cc9b4b0 organize vest state hooks (ealush)
|
|
52
|
+
- df84261 patch(vest): infer omitted optional fields (ealush)
|
|
53
|
+
- 2534cf6 vx: add dev bundle to exports (ealush)
|
|
54
|
+
- 4d450aa fix(vest): prevent changing test status once it was finalized (ealush)
|
|
55
|
+
- 8730e25 patch(vest): add cache invalidation for canceled tests (ealush)
|
|
56
|
+
- 4cb2c6c types(vest): add safeguard around shouldUseErrorAsMessage (ealush)
|
|
57
|
+
- c214a12 patch(vest): some cleanups (ealush)
|
|
58
|
+
- 858458d patch(vest): remove duplicate code handling perv test registration (ealush)
|
|
59
|
+
- ec69173 vx: use context directly from published package (ealush)
|
|
60
|
+
- 962bc06 patch: run done callbacks via event (ealush)
|
|
61
|
+
- d96f428 patch(vest): removed state subscription (ealush)
|
|
41
62
|
- package.json
|
|
42
63
|
- packages/anyone/package.json
|
|
43
64
|
- packages/anyone/package.json
|
|
44
65
|
- packages/vast/types/vast.d.ts
|
|
45
|
-
-
|
|
46
|
-
-
|
|
47
|
-
-
|
|
48
|
-
-
|
|
49
|
-
-
|
|
50
|
-
-
|
|
51
|
-
-
|
|
66
|
+
- e4e1746 patch(vest): remove pending and lagging state (ealush)
|
|
67
|
+
- 9d8fd21 patch(vest): convert boolean flags to a single status key (ealush)
|
|
68
|
+
- dbb836e patch: simplify fieldname comparison (ealush)
|
|
69
|
+
- 5a78179 patch(vest): add useAllIncomplete hook (ealush)
|
|
70
|
+
- b5ae658 patch(vest): invalid accounts for warning tests as well (ealush)
|
|
71
|
+
- 4563b8d patch(vest): isValid counts all required tests per field (ealush)
|
|
72
|
+
- 3fe2f21 chore: cleanups (ealush)
|
|
52
73
|
- .eslintrc.js
|
|
53
|
-
-
|
|
74
|
+
- 149aab3 add(n4s): enforce.condition (ealush)
|
|
54
75
|
- package.json
|
|
55
76
|
- packages/anyone/package.json
|
|
56
|
-
-
|
|
57
|
-
-
|
|
77
|
+
- 40ef071 chore: cleanup residue (undefined)
|
|
78
|
+
- eeac20e chore: remove duplicate types (undefined)
|
|
58
79
|
- packages/anyone/package.json
|
|
59
|
-
-
|
|
60
|
-
-
|
|
80
|
+
- 4d88c04 patch: add nodejs exports (undefined)
|
|
81
|
+
- 6e57aa1 patch: remove unused exports (undefined)
|
|
61
82
|
- packages/anyone/package.json
|
|
62
|
-
-
|
|
83
|
+
- 26af06b chore: reduce complexity, remove all lint errors (undefined)
|
|
63
84
|
- packages/anyone/.npmignore
|
|
64
85
|
- .github/PULL_REQUEST_TEMPLATE.md
|
|
65
|
-
-
|
|
66
|
-
-
|
|
86
|
+
- 5be986d chore: cleanup unused code (ealush)
|
|
87
|
+
- ba68539 lint: handling lint of all packages (ealush)
|
|
67
88
|
- .gitignore
|
|
68
|
-
-
|
|
69
|
-
-
|
|
70
|
-
-
|
|
71
|
-
-
|
|
72
|
-
-
|
|
89
|
+
- cca5130 patch(n4s): add ruleReturn default values (ealush)
|
|
90
|
+
- 75306ff fix(n4s): make enforce compound runners fall back to correct response (ealush)
|
|
91
|
+
- 4751584 fix(n4s): make enforce chaining work (ealush)
|
|
92
|
+
- 73b28a1 chore: some lint fixes (ealush)
|
|
93
|
+
- 4135ce3 add cli options support (ealush)
|
|
73
94
|
|
|
74
95
|
## 3.2.7 - 2021-07-17
|
|
75
96
|
|
package/README.md
CHANGED
|
@@ -1,113 +1,3 @@
|
|
|
1
|
-
|
|
1
|
+
# Vest
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-

|
|
6
|
-

|
|
7
|
-
|
|
8
|
-
[](https://badge.fury.io/js/vest) [](https://travis-ci.org/ealush/vest) [](https://snyk.io/test/npm/vest)
|
|
9
|
-

|
|
10
|
-
|
|
11
|
-
[](https://discord.gg/WmADZpJnSe)
|
|
12
|
-
|
|
13
|
-
- [Documentation homepage](https://vestjs.dev)
|
|
14
|
-
- **Try vest live**
|
|
15
|
-
- [Vanilla JS Example](https://stackblitz.com/edit/vest-vanilla-support-example?file=validation.js)
|
|
16
|
-
- ReactJS Examples:
|
|
17
|
-
- [Example 1 (groups)](https://codesandbox.io/s/ecstatic-waterfall-4i2ne?file=/src/validate.js)
|
|
18
|
-
- [Example 2 (Async)](https://codesandbox.io/s/youthful-williamson-loijb?file=/src/validate.js)
|
|
19
|
-
- [Example 3](https://stackblitz.com/edit/vest-react-support-example?file=validation.js)
|
|
20
|
-
- [Example 4](https://stackblitz.com/edit/vest-react-registration?file=validate.js)
|
|
21
|
-
- [Example 5 (Password validator)](https://codesandbox.io/s/password-validator-example-6puvy?file=/src/validate.js)
|
|
22
|
-
- [VueJS Example](https://codesandbox.io/s/vest-vue-example-1j6r8?file=/src/validations.js)
|
|
23
|
-
- [Svelte Example](https://codesandbox.io/s/vestdocssvelteexample-k87t7?file=/validate.js)
|
|
24
|
-
|
|
25
|
-
## Tutorials
|
|
26
|
-
|
|
27
|
-
[Step By Step React Tutorial](https://dev.to/ealush/dead-simple-form-validation-with-vest-5gf8)
|
|
28
|
-
|
|
29
|
-
## [Release Notes](https://github.com/ealush/vest/releases)
|
|
30
|
-
|
|
31
|
-
## 🦺 What is Vest?
|
|
32
|
-
|
|
33
|
-
Vest is a validations library for JS apps that derives its syntax from modern JS unit testing frameworks such as Mocha or Jest. It is easy to learn due to its use of already common declarative patterns.
|
|
34
|
-
It works great with user-input validation and with validating upon user interaction to provide the best possible user experience.
|
|
35
|
-
|
|
36
|
-
The idea behind Vest is that your validations can be described as a 'spec' or a contract that reflects your form or feature structure. Your validations run in production, and they are framework agnostic - meaning Vest works well with React, Angular, Vue, or even without a framework at all.
|
|
37
|
-
|
|
38
|
-
Using Vest for form validation can reduce bloat, improve feature readability and maintainability.
|
|
39
|
-
|
|
40
|
-
**Basic Example**
|
|
41
|
-

|
|
42
|
-
|
|
43
|
-
**Memoized async test**
|
|
44
|
-

|
|
45
|
-
|
|
46
|
-
## ✅ Motivation
|
|
47
|
-
|
|
48
|
-
Writing forms is an integral part of building web apps, and even though it may seem trivial at first - as your feature grows over time, so does your validation logic grows in complexity.
|
|
49
|
-
|
|
50
|
-
Vest tries to remediate this by separating validation logic from feature logic so it is easier to maintain over time and refactor when needed.
|
|
51
|
-
|
|
52
|
-
## ✨ Vest's features
|
|
53
|
-
|
|
54
|
-
- 🎨 Framework agnostic (BYOUI)
|
|
55
|
-
- ⚡️ Rich, extendable, assertions library (enforce) ([doc](http://vestjs.dev/#/enforce))
|
|
56
|
-
- 🚥 Multiple validations for the same field
|
|
57
|
-
- ⚠️ Warning (non failing) tests ([doc](http://vestjs.dev/#/warn))
|
|
58
|
-
- 📝 Validate only the fields the user interacted with ([doc](http://vestjs.dev/#/exclusion))
|
|
59
|
-
- ⏳ Memoize async validations to reduce calls to the server ([doc](http://vestjs.dev/#/test?id=testmemo-for-memoized-tests))
|
|
60
|
-
- 🚦 Test grouping ([doc](http://vestjs.dev/#/group))
|
|
61
|
-
|
|
62
|
-
## Example code ([Run in sandbox](https://codesandbox.io/s/vest-react-tutorial-finished-ztt8t?file=/src/validate.js))
|
|
63
|
-
|
|
64
|
-
```js
|
|
65
|
-
import { create, only, test, enforce, warn, skipWhen } from 'vest';
|
|
66
|
-
|
|
67
|
-
export default create((data = {}, currentField) => {
|
|
68
|
-
only(currentField);
|
|
69
|
-
|
|
70
|
-
test('username', 'Username is required', () => {
|
|
71
|
-
enforce(data.username).isNotEmpty();
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
test('username', 'Username is too short', () => {
|
|
75
|
-
enforce(data.username).longerThanOrEquals(3);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
test('password', 'Password is required', () => {
|
|
79
|
-
enforce(data.password).isNotEmpty();
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
test('password', 'Password must be at least 6 chars long', () => {
|
|
83
|
-
enforce(data.password).longerThanOrEquals(6);
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
test('password', 'Password is weak, Maybe add a number?', () => {
|
|
87
|
-
warn();
|
|
88
|
-
enforce(data.password).matches(/[0-9]/);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
skipWhen(!data.password, () => {
|
|
92
|
-
test('confirm_password', 'Passwords do not match', () => {
|
|
93
|
-
enforce(data.confirm_password).equals(data.password);
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
test('email', 'Email Address is not valid', () => {
|
|
98
|
-
enforce(data.email).isEmail();
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
test('tos', () => {
|
|
102
|
-
enforce(data.tos).isTruthy();
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
## Why Vest?
|
|
108
|
-
|
|
109
|
-
- 🧠 Vest is really easy to learn. You can take your existing knowledge of unit tests and transfer it to validations.
|
|
110
|
-
- ✏️ Vest takes into account user interaction and warn only validations.
|
|
111
|
-
- 🧱 Your validations are structured, making it very simple to read and write. All validation files look the same.
|
|
112
|
-
- 🖇 Your validation logic is separate from your feature logic, preventing the spaghetti code that's usually involved with writing validations.
|
|
113
|
-
- 🧩 Validation logic is easy to share and reuse across features.
|
|
3
|
+
Home of the Vest source code.
|
|
@@ -56,10 +56,10 @@ function parse(res) {
|
|
|
56
56
|
/**
|
|
57
57
|
* Creates a function that returns class names that match the validation result
|
|
58
58
|
*/
|
|
59
|
-
function
|
|
59
|
+
function classnames(res, classes) {
|
|
60
60
|
if (classes === void 0) { classes = {}; }
|
|
61
61
|
if (!res || !isFunction(res.hasErrors)) {
|
|
62
|
-
throwError("
|
|
62
|
+
throwError("classnames: Expected first argument to be Vest's result object.");
|
|
63
63
|
}
|
|
64
64
|
var selectors = parse(res);
|
|
65
65
|
return function (key) {
|
|
@@ -74,4 +74,4 @@ function classNames(res, classes) {
|
|
|
74
74
|
};
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
module.exports =
|
|
77
|
+
module.exports = classnames;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";function t(t){return"function"==typeof t}function r(r,n){var e;return null!==(e=function(r){for(var n=[],e=1;e<arguments.length;e++)n[e-1]=arguments[e];return t(r)?r.apply(void 0,n):r}(r))&&void 0!==e?e:n}function n(t){var r=Number(t);return!(isNaN(parseFloat(String(t)))||isNaN(Number(t))||!isFinite(r))}function e(t,r){return n(t)&&n(r)&&Number(t)>Number(r)}function u(t,r){return Object.prototype.hasOwnProperty.call(t,r)}module.exports=function(n,o){void 0===o&&(o={}),n&&t(n.hasErrors)||function(t,n){throw Error(r(n,t))}("
|
|
1
|
+
"use strict";function t(t){return"function"==typeof t}function r(r,n){var e;return null!==(e=function(r){for(var n=[],e=1;e<arguments.length;e++)n[e-1]=arguments[e];return t(r)?r.apply(void 0,n):r}(r))&&void 0!==e?e:n}function n(t){var r=Number(t);return!(isNaN(parseFloat(String(t)))||isNaN(Number(t))||!isFinite(r))}function e(t,r){return n(t)&&n(r)&&Number(t)>Number(r)}function u(t,r){return Object.prototype.hasOwnProperty.call(t,r)}module.exports=function(n,o){void 0===o&&(o={}),n&&t(n.hasErrors)||function(t,n){throw Error(r(n,t))}("classnames: Expected first argument to be Vest's result object.");var s=function(t){var r={},n={invalid:t.hasErrors,tested:function(o){return o?u(r,o)?r[o]:(r[o]=u(t.tests,o)&&e(t.tests[o].testCount,0),n.tested(o)):e(t.testCount,0)},untested:function(r){return 0===t.testCount||!n.tested(r)},valid:t.isValid,warning:t.hasWarnings};return n}(n);return function(r){var n,e=[];for(n in o){var u=n;t(s[u])&&s[u](r)&&e.push(o[u])}return e.join(" ")}};
|
|
@@ -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,132 @@
|
|
|
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
|
+
n4s.enforce.extend({ allOf: allOf, anyOf: anyOf, noneOf: noneOf, oneOf: oneOf });
|
|
@@ -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,f=0;f<n.length;f++)if(r(n[f],t,f),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 f(){return e(!1)}function o(n){return u(n,f())}function i(n){return u(n,e(!0))}function c(n,r){try{return n.run(r)}catch(n){return f()}}function a(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 i(r(t,(function(r,t){(r=c(r,n)).pass||t(r)})))},anyOf:function(n){for(var t=[],u=1;u<arguments.length;u++)t[u-1]=arguments[u];return o(r(t,(function(r,t){(r=c(r,n)).pass&&t(r)})))},noneOf:function(n){for(var t=[],u=1;u<arguments.length;u++)t[u-1]=arguments[u];return i(r(t,(function(r,t){c(r,n).pass&&t(f())})))},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=c(r,n)).pass&&u.push(r)})),e(a(u,1))}});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|