vest 4.0.0-next-c704bc → 4.0.1-dev-6078f7

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.
Files changed (61) hide show
  1. package/CHANGELOG.md +0 -89
  2. package/README.md +57 -2
  3. package/dist/cjs/classnames.development.js +77 -0
  4. package/dist/cjs/compose.js +7 -0
  5. package/dist/cjs/compounds.js +7 -0
  6. package/dist/cjs/enforce/compose.development.js +139 -0
  7. package/dist/cjs/enforce/compose.production.js +1 -0
  8. package/dist/cjs/enforce/compounds.development.js +132 -0
  9. package/dist/cjs/enforce/compounds.production.js +1 -0
  10. package/dist/cjs/enforce/package.json +1 -0
  11. package/dist/cjs/enforce/schema.development.js +144 -0
  12. package/dist/cjs/enforce/schema.production.js +1 -0
  13. package/dist/cjs/parser.development.js +47 -0
  14. package/dist/cjs/parser.js +7 -0
  15. package/dist/cjs/parser.production.js +1 -0
  16. package/dist/cjs/promisify.development.js +27 -0
  17. package/dist/cjs/promisify.js +7 -0
  18. package/dist/cjs/promisify.production.js +1 -0
  19. package/dist/cjs/schema.js +7 -0
  20. package/dist/cjs/vest.development.js +1813 -0
  21. package/dist/cjs/vest.production.js +1 -1
  22. package/dist/es/classnames.development.js +75 -0
  23. package/dist/es/classnames.production.js +1 -0
  24. package/dist/es/enforce/compose.development.js +137 -0
  25. package/dist/es/enforce/compose.production.js +1 -0
  26. package/dist/es/enforce/compounds.development.js +130 -0
  27. package/dist/es/enforce/compounds.production.js +1 -0
  28. package/dist/es/enforce/package.json +1 -0
  29. package/dist/es/enforce/schema.development.js +140 -0
  30. package/dist/es/enforce/schema.production.js +1 -0
  31. package/dist/es/parser.development.js +43 -0
  32. package/dist/es/parser.production.js +1 -0
  33. package/dist/es/promisify.development.js +25 -0
  34. package/dist/es/promisify.production.js +1 -0
  35. package/dist/es/vest.development.js +1791 -0
  36. package/dist/es/vest.production.js +1 -1
  37. package/dist/umd/classnames.development.js +83 -0
  38. package/dist/umd/enforce/compose.development.js +143 -0
  39. package/dist/umd/enforce/compose.production.js +1 -0
  40. package/dist/umd/enforce/compounds.development.js +136 -0
  41. package/dist/umd/enforce/compounds.production.js +1 -0
  42. package/dist/umd/enforce/schema.development.js +148 -0
  43. package/dist/umd/enforce/schema.production.js +1 -0
  44. package/dist/umd/parser.development.js +53 -0
  45. package/dist/umd/parser.production.js +1 -0
  46. package/dist/umd/promisify.development.js +33 -0
  47. package/dist/umd/promisify.production.js +1 -0
  48. package/dist/umd/vest.development.js +1816 -0
  49. package/dist/umd/vest.production.js +1 -1
  50. package/enforce/compose/package.json +7 -0
  51. package/enforce/compounds/package.json +7 -0
  52. package/enforce/schema/package.json +7 -0
  53. package/package.json +3 -3
  54. package/parser/package.json +7 -0
  55. package/promisify/package.json +7 -0
  56. package/types/enforce/compose.d.ts +134 -0
  57. package/types/enforce/compounds.d.ts +146 -0
  58. package/types/enforce/schema.d.ts +151 -0
  59. package/types/parser.d.ts +66 -0
  60. package/types/promisify.d.ts +60 -0
  61. package/types/vest.d.ts +167 -8
