Skip to content

Extension which has a return value in setup() does not call validate() #1747

@ben174

Description

@ben174

Context

  • node version: 8.11.3
  • joi version: 11.4.0
  • environment (node, browser): node
  • used with (hapi, standalone, ...): hapi
  • any other relevant information:

What are you trying to achieve or the steps to reproduce ?

I have created an extension which implements both setup() and validate(). I expect both methods to be called, which they are. But if my setup() method returns a value, validate() is skipped completely and never called.

I would like to be able to have my setup() return a value and still be able to to validation in validate().

const WebhookUrlExtension = (joi) => joi.extend({
  base: joi.string(),
  name: 'string',
  rules: [
    {
      name: 'webhookUrl',
      validate (params, value, state, options) {
        console.log('VALIDATE') // not firing :( 
        return value;
      },
      params: {
        options: joi.object({
          scheme: joi.array().items(joi.string()).optional()
        })
      },
      setup (params) {
        console.log('SETUP') // this gets executed fine
        const regex = /^.+:\/\/.+/;
        const uriOpts = {
          scheme: Hoek.reach(params, 'options.scheme', { default: ['https'] })
        };
        return Joi.string().uri(uriOpts).regex(regex);  // if I remove this return statement, validate does run
      }
    }
  ]
});

Which result you had ?

Only setup() is called

What did you expect ?

setup() to be called as well as validate()

Metadata

Metadata

Assignees

Labels

bugBug or defect

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions