n4s 5.0.1 → 6.0.0-next-7a898c

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 (46) hide show
  1. package/CHANGELOG.md +0 -4
  2. package/README.md +2 -0
  3. package/compose/package.json +2 -0
  4. package/compounds/package.json +2 -0
  5. package/dist/cjs/compose.development.js +14 -97
  6. package/dist/cjs/compose.production.js +1 -1
  7. package/dist/cjs/compounds.development.js +30 -86
  8. package/dist/cjs/compounds.production.js +1 -1
  9. package/dist/cjs/n4s.development.js +208 -385
  10. package/dist/cjs/n4s.production.js +1 -1
  11. package/dist/cjs/schema.development.js +19 -78
  12. package/dist/cjs/schema.production.js +1 -1
  13. package/dist/es/compose.development.js +14 -97
  14. package/dist/es/compose.production.js +1 -1
  15. package/dist/es/compounds.development.js +28 -84
  16. package/dist/es/compounds.production.js +1 -1
  17. package/dist/es/n4s.development.js +195 -372
  18. package/dist/es/n4s.production.js +1 -1
  19. package/dist/es/schema.development.js +16 -75
  20. package/dist/es/schema.production.js +1 -1
  21. package/dist/umd/compose.development.js +18 -102
  22. package/dist/umd/compose.production.js +1 -1
  23. package/dist/umd/compounds.development.js +34 -91
  24. package/dist/umd/compounds.production.js +1 -1
  25. package/dist/umd/n4s.development.js +212 -390
  26. package/dist/umd/n4s.production.js +1 -1
  27. package/dist/umd/schema.development.js +23 -83
  28. package/dist/umd/schema.production.js +1 -1
  29. package/package.json +15 -5
  30. package/schema/package.json +2 -0
  31. package/testUtils/TEnforceMock.ts +3 -0
  32. package/types/compose.d.ts +42 -54
  33. package/types/compose.d.ts.map +1 -0
  34. package/types/compounds.d.ts +46 -61
  35. package/types/compounds.d.ts.map +1 -0
  36. package/types/n4s.d.ts +56 -70
  37. package/types/n4s.d.ts.map +1 -0
  38. package/types/schema.d.ts +51 -62
  39. package/types/schema.d.ts.map +1 -0
  40. package/docs/.nojekyll +0 -0
  41. package/docs/README.md +0 -44
  42. package/docs/_sidebar.md +0 -5
  43. package/docs/external.md +0 -27
  44. package/docs/index.html +0 -32
  45. package/docs/rules.md +0 -1282
  46. package/tsconfig.json +0 -8
package/CHANGELOG.md CHANGED
@@ -4,10 +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
- ## 5.0.1 - 2021-12-24
8
- ### Fixed and improved
9
- - 7e8d48d Update changelog (ealush)
10
-
11
7
  ## 4.0.0 2021-12-24
12
8
 
13
9
  - 7daf6d2 rule(n4s): add isNullish rule, improve isBlank (ealush)
package/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Enforce - n4s
2
2
 
