## 概要 [Task2502: [Sp17完了MISO]バリデータをクラスを使用した記述に統一する](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/2502) バリデータの記述方法をクラスで外だしする形に統一しました。 また、未使用の引数を削除しました。 ## レビューポイント 期待通りの修正内容であるか。 未使用の引数を削除してしまったが、問題ないか。 ## UIの変更 なし ## 動作確認状況 ローカルで該当バリデーションを使用しているAPIを実行し、動作を確認済み ## 補足 なし
71 lines
1.9 KiB
TypeScript
71 lines
1.9 KiB
TypeScript
import {
|
||
registerDecorator,
|
||
ValidationArguments,
|
||
ValidationOptions,
|
||
ValidatorConstraint,
|
||
ValidatorConstraintInterface,
|
||
} from 'class-validator';
|
||
import { SignupRequest } from '../../features/users/types/types';
|
||
|
||
@ValidatorConstraint()
|
||
export class IsPassword implements ValidatorConstraintInterface {
|
||
validate(value: string | undefined): boolean {
|
||
// passwordが設定されていない場合はチェックしない
|
||
if (value === undefined) {
|
||
return true;
|
||
}
|
||
// 正規表現でパスワードのチェックを行う
|
||
// 4~16文字の半角英数字と記号のみ
|
||
const regex = /^[!-~]{4,16}$/;
|
||
if (!regex.test(value)) {
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
defaultMessage(): string {
|
||
return 'EncryptionPassword rule not satisfied';
|
||
}
|
||
}
|
||
|
||
@ValidatorConstraint()
|
||
export class IsEncryptionPassword implements ValidatorConstraintInterface {
|
||
validate(value: string | undefined, args: ValidationArguments): boolean {
|
||
const { encryption } = args.object as SignupRequest;
|
||
if (encryption === true && !value) {
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
defaultMessage(): string {
|
||
return 'Encryption password is required when encryption is enabled';
|
||
}
|
||
}
|
||
|
||
export const IsPasswordvalid = (validationOptions?: ValidationOptions) => {
|
||
return (object: any, propertyName: string) => {
|
||
registerDecorator({
|
||
name: 'IsPasswordvalid',
|
||
target: object.constructor,
|
||
propertyName: propertyName,
|
||
constraints: [],
|
||
options: validationOptions,
|
||
validator: IsPassword,
|
||
});
|
||
};
|
||
};
|
||
export const IsEncryptionPasswordPresent = (
|
||
validationOptions?: ValidationOptions,
|
||
) => {
|
||
return (object: SignupRequest, propertyName: string) => {
|
||
registerDecorator({
|
||
name: 'IsEncryptionPasswordValid',
|
||
target: object.constructor,
|
||
propertyName: propertyName,
|
||
constraints: [],
|
||
options: validationOptions,
|
||
validator: IsEncryptionPassword,
|
||
});
|
||
};
|
||
};
|