Reputation: 19
when I run this command curl -vd '{"NickName":"Marry","Password":"pwd"}' -H "Content-type: application/json" http://127.0.0.1:3000/signin
, I got
No metadata found. There is more than once class-validator version installed probably. You need to flatten your dependencies.
printed on Server side, and validate
did not perform correctly.
Before all this, I npm install
some dependencies:
npm install --save routing-controllers
npm install --save class-transformer
npm install --save class-validator
Could Somebody help? How can I fix this?Thanks!
import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn } from 'typeorm';
import { Length } from 'class-validator';
import * as ErrorCode from '../error/errorcode'
@Entity()
export class User {
@PrimaryGeneratedColumn()
@Column({
name: "id"
})
Id?: number;
@Column({
name: "nickname"
})
@Length(1, 20, {
message: "NickName must be 1 to 20 characters",
context: {
errorCode: ErrorCode.ParamLengthNotInRange
}
})
Nickname: string;
@Column({
name: "password"
})
@Length(6,20)
Password: string;
//constructor(input : { Id : number , Nickname: string, Password: string}){
constructor(input : { Nickname: string, Password: string}){
//this.Id = input.Id;
this.Nickname = input.Nickname;
this.Password = input.Password;
}
}
import { JsonController, Post, Body, Req } from "routing-controllers";
import { validate, ValidationError } from 'class-validator';
import {User} from '../entity'
@JsonController()
export class UserController {
@Post('/signin')
async signin(@Body() user: User) {
const errors: ValidationError[] = await validate(user)
if (errors && errors.length > 0) {
console.log(errors[0].contexts!['Length'].errorCode)
}
console.log(user)
return 'this is signin'
}
}
Upvotes: 0
Views: 1432
Reputation: 581
We had the same message. Update of class-validator
from 0.12.2 to 0.13.1 solved it.
Upvotes: 1
Reputation: 240
You can check if you have more versions of class-validator
installed by doing:
npm ls class-validator
In my case I was using type-graphql
and found that type-graphql
was also installing an earlier version of class-validator
. So I just downgraded the one in package.json to the earlier version to make them match then did a fresh install and the message disappeared.
Hope this helps.
Upvotes: 1