3
+ [![Join Discord](https://badgen.net/discord/online-members/WmADZpJnSe?icon=discord&label=Discord)](https://discord.gg/WmADZpJnSe) [![Version](https://badgen.net/npm/v/vest?&icon=npm)](https://www.npmjs.com/package/n4s) [![Downloads](https://badgen.net/npm/dt/n4s?label=Downloads)](https://www.npmjs.com/package/n4s) [![bundlephobia](https://badgen.net/bundlephobia/minzip/n4s)](https://bundlephobia.com/package/n4s) [![Status](https://badgen.net/github/status/ealush/vest)](https://github.com/ealush/vest/actions)
4
+
3
5
  Enforce is a validations assertions library. It provides rules that you can test your data against.
4
6
 
5
7
  By default, enforce throws an error when your validations fail. These errors should be caught by a validation testing framework such as [Vest](https://github.com/ealush/vest).
@@ -1,6 +1,8 @@
1
1
  {
2
2
  "main": "../dist/cjs/compose.js",
3
3
  "module": "../dist/es/compose.production.js",
4
+ "unpkg": "../dist/umd/compose.production.js",
5
+ "jsdelivr": "../dist/umd/compose.production.js",
4
6
  "name": "compose",
5
7
  "types": "../types/compose.d.ts",
6
8
  "private": true
@@ -1,6 +1,8 @@
1
1
  {
2
2
  "main": "../dist/cjs/compounds.js",
3
3
  "module": "../dist/es/compounds.production.js",
4
+ "unpkg": "../dist/umd/compounds.production.js",
5
+ "jsdelivr": "../dist/umd/compounds.production.js",
4
6
  "name": "compounds",
5
7
  "types": "../types/compounds.d.ts",
6
8
  "private": true
@@ -1,79 +1,10 @@
1
1
  'use strict';
2
2
 
3
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
- }
4
+ var vestUtils = require('vest-utils');
74
5
 
75
6
  function ruleReturn(pass, message) {
76
- var output = { pass: pass };
7
+ const output = { pass };
77
8
  if (message) {
78
9
  output.message = message;
79
10
  }
@@ -86,7 +17,7 @@ function passing() {
86
17
  return ruleReturn(true);
87
18
  }
88
19
  function defaultToPassing(callback) {
89
- return defaultTo(callback, passing());
20
+ return vestUtils.defaultTo(callback, passing());
90
21
  }
91
22
 
92
23
  function runLazyRule(lazyRule, currentValue) {
@@ -99,38 +30,24 @@ function runLazyRule(lazyRule, currentValue) {
99
30
  }
100
31
 
101
32
  /* 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
- }
33
+ function compose(...composites) {
34
+ return vestUtils.assign((value) => {
35
+ const res = run(value);
36
+ vestUtils.invariant(res.pass, vestUtils.StringObject(res.message));
118
37
  }, {
119
- run: run,
120
- test: function (value) { return run(value).pass; }
38
+ run,
39
+ test: (value) => run(value).pass,
121
40
  });
122
41
  function run(value) {
123
- return n4s.ctx.run({ value: value }, function () {
124
- return defaultToPassing(mapFirst(composites, function (composite, breakout) {
42
+ return n4s.ctx.run({ value }, () => {
43
+ return defaultToPassing(vestUtils.mapFirst(composites, (composite, breakout) => {
125
44
  /* HACK: Just a small white lie. ~~HELP WANTED~~.
126
- The ideal is that instead of `TLazyRuleRunners` We would simply use `TLazy` to begin with.
45
+ The ideal is that instead of `LazyRuleRunners` We would simply use `Lazy` to begin with.
127
46
  The problem is that lazy rules can't really be passed to this function due to some generic hell
128
47
  so we're limiting it to a small set of functions.
129
48
  */
130
- var res = runLazyRule(composite, value);
131
- if (!res.pass) {
132
- breakout(res);
133
- }
49
+ const res = runLazyRule(composite, value);
50
+ breakout(!res.pass, res);
134
51
  }));
135
52
  });
136
53
  }
@@ -1 +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}})};
1
+ "use strict";var t=require("n4s"),n=require("vest-utils");function r(t,n){const r={pass:t};return n&&(r.message=n),r}function s(t){return n.defaultTo(t,r(!0))}function e(t,n){try{return t.run(n)}catch(t){return r(!1)}}module.exports=function(...r){return n.assign((t=>{const r=u(t);n.invariant(r.pass,n.StringObject(r.message))}),{run:u,test:t=>u(t).pass});function u(u){return t.ctx.run({value:u},(()=>s(n.mapFirst(r,((t,n)=>{const r=e(t,u);n(!r.pass,r)})))))}};
@@ -1,41 +1,10 @@
1
1
  'use strict';
2
2
 
3
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
- }
4
+ var vestUtils = require('vest-utils');
36
5
 
