Skip to content

Commit

Permalink
refactor: centralização das regras de validação de senha devhatt#287 (d…
Browse files Browse the repository at this point in the history
  • Loading branch information
EdiltonOliveira authored Aug 22, 2024
1 parent 12e425f commit a20d5d9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 21 deletions.
16 changes: 4 additions & 12 deletions src/components/ChangePassword/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component } from 'pet-dex-utilities';
import { isPasswordValid } from '../../utils/validations';
import TextInput from '../TextInput';
import Button from '../Button';
import './index.scss';
Expand Down Expand Up @@ -28,22 +29,13 @@ const html = `
<span data-select="confirm-password-error-match" class="change-password__error">As senhas não coincidem</span>
<ul class="change-password__tips">
<li>Insira no mínimo 6 caracteres</li>
<li>Insira no mínimo 10 caracteres</li>
<li>A senha deve conter uma letra maiúscula</li>
<li>Deve conter um caractere especial</li>
</ul>
</form>
`;

const validatePassword = (password) => {
const hasMinLength = password.length >= 10;
const hasUppercase = /[A-Z]/g.test(password);
const hasNumber = /[0-9]/g.test(password);
const hasSpecialCharacter = /[!@#$%^&*(),.?":{}|<>]/g.test(password);

return hasMinLength && hasUppercase && hasNumber && hasSpecialCharacter;
};

export default function ChangePassword() {
Component.call(this, { html, events });
const $changePasswordForm = this.selected.get('change-password');
Expand Down Expand Up @@ -123,8 +115,8 @@ export default function ChangePassword() {
confirmPasswordInput.selected.get('input-text').value;

const showErrorMessage = (field, error) => {
const fieldValue = field.selected.get('input-text').value;
if (!validatePassword(fieldValue)) {
const password = field.selected.get('input-text').value;
if (!isPasswordValid(password)) {
validPasswords = false;
error.classList.add('show-error');
field.inputError();
Expand Down
12 changes: 3 additions & 9 deletions src/components/LoginForm/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Component } from 'pet-dex-utilities';
import { isPasswordValid } from '../../utils/validations';
import TextInput from '../TextInput';
import Button from '../Button';
import Toggle from '../Toggle';
Expand Down Expand Up @@ -112,7 +113,7 @@ export default function LoginForm() {
emailInput.inputError();
}

if (!this.validatePassword(password)) {
if (!this.isPasswordValid(password)) {
validPassword = false;
$passwordErrorMessage.classList.add('show-error');
$passwordErrorMessage.innerText =
Expand All @@ -138,12 +139,5 @@ LoginForm.prototype = Object.assign(LoginForm.prototype, Component.prototype, {

return emailRegex.test(email);
},
validatePassword(password) {
const hasMinLength = password.length >= 10;
const hasUppercase = /[A-Z]/g.test(password);
const hasNumber = /[0-9]/g.test(password);
const hasSpecialCharacter = /[!@#$%^&*(),.?":{}|<>]/g.test(password);

return hasMinLength && hasUppercase && hasNumber && hasSpecialCharacter;
},
isPasswordValid,
});

0 comments on commit a20d5d9

Please sign in to comment.