package/CHANGELOG.md CHANGED
@@ -4,95 +4,6 @@ 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-12-17
8
- ### Changed or removed
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)
13
-
14
- ### Added
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)
23
-
24
- ### Fixed and improved
25
- - c704bc9 add(vest): omitWhen (#738) (Evyatar)
26
- - 1384c08 fix(vest): only.group works in conjunction with only (#734) (Evyatar)
27
- - README.md
28
- - 7daf6d2 rule(n4s): add isNullish rule, improve isBlank (ealush)
29
- - c4a4140 chore: organize imports (ealush)
30
- - 9f9b970 vx: improve package.json generation (ealush)
31
- - 0370fc1 n4s: extract non-required modules (ealush)
32
- - 03ba92c vx: support namespaced exports (ealush)
33
- - 38a54ae vx: support installing of local packages (ealush)
34
- - 7baedf2 n4s: use named export in entry (ealush)
35
- - 366001b patch(vest): better handling of skipped async tests (ealush)
36
- - 2380679 types(vest): allow unprovided suite name in tests (ealush)
37
- - f721b2d patch(vest): replace warns boolean flag with enum (ealush)
38
- - 0acb24d patch(vest): Pass draft result to skipWhen conditional (ealush)
39
- - e84cd9d patch(vest): add back optional suite name argument (ealush)
40
- - 8b30b66 patch(vest): make compose an external export (ealush)
41
- - ddfa9fd patch(vest): move creation functions to event bus (ealush)
42
- - fe83e88 patch: Some cleanups (ealush)
43
- - 9275707 fix(vest): prevent pending tests from being marked as skipped (ealush)
44
- - 94e00a8 patch(vest): Add isolate module for containing re-orders (ealush)
45
- - 5bcc14a patch(vest): refresh tests after cancel (ealush)
46
- - fd049e8 patch: add cursor util (ealush)
47
- - 9380c09 patch(vest): move cursor into context (ealush)
48
- - 49b72f4 patch(vest): automatically purge not-found test results (ealush)
49
- - 2dfedaf vx: correctly pass cli options to build script (ealush)
50
- - aae250a patch(vest): Throw error when tests are called in the wrong order (ealush)
51
- - a87824b vx: add types to exports (ealush)
52
- - cc9b4b0 organize vest state hooks (ealush)
53
- - df84261 patch(vest): infer omitted optional fields (ealush)
54
- - 2534cf6 vx: add dev bundle to exports (ealush)
55
- - 4d450aa fix(vest): prevent changing test status once it was finalized (ealush)
56
- - 8730e25 patch(vest): add cache invalidation for canceled tests (ealush)
57
- - 4cb2c6c types(vest): add safeguard around shouldUseErrorAsMessage (ealush)
58
- - c214a12 patch(vest): some cleanups (ealush)
59
- - 858458d patch(vest): remove duplicate code handling perv test registration (ealush)
60
- - ec69173 vx: use context directly from published package (ealush)
61
- - 962bc06 patch: run done callbacks via event (ealush)
62
- - d96f428 patch(vest): removed state subscription (ealush)
63
- - package.json
64
- - packages/anyone/package.json
65
- - packages/anyone/package.json
66
- - packages/vast/types/vast.d.ts
67
- - e4e1746 patch(vest): remove pending and lagging state (ealush)
68
- - 9d8fd21 patch(vest): convert boolean flags to a single status key (ealush)
69
- - dbb836e patch: simplify fieldname comparison (ealush)
70
- - 5a78179 patch(vest): add useAllIncomplete hook (ealush)
71
- - b5ae658 patch(vest): invalid accounts for warning tests as well (ealush)
72
- - 4563b8d patch(vest): isValid counts all required tests per field (ealush)
73
- - 3fe2f21 chore: cleanups (ealush)
74
- - .eslintrc.js
75
- - 149aab3 add(n4s): enforce.condition (ealush)
76
- - package.json
77
- - packages/anyone/package.json
78
- - 40ef071 chore: cleanup residue (undefined)
79
- - eeac20e chore: remove duplicate types (undefined)
80
- - packages/anyone/package.json
81
- - 4d88c04 patch: add nodejs exports (undefined)
82
- - 6e57aa1 patch: remove unused exports (undefined)
83
- - packages/anyone/package.json
84
- - 26af06b chore: reduce complexity, remove all lint errors (undefined)
85
- - packages/anyone/.npmignore
86
- - .github/PULL_REQUEST_TEMPLATE.md
87
- - 5be986d chore: cleanup unused code (ealush)
88
- - ba68539 lint: handling lint of all packages (ealush)
89
- - .gitignore
90
- - cca5130 patch(n4s): add ruleReturn default values (ealush)
91
- - 75306ff fix(n4s): make enforce compound runners fall back to correct response (ealush)
92
- - 4751584 fix(n4s): make enforce chaining work (ealush)
93
- - 73b28a1 chore: some lint fixes (ealush)
94
- - 4135ce3 add cli options support (ealush)
95
-
96
7
  ## 3.2.7 - 2021-07-17
97
8
 
98
9
  ### Fixed and improved
package/README.md CHANGED
@@ -1,3 +1,58 @@
1
- # Vest
1
+ # Vest - Declarative validations framework
2
2
 
3
- Home of the Vest source code.
3
+ [Vest Documentation](https://vestjs.dev)
4
+
5
+ [![Join Discord](https://badgen.net/discord/online-members/WmADZpJnSe?icon=discord&label=Discord)](https://discord.gg/WmADZpJnSe) [![Github Stars](https://badgen.net/github/stars/ealush/vest?color=yellow&label=Github%20🌟)](https://github.com/ealush/vest) [![Version](https://badgen.net/npm/v/vest?&icon=npm)](https://www.npmjs.com/package/vest) [![Downloads](https://badgen.net/npm/dt/vest?label=Downloads)](https://www.npmjs.com/package/vest) [![bundlephobia](https://badgen.net/bundlephobia/minzip/vest)](https://bundlephobia.com/package/vest) [![Status](https://badgen.net/github/status/ealush/vest)](https://github.com/ealush/vest/actions)
6
+
7
+ ![Vest](https://cdn.jsdelivr.net/gh/ealush/vest@assets/logo_250.png 'Vest')
8
+
9
+ Vest is a form-validation framework inspired by unit testing libraries like Mocha or Jest; It is designed to be easy to use and easy to learn by introducing their declarative syntax.
10
+
11
+ The idea behind Vest is that your validations can be described as a suite - a contract that reflects your form or feature structure. Vest is framework agnostic, meaning it can be used with any UI framework, or without any framework at all.
12
+
13
+ Using Vest for form validation can reduce bloat, improve feature readability and maintainability.
14
+
15
+ ```js
16
+ test('username', 'Username is required', () => {
17
+ enforce(data.username).isNotBlank();
18
+ });
19
+
20
+ test('username', 'Username must be at least 3 chars', () => {
21
+ enforce(data.username).longerThanOrEquals(3);
22
+ });
23
+ ```
24
+
25
+ ## Installation
26
+
27
+ ```
28
+ npm i vest
29
+ ```
30
+
31
+ ## Motivation
32
+
33
+ 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.
34
+
35
+ Vest tries to remediate this by separating validation logic from feature logic, so it's easier to maintain over time and refactor when needed.
36
+
37
+ # Why Vest?
38
+
39
+ 💡 Vest is easy to Learn. Vest adopts the syntax and style of unit testing frameworks, so you can leverage the knowledge you already have to write your form validations.
40
+
41
+ 🎨 Vest is framework agnostic. You can use Vest with any UI framework out there.
42
+
43
+ 🧠 Vest takes care of all the annoying parts for you. It manages its validation state, handles async validations, and much more.
44
+
45
+ 🧩 Vest is extendable. You can easily add new kinds of validations to Vest according to your needs.
46
+
47
+ ♻️ Validation logic in Vest can be shared across multiple features in your app.
48
+
49
+ # Getting Started
50
+
51
+ [Vest Documentation](https://vestjs.dev)
52
+
53
+ Here are some code sandboxes to get you started:
54
+
55
+ - [React](https://codesandbox.io/s/react-28jwx)
56
+ - [Vue](https://codesandbox.io/s/vue-hsyt8)
57
+ - [Svelte](https://codesandbox.io/s/svelte-tsfhx)
58
+ - [Vanilla](https://codesandbox.io/s/vest-vanilla-js-35u8e)
@@ -0,0 +1,77 @@
1
+ 'use strict';
2
+
3
+ function isFunction(value) {
4
+ return typeof value === 'function';
5
+ }
6
+
7
+ /**
8
+ * Throws a timed out error.
9
+ */
10
+ function throwError(devMessage, productionMessage) {
11
+ throw new Error(devMessage );
12
+ }
13
+
14
+ function isNumeric(value) {
15
+ var str = String(value);
16
+ var num = Number(value);
17
+ var result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num);
18
+ return Boolean(result);
19
+ }
20
+
21
+ function greaterThan(value, gt) {
22
+ return isNumeric(value) && isNumeric(gt) && Number(value) > Number(gt);
23
+ }
24
+
25
+ /**
26
+ * A safe hasOwnProperty access
27
+ */
28
+ function hasOwnProperty(obj, key) {
29
+ return Object.prototype.hasOwnProperty.call(obj, key);
30
+ }
31
+
32
+ function parse(res) {
33
+ var testedStorage = {};
34
+ var selectors = {
35
+ invalid: res.hasErrors,
36
+ tested: function (fieldName) {
37
+ if (!fieldName) {
38
+ return greaterThan(res.testCount, 0);
39
+ }
40
+ if (hasOwnProperty(testedStorage, fieldName))
41
+ return testedStorage[fieldName];
42
+ testedStorage[fieldName] =
43
+ hasOwnProperty(res.tests, fieldName) &&
44
+ greaterThan(res.tests[fieldName].testCount, 0);
45
+ return selectors.tested(fieldName);
46
+ },
47
+ untested: function (fieldName) {
48
+ return res.testCount === 0 || !selectors.tested(fieldName);
49
+ },
50
+ valid: res.isValid,
51
+ warning: res.hasWarnings
52
+ };
53
+ return selectors;
54
+ }
55
+
56
+ /**
57
+ * Creates a function that returns class names that match the validation result
58
+ */
59
+ function classnames(res, classes) {
60
+ if (classes === void 0) { classes = {}; }
61
+ if (!res || !isFunction(res.hasErrors)) {
62
+ throwError("classnames: Expected first argument to be Vest's result object.");
63
+ }
64
+ var selectors = parse(res);
65
+ return function (key) {
66
+ var classesArray = [];
67
+ for (var selector in classes) {
68
+ var sel = selector;
69
+ if (isFunction(selectors[sel]) && selectors[sel](key)) {
70
+ classesArray.push(classes[sel]);
71
+ }
72
+ }
73
+ return classesArray.join(' ');
74
+ };
75
+ }
76
+
77
+ module.exports = classnames;
@@ -0,0 +1,7 @@
1
+ 'use strict'
2
+
3
+ if (process.env.NODE_ENV === 'production') {
4
+ module.exports = require('./compose.production.js');
5
+ } else {
6
+ module.exports = require('./compose.development.js');
7
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict'
2
+
3
+ if (process.env.NODE_ENV === 'production') {
4
+ module.exports = require('./compounds.production.js');
5
+ } else {
6
+ module.exports = require('./compounds.development.js');
7
+ }
@@ -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"}
@@ -0,0 +1,144 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var n4s = require('n4s');
6
+
7
+ function mapFirst(array, callback) {
8
+ var broke = false;
9
+ var breakoutValue = null;
10
+ for (var i = 0; i < array.length; i++) {
11
+ callback(array[i], breakout, i);
12
+ if (broke) {
13
+ return breakoutValue;
14
+ }
15
+ }
16
+ function breakout(value) {
17
+ broke = true;
18
+ breakoutValue = value;
19
+ }
20
+ }
21
+
22
+ function isFunction(value) {
23
+ return typeof value === 'function';
24
+ }
25
+
26
+ function optionalFunctionValue(value) {
27
+ var args = [];
28
+ for (var _i = 1; _i < arguments.length; _i++) {
29
+ args[_i - 1] = arguments[_i];
30
+ }
31
+ return isFunction(value) ? value.apply(void 0, args) : value;
32
+ }
33
+
34
+ function defaultTo(callback, defaultValue) {
35
+ var _a;
36
+ return (_a = optionalFunctionValue(callback)) !== null && _a !== void 0 ? _a : defaultValue;
37
+ }
38
+
39
+ function ruleReturn(pass, message) {
40
+ var output = { pass: pass };
41
+ if (message) {
42
+ output.message = message;
43
+ }
44
+ return output;
45
+ }
46
+ function failing() {
47
+ return ruleReturn(false);
48
+ }
49
+ function passing() {
50
+ return ruleReturn(true);
51
+ }
52
+ function defaultToPassing(callback) {
53
+ return defaultTo(callback, passing());
54
+ }
55
+
56
+ function runLazyRule(lazyRule, currentValue) {
57
+ try {
58
+ return lazyRule.run(currentValue);
59
+ }
60
+ catch (_a) {
61
+ return failing();
62
+ }
63
+ }
64
+
65
+ function isArrayOf(inputArray, currentRule) {
66
+ return defaultToPassing(mapFirst(inputArray, function (currentValue, breakout, index) {
67
+ var res = n4s.ctx.run({ value: currentValue, set: true, meta: { index: index } }, function () { return runLazyRule(currentRule, currentValue); });
68
+ if (!res.pass) {
69
+ breakout(res);
70
+ }
71
+ }));
72
+ }
73
+
74
+ function loose(inputObject, shapeObject) {
75
+ var _loop_1 = function (key) {
76
+ var currentValue = inputObject[key];
77
+ var currentRule = shapeObject[key];
78
+ var res = n4s.ctx.run({ value: currentValue, set: true, meta: { key: key } }, function () {
79
+ return runLazyRule(currentRule, currentValue);
80
+ });
81
+ if (!res.pass) {
82
+ return { value: res };
83
+ }
84
+ };
85
+ for (var key in shapeObject) {
86
+ var state_1 = _loop_1(key);
87
+ if (typeof state_1 === "object")
88
+ return state_1.value;
89
+ }
90
+ return passing();
91
+ }
92
+
93
+ function isNull(value) {
94
+ return value === null;
95
+ }
96
+
97
+ function isUndefined(value) {
98
+ return value === undefined;
99
+ }
100
+
101
+ function isNullish(value) {
102
+ return isNull(value) || isUndefined(value);
103
+ }
104
+
105
+ function optional(value, ruleChain) {
106
+ if (isNullish(value)) {
107
+ return passing();
108
+ }
109
+ return runLazyRule(ruleChain, value);
110
+ }
111
+
112
+ /**
113
+ * A safe hasOwnProperty access
114
+ */
115
+ function hasOwnProperty(obj, key) {
116
+ return Object.prototype.hasOwnProperty.call(obj, key);
117
+ }
118
+
119
+ function shape(inputObject, shapeObject) {
120
+ var baseRes = loose(inputObject, shapeObject);
121
+ if (!baseRes.pass) {
122
+ return baseRes;
123
+ }
124
+ for (var key in inputObject) {
125
+ if (!hasOwnProperty(shapeObject, key)) {
126
+ return failing();
127
+ }
128
+ }
129
+ return passing();
130
+ }
131
+
132
+ // Help needed improving the typings of this file.
133
+ // Ideally, we'd be able to extend IShapeObject, but that's not possible.
134
+ function partial(shapeObject) {
135
+ var output = {};
136
+ for (var key in shapeObject) {
137
+ output[key] = n4s.enforce.optional(shapeObject[key]);
138
+ }
139
+ return output;
140
+ }
141
+
142
+ n4s.enforce.extend({ isArrayOf: isArrayOf, loose: loose, optional: optional, shape: shape });
143
+
144
+ exports.partial = partial;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var n=require("n4s");function r(n){return"function"==typeof n}function t(n,t){var e;return null!==(e=function(n){for(var t=[],e=1;e<arguments.length;e++)t[e-1]=arguments[e];return r(n)?n.apply(void 0,t):n}(n))&&void 0!==e?e:t}function e(n,r){return n={pass:n},r&&(n.message=r),n}function u(){return e(!1)}function o(){return e(!0)}function i(n,r){try{return n.run(r)}catch(n){return u()}}function f(r,t){var e,u=function(e){var u=r[e],o=t[e];if(!(e=n.ctx.run({value:u,set:!0,meta:{key:e}},(function(){return i(o,u)}))).pass)return{value:e}};for(e in t){var f=u(e);if("object"==typeof f)return f.value}return o()}function c(n,r){return Object.prototype.hasOwnProperty.call(n,r)}n.enforce.extend({isArrayOf:function(r,e){return function(n){return t(n,o())}(function(n,r){function t(n){e=!0,u=n}for(var e=!1,u=null,o=0;o<n.length;o++)if(r(n[o],t,o),e)return u}(r,(function(r,t,u){(u=n.ctx.run({value:r,set:!0,meta:{index:u}},(function(){return i(e,r)}))).pass||t(u)})))},loose:f,optional:function(n,r){return function(n){return function(n){return null===n}(n)||function(n){return void 0===n}(n)}(n)?o():i(r,n)},shape:function(n,r){var t=f(n,r);if(!t.pass)return t;for(var e in n)if(!c(r,e))return u();return o()}}),exports.partial=function(r){var t,e={};for(t in r)e[t]=n.enforce.optional(r[t]);return e};