nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.69k stars 7.63k forks source link

DI does not work when validating in a custom validator #9824

Closed Bezlepkin closed 2 years ago

Bezlepkin commented 2 years ago

Is there an existing issue for this?

Current behavior

I created my own validator. It has a DI. At the time of initialization of the application, the constructor has information about the class that is injected. But if i try to validate data (@Validate(Unique Email)) i getting undefined in the method validate 2022-06-23_12-39-10 2022-06-23_12-38-17 2022-06-23_12-38-04 2022-06-23_12-37-42

.

Minimum reproduction code

https://codesandbox.io/s/nervous-pateu-skz50j

Steps to reproduce

No response

Expected behavior


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

// validator
import { ValidatorConstraint, ValidatorConstraintInterface } from 'class-validator';
import { UserService } from '../services';
import { Injectable } from '@nestjs/common';

@ValidatorConstraint({ name: 'uniqueEmail', async: true })
@Injectable()
export class UniqueEmail implements ValidatorConstraintInterface {
  constructor(private readonly userService: UserService) {
    console.log(userService);
  }

  async validate(email: string): Promise<any> {
    console.log(this.userService.getUserByEmail(email));
    const user = await this.userService.getUserByEmail(email);
    return !!user;
  }

  defaultMessage(): string {
    return 'E-mail адрес уже используется!';
  }
}

// dto
import { ApiProperty } from '@nestjs/swagger';
import { IsEmail, IsString, IsNotEmpty, Validate, IsOptional } from 'class-validator';
import { UniqueEmail } from '../../validators/email.validator';

export class CreateAccountDTO {
  @ApiProperty()
  @IsNotEmpty({ message: 'Введите адрес электронной почты.' })
  @IsEmail({}, { message: 'E-mail адрес введен некорректно.' })
  @Validate(UniqueEmail)
  readonly email: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  readonly username: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  readonly password: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  readonly first_name: string;

  @ApiProperty()
  @IsNotEmpty()
  @IsString()
  readonly last_name: string;

  @ApiProperty()
  @IsOptional()
  @IsString()
  readonly referal_code: string;
}

### Package

- [ ] I don't know. Or some 3rd-party package
- [X] <code>@nestjs/common</code>
- [X] <code>@nestjs/core</code>
- [ ] <code>@nestjs/microservices</code>
- [X] <code>@nestjs/platform-express</code>
- [ ] <code>@nestjs/platform-fastify</code>
- [ ] <code>@nestjs/platform-socket.io</code>
- [ ] <code>@nestjs/platform-ws</code>
- [X] <code>@nestjs/testing</code>
- [ ] <code>@nestjs/websockets</code>
- [ ] Other (see below)

### Other package

_No response_

### NestJS version

8.0.0

### Packages versions

```json

  "dependencies": {
    "@auth0/s3": "^1.0.0",
    "@metamask/eth-sig-util": "^4.0.1",
    "@nestjs/common": "^8.0.0",
    "@nestjs/config": "^2.0.1",
    "@nestjs/core": "^8.0.0",
    "@nestjs/passport": "^8.2.1",
    "@nestjs/platform-express": "^8.0.0",
    "@nestjs/swagger": "^5.2.1",
    "@nestjs/throttler": "^2.0.1",
    "@nestjs/typeorm": "^8.0.4",
    "@types/multer": "^1.4.7",
    "aws-sdk": "^2.1148.0",
    "bcryptjs": "^2.4.3",
    "class-transformer": "^0.5.1",
    "class-transformer-validator": "^0.9.1",
    "class-validator": "^0.13.2",
    "dotenv": "^16.0.1",
    "mysql2": "^2.3.3",
    "normalize-email": "^1.1.1",
    "passport": "^0.6.0",
    "passport-http-bearer": "^1.0.1",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0",
    "s3": "^4.4.0",
    "swagger-ui-express": "^4.4.0",
    "tronweb": "^4.2.0",
    "typeorm": "^0.2.45"
  },
  "devDependencies": {
    "@nestjs/cli": "^8.0.0",
    "@nestjs/schematics": "^8.0.0",
    "@nestjs/testing": "^8.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "27.5.0",
    "@types/node": "^16.0.0",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "28.0.3",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "28.0.1",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.0.0",
    "typescript": "^4.3.5"
  },

### Node.js version

14.17

### In which operating systems have you tested?

- [X] macOS
- [ ] Windows
- [ ] Linux

### Other

_No response_
kamilmysliwiec commented 2 years ago

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.