{
"email": "{{$randomExampleEmail}}",
"dateofbirth": "2000-01-09",
"personal_document":{
"document_front":"url",
"document_back":"url",
"document_type":"CITIZENSHIP"
},
"company": {
"address":{
"city":"CITY",
"state":"STATE",
},
"company_name":"${{$randomCompanyName}}",
"company_document": {
"companyRegistrationDoc":"example_url",
"documentType": "vat",
"registationNumber": "1232223"
}
},
"isCompanyRegistration":false,
"role": "VERIFIED_SELLER"
}
当客户端发送
isCompanyRegsitration: true
和role: VERIFIED_SELLER
时,我需要确保company property exists
否则不会,当isCompanyRegistration:false
和role: SELLER
时,则personal_document is required
My Joi Schema
const schema = Joi.object({
email: Joi.string().trim().email().required(),
company: Joi.object({
company_name: Joi.string().trim().min(3).max(50).required(),
address: Joi.object({
city: Joi.string().trim().min(3).max(50).required(),
}),
company_document: Joi.object({
documentType: Joi.any().required().valid("pan", "vat"),
companyRegistrationDoc: Joi.string().required(),
registationNumber: Joi.string().required(),
}),
}),
personal_document: Joi.object({
document_type: Joi.any()
.required()
.valid(...Object.keys(DOCUMENT_TYPE)),
document_front: Joi.string().required(),
document_back: Joi.string().required(),
}),
isCompanyRegistration: Joi.boolean().required(),
role: Joi.any()
.required()
.valid(...Object.keys(ACCOUNT_TYPE)), //account type is VERIFIED_SELLER or SELLER
});
我试着做下面提到的事情,但它似乎不工作。我也尝试了xor和and,但我看不出值是真还是假
when("role", {
is: Joi.string().valid(ACCOUNT_TYPE.SELLER),
then: Joi.object({
document_type: Joi.any()
.required()
.valid(...Object.keys(DOCUMENT_TYPE)),
document_front: Joi.string().required(),
document_back: Joi.string().required(),
}),
}),
但这不起作用,似乎是多余的
尝试以下
const schema = Joi.object({
email: Joi.string().trim().email().required(),
company: Joi.object({
company_name: Joi.string().trim().min(3).max(50).required(),
address: Joi.object({
city: Joi.string().trim().min(3).max(50).required(),
}),
company_document: Joi.object({
documentType: Joi.any().required().valid("pan", "vat"),
companyRegistrationDoc: Joi.string().required(),
registationNumber: Joi.string().required(),
}),
}).when('role', { is: 'VERIFIED_SELLER', then: Joi.required() }),
personal_document: Joi.object({
document_type: Joi.any()
.required()
.valid(...Object.keys(DOCUMENT_TYPE)),
document_front: Joi.string().required(),
document_back: Joi.string().required(),
}).when('role', { is: 'SELLER', then: Joi.required() }),
isCompanyRegistration: Joi.boolean().required(),
role: Joi.any()
.required()
.valid(...Object.keys(ACCOUNT_TYPE)), //account type is VERIFIED_SELLER or SELLER
});
基本上你需要为每个对象添加内联条件
personal_document: Joi.object({...}).when('role', { is: 'SELLER', then: Joi.required() })
company: Joi.object({...}).when('role', { is: 'VERIFIED_SELLER', then: Joi.required() })