我正在使用 class-validator npm 包,并想驗證請求正文的 type 屬性是否與“組織”或“學生”匹配。
我將如何用這個包做到這一點?
這不起作用:
@IsIn(['organization', 'student'])
type: string
請求正文示例:
{
"type": "organization",
"email": "[email protected]",
"password": "veryscretpwd"
}
uj5u.com熱心網友回復:
您可以在模型類中使用類似的東西。
{
"type": {
type: String,
enum: ['organization', 'student'],
required: [true, 'Type is required.']
},
"email": ...
}
而不是@IsIn 使用列舉。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/462493.html
標籤:javascript 验证 类验证器
上一篇:洗掉所有物件中的屬性及其值
