Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested objects validation not working as expected #2959

Closed
MallRoy opened this issue Jun 27, 2023 · 2 comments
Closed

Nested objects validation not working as expected #2959

MallRoy opened this issue Jun 27, 2023 · 2 comments
Labels
support Questions, discussions, and general support

Comments

@MallRoy
Copy link

MallRoy commented Jun 27, 2023

Support plan

  • *is this issue currently blocking your project?: yes
  • *is this issue affecting a production system?: yes

Context

  • node version: 14.17.0
  • module version: 17.9.1
  • environment : node
  • *used with: express framework
  • any other relevant information: Please test the below schema in https://joi.dev/tester/

How can we help?

I am expecting error in this case, but the validation is getting success.

Joi.object({
  isTemplate: Joi.boolean().optional().valid(true, false),
  fileId: Joi.string().optional(),
  req:Joi.object({ 
    template: Joi.alternatives().conditional('isTemplate', {
      is: Joi.valid(true),
      then: Joi.string().required(),
      otherwise: Joi.string().optional(),
    })
  })
});

Sample data: { 
 isTemplate: true,
 fileId: "435"
}
@MallRoy MallRoy added the support Questions, discussions, and general support label Jun 27, 2023
@BenceSzalai
Copy link

You need to add .required() to the req: Joi.object(/* ... */) part:

Joi.object({
  isTemplate: Joi.boolean().optional().valid(true, false),
  fileId: Joi.string().optional(),
  req:Joi.object({ 
    template: Joi.alternatives().conditional('isTemplate', {
      is: Joi.valid(true),
      then: Joi.string().required(),
      otherwise: Joi.string().optional(),
    })
  }).required()
});

@aiyeola
Copy link

aiyeola commented May 7, 2024

@MallRoy change .alternatives().conditional to .when and viola

@Marsup Marsup closed this as completed May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

4 participants