Merged PR 147: Task一覧APIのstatusの入力チェックを行うデコレータを実装する
## 概要 [Task1957: Task一覧APIのstatusの入力チェックを行うデコレータを実装する](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/1957) - Statusに該当する文字列が `,` で連結されている場合のみバリデーション通過となるようなデコレータを実装 ## レビューポイント - 実装コストを考慮して汎用的な指定をできるようにしなかったが問題ないか - 実装コードは妥当な実装になっているか ## 動作確認状況 - ローカルで確認
This commit is contained in:
parent
26098cc400
commit
f5ce65add8
43
dictation_server/src/common/validators/status.validator.ts
Normal file
43
dictation_server/src/common/validators/status.validator.ts
Normal file
@ -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,
|
||||
});
|
||||
};
|
||||
}
|
||||
@ -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({
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user