Files
gridpilot.gg/core/identity/domain/value-objects/EmailAddress.ts
2026-01-16 16:46:57 +01:00

48 lines
1.3 KiB
TypeScript

import type { ValueObject } from '@core/shared/domain/ValueObject';
import type { EmailValidationResult } from '../types/EmailAddress';
import { validateEmail, isDisposableEmail } from '../types/EmailAddress';
export interface EmailAddressProps {
value: string;
}
/**
* Value Object: EmailAddress
*
* Wraps a validated, normalized email string and provides equality semantics.
* Validation and helper utilities live in domain/types/EmailAddress.
*/
export class EmailAddress implements ValueObject<EmailAddressProps> {
public readonly props: EmailAddressProps;
private constructor(value: string) {
this.props = { value };
}
static create(raw: string): EmailAddress {
const result: EmailValidationResult = validateEmail(raw);
if (!result.success) {
throw new Error(result.error);
}
return new EmailAddress(result.email);
}
static fromValidated(value: string): EmailAddress {
return new EmailAddress(value);
}
get value(): string {
return this.props.value;
}
equals(other: ValueObject<EmailAddressProps>): boolean {
return this.props.value === other.props.value;
}
isDisposable(): boolean {
return isDisposableEmail(this.props.value);
}
}
export type { EmailValidationResult } from '../types/EmailAddress';
export { validateEmail, isDisposableEmail } from '../types/EmailAddress';