import { Result } from "@repo/rdx-utils"; import * as z from "zod/v4"; import { translateZodValidationError } from "../helpers"; import { ValueObject } from "./value-object"; interface TaxCodeProps { value: string; } export class TaxCode extends ValueObject { protected static readonly MIN_LENGTH = 1; protected static readonly MAX_LENGTH = 10; protected static validate(value: string) { const schema = z .string() .trim() .regex(/^[a-z0-9]+([_-][a-z0-9]+)*$/, { message: "TaxCode must contain only lowercase letters, numbers, and underscores", }) .min(TaxCode.MIN_LENGTH, { message: `TaxCode must be at least ${TaxCode.MIN_LENGTH} characters long`, }) .max(TaxCode.MAX_LENGTH, { message: `TaxCode must be at most ${TaxCode.MAX_LENGTH} characters long`, }); return schema.safeParse(value); } static create(value: string) { const valueIsValid = TaxCode.validate(value); if (!valueIsValid.success) { return Result.fail( translateZodValidationError("TaxCode creation failed", valueIsValid.error) ); } // biome-ignore lint/style/noNonNullAssertion: return Result.ok(new TaxCode({ value: valueIsValid.data! })); } getProps(): string { return this.props.value; } toPrimitive(): string { return this.getProps(); } toString() { return String(this.props.value); } }