플젝 이슈 해결, see-me

00_8_3·2022년 3월 6일
0

SEEME 이슈

목록 보기
4/10

커스텀 데코레이터

  • class-validator에 빈 공백 제거해주는 기능이 없음

  • trim을 이용해 해결 하려 했지만
    맨 왼쪽, 맨 오른쪽 문자열 공백만 제거 해주기 때문에
    원하는 기능과 다름

해결

  • 정규표현식을 이용하여 /\s/ 문자열에 빈 공백이 있는 경우
    유효성 검사에서 실패하는 데코레이터 만들어 해결

코드

export function IsNotBlank(
  property: string,
  validationOptions?: ValidationOptions
) {
  // eslint-disable-next-line @typescript-eslint/ban-types
  return (object: Object, propertyName: string) => {
    const isSpaceRegex = /\s/;
    registerDecorator({
      name: "isNotBlank",
      target: object.constructor,
      propertyName,
      constraints: [property],
      options: validationOptions,
      validator: {
        validate(value: any) {
          return typeof value === "string" && !value.match(isSpaceRegex);
        },
        defaultMessage() {
          return `Remove the space from the string.`;
        },
      },
    });
  };
}

loggingReq 미들웨어

로거에 넘겨줄 body와 다음 미들웨어의 body가 분리되지 않은 문제

예를들어 password값을 필터링 했는데 다음 미들웨어에도 필터링된 상태

전개연산자로 deep copy해서filterdBody 생성 후 logger에 넘겨줘서 해결

코드

export const loggingReq = () => {

	...
    
    const filteredBody = { ...body };
    Object.keys(body).forEach((k) => {
      if (k.toLocaleLowerCase().indexOf("password") > -1) {
        filteredBody.password = "FILTERED";
      }
      if (k.toLocaleLowerCase().indexOf("passwordConfirm") > -1) {
        filteredBody.passwordconfirm = "FILTERED";
      }
    });
    logger.info(`Parameters : ${JSON.stringify(filteredBody)}`);
}

0개의 댓글