Skip to content

Commit

Permalink
order errors b00tc4mp#220
Browse files Browse the repository at this point in the history
  • Loading branch information
venturars committed Oct 28, 2024
1 parent e507e8e commit d8ffcc1
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import classNames from "classnames";
import ES from "../locales/es.json";

function FormErrorsSection({ className, errors }) {
errors?.sort((a, b) => a.order - b.order);
debugger;
return (
<>
{errors instanceof Array && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ function LoginForm({ className, onSubmit }) {

if (!Validator.password(inputPassword.value)) {
newErrors.push(new PasswordNotValidError("Password is not valid"));
newErrors[newErrors.length - 1].order = 2;
inputPassword.focus();
}

if (!Validator.email(inputEmail.value)) {
newErrors.push(new EmailNotValidError("Email is not valid"));
newErrors[newErrors.length - 1].order = 1;
inputEmail.focus();
}

Expand Down
15 changes: 10 additions & 5 deletions staff/ventura-rodriguez/social-app/src/components/SignupForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ES from "../locales/es.json";
import { Validator } from "../tools";
import {
BadRequestError,
DateOfBirthNotValidError,
EmailNotValidError,
PasswordNotValidError,
ServerError,
Expand All @@ -28,14 +29,14 @@ function SignupForm({ className, onSubmit }) {
password: inputPassword,
repeatPassword: inputRepeatPassword,
} = event.target;
debugger;

const newErrors = [];

if (!(inputPassword.value === inputRepeatPassword.value)) {
newErrors.push(
new PasswordNotValidError("Password and repeatPassword do not match")
);
newErrors[newErrors.length - 1].order = 4;
inputPassword.value = "";
inputRepeatPassword.value = "";
inputPassword.focus();
Expand All @@ -46,21 +47,25 @@ function SignupForm({ className, onSubmit }) {
!Validator.password(inputPassword.value)
) {
newErrors.push(new PasswordNotValidError("Password is not valid"));
newErrors[newErrors.length - 1].order = 4;
inputPassword.focus();
}

if (!Validator.email(inputEmail.value)) {
newErrors.push(new EmailNotValidError("Email is not valid"));
newErrors[newErrors.length - 1].order = 3;
inputEmail.focus();
}

// if (!Validator.username(inputUsername.value)) {
// newErrors.push(new UsernameNotValidError("Username is not valid"));
// inputPassword.focus();
// }
if (!Validator.dateOfBirth(inputDateOfBirth.value)) {
newErrors.push(new DateOfBirthNotValidError("DateOfBirth is not valid"));
newErrors[newErrors.length - 1].order = 2;
// inputDateOfBirth.focus();
}

if (!Validator.username(inputUsername.value)) {
newErrors.push(new UsernameNotValidError("Username is not valid"));
newErrors[newErrors.length - 1].order = 1;
inputUsername.focus();
}

Expand Down
3 changes: 2 additions & 1 deletion staff/ventura-rodriguez/social-app/src/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"BadRequestError": "Los datos introducidos no son correctos",
"ServerError": "Ha habido un problema inesperado, prueba de nuevo más tarde",
"UnexpectedError": "Ha ocurrido un error inesperado, por favor, póngase en contacto con el equipo de soporte",
"UsernameNotValidError": "El nombre de usuario no es válido"
"UsernameNotValidError": "El nombre de usuario no es válido",
"DateOfBirthNotValidError": "La fecha de nacimiento no es válida"
},
"loginForm": {
"title": "Bienvenido de vuelta",
Expand Down
5 changes: 3 additions & 2 deletions staff/ventura-rodriguez/social-app/src/logic/registerUser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Validator } from "../tools";
import {
BadRequestError,
DateOfBirthNotValidError,
EmailNotValidError,
PasswordNotValidError,
ServerError,
Expand All @@ -21,8 +22,8 @@ const registerUser = ({
throw new PasswordNotValidError("Password is not valid");
if (!Validator.email(email))
throw new EmailNotValidError("Email is not valid");
// if (!Validator.email(email))
// throw new EmailNotValidError("Email is not valid");
if (!Validator.dateOfBirth(dateOfBirth))
throw new DateOfBirthNotValidError("DateOfBirth is not valid");
if (!Validator.username(username))
throw new UsernameNotValidError("Username is not valid");

Expand Down
9 changes: 9 additions & 0 deletions staff/ventura-rodriguez/social-app/src/tools/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,12 @@ export class UsernameNotValidError extends Error {
}
}
}

export class DateOfBirthNotValidError extends Error {
constructor(message) {
super(message);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, DateOfBirthNotValidError);
}
}
}
18 changes: 18 additions & 0 deletions staff/ventura-rodriguez/social-app/src/tools/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@ class Validator {

return regExp.test(value);
}

static dateOfBirth(value) {
const dateOfBirthRegExp =
/^(0[1-9]|1[0-2])\/(0[1-9]|1[0-9]|2[0-9]|3[01])\/\d{4}$/;

if (!dateOfBirthRegExp.test(value)) return false; // Invalid format

const [day, month, year] = value.split("/").map(Number);
const today = new Date();
const birthDate = new Date(year, month - 1, day); // Months are 0-indexed

const age = today.getFullYear() - birthDate.getFullYear();
const monthDiff = today.getMonth() - birthDate.getMonth();

if (age < 18 || (age === 18 && monthDiff < 0)) return false; // Not 18 years old yet

return true;
}
}

export default Validator;

0 comments on commit d8ffcc1

Please sign in to comment.