UNPKG

@wenn/onb

Version:

onb-core

298 lines (272 loc) 8.02 kB
import MESSAGES from './messages'; import AREA_CODES from '../constants/AREA_CODES'; import { formatMoney } from '../index.js'; import { AR, UY, MX, ES } from '../constants/COUNTRIES'; /** * @function [required] * @description Prevents the user from submitting empty fields * @param value */ export const required = value => value && value !== '0' ? undefined : MESSAGES.REQUIRED; /** * @function [verifyDigit] * @description Verification for Uruguay's national ID. * @param value */ export const verifyDigit = value => { const digits = [2, 9, 8, 7, 6, 3, 4]; const valuesNumber = value.split('').filter(v => v !== '-'); valuesNumber.splice(-1, 1); const sum = valuesNumber.reduce( (acum, val, index) => acum + val * digits[index], 0 ); const finalDigit = sum % 10 === 0 ? 0 : (10 - (sum % 10)) % 10; return finalDigit.toString() === value.charAt(value.length - 1) ? undefined : MESSAGES.NATIONAL_ID_UY; }; /** * @function [CBUValidation] * @description Validates if the CBU value is the same as the bank's ID. * @param CBU, bankValue */ export const CBUValidation = (CBU, bankValue) => CBU.substring(0, 3) === bankValue ? undefined : MESSAGES.INVALID_CBU; /** * @function [notStartNumber] * @description String must not start with any number from the received string. * @param number */ export const notStartNumber = number => value => value && number.includes(value[0]) ? MESSAGES.notStartNumber(value[0]) : undefined; /** * @function [startNumber] * @description String must start with any number from the received string. * @param number */ export const startNumber = number => value => value && number.includes(value[0]) ? undefined : MESSAGES.START_NUMBER; /** * @function [notEqualPhone] * @description Compares the value of two fields and validates if both have the same value. * @param number */ export const notEqualPhone = number => primary => number === primary ? MESSAGES.IS_EQUAL_NUMBER : undefined; /** * @function [differentPhone] * @description Compares the value of two fields and validates if both have the same value. * @param number */ export const differentPhone = number => primary => number === primary ? MESSAGES.IS_EQUAL_NUMBER : undefined; /** * @function [requiredLength] * @description Input value must have a required length. * @param max */ export const requiredLength = max => (value, subfix) => value && value.length !== max ? MESSAGES.requiredLength(max, subfix) : undefined; /** * @function [maxLength] * @description Validates the max length of a string. * @param max */ export const maxLength = max => (value, subfix) => value && value.length > max ? MESSAGES.maxLength(max, subfix) : undefined; /** * @function [alphabetic] * @description Checks if value is an alphabetic string. * @param value */ export const alphabetic = value => value && !/^[A-Z ]+$/i.test(value) ? MESSAGES.IS_ALPHABETIC : undefined; /** * @function [minLength] * @description Email validation * @param min */ export const minLength = min => value => value && value.length < min ? MESSAGES.minLength(min) : undefined; /** * @function [number] * @description Validates if the input value is a number. * @param value */ export const number = value => value && !/^(\d)+$/.test(value) ? MESSAGES.IS_NUMBER : undefined; /** * @function [minValue] * @description Email validation * @param min */ export const minValue = min => value => value && value < min ? MESSAGES.minValue(formatMoney(min)) : undefined; /** * @function [maxValue] * @description Email validation * @param max */ export const maxValue = max => value => value && value > max ? MESSAGES.maxValue(formatMoney(max)) : undefined; /* eslint-disable */ /** * @function [email] * @description Email validation * @param value */ export const email = (value) => value && !/^(([^áéíóúñàèìòù²<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/gi.test( value ) ? MESSAGES.IS_EMAIL : undefined; /* eslint-disable */ /** * @function [validAreaCode] * @description Telephone area code validation * @param value * @param country */ export const validAreaCode = (value, country) => { switch(country){ case AR: return AREA_CODES.indexOf(value) < 0 ? MESSAGES.AREA_CODE_MATCH_AR : undefined; case MX: return value !== '52' ? 'Código inválido' : undefined; default: break; } }; /** * @function [phoneNumber] * @description Phone number validation for different countries * @param value * @param country */ export const phoneNumber = (value, country) => { switch (country) { case AR: return value && !/^9[1-9]{1}[0-9]{6}/i.test(value) ? MESSAGES.PHONE_MATCH : undefined; case UY: return value && !/^9[1-9]{1}[0-9]{6}/i.test(value) ? MESSAGES.PHONE_MATCH : undefined; case MX: return value && !/[0-9]{8}/i.test(value) ? MESSAGES.PHONE_MATCH : undefined; case ES: return value && !/[0-9]{8}$/.test(value) ? MESSAGES.PHONE_MATCH : undefined; default: return value && !/^(0|[1-9][0-9]{9})$/i.test(value) ? MESSAGES.PHONE_MATCH : undefined; } }; /** * @function [postalCode] * @description Postal Code validation for different countries * @param value * @param country */ export const postalCode = (value, country) => { switch (country) { case UY: return value && !/^(\d{5})$/.test(value) ? MESSAGES.POSTAL_CODE : undefined; case MX: return value && !/^(\d{5})$/.test(value) ? MESSAGES.POSTAL_CODE : undefined; case ES: return value && !/^(?:0[1-9]|[1-4]\d|5[0-2])\d{3}$/.test(value) ? MESSAGES.POSTAL_CODE : undefined; case AR: default: return value && !/^(\d{4})$/.test(value) ? MESSAGES.POSTAL_CODE : undefined; } }; /** * @function [getDate] * @description Validates the age of the user. Must be between 18 and 65. * @param minusYears */ const getDate = (minusYears) => { let d = new Date(); d.setFullYear(d.getFullYear() - minusYears); return d; }; const maxDate = getDate(18); const minDate = getDate(65); /** * @function [validateBirthday] * @description CURP(Clave Única de Registro de Población) validation for MX. * @param value */ export const validateBirthday = (value) => { try { let valueDate = new Date(value + ' 00:00:00'); if (valueDate < minDate || valueDate > maxDate) { return MESSAGES.MIN_MAX_AGE; } } catch(e) { return MESSAGES.REQUIRED; } }; // ESPAÑA /** * @function [validateNif] * @description NIF(Número de Identificación Fiscal) validation for ES. * NIF is for residents and NIE is for foreigners, which can't take loans. * @param value */ export const validateNif = (value) => value && !/^([a-z]|[A-Z]|[0-9])[0-9]{7}([a-z]|[A-Z]|[0-9])/i.test(value) ? MESSAGES.INVALID_NIF : undefined; // MEXICO /** * @function [validateCURP] * @description CURP(Clave Única de Registro de Población) validation for MX. * Each one is an alphanumeric 18-character string. * @param value */ export const validateCURP = value => { const regex = /^([A-Z][AEIOUX][A-Z]{2}\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])[HM](?:AS|B[CS]|C[CLMSH]|D[FG]|G[TR]|HG|JC|M[CNS]|N[ETL]|OC|PL|Q[TR]|S[PLR]|T[CSL]|VZ|YN|ZS)[B-DF-HJ-NP-TV-Z]{3}[A-Z\d])(\d)$/; return regex.test(value) ? undefined : MESSAGES.INVALID_CURP; }; // ???? export const validCityCode = (value, country, cityCode) => { switch(country){ case MX: return !cityCode.includes(Number(value)) ? 'Código inválido' : undefined; } };