import { ValueObject } from "core/common/domain"; import { Result } from "core/common/helpers"; import { z } from "zod"; interface UsernameProps { value: string; } export class Username extends ValueObject { static create(username: string): Result { const result = Username.validate(username); return result.success ? Result.ok(new Username({ value: result.data })) : Result.fail(new Error(result.error.errors[0].message)); } private static validate(username: string) { const schema = z .string() .min(3, { message: "Username must be at least 3 characters long" }) .max(30, { message: "Username cannot exceed 30 characters" }) .regex(/^[a-zA-Z0-9_]+$/, { message: "Username can only contain letters, numbers, and underscores", }); return schema.safeParse(username); } getValue() { return this.props.value; } toString() { return this.props.value; } }