diff --git a/dictation_server/src/common/validators/status.validator.ts b/dictation_server/src/common/validators/status.validator.ts new file mode 100644 index 0000000..eca361e --- /dev/null +++ b/dictation_server/src/common/validators/status.validator.ts @@ -0,0 +1,43 @@ +import { + registerDecorator, + ValidationArguments, + ValidationOptions, + ValidatorConstraint, + ValidatorConstraintInterface, +} from 'class-validator'; +import { TASK_STATUS } from '../../constants'; + +@ValidatorConstraint() +export class IsStatusConstraint implements ValidatorConstraintInterface { + private readonly STATUS: string[] = Object.values(TASK_STATUS); + // eslint-disable-next-line @typescript-eslint/no-unused-vars + validate(value: string, args: ValidationArguments): boolean { + if (value) { + // ,で分割した文字列のすべてがTASK_STATUSのプロパティに存在する値であった場合のみtrue + return value.split(',').every((state) => this.STATUS.includes(state)); + } else { + return false; + } + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + defaultMessage(validationArguments?: ValidationArguments): string { + return `invalid status string`; + } +} +/** + * ,で分割した文字列のすべてがTASK_STATUSのプロパティに存在する値であるかをチェックする + * @param [validationOptions] + * @returns + */ +export function IsStatus(validationOptions?: ValidationOptions) { + return (object: object, propertyName: string) => { + registerDecorator({ + name: 'IsStatus', + target: object.constructor, + propertyName, + options: validationOptions, + validator: IsStatusConstraint, + }); + }; +} diff --git a/dictation_server/src/features/tasks/types/types.ts b/dictation_server/src/features/tasks/types/types.ts index 7fe5ab6..fcaa5b1 100644 --- a/dictation_server/src/features/tasks/types/types.ts +++ b/dictation_server/src/features/tasks/types/types.ts @@ -3,6 +3,7 @@ import { AudioOptionItem } from '../../../features/files/types/types'; import { Type } from 'class-transformer'; import { IsIn, IsInt, IsOptional, Min } from 'class-validator'; import { TASK_LIST_SORTABLE_ATTRIBUTES } from '../../../constants'; +import { IsStatus } from '../../../common/validators/status.validator'; export class TasksRequest { @ApiProperty({ @@ -37,7 +38,7 @@ export class TasksRequest { example: 'Uploaded,Pending,InProgress', }) @IsOptional() - // TODO: 入力チェックを行うデコレータを追加する。@Matches or カスタムデコレータで実装想定。statusの値は先頭大文字であることまで一致しなくていいはず?(実装時レビューにて要確認) + @IsStatus() status?: string; @ApiProperty({