38 lines
932 B
TypeScript
38 lines
932 B
TypeScript
/**
|
|
* login-user.dto.ts
|
|
* DTO para iniciar sesión.
|
|
* Define qué datos esperamos al hacer login.
|
|
*/
|
|
|
|
export interface LoginUserDto {
|
|
email: string;
|
|
password: string;
|
|
}
|
|
|
|
export class LoginUserDtoValidator {
|
|
static validate(data: unknown): LoginUserDto {
|
|
if (typeof data !== "object" || data === null) {
|
|
throw new Error("Invalid data");
|
|
}
|
|
|
|
const obj = data as Record<string, unknown>;
|
|
|
|
if (typeof obj.email !== "string" || !this.isValidEmail(obj.email)) {
|
|
throw new Error("Email is required and must be valid");
|
|
}
|
|
|
|
if (typeof obj.password !== "string" || obj.password.length === 0) {
|
|
throw new Error("Password is required");
|
|
}
|
|
|
|
return {
|
|
email: obj.email.trim().toLowerCase(),
|
|
password: obj.password,
|
|
};
|
|
}
|
|
|
|
private static isValidEmail(email: string): boolean {
|
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
return emailRegex.test(email);
|
|
}
|
|
} |