56 lines
1.2 KiB
TypeScript
56 lines
1.2 KiB
TypeScript
import { Result } from "@repo/rdx-utils";
|
|
import * as z from "zod/v4";
|
|
import { translateZodValidationError } from "../helpers";
|
|
import { ValueObject } from "./value-object";
|
|
|
|
interface EmailAddressProps {
|
|
value: string;
|
|
}
|
|
|
|
export class EmailAddress extends ValueObject<EmailAddressProps> {
|
|
static create(value: string): Result<EmailAddress, Error> {
|
|
const valueIsValid = EmailAddress.validate(value);
|
|
|
|
if (!valueIsValid.success) {
|
|
return Result.fail(
|
|
translateZodValidationError("EmailAddress creation failed", valueIsValid.error)
|
|
);
|
|
}
|
|
|
|
return Result.ok(new EmailAddress({ value: valueIsValid.data }));
|
|
}
|
|
|
|
private static validate(value: string) {
|
|
const schema = z.email({ message: "Invalid email format" });
|
|
return schema.safeParse(value);
|
|
}
|
|
|
|
getLocalPart(): string {
|
|
return this.props.value.split("@")[0];
|
|
}
|
|
|
|
getDomain(): string {
|
|
return this.props.value.split("@")[1];
|
|
}
|
|
|
|
getDomainExtension(): string {
|
|
return this.getDomain().split(".")[1];
|
|
}
|
|
|
|
getDomainName(): string {
|
|
return this.getDomain().split(".")[0];
|
|
}
|
|
|
|
getProps(): string {
|
|
return this.props.value;
|
|
}
|
|
|
|
toPrimitive() {
|
|
return this.getProps();
|
|
}
|
|
|
|
toString() {
|
|
return String(this.props.value);
|
|
}
|
|
}
|