n4s 5.0.0-dev-781e21 → 5.0.0-dev-ec989a
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/README.md +2 -0
- package/dist/cjs/compose.development.js +9 -13
- package/dist/cjs/compose.production.js +1 -1
- package/dist/cjs/compounds.development.js +16 -32
- package/dist/cjs/compounds.production.js +1 -1
- package/dist/cjs/n4s.development.js +137 -213
- package/dist/cjs/n4s.production.js +1 -1
- package/dist/cjs/schema.development.js +13 -22
- package/dist/cjs/schema.production.js +1 -1
- package/dist/es/compose.development.js +11 -15
- package/dist/es/compose.production.js +1 -1
- package/dist/es/compounds.development.js +16 -32
- package/dist/es/compounds.production.js +1 -1
- package/dist/es/n4s.development.js +158 -232
- package/dist/es/n4s.production.js +1 -1
- package/dist/es/schema.development.js +13 -20
- package/dist/es/schema.production.js +1 -1
- package/dist/umd/compose.development.js +12 -16
- package/dist/umd/compose.production.js +1 -1
- package/dist/umd/compounds.development.js +19 -35
- package/dist/umd/compounds.production.js +1 -1
- package/dist/umd/n4s.development.js +140 -216
- package/dist/umd/n4s.production.js +1 -1
- package/dist/umd/schema.development.js +16 -25
- package/dist/umd/schema.production.js +1 -1
- package/package.json +62 -55
- package/testUtils/TEnforceMock.ts +3 -0
- package/types/compose.d.ts +17 -21
- package/types/compose.d.ts.map +1 -0
- package/types/compounds.d.ts +17 -22
- package/types/compounds.d.ts.map +1 -0
- package/types/n4s.d.ts +22 -22
- package/types/n4s.d.ts.map +1 -0
- package/types/schema.d.ts +17 -21
- package/types/schema.d.ts.map +1 -0
- package/tsconfig.json +0 -8
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Enforce - n4s
|
|
2
2
|
|
|
3
|
+
[](https://discord.gg/WmADZpJnSe) [](https://www.npmjs.com/package/n4s) [](https://www.npmjs.com/package/n4s) [](https://bundlephobia.com/package/n4s) [](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).
|
|
@@ -4,7 +4,7 @@ var n4s = require('n4s');
|
|
|
4
4
|
var vestUtils = require('vest-utils');
|
|
5
5
|
|
|
6
6
|
function ruleReturn(pass, message) {
|
|
7
|
-
|
|
7
|
+
const output = { pass };
|
|
8
8
|
if (message) {
|
|
9
9
|
output.message = message;
|
|
10
10
|
}
|
|
@@ -30,27 +30,23 @@ function runLazyRule(lazyRule, currentValue) {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
/* eslint-disable max-lines-per-function */
|
|
33
|
-
function compose() {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
composites[_i] = arguments[_i];
|
|
37
|
-
}
|
|
38
|
-
return vestUtils.assign(function (value) {
|
|
39
|
-
var res = run(value);
|
|
33
|
+
function compose(...composites) {
|
|
34
|
+
return vestUtils.assign((value) => {
|
|
35
|
+
const res = run(value);
|
|
40
36
|
vestUtils.invariant(res.pass, vestUtils.StringObject(res.message));
|
|
41
37
|
}, {
|
|
42
|
-
run
|
|
43
|
-
test:
|
|
38
|
+
run,
|
|
39
|
+
test: (value) => run(value).pass,
|
|
44
40
|
});
|
|
45
41
|
function run(value) {
|
|
46
|
-
return n4s.ctx.run({ value
|
|
47
|
-
return defaultToPassing(vestUtils.mapFirst(composites,
|
|
42
|
+
return n4s.ctx.run({ value }, () => {
|
|
43
|
+
return defaultToPassing(vestUtils.mapFirst(composites, (composite, breakout) => {
|
|
48
44
|
/* HACK: Just a small white lie. ~~HELP WANTED~~.
|
|
49
45
|
The ideal is that instead of `LazyRuleRunners` We would simply use `Lazy` to begin with.
|
|
50
46
|
The problem is that lazy rules can't really be passed to this function due to some generic hell
|
|
51
47
|
so we're limiting it to a small set of functions.
|
|
52
48
|
*/
|
|
53
|
-
|
|
49
|
+
const res = runLazyRule(composite, value);
|
|
54
50
|
breakout(!res.pass, res);
|
|
55
51
|
}));
|
|
56
52
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
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)})))))}};
|
|
@@ -4,7 +4,7 @@ var n4s = require('n4s');
|
|
|
4
4
|
var vestUtils = require('vest-utils');
|
|
5
5
|
|
|
6
6
|
function ruleReturn(pass, message) {
|
|
7
|
-
|
|
7
|
+
const output = { pass };
|
|
8
8
|
if (message) {
|
|
9
9
|
output.message = message;
|
|
10
10
|
}
|
|
@@ -32,35 +32,23 @@ function runLazyRule(lazyRule, currentValue) {
|
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
function allOf(value) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
rules[_i - 1] = arguments[_i];
|
|
39
|
-
}
|
|
40
|
-
return defaultToPassing(vestUtils.mapFirst(rules, function (rule, breakout) {
|
|
41
|
-
var res = runLazyRule(rule, value);
|
|
35
|
+
function allOf(value, ...rules) {
|
|
36
|
+
return defaultToPassing(vestUtils.mapFirst(rules, (rule, breakout) => {
|
|
37
|
+
const res = runLazyRule(rule, value);
|
|
42
38
|
breakout(!res.pass, res);
|
|
43
39
|
}));
|
|
44
40
|
}
|
|
45
41
|
|
|
46
|
-
function anyOf(value) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
rules[_i - 1] = arguments[_i];
|
|
50
|
-
}
|
|
51
|
-
return defaultToFailing(vestUtils.mapFirst(rules, function (rule, breakout) {
|
|
52
|
-
var res = runLazyRule(rule, value);
|
|
42
|
+
function anyOf(value, ...rules) {
|
|
43
|
+
return defaultToFailing(vestUtils.mapFirst(rules, (rule, breakout) => {
|
|
44
|
+
const res = runLazyRule(rule, value);
|
|
53
45
|
breakout(res.pass, res);
|
|
54
46
|
}));
|
|
55
47
|
}
|
|
56
48
|
|
|
57
|
-
function noneOf(value) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
rules[_i - 1] = arguments[_i];
|
|
61
|
-
}
|
|
62
|
-
return defaultToPassing(vestUtils.mapFirst(rules, function (rule, breakout) {
|
|
63
|
-
var res = runLazyRule(rule, value);
|
|
49
|
+
function noneOf(value, ...rules) {
|
|
50
|
+
return defaultToPassing(vestUtils.mapFirst(rules, (rule, breakout) => {
|
|
51
|
+
const res = runLazyRule(rule, value);
|
|
64
52
|
breakout(res.pass, failing());
|
|
65
53
|
}));
|
|
66
54
|
}
|
|
@@ -70,15 +58,11 @@ function equals(value, arg1) {
|
|
|
70
58
|
}
|
|
71
59
|
vestUtils.bindNot(equals);
|
|
72
60
|
|
|
73
|
-
|
|
74
|
-
function oneOf(value) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
var passingCount = 0;
|
|
80
|
-
rules.some(function (rule) {
|
|
81
|
-
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);
|
|
82
66
|
if (res.pass) {
|
|
83
67
|
passingCount++;
|
|
84
68
|
}
|
|
@@ -89,4 +73,4 @@ function oneOf(value) {
|
|
|
89
73
|
return ruleReturn(equals(passingCount, REQUIRED_COUNT));
|
|
90
74
|
}
|
|
91
75
|
|
|
92
|
-
n4s.enforce.extend({ allOf
|
|
76
|
+
n4s.enforce.extend({ allOf, anyOf, noneOf, oneOf });
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var n=require("n4s"),
|
|
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))}});
|