40 lines
1.0 KiB
TypeScript
40 lines
1.0 KiB
TypeScript
|
|
|
|
export interface RegisterUserDto {
|
|
name: string;
|
|
email: string;
|
|
password: string;
|
|
}
|
|
|
|
export class RegisterUserDtoValidator {
|
|
static validate(data: unknown): RegisterUserDto {
|
|
if (typeof data !== "object" || data === null) {
|
|
throw new Error("Invalid data");
|
|
}
|
|
|
|
const obj = data as Record<string, unknown>;
|
|
|
|
if (typeof obj.name !== "string" || obj.name.trim().length === 0) {
|
|
throw new Error("Name is required and must be a non-empty string");
|
|
}
|
|
|
|
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 < 6) {
|
|
throw new Error("Password is required and must be at least 6 characters");
|
|
}
|
|
|
|
return {
|
|
name: obj.name.trim(),
|
|
email: obj.email.trim().toLowerCase(),
|
|
password: obj.password,
|
|
};
|
|
}
|
|
|
|
private static isValidEmail(email: string): boolean {
|
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
return emailRegex.test(email);
|
|
}
|
|
} |