Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nest #99

Open
xiaotiandada opened this issue Apr 18, 2022 · 0 comments
Open

Nest #99

xiaotiandada opened this issue Apr 18, 2022 · 0 comments
Labels

Comments

@xiaotiandada
Copy link
Owner

xiaotiandada commented Apr 18, 2022

class-validator

ValidationPipe

https://docs.nestjs.com/techniques/validation#stripping-properties
http://static.kancloud.cn/juukee/nestjs/2675355
https://juejin.cn/post/7064595876680826910

transform

app.useGlobalPipes(
    new ValidationPipe({
      transform: true,
    }),
);
@Get(':id')
findOne(@Param('id') id: number) {
 console.log(typeof id === 'number'); // true
 return 'This action returns a user';
}

id 会自动从 string 转换成 number

whitelist

app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true,
    }),
);

true 时,自动删除非白名单属性(在验证类中没有任何修饰符的属性)。

// Bad
// 所有参数都将被过滤
export class CreateCatDto {
  public name: string;

  public age: number;
  
  public breed: string;
}


// Good
import { IsNumber, IsString } from 'class-validator';

export class CreateCatDto {
  @IsString()
  public name: string;

  @IsNumber()
  public age: number;

  @IsString()
  public breed: string;
}

typeorm

    "typeorm": "^0.3.6",
    "typeorm": "0.2.45",

0.3.x 按照文档无法工作
0.2.x 按照文档正常工作

@xiaotiandada xiaotiandada changed the title NestJS 学习 Nest 学习 Apr 22, 2022
@xiaotiandada xiaotiandada changed the title Nest 学习 Nest Jun 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant