20 lines
543 B
TypeScript
20 lines
543 B
TypeScript
|
|
import { Result, ValueObject } from "@common/domain";
|
||
|
|
import { z } from "zod";
|
||
|
|
|
||
|
|
export class Token extends ValueObject<string> {
|
||
|
|
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(result.data));
|
||
|
|
}
|
||
|
|
|
||
|
|
private static validate(token: string) {
|
||
|
|
const schema = z.string().min(319, { message: "Invalid token string" });
|
||
|
|
return schema.safeParse(token);
|
||
|
|
}
|
||
|
|
}
|