1

I am trying to write a validation method for the following object ( associative array ):

{
  "10:00": {
    discount: 10,
    time: "10:00",
  },
  "11:00": {
    discount: 11,
    time: "11:00",
  },
  ...
  ....
}

Using Joi (https://github.com/hapijs/joi) What i got so far is this:

Joi.object().keys(
            {time:{
                discount: Joi.number(),
                time: Joi.string(),
            }}
        ),

which is obviously wrong and failing with : ValidationError: child "discounts" fails because ["10:00" is not allowed, "11:00" is not allowed]

can anyone suggest how to write validation for objects with variable number of keys ( associative array )

Shrouk Khan
  • 1,440
  • 6
  • 27
  • 53

1 Answers1

1

sorted it out after reading through : Is there a way to validate dynamic key names?

Joi.object().pattern(/^/, [
            Joi.object({
                discount: Joi.number(),
                time: Joi.string()
            })
        ])
Shrouk Khan
  • 1,440
  • 6
  • 27
  • 53