Thanks everyone for your answer!
After careful consideration, I've decided to use class-validator
and class-transformer
for validation. This allows me to validate required properties and their types without manually specifying each property's name and type. The example here demonstrates how class-validator works, but in my project, I’ll use class-transformer
to convert data with plainToInstance
before running validations.
`
import { IsNotEmpty, IsString, IsInt } from "class-validator";
import "reflect-metadata";
export class Car {
@IsNotEmpty()
@IsString()
model: string;
@IsInt()
year: number;
@IsString()
price: string;
}
Call validate
validate(car).then(errors => {
if (errors.length > 0) {
throw new Error("Errors: " + errors.join("\n"));
}
});