# 使用 Validation

要使用验证,请使用class-validator (opens new window)。 示例如何在 TypeORM 中使用 class-validator:

import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";
import { Contains, IsInt, Length, IsEmail, IsFQDN, IsDate, Min, Max } from "class-validator";

@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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

验证:

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);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16