Files
bflows-bandi-fe/src/helpers/validators.js
2025-09-12 10:06:33 +02:00

97 lines
4.3 KiB
JavaScript

import validate from 'validate.js';
import { match, isEmpty, is, pathOr, isNil } from 'ramda';
import CodiceFiscale from 'codice-fiscale-js';
export const isPIVA = (v) => {
const regexp = new RegExp(/^[0-9]{11}$/);
//console.log('isPIVA', !isEmpty(match(regexp, String(v))))
return v && !isEmpty(v) && !isEmpty(match(regexp, String(v)));
}
// example: DSDDSD99P23B352G
export const isCodiceFiscale = (v) => {
return v && !isEmpty(v) && CodiceFiscale.check(v);
}
export const isCAP = (v) => {
const regexp = new RegExp(/^[0-9]{5}$/);
return v && !isEmpty(v) && !isEmpty(match(regexp, String(v)));
}
export const isIBAN = (v) => {
const regexp = new RegExp(/^((NO)[0-9A-Z]{2}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{3}|(NO)[0-9A-Z]{13}|(BE)[0-9A-Z]{2}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}|(BE)[0-9A-Z]{14}|(DK|FO|FI|GL|NL)[0-9A-Z]{2}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{2}|(DK|FO|FI|GL|NL)[0-9A-Z]{16}|(MK|SI)[0-9A-Z]{2}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{3}|(MK|SI)[0-9A-Z]{17}|(BA|EE|KZ|LT|LU|AT)[0-9A-Z]{2}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}|(BA|EE|KZ|LT|LU|AT)[0-9A-Z]{18}|(HR|LI|LV|CH)[0-9A-Z]{2}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{1}|(HR|LI|LV|CH)[0-9A-Z]{19}|(BG|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z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{2}|(MU)[0-9A-Z]{28}|(MT)[0-9A-Z]{2}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{4}[ ][0-9A-Z]{3}|(MT)[0-9A-Z]{29})$/)
return v && !isEmpty(v) && !isEmpty(match(regexp, v));
}
export const isEmail = (v) => {
const constraints = {
from: {
email: true
}
};
const res = validate({from: v}, constraints);
return !res;
}
export const isEmailPEC = (v) => {
const constraints = {
from: {
email: true
}
};
const res = validate({from: v}, constraints);
return !res;
}
export const isUrl = (v) => {
const constraints = {
from: {
url: true
}
};
const res = validate({from: v}, constraints);
return !res;
}
export const isMarcaDaBollo = (v) => {
const regexp = new RegExp(/^[0-9]{14}$/);
return v && !isEmpty(match(regexp, String(v)));
}
export const minChecks = (v, num) => {
return is(Array, v) ? v.length >= parseInt(num) : false;
}
export const maxChecks = (v, num) => {
return is(Array, v) ? v.length <= parseInt(num) : false;
}
export const nonEmptyTables = (v = [], tableCfg = []) => {
const colsCfg = pathOr([], ['stateFieldData'], tableCfg);
const nonPredefinedCells = colsCfg
.filter(o => !o.predefined)
.map(o => o.name);
let isTableValid = true;
let atLeastOneCellFilled = false
if (is(Array, v)) {
// eslint-disable-next-line array-callback-return
v.map((row) => {
if (isEmpty(row)) {
isTableValid = false;
} else {
// eslint-disable-next-line array-callback-return
nonPredefinedCells.map((k) => {
if (isNil(row[k]) || isEmpty(row[k])) {
isTableValid = atLeastOneCellFilled;
} else {
atLeastOneCellFilled = true;
isTableValid = true;
}
});
}
});
}
return is(Array, v) ? v.length >= 1 && isTableValid : false;
}