Edit DTO validation with class-validator

Viewed 470

Today, I am trying to figure out how to validate a patch request in the nestjs using class-validator, using custom decorator.

pets-edit.dto.ts

  @_IsUnique({ table: 'pets' })
  readonly phone: string;

unique.decorator.ts

        async validate(columnNameValue: any, args: ValidationArguments) {
          try {
            const params = args.constraints[0];
            const record = await getManager().query(
              `SELECT * FROM ${params.table} WHERE ${propertyName} = '${columnNameValue}'`
            );
            if (record[0]) return false;
            return true;
          } catch (error) {
            return false;
          }
        }

But when I send updated object to the dto, It will return already exists error. So I want to stop checking updating raw.

0 Answers
Related