import { z } from "zod"; import { Maybe, Result } from "../../helpers"; import { ValueObject } from "./value-object"; // 📌 Validaciones usando `zod` const postalCodeSchema = z .string() .min(4, "Invalid postal code format") .max(10, "Invalid postal code format") .regex(/^\d{4,10}$/, { message: "Invalid postal code format", }); const countrySchema = z.string().min(2).max(56); const provinceSchema = z.string().min(2).max(50); const citySchema = z.string().min(2).max(50); const streetSchema = z.string().min(2).max(255); const street2Schema = z.string().optional(); interface IPostalAddressProps { street: string; street2?: string; city: string; postalCode: string; state: string; country: string; } export class PostalAddress extends ValueObject { protected static validate(values: IPostalAddressProps) { return z .object({ street: streetSchema, street2: street2Schema, city: citySchema, postalCode: postalCodeSchema, state: provinceSchema, country: countrySchema, }) .safeParse(values); } static create(values: IPostalAddressProps): Result { const valueIsValid = PostalAddress.validate(values); if (!valueIsValid.success) { return Result.fail(new Error(valueIsValid.error.errors[0].message)); } return Result.ok(new PostalAddress(values)); } static createNullable(values?: IPostalAddressProps): Result, Error> { if (!values || Object.values(values).every((value) => value.trim() === "")) { return Result.ok(Maybe.none()); } return PostalAddress.create(values!).map((value) => Maybe.some(value)); } get street(): string { return this.props.street; } get street2(): Maybe { return Maybe.fromNullable(this.props.street2); } get city(): string { return this.props.city; } get postalCode(): string { return this.props.postalCode; } get state(): string { return this.props.state; } get country(): string { return this.props.country; } getValue(): IPostalAddressProps { return this.props; } toString(): string { return `${this.props.street}, ${this.props.street2}, ${this.props.city}, ${this.props.postalCode}, ${this.props.state}, ${this.props.country}`; } }