adonisJS/ 유효성검사에서 Cannot read property 'fatal' of undefined 에러

flobeeee·2022년 2월 7일
1

시행착오

목록 보기
30/43

🤓 상황

유효성검사에서 필수값으로 설정해놨는데도
Cannot read property 'fatal' of undefined 에러가 발생했다.

유효성에러가 뜨는 게 정상인데, 저런 에러는 어디서 뭐가 없다는 건지 신속하게 파악하기 어렵다.

🤓 문제 코드


const validator = {
    schema: schema.create({
      type: schema.string(),
      name: schema.string({}, [
        rules.unique({
          table: User.table,
          column: 'name',
          where: { deleted_at: null, type: request.input('type') // 이 부분이 문제였다.
        }),
      ]),
    }),
}

type이 들어오지 않으면 필수값이 들어오지 않았다고 에러가 떠야 하는데,
name 확인할 때, DB를 확인하는 부분이 먼저 작동해서
where: { deleted_at: null, type: null } 로 들어가는 것이었다.

🤓 해결 코드

유효성검사 코드를 한 번 더 추가했다.

if (!(request.input('type'))) {
  throw new ValidationFailedException()
}

만약에 type이 들어오지 않으면 에러가 발생했던 코드까지 가지도 않고, 바로 위 코드에서 걸러진다.

profile
기록하는 백엔드 개발자

0개의 댓글