Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mixed version error from Joi.isSchema() for validation params set to "false" (nothing allowed). #756

Open
Invader444 opened this issue May 24, 2022 · 3 comments

Comments

@Invader444
Copy link

Invader444 commented May 24, 2022

Issue

Hapi 20 uses @hapi/validate internally, and for validation options set to false, it will use this as the schema:
Validator.object({}).allow(null)

Of course, its internal version will not match Joi's, and so we have a mixed version error from Joi.isSchema().

E.g.

response: {
  schema: false
}

Environment

npx envinfo --npmPackages '*hapi*' --binaries

Binaries:
Node: 14.16.1 - ~/.nvm/versions/node/v14.16.1/bin/node
npm: 6.14.12 - ~/.nvm/versions/node/v14.16.1/bin/npm
npmPackages:
hapi-swagger: ^14.5.1 => 14.5.1

Steps to Reproduce

Example route:

{
  method: 'GET',
  path: '/global',
  handler: (request, h) => {
    return null;
  },
  options: {
    tags: ['api'],
    response: {
      schema: false
    }
  }
}

Expected Behavior

The swagger documentation is generated normally.

Actual Behavior

This error is thrown when fetching swagger.json:

Error: Cannot mix different versions of joi schemas
    at new module.exports (~/[redacted]/node_modules/@hapi/hoek/lib/error.js:23:19)
    at module.exports (~/[redacted]/node_modules/@hapi/hoek/lib/assert.js:20:11)
    at Object.exports.isSchema (~/[redacted]/node_modules/joi/lib/common.js:132:5)
    at Object.utilities.isJoi (~/[redacted]/node_modules/hapi-swagger/lib/utilities.js:249:24)
    at module.exports.internals.properties.internals.properties.parseProperty (~/[redacted]/debugServer/node_modules/hapi-swagger/lib/properties.js:67:18)
    at module.exports.internals.responses.internals.responses.getResponse (~/[redacted]/node_modules/hapi-swagger/lib/responses.js:142:41)
    at module.exports.internals.responses.internals.responses.build (~/[redacted]/node_modules/hapi-swagger/lib/responses.js:52:29)
    at ~/[redacted]/node_modules/hapi-swagger/lib/paths.js:311:36
    at Array.forEach (<anonymous>)
    at module.exports.internals.paths.internals.paths.buildRoutes (~/[redacted]/node_modules/hapi-swagger/lib/paths.js:164:10)
@Invader444
Copy link
Author

Invader444 commented May 24, 2022

Not really sure what the optimal solution is here. Could maybe catch this error and try Validator.isSchema in the catch?

Then again, Validator shouldn't be used directly by anything outside Hapi internals...

@Invader444
Copy link
Author

The workaround is to explicitly set these to Joi.object({}).allow(null) so they use Joi (which is what Hapi 19 would generate) instead of false, but that isn't ideal, especially since the Hapi docs recommend false for this purpose...

@robmcguinness
Copy link
Collaborator

Related hapijs/hapi#4246

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants