79183346

Date: 2024-11-13 03:43:12
Score: 1
Natty:
Report link

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"));
  }
});
Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Khanh Do