joi icon indicating copy to clipboard operation
joi copied to clipboard

Validation with multiple refs

Open tbolt opened this issue 1 year ago • 0 comments

Support plan

  • is this issue currently blocking your project? (yes/no): no
  • is this issue affecting a production system? (yes/no): no

Context

  • node version: 16.16.0
  • module version: unsure
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): react-hook-form / standalone
  • any other relevant information:

How can we help?

I need to validate with a structure like the one provided below. Each object is keyed 1-4 for 4 quarters of the year. Within them there's an object that contains a percentage. I need to validate that ALL 4 quarters percentages add up to 100, otherwise I want to return a validation message. I need to do it per percent field.

  1: Joi.object({
    employees: Joi.object({
      percent: Joi.number()
    }),
  }),
  2: Joi.object({
    employees: Joi.object({
      percent: Joi.number()
    }),
  }),
  3: Joi.object({
    employees: Joi.object({
      percent: Joi.number()
    }),
  }),
  4: Joi.object({
    employees: Joi.object({
      percent: Joi.number()
    }),
  }),

I've tried different combinations of using an expression and Joi.alternatives.conditional() but I don't think I am getting the template syntax right.

Thanks

tbolt avatar Oct 07 '22 13:10 tbolt