0

I have an example model constructed like so:

const MyModelResponse = Joi.object().keys({
  id: Joi.number().integer().required()
    .description('ID of the example model'),
  description: Joi.string()
    .description('Description of the example model'),
})
  .description('example instance of MyModel with the unique ID present')
  .label('MyModelResponse');

In my route, I want to make sure that my input parameter is validated against the id property of MyModeResponse like so:

     validate: {
        params: {
          id: MyModelResponse.id,
        },
        options: { presence: 'required' },
        failAction: failAction('request'),
      }

This results in the schema validation error upon server start:

AssertionError [ERR_ASSERTION]: Invalid schema content: (id)

Is there a way to reference a key of a schema? Currently, I have to resort to either of the following:

Not referencing my MyModelResponse schema at all:

     validate: {
        params: {
          id: Joi.number().integer().description('id of MyModel instance to get'),
        },
        options: { presence: 'required' },
        failAction: failAction('request'),
      }

Not using the Joi.object.keys() constructor by defining my model like this:

const MyModelResponse = {
  id: Joi.number().integer().required()
    .description('ID of the example model'),
  description: Joi.string()
    .description('Description of the example model'),
}

The bottom approach allows me to reference the id property in my route but doesn't allow me to add descriptions and labels to my schema. I have tried using MyModel.describe().children.id in my route validation and I have made some attempts to deserialize the id object into a schema object to no avail.

Any suggestions would be appreciated.

HailZeon
  • 954
  • 9
  • 17

1 Answers1

1

Remove the keys() and use as follows

const MyModelResponse = Joi.object({
  id: Joi.number().integer().required()
    .description('ID of the example model'),
  description: Joi.string()
    .description('Description of the example model'),
})
  .description('example instance of MyModel with the unique ID present')
  .label('MyModelResponse');