import { Result, generateUUIDv4 } from "@repo/rdx-utils"; import { z } from "zod/v4"; import { translateZodValidationError } from "../helpers"; import { ValueObject } from "./value-object"; export class UniqueID extends ValueObject { static validate(value: string) { const schema = z.uuid({ message: "Invalid UUID format" }); return schema.safeParse(value); } static create(id?: string, generateOnEmpty = false): Result { if (!id || id?.trim() === "") { if (!generateOnEmpty) { return Result.fail(new Error("ID cannot be undefined or null")); } return Result.ok(UniqueID.generateNewID()); } // biome-ignore lint/style/noNonNullAssertion: const valueIsValid = UniqueID.validate(id!); if (!valueIsValid.success) { return Result.fail( translateZodValidationError("UniqueID creation failed", valueIsValid.error) ); } return Result.ok(new UniqueID(valueIsValid.data)); } static generateNewID(): UniqueID { return new UniqueID(generateUUIDv4()); } getProps(): string { return this.props; } toString(): string { return String(this.getProps()); } toPrimitive() { return this.toString(); } }