Answers for "nest custom class validator"

0

nestjs class-validator

class-validator
Posted by: Guest on August-11-2021
-1

nest custom class validator

useContainer(app.select(AppModule), { fallbackOnErrors: true });

@ValidatorConstraint({ name: 'UserExists', async: true })
@Injectable()
export class UserExistsRule implements ValidatorConstraintInterface {
  constructor(private usersRepository: UsersRepository) {}

  async validate(value: number) {
    try {
      await this.usersRepository.getOneOrFail(value);
    } catch (e) {
      return false;
    }

    return true;
  }

  defaultMessage(args: ValidationArguments) {
    return `User doesn't exist`;
  }
}

export function UserExists(validationOptions?: ValidationOptions) {
  return function (object: any, propertyName: string) {
    registerDecorator({
      name: 'UserExists',
      target: object.constructor,
      propertyName: propertyName,
      options: validationOptions,
      validator: UserExistsRule,
    });
  };
}
Posted by: Guest on June-19-2021

Code answers related to "nest custom class validator"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language