NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
[TOC] ## 概述 示例 ``` @Entity() export class Post { @PrimaryGeneratedColumn() id: number; @Column() @Length(10, 20) title: string; @Column() @Contains("hello") text: string; @Column() @IsInt() @Min(0) @Max(10) rating: number; @Column() @IsEmail() email: string; @Column() @IsFQDN() site: string; @Column() @IsDate() createDate: Date; } ``` 验证 ``` import { getManager } from "typeorm"; import { validate } from "class-validator"; let post = new Post(); post.title = "Hello"; // 不应该通过 post.text = "this is a great post about hell world"; //不应该通过 post.rating = 11; //不应该通过 post.email = "google.com"; //不应该通过 post.site = "googlecom"; //不应该通过 const errors = await validate(post); if (errors.length > 0) { throw new Error(`Validation failed!`); } else { await getManager().save(post); } ```