'Pick schema of key in Joi schema

If I have a JOI schema

const OBJ1 = Joi.object({
  foo: Joi.number(), 
  bar: Joi.string().pattern(/abc/)
});

And then I want to have a second schema OBJ2 which uses the same /abc/ schema as the bar property in OBJ1 for another property:

const OBJ2 = Joi.object({
  baz: Joi.string().pattern(/abc/)}
);

Does the API then support picking the schema for the key bar out of OBJ1 instead of definining it again in OBJ2?

I realize I can do the following:

const ABC = Joi.string().pattern(/abc/);
const OBJ1 = Joi.object({
  foo: Joi.number(), 
  bar: ABC
});
const OBJ2 = Joi.object({
  baz: ABC
});

But what I'm after is something like:

const OBJ1 = Joi.object({
  foo: Joi.number(), 
  bar: Joi.string().pattern(/abc/)
});
const OBJ2 = Joi.object({
  baz: OBJ1.pick('bar')
});

But I wasn't able to find anything like this in the JOI API reference.

joi


Solution 1:[1]

I found the answer myself: extract(key):

const OBJ2 = Joi.object({
  baz: OBJ1.extract('bar')
});

Simple as that, but hard to find in the docs, apparently.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JHH