#NestJS class-validator generic enum
2 messages · Page 1 of 1 (latest)
type Constructor<T = object> = new (...args: any[]) => T;
export function SortQueryDto<T extends Constructor, K extends keyof T>(
classRef: T,
keys: readonly K[],
): T {
const PossibleValues = keys.reduce((a, v) => ({ ...a, [v]: v }), {});
class MixinSortQuery extends classRef {
@IsEnum(PossibleValues)
@IsNotEmpty()
field: K;
@IsEnum(SortDirection)
@IsNotEmpty()
direction: SortDirection;
}
return MixinSortQuery;
}
const SortQueryDtoHost = SortQueryDto(Task, [
'createdAt',
'updatedAt',
'title',
]);