UNPKG

hannah-password-rules

Version:

Make validations defined from dashboard of Platform

74 lines (73 loc) 3.26 kB
// src/functions.ts function passwordRules(password, validation, props = {}) { function escapeRegex(expresion) { return expresion.replace(/[/\-\\^$*+?.()|[\]{}]/g, "\\$&"); } function prepareStringForValidation(word) { return word.replaceAll(" ", ""); } const validations = () => ({ max_length: () => password.trim().length <= validation.rules.max_length?.value, min_length: () => password.trim().length >= validation.rules.min_length?.value, mayus_quantity: () => validation.rules.mayus_quantity?.required === false || new RegExp( `(?=(.*[A-Z]){${escapeRegex(`${validation.rules.mayus_quantity?.value}`)}})`, "g" ).test(password), minus_quantity: () => validation.rules.minus_quantity?.required === false || new RegExp( `(?=(.*[a-z]){${escapeRegex(`${validation.rules.minus_quantity?.value}`)}})`, "g" ).test(password), number_quantity: () => validation.rules.number_quantity?.required === false || new RegExp( `(?=(.*[0-9]){${escapeRegex(`${validation.rules.number_quantity?.value}`)}})`, "g" ).test(password), special_char_quantity: () => validation.rules.special_char_quantity?.required === false || new RegExp( `(?=(.*[*.!@$%&#:,.?_+-]){${escapeRegex(`${validation.rules.special_char_quantity?.value}`)}})`, "g" ).test(password), with_disallowed_words: () => { const trueValues = validation.words.map( (word) => prepareStringForValidation(password.toLowerCase()).indexOf(word.toLocaleLowerCase()) >= 0 ).filter((value) => value === true); return validation.rules.with_disallowed_words?.required === false || !trueValues.length; }, with_disallowed_user_name: () => { const words = props.name?.split(" ") ?? []; const trueValues = words.map( (word) => prepareStringForValidation(password.toLowerCase()).indexOf(word.toLocaleLowerCase()) >= 0 ).filter((value) => value === true); console.log("TRUE NAME", trueValues); return validation.rules.with_disallowed_user_name?.required === false || props.name !== void 0 && !trueValues.length; }, with_disallowed_user_email: () => { const words = props.email?.split("@") ?? []; const trueValues = words.map( (word) => prepareStringForValidation(password.toLowerCase()).indexOf(word.toLocaleLowerCase()) >= 0 ).filter((value) => value === true); return validation.rules.with_disallowed_user_email?.required === false || props.email !== void 0 && !trueValues.length; }, with_disallowed_user_phone: () => { const words = props.phone?.split("@") ?? []; const trueValues = words.map( (word) => prepareStringForValidation(password.toLowerCase()).indexOf(word.toLocaleLowerCase()) >= 0 ).filter((value) => value === true); return validation.rules.with_disallowed_user_phone?.required === false || props.phone !== void 0 && !trueValues.length; } }); const test = function() { const results = {}; Object.keys(validation.rules).forEach((key) => { console.log(key); results[key] = validations()[key](); console.log(results[key]); }); return results; }; return { validations, test }; } export { passwordRules };