Using Validation

To use validation use class-validator. Example how to use class-validator with TypeORM:

import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
import {Contains, IsInt, Length, IsEmail, IsFQDN, IsDate, Min, Max} from "class-validator";
export class Post {
id: number;
@Length(10, 20)
title: string;
text: string;
rating: number;
email: string;
site: string;
createDate: Date;


import {getManager} from "typeorm";
import {validate} from "class-validator";
let post = new Post();
post.title = "Hello"; // should not pass
post.text = "this is a great post about hell world"; // should not pass
post.rating = 11; // should not pass = ""; // should not pass = "googlecom"; // should not pass
const errors = await validate(post);
if (errors.length > 0) {
throw new Error(`Validation failed!`);
} else {
await getManager().save(post);