import { Maybe, Result } from "@rdx/utils"; import * as z from "zod/v4"; import { ValueObject } from "./value-object"; interface SlugProps { value: string; } export class Slug extends ValueObject { protected static readonly MIN_LENGTH = 2; protected static readonly MAX_LENGTH = 100; protected static validate(value: string) { const schema = z .string() .trim() .regex(/^[a-z0-9-]+$/, { message: "Slug must contain only lowercase letters, numbers, and hyphens", }) .min(Slug.MIN_LENGTH, { message: `Slug must be at least ${Slug.MIN_LENGTH} characters long` }) .max(Slug.MAX_LENGTH, { message: `Slug must be at most ${Slug.MAX_LENGTH} characters long` }); return schema.safeParse(value); } static create(value: string) { const valueIsValid = Slug.validate(value); if (!valueIsValid.success) { return Result.fail(new Error(valueIsValid.error.issues[0].message)); } return Result.ok(new Slug({ value: valueIsValid.data! })); } static createNullable(value?: string): Result, Error> { if (!value || value.trim() === "") { return Result.ok(Maybe.none()); } return Slug.create(value).map((value: Slug) => Maybe.some(value)); } getValue(): string { return this.props.value; } toPrimitive(): string { return this.getValue(); } }