0

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()
    })

1 Answer 1

1

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()
  }),
Sign up to request clarification or add additional context in comments.

2 Comments

i updated the question , i forgot to add amount , bonus , userId to first set, [amount,bonus,userId] is common to both.
I have provided you the way to do it. If it is done for one field, I am sure you can workout for the other fields as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.