使用启用了严格模式的NestJS中的类。

huangapple go评论64阅读模式
英文:

Using classes in nestjs with strict enabled

问题

I'm working with NestJS while strict is enabled in tsconfig.json.

这是在tsconfig.json中启用了严格模式的情况下,我正在使用NestJS

This causes some unexpected behavior with validation.

这会导致验证出现一些意外行为。

Because we are using the class-validator library for DTO validation, we have to create DTOs as classes, and because we have strict enabled, and in particular strictNullChecks:true enabled, we have to explicitly initialize every property in the class that is not optional.

因为我们在DTO验证中使用了class-validator库,所以我们必须将DTO创建为类,并且由于启用了严格模式,尤其是启用了strictNullChecks:true,我们必须明确初始化类中不是可选的每个属性。

If we initialize properties with default values, class-validator will fail to validate the DTO for non-existent properties because there will be default values in all properties then nestjs will call new on the class.

如果我们使用默认值初始化属性,class-validator将无法验证不存在的属性,因为所有属性都会有默认值,然后nestjs会在类上调用new

In addition, nest will call default constructor on the DTO and will not pass the received value into the constructor and so I cannot implement constructor that receives initial value.

此外,nestjs将在DTO上调用默认构造函数,并且不会将接收到的值传递给构造函数,因此我无法实现接收初始值的构造函数。

So as far as I can see now I'll either need to disable strictNullChecks:true or find another way to validate DTO's.

就我目前所看到的情况而言,我要么需要禁用strictNullChecks:true,要么找到另一种验证DTO的方法。

Any suggestions?

有什么建议吗?

英文:

I'm working with NestJS while strict is enabled in tsconfig.json.

This causes some unexpected behavior with validation. Because we are using the class-validator library for DTO validation, we have to create DTOs as classes, and because we have strict enabled, and in particular strictNullChecks:true enabled, we have to explicitly initialize every property in the class that is not optional.

If we initialize properties with default values, class-validator will fail to validate the DTO for non-existent properties because there will be default values in all properties then nestjs will call `new' on the class.

In addition, nest will call default constructor on the DTO and will not pass the received value into the constructor and so I cannot implement constructor that receives initial value.

So as far as I can see now I'll either need to disable strictNullChecks:true or find another way to validate DTO's.

Any suggestions?

答案1

得分: 1

你可以在你的DTO中使用非空断言运算符("!"),然后确保你使用了正确的装饰器来防止该属性为null或undefined。如果属性可以为undefined,你可以明确地允许undefined作为有效选项。

export class MyDto {
    @Is

<details>
<summary>英文:</summary>

You can use the [Non-null assertion operator][1] (&quot;!&quot;) within your DTO. You should then make shure that you use the right decorators to prevent that this property will be null or undefined. You can explicitly allow `undefined` if that is a valid option for a property. 

```ts
export class MyDto {
    @IsString()
    readonly willNotBeUndefined!: string; // &lt;-- notice the &quot;!&quot;

    @IsOptional()
    readonly couldBeUndefined?: string;

    //   Or like this: 
    //   @IsOptional()
    //   readonly couldBeUndefined: string | undefined;
}

huangapple
  • 本文由 发表于 2023年5月29日 17:18:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356088.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定