[英]JOI - Validating complex object
我試過,試過,但想不通:(
這是我需要驗證的對象:
let body = {
greeting:
{
stringValue: 'Hello !',
stringListValues: [],
binaryListValues: [],
dataType: 'String'
},
newsletterId:
{
stringValue: '123456789',
stringListValues: [],
binaryListValues: [],
dataType: 'String'
}
};
我需要驗證有一個greeting ,並且它有 key stringValue並且它不是空的。 其他值我不在乎。
此外,對於第二個對象newsletterId ,它也有鍵stringValue並且不為空。 其他值我不在乎。
我想出了只檢查根對象,使用這個架構:
const schema = {
greeting: Joi.required(),
newsletterId: Joi.required()
};
我閱讀了很多示例,但找不到具有這種結構的示例。
讓我們定義一個架構:
const schema = Joi.object().keys({
greeting: Joi.object({
stringValue: Joi.string().required().empty(['', null]),
stringListValues: Joi.array().items(Joi.string()),
binaryListValues: Joi.array().items(Joi.binary())
}).required(),
newsletterId: // same as above
});
並像這樣測試它:
Joi.validate(myObjectToTest, schema, function(error, cleanObject){
console.log(error, cleanObject);
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.