Reputation: 85
In the following schema i need like , if type is withdraw => [name,accountNumber,ifscCode,branchName,comments,upiId,amount,bonus,userId] to pass but if type is deposit i need => [txnReferenceId,amount,bonus,userId] to pass.
const withdrawDepositValidatorSchema = Joi.object({
type: Joi.string(),
name: Joi.string(),
accountNumber: Joi.number(),
ifscCode: Joi.string().alphanum(),
branchName: Joi.string(),
comments: Joi.string(),
upiId: Joi.string(),
txnReferenceId: Joi.number(),
amount: Joi.number(),
bonus: Joi.number(),
userId: Joi.string()
})
Upvotes: 0
Views: 173
Reputation: 7770
You can use when
condition. I am giving one example here but can be done for other fields as well.
const withdrawDepositValidatorSchema = Joi.object({
type: Joi.string(),
name: Joi.when(Joi.ref("type"), {
"is": Joi.string().valid("Withdraw"),
"then": Joi.string(),
"otherwise": Joi.forbidden()
}),
Upvotes: 1