mirror of
https://github.com/unraid/api.git
synced 2026-01-07 17:19:52 -06:00
feat(api): add enum validation utility for improved type safety
- Introduced `isValidEnumValue` and `validateEnumValue` functions to validate enum values, enhancing type safety in the application. - Updated `DisplayService` to utilize `validateEnumValue` for theme and unit properties, ensuring only valid enum values are assigned.
This commit is contained in:
17
api/src/core/utils/validation/enum-validator.ts
Normal file
17
api/src/core/utils/validation/enum-validator.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
export function isValidEnumValue<T extends Record<string, string | number>>(
|
||||
value: unknown,
|
||||
enumObject: T
|
||||
): value is T[keyof T] {
|
||||
if (value == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return Object.values(enumObject).includes(value as T[keyof T]);
|
||||
}
|
||||
|
||||
export function validateEnumValue<T extends Record<string, string | number>>(
|
||||
value: unknown,
|
||||
enumObject: T
|
||||
): T[keyof T] | undefined {
|
||||
return isValidEnumValue(value, enumObject) ? (value as T[keyof T]) : undefined;
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import { type DynamixConfig } from '@app/core/types/ini.js';
|
||||
import { toBoolean } from '@app/core/utils/casting.js';
|
||||
import { fileExists } from '@app/core/utils/files/file-exists.js';
|
||||
import { loadState } from '@app/core/utils/misc/load-state.js';
|
||||
import { validateEnumValue } from '@app/core/utils/validation/enum-validator.js';
|
||||
import { getters } from '@app/store/index.js';
|
||||
import { ThemeName } from '@app/unraid-api/graph/resolvers/customization/theme.model.js';
|
||||
import { Display, Temperature } from '@app/unraid-api/graph/resolvers/info/display/display.model.js';
|
||||
@@ -77,8 +78,8 @@ export class DisplayService {
|
||||
const display: Display = {
|
||||
id: 'info/display',
|
||||
case: caseInfo,
|
||||
theme: config.theme || ThemeName.white,
|
||||
unit: config.unit || Temperature.CELSIUS,
|
||||
theme: config.theme ?? ThemeName.white,
|
||||
unit: config.unit ?? Temperature.CELSIUS,
|
||||
scale: config.scale ?? false,
|
||||
tabs: config.tabs ?? true,
|
||||
resize: config.resize ?? true,
|
||||
@@ -145,10 +146,11 @@ export class DisplayService {
|
||||
}
|
||||
|
||||
const { theme, unit, ...display } = state.display;
|
||||
|
||||
return {
|
||||
...display,
|
||||
theme: theme as ThemeName,
|
||||
unit: unit as Temperature,
|
||||
theme: validateEnumValue(theme, ThemeName),
|
||||
unit: validateEnumValue(unit, Temperature),
|
||||
scale: toBoolean(display.scale),
|
||||
tabs: toBoolean(display.tabs),
|
||||
resize: toBoolean(display.resize),
|
||||
|
||||
Reference in New Issue
Block a user