Joi: possible to extend a base schema?

Created on 13 Jun 2014  路  5Comments  路  Source: sideway/joi

Is it possbile to extend a base schema or overwritte some keys of the base schema.
I have a base type, with many subtypes. A lot of the items are the same for all subtypes, but some keys are different and so I wanted to reuse the definition of the base type:

var base = Joi.object().keys({
  type: Joi.string().required(),
  x: ...
  y: ....
  z: Joi.object().keys({
    .....
  })
});

var concrete1 = clone(base);
concrete1.type = ....
concrete1. z.a = ...

Is something like this possible? If so how?

support

Most helpful comment

var base = Joi.object({ a: Joi.string() });
var other = base.keys({ b: Joi.number() });

All 5 comments

var base = Joi.object({ a: Joi.string() });
var other = base.keys({ b: Joi.number() });

Thanks!

@hueniverse I think the documentation should mention this. I just read that the keys function "Sets the allowed object keys", which sounds like it replaces any previous ones. I had to search the issues to find this solution.

You can open a PR to fix the documentation.

Done as PR #479.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PaunPrashant picture PaunPrashant  路  3Comments

longweiquan picture longweiquan  路  3Comments

JbIPS picture JbIPS  路  4Comments

Dreamystify picture Dreamystify  路  4Comments

REBELinBLUE picture REBELinBLUE  路  3Comments