Uecko_ERP/packages/rdx-ddd/src/value-objects/email-address.ts

49 lines
1.1 KiB
TypeScript
Raw Normal View History

2025-09-01 14:07:59 +00:00
import { Result } from "@repo/rdx-utils";
2025-06-24 18:38:57 +00:00
import * as z from "zod/v4";
2025-02-21 10:06:27 +00:00
import { ValueObject } from "./value-object";
2025-02-20 18:55:24 +00:00
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) {
2025-08-26 18:55:59 +00:00
return Result.fail(new Error(valueIsValid.error.issues[0].message));
2025-02-20 18:55:24 +00:00
}
2025-05-09 10:45:32 +00:00
return Result.ok(new EmailAddress({ value: valueIsValid.data }));
2025-02-20 18:55:24 +00:00
}
private static validate(value: string) {
2025-09-01 14:07:59 +00:00
const schema = z.email({ message: "Invalid email format" });
2025-02-20 18:55:24 +00:00
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];
}
2025-09-04 10:02:24 +00:00
getProps(): string {
2025-02-20 18:55:24 +00:00
return this.props.value;
}
2025-02-21 10:06:27 +00:00
2025-04-01 14:26:15 +00:00
toPrimitive() {
2025-09-04 10:02:24 +00:00
return this.getProps();
2025-02-21 10:06:27 +00:00
}
2025-02-20 18:55:24 +00:00
}