23 lines
620 B
TypeScript
23 lines
620 B
TypeScript
|
|
import { Result, ValueObject } from "@common/domain";
|
||
|
|
import { z } from "zod";
|
||
|
|
|
||
|
|
const RoleSchema = z.enum(["Admin", "User", "Manager", "Editor"]);
|
||
|
|
|
||
|
|
export class UserRoles extends ValueObject<string[]> {
|
||
|
|
static create(roles: string[]): Result<UserRoles, Error> {
|
||
|
|
const result = UserRoles.validate(roles);
|
||
|
|
|
||
|
|
return result.success
|
||
|
|
? Result.ok(new UserRoles(result.data))
|
||
|
|
: Result.fail(new Error("Invalid user roles"));
|
||
|
|
}
|
||
|
|
|
||
|
|
private static validate(roles: string[]) {
|
||
|
|
return z.array(RoleSchema).safeParse(roles);
|
||
|
|
}
|
||
|
|
|
||
|
|
hasRole(role: string): boolean {
|
||
|
|
return this.value.includes(role);
|
||
|
|
}
|
||
|
|
}
|