Uecko_ERP/apps/server/archive/contexts/auth/domain/value-objects/token.ts
2025-05-09 12:45:32 +02:00

37 lines
789 B
TypeScript

import { ValueObject } from "@/core/common/domain";
import { Result } from "@repo/rdx-utils";
import { z } from "zod";
interface TokenProps {
value: string;
}
export class Token extends ValueObject<TokenProps> {
static create(token: string): Result<Token, Error> {
const result = Token.validate(token);
if (!result.success) {
return Result.fail(new Error(result.error.errors[0].message));
}
return Result.ok(new Token({ value: result.data }));
}
private static validate(token: string) {
const schema = z.string().min(1, { message: "Invalid token string" });
return schema.safeParse(token);
}
getValue() {
return this.props.value;
}
toString() {
return this.props.value;
}
toPrimitive() {
return this.props.value;
}
}