[英]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.