Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

typescript - Check for a Valid Instance of in NestJS

I'v'e been trying to validate my request based on my entity (Product) dto. Everything is working fine, except for 'From' and 'To' fields. Customer and Type fields are validated correctly, if i try to send a request with wrong data, that's what i get.

"statusCode": 400,
"message": [
    "type must be a valid enum value"
],
"error": "Bad Request"

Expected Behavior ProductDto should validate 'From' and 'To' fields from LeagueEntity. enter image description here enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to add @Type(() => LeagueEntity) to your from and to properties so that class-transformer and class-validator know what they are properly validating. As these are sub classes, you'll probably want to add @ValidateNested() as well


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...