找不到类别验证器的元数据

时间:2019-04-03 09:01:25

标签: javascript node.js typescript nestjs class-validator

我正在尝试使用ValidationPipe,但是无论我如何编写代码,在发送请求时都会收到以下警告:No metadata found. There is more than once class-validator version installed probably. You need to flatten your dependencies

我的路线看起来像这样:

@Get()
@UsePipes(new ValidationPipe({ transform: true }))
async findAll(@Query() queryDto: QueryDto) {
    return await this.myService.findAll(queryDto);
}

我的DTO看起来像这样:

export class queryDto
{
    @ApiModelProperty({
        description: 'Maximum number of results',
        type: Number,
        example: 50,
        default: 50,
        required: false
    })
    readonly limit: number = 50;
}

the doc之后,我尝试了几种使用ValidationPipe的方法,但是对我来说没有用。我知道这是行不通的,因为尽管请求得到了响应,但当查询为空时,并未使用我在DTO中为属性limit写的默认值50。因此,当查询中未提供limit时,limit的值是未定义的,而它的值应该是50(这意味着不使用ValidationPipe)。 / p>

我的package.json似乎正确:

npm ls class-validator
api-sport@0.0.1 /home/pierre_t/Bureau/dev/ApiSport
└── class-validator@0.9.1

完整package.json

{
  "name": "api-sport",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "license": "MIT",
  "scripts": {
    "build": "tsc -p tsconfig.build.json",
    "format": "prettier --write \"src/**/*.ts\"",
    "start": "ts-node -r tsconfig-paths/register src/main.ts",
    "start:dev": "nodemon",
    "start:debug": "nodemon --config nodemon-debug.json",
    "start:prod": "pm2 start ./src/main.js --no-daemon",
    "lint": "tslint -p tsconfig.json -c tslint.json",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "^6.0.5",
    "@nestjs/core": "^6.0.5",
    "@nestjs/platform-express": "^6.0.5",
    "@nestjs/swagger": "^3.0.1",
    "@nestjs/typeorm": "^6.0.0",
    "@types/lodash": "^4.14.123",
    "class-transformer": "^0.2.0",
    "class-validator": "^0.9.1",
    "dotenv": "^7.0.0",
    "hbs": "^4.0.3",
    "mysql": "^2.16.0",
    "pm2": "^3.4.1",
    "reflect-metadata": "^0.1.12",
    "rimraf": "^2.6.2",
    "rxjs": "^6.3.3",
    "swagger-ui-express": "^4.0.2",
    "typeorm": "^0.2.16"
  },
  "devDependencies": {
    "@nestjs/testing": "^6.0.5",
    "@types/express": "^4.16.0",
    "@types/jest": "^23.3.13",
    "@types/node": "^10.14.4",
    "@types/supertest": "^2.0.7",
    "jest": "^23.6.0",
    "nodemon": "^1.18.9",
    "prettier": "^1.15.3",
    "supertest": "^3.4.1",
    "ts-jest": "^23.10.5",
    "ts-node": "^7.0.1",
    "tsconfig-paths": "^3.7.0",
    "tslint": "5.12.1",
    "typescript": "^3.4.1"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

为什么会收到此消息以及如何使用ValidationPipe

3 个答案:

答案 0 :(得分:1)

这是因为您使用的是class-validator,但未经任何验证,请参阅以下issue

  

基本上,它会警告您存储中没有任何元数据,   这意味着您没有使用过class-validator中的任何装饰器。那   表示您不执行任何验证,因此您应该通过   validate: false的{​​{1}}选项可禁用自动验证。

我不确定是否可以对Nest的buildSchema进行验证,但是您也可以仅在dto中添加一个断言(如果有意义),例如:

ValidationPipe

顺便说一句:由于您的import { Min } from 'class-validator'; export class QueryDto { @Min(1) readonly limit: number = 50; } 仅具有字符串属性,因此您可能希望将@Querylimit转换为string。看看这个answer

答案 1 :(得分:1)

这个问题已经回答了,但是供以后遇到相同问题的人参考...

通过class-validator,您可以绕过某些属性(whitelisting)特殊标志的验证来验证任何属性。

作为docs

  

这将删除所有没有装饰器的属性。如果不   其他装饰器适合您的属性,您可以使用 @Allow   装饰器

例如:

import {validate, Allow, Min} from "class-validator";

export class Post {

    @Allow()
    title: string;

    @Min(0)
    views: number;

    nonWhitelistedProperty: number;
}

答案 2 :(得分:0)

这是我的引导程序,它适用于class-validator

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new ValidationPipe());
  await app.listen(3000);
}
bootstrap();