Uecko_ERP/apps/server/src/contexts/auth/domain/value-objects/token.ts

33 lines
743 B
TypeScript
Raw Normal View History

2025-05-02 21:43:51 +00:00
import { ValueObject } from "@/core/common/domain";
import { Result } from "@/core/common/helpers";
2025-02-15 21:30:12 +00:00
import { z } from "zod";
2025-02-20 18:55:24 +00:00
interface TokenProps {
value: string;
}
export class Token extends ValueObject<TokenProps> {
2025-02-15 21:30:12 +00:00
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));
}
2025-02-20 18:55:24 +00:00
return Result.ok(new Token({ value: result.data }));
2025-02-15 21:30:12 +00:00
}
private static validate(token: string) {
2025-02-16 19:30:20 +00:00
const schema = z.string().min(1, { message: "Invalid token string" });
2025-02-15 21:30:12 +00:00
return schema.safeParse(token);
}
2025-02-20 18:55:24 +00:00
getValue() {
return this.props.value;
}
toString() {
return this.props.value;
}
2025-02-15 21:30:12 +00:00
}