Skip to content

Destructuring Throws Fatal #1631

@shellscape

Description

@shellscape

Context

  • node version: 10.11.0
  • joi version: 14.0.3
  • environment (node, browser): Node
  • used with (hapi, standalone, ...): standalone
  • any other relevant information:

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

const Joi = require('joi');
const { boolean,validate } = Joi;
const schema = boolean(); // source of error
const result = validate(true, schema);

Which result you had ?

AssertionError [ERR_ASSERTION]: Must be invoked on a Joi instance.
    at new AssertionError (internal/assert.js:268:11)
    at Object.exports.assert (/user/proj/node_modules/hoek/lib/index.js:517:11)
    at internals.callWithDefaults (/user/proj/node_modules/joi/lib/index.js:31:10)
    at root.boolean.root.bool (/user/proj/node_modules/joi/lib/index.js:73:43)

What did you expect ?

Prepending Joi onto the beginning of every assertion type is rather messy (completely subjective, personal opinion) and while I absolutely dig the expressive nature of joi, I'm looking for ways to dial down the noise in code and make the schema setup a little cleaner. I was very surprised to learn that none of the assertion methods can be safely destructured from the main instance (it's not mentioned anywhere in the docs nor issues).

It's worth noting that this fails as well:

const boolean = Joi.boolean;
const validate = Joi.validate;
const schema = boolean(); // source of error
const result = validate(true, schema);

I could get nuts and implement a Proxy to make the assignment happen, but I'd rather not slow down the validation process just for the sake of some pretty code. I'm really hoping there's a way you can cater to those who want to use the assertions and destructure the method names. I'm being pushed hard to use yup for that reason alone, but I prefer the nuances of joi.

Metadata

Metadata

Assignees

Labels

featureNew functionality or improvement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions