check-rule-mate 0.2.1 → 0.4.0
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/dist/main.cjs.js +1 -1
- package/package.json +1 -1
- package/src/js/dataValidate.js +18 -15
package/dist/main.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var h=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var R=Object.getOwnPropertyNames;var M=Object.prototype.hasOwnProperty;var O=(n,s)=>{for(var o in s)h(n,o,{get:s[o],enumerable:!0})},b=(n,s,o,i)=>{if(s&&typeof s=="object"||typeof s=="function")for(let u of R(s))!M.call(n,u)&&u!==o&&h(n,u,{get:()=>s[u],enumerable:!(i=w(s,u))||i.enumerable});return n};var q=n=>b(h({},"__esModule",{value:!0}),n);var P={};O(P,{dataValidate:()=>V});module.exports=q(P);function V(n,{validationHelpers:s={},rules:o,dataRule:i,dataErrorMessages:u={}}){let y={};function g(e,f){if(!e||typeof f!="string")return;let l=f.split("."),t=e;for(let c of l){if(t[c]===void 0)return;t=t[c]}return t}async function a(e,f=null){let{rule:l,required:t}=i[e.key];if((l&&t||!t&&e.value!="")&&l){let c=l.split("--")[0],r=l.split("--").length>1?l.split("--")[1]:"",m=E(e.value,o[c],r,s,f),{isValid:p,errorMessage:v,errorType:k}=await m.validate();return p||(y[e.key]={name:e.key,error:!0,errorMessage:g(u,v)||v,errorType:k}),p}return!0}async function d(){let e=Object.keys(n).map(f=>({key:f,value:n[f]}));if(e&&e.length>0){if(!Object.keys(i).every(r=>n.hasOwnProperty(r)))return{error:!0,errorMessage:"Missing properties"};if((await Promise.all([...e].map(async r=>await a(r,n)))).some(r=>!r))return{error:!0,dataErrors:y};let l=Object.keys(i).map(r=>({key:r,required:i[r].required})),t=e.map(r=>r.key);if(!l.filter(r=>r.required).map(r=>r.key).every(r=>t.includes(r)))return{error:!0}}else if(!e||e.length===0)return{error:!0,errorMessage:"Missing fields for dataRules"};return{ok:!0}}return d()}function E(n,s,o=null,i=null,u=null){async function y(a){let d,e;return{isValid:!(await Promise.all(a.validate.map(async t=>{let c=!0;if(a.params&&a.params[t]&&a.params[t].length>0){let r=a.params[t].map(p=>typeof p=="string"&&p[0]==="$"?p.substring(1,p.length):p);c=await this[t](...r)}else c=await this[t]();return!c&&!d&&(a!=null&&a.error[t])&&(d=a.error[t],e=t),c}))).some(t=>!t),errorMessage:d,errorType:e}}async function g(){if(i&&typeof i=="function"){let d=i(n,s,o,u);Object.keys(d).forEach(e=>{this[e]=d[e]})}return o?await y.call(this,s.modifier[o]):await y.call(this,s)}return{validate:g}}0&&(module.exports={dataValidate});
|
package/package.json
CHANGED
package/src/js/dataValidate.js
CHANGED
|
@@ -21,7 +21,7 @@ export function dataValidate(data, {validationHelpers = {}, rules, dataRule, dat
|
|
|
21
21
|
return current;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
function inputValidation(dataAttribute, data = null) {
|
|
24
|
+
async function inputValidation(dataAttribute, data = null) {
|
|
25
25
|
const { rule, required } = dataRule[dataAttribute.key];
|
|
26
26
|
|
|
27
27
|
if ((rule && required) || (!required && dataAttribute.value != '')) {
|
|
@@ -29,7 +29,7 @@ export function dataValidate(data, {validationHelpers = {}, rules, dataRule, dat
|
|
|
29
29
|
const INPUT_RULE = rule.split('--')[0];
|
|
30
30
|
const RULE_MODIFIER = rule.split('--').length > 1 ? rule.split('--')[1] : '';
|
|
31
31
|
const dataAttributeValidation = dataAttributeValidator(dataAttribute.value, rules[INPUT_RULE], RULE_MODIFIER, validationHelpers, data);
|
|
32
|
-
const { isValid, errorMessage, errorType } = dataAttributeValidation.validate();
|
|
32
|
+
const { isValid, errorMessage, errorType } = await dataAttributeValidation.validate();
|
|
33
33
|
if (!isValid) {
|
|
34
34
|
dataErrors[dataAttribute.key] = {
|
|
35
35
|
name: dataAttribute.key,
|
|
@@ -44,13 +44,13 @@ export function dataValidate(data, {validationHelpers = {}, rules, dataRule, dat
|
|
|
44
44
|
return true;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
function validate() {
|
|
47
|
+
async function validate() {
|
|
48
48
|
let dataArr = Object.keys(data).map((key) => ({ key, value: data[key] }));
|
|
49
49
|
if (dataArr && dataArr.length > 0) {
|
|
50
50
|
if(!Object.keys(dataRule).every((key) => data.hasOwnProperty(key))) {
|
|
51
51
|
return { error: true, errorMessage: "Missing properties"}
|
|
52
52
|
}
|
|
53
|
-
const dataValidators = [...dataArr].map((input) => inputValidation(input, data));
|
|
53
|
+
const dataValidators = await Promise.all([...dataArr].map(async (input) => await inputValidation(input, data)));
|
|
54
54
|
|
|
55
55
|
if (dataValidators.some((element) => !element)) {
|
|
56
56
|
return { error: true, dataErrors: dataErrors };
|
|
@@ -82,35 +82,38 @@ export function dataValidate(data, {validationHelpers = {}, rules, dataRule, dat
|
|
|
82
82
|
* @returns {{validate: Function}} the built function to validate all this parameters
|
|
83
83
|
*/
|
|
84
84
|
function dataAttributeValidator (value, rule, modifier = null, customValidation = null, data = null) {
|
|
85
|
-
function validateRules(rule) {
|
|
85
|
+
async function validateRules(rule) {
|
|
86
86
|
let errorMessage;
|
|
87
87
|
let errorType;
|
|
88
|
-
const
|
|
89
|
-
let
|
|
88
|
+
const validations = await Promise.all(rule.validate.map(async (validation) => {
|
|
89
|
+
let isValid = true;
|
|
90
90
|
if ((rule.params && rule.params[validation] && rule.params[validation].length > 0)) {
|
|
91
91
|
const params = rule.params[validation].map((param) => (typeof param === 'string' && param[0] === '$') ? param.substring(1, param.length) : param);
|
|
92
|
-
|
|
92
|
+
const validateResponse = await this[validation](...params);
|
|
93
|
+
isValid = validateResponse;
|
|
93
94
|
} else {
|
|
94
|
-
|
|
95
|
+
const validateResponse = await this[validation]();
|
|
96
|
+
isValid = validateResponse;
|
|
95
97
|
}
|
|
96
|
-
if (
|
|
98
|
+
if (!isValid && !errorMessage && rule?.error[validation]) {
|
|
97
99
|
errorMessage = rule.error[validation];
|
|
98
100
|
errorType = validation;
|
|
99
101
|
}
|
|
100
|
-
return
|
|
101
|
-
});
|
|
102
|
+
return isValid;
|
|
103
|
+
}));
|
|
104
|
+
const isValid = !validations.some((validation) => !validation);
|
|
102
105
|
return {isValid, errorMessage, errorType}
|
|
103
106
|
}
|
|
104
107
|
|
|
105
|
-
function validate() {
|
|
108
|
+
async function validate() {
|
|
106
109
|
if (customValidation && typeof customValidation === 'function') {
|
|
107
110
|
const validation = customValidation(value, rule, modifier, data);
|
|
108
111
|
Object.keys(validation).forEach((key) => {
|
|
109
112
|
this[key] = validation[key];
|
|
110
113
|
})
|
|
111
114
|
}
|
|
112
|
-
|
|
113
|
-
return
|
|
115
|
+
const response = modifier ? await validateRules.call(this, rule.modifier[modifier]) : await validateRules.call(this, rule);
|
|
116
|
+
return response;
|
|
114
117
|
}
|
|
115
118
|
|
|
116
119
|
return({
|