37
6
  function ruleReturn(pass, message) {
38
- var output = { pass: pass };
7
+ const output = { pass };
39
8
  if (message) {
40
9
  output.message = message;
41
10
  }
@@ -48,10 +17,10 @@ function passing() {
48
17
  return ruleReturn(true);
49
18
  }
50
19
  function defaultToFailing(callback) {
51
- return defaultTo(callback, failing());
20
+ return vestUtils.defaultTo(callback, failing());
52
21
  }
53
22
  function defaultToPassing(callback) {
54
- return defaultTo(callback, passing());
23
+ return vestUtils.defaultTo(callback, passing());
55
24
  }
56
25
 
57
26
  function runLazyRule(lazyRule, currentValue) {
@@ -63,70 +32,45 @@ function runLazyRule(lazyRule, currentValue) {
63
32
  }
64
33
  }
65
34
 
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
- }
35
+ function allOf(value, ...rules) {
36
+ return defaultToPassing(vestUtils.mapFirst(rules, (rule, breakout) => {
37
+ const res = runLazyRule(rule, value);
38
+ breakout(!res.pass, res);
76
39
  }));
77
40
  }
78
41
 
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
- }
42
+ function anyOf(value, ...rules) {
43
+ return defaultToFailing(vestUtils.mapFirst(rules, (rule, breakout) => {
44
+ const res = runLazyRule(rule, value);
45
+ breakout(res.pass, res);
89
46
  }));
90
47
  }
91
48
 
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
- }
49
+ function noneOf(value, ...rules) {
50
+ return defaultToPassing(vestUtils.mapFirst(rules, (rule, breakout) => {
51
+ const res = runLazyRule(rule, value);
52
+ breakout(res.pass, failing());
102
53
  }));
103
54
  }
104
55
 
105
- function lengthEquals(value, arg1) {
106
- return value.length === Number(arg1);
56
+ function equals(value, arg1) {
57
+ return value === arg1;
107
58
  }
59
+ vestUtils.bindNot(equals);
108
60
 
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);
61
+ const REQUIRED_COUNT = 1;
62
+ function oneOf(value, ...rules) {
63
+ let passingCount = 0;
64
+ rules.some(rule => {
65
+ const res = runLazyRule(rule, value);
125
66
  if (res.pass) {
126
- passing.push(res);
67
+ passingCount++;
68
+ }
69
+ if (vestUtils.greaterThan(passingCount, REQUIRED_COUNT)) {
70
+ return false;
127
71
  }
128
72
  });
129
- return ruleReturn(lengthEquals(passing, REQUIRED_COUNT));
73
+ return ruleReturn(equals(passingCount, REQUIRED_COUNT));
130
74
  }
131
75
 
132
- n4s.enforce.extend({ allOf: allOf, anyOf: anyOf, noneOf: noneOf, oneOf: oneOf });
76
+ n4s.enforce.extend({ allOf, anyOf, noneOf, oneOf });
@@ -1 +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))}});
1
+ "use strict";var n=require("n4s"),t=require("vest-utils");function r(n,t){const r={pass:n};return t&&(r.message=t),r}function e(){return r(!1)}function u(n){return t.defaultTo(n,r(!0))}function s(n,t){try{return n.run(t)}catch(n){return e()}}function o(n,t){return n===t}t.bindNot(o);n.enforce.extend({allOf:function(n,...r){return u(t.mapFirst(r,((t,r)=>{const e=s(t,n);r(!e.pass,e)})))},anyOf:function(n,...r){return u=t.mapFirst(r,((t,r)=>{const e=s(t,n);r(e.pass,e)})),t.defaultTo(u,e());var u},noneOf:function(n,...r){return u(t.mapFirst(r,((t,r)=>{r(s(t,n).pass,e())})))},oneOf:function(n,...e){let u=0;return e.some((r=>{if(s(r,n).pass&&u++,t.greaterThan(u,1))return!1})),r(o(u,1))}});