英文:
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] ("!") 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; // <-- notice the "!"
@IsOptional()
readonly couldBeUndefined?: string;
// Or like this:
// @IsOptional()
// readonly couldBeUndefined: string | undefined;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论