Refactor type guards for LoginResponse and UserRole to improve validation logic

This commit is contained in:
2025-05-24 14:10:13 +02:00
parent ab3cb56aa1
commit 07e0647174
2 changed files with 8 additions and 5 deletions

View File

@@ -42,10 +42,10 @@ export function isLoginResponse(obj: unknown): obj is LoginResponse {
obj !== null && obj !== null &&
'user' in obj && 'user' in obj &&
isUser(obj.user) && isUser(obj.user) &&
'sessionId' in obj && (!('sessionId' in obj) ||
typeof (obj as LoginResponse).sessionId === 'string' && typeof (obj as LoginResponse).sessionId === 'string') &&
'expiresAt' in obj && (!('expiresAt' in obj) ||
typeof (obj as LoginResponse).expiresAt === 'string' typeof (obj as LoginResponse).expiresAt === 'string')
); );
} }

View File

@@ -46,7 +46,10 @@ export enum UserRole {
* Type guard to check if a value is a valid UserRole * Type guard to check if a value is a valid UserRole
*/ */
export function isUserRole(value: unknown): value is UserRole { export function isUserRole(value: unknown): value is UserRole {
return typeof value === 'string' && value in UserRole; return (
typeof value === 'string' &&
Object.values(UserRole).includes(value as UserRole)
);
} }
export enum Theme { export enum Theme {