Joi override keys nested schemas

Viewed 646

So I have a schema like

const buttonSchema = Joi.object().keys({
      object: Joi.string().valid('button').required(),
      id: Joi.string().required(),
      text: textSchema.required(),
      icon: Joi.string().uri({
        scheme: 'https'
      }),
      action: actionSchema,
      styles: stylesSchema.required()
    });
const stylesSchema = Joi.object().keys({
      width: Joi.string().pattern(/^[1-9][0-9]?%$|^100%$/).required(),      // 1 - 100 %
      height: Joi.number().positive(),
      position: Joi.string().valid('left', 'center', 'right'),
      align: Joi.string().valid('left', 'center', 'right'),
      default: defaultStylesSchema.required(),
      disabled: defaultStylesSchema.required(),
      pressed: defaultStylesSchema.required()
    });

And in another file I'm trying to override the buttonSchema.styles.align property .. to basically add a required()..

I have tried a lot of variation .. This is the most prominent one

const reactionsButtonSchema = buttonSchema.keys({
      styles: buttonSchema.styles.keys({
        align: Joi.string().valid('left', 'center', 'right').required(),
      })
    });

But it doesn't work.. Anyone have an idea on how to achieve this?

1 Answers

After a support response from JOI, this can be done using

const reactionsButtonSchema = buttonSchema.concat(Joi.object({ styles: { align: Joi.required() }}));

If anyone else faces this, you have the answer.

Related