joi icon indicating copy to clipboard operation
joi copied to clipboard

Is there a fix for "Must be invoked on a Joi instance"?

Open catamphetamine opened this issue 6 years ago • 4 comments

import { object, string, number, array } from '@hapi/joi';
...

throws Must be invoked on a Joi instance.

There has been an issue about it, that has been locked for some reason: https://github.com/hapijs/joi/issues/1349

Has anything changed since then? Is there a fix for this bug now?

catamphetamine avatar Feb 06 '20 00:02 catamphetamine

Ok, this seems to work:

import Joi from '@hapi/joi';
// import { object, string, number, array, any } from '@hapi/joi';

const object = (args) => Joi.object(args);
const string = () => Joi.string();
const number = () => Joi.number();
const array = () => Joi.array();
const any = () => Joi.any();

There's also types() function. https://hapi.dev/family/joi/api/?v=17.1.0#types But it returns different API.

catamphetamine avatar Feb 06 '20 01:02 catamphetamine

very helpful

fprackwieser avatar Jun 05 '21 11:06 fprackwieser

the destructuring could be your issue, you can instead import Joi from @hapi/joi and access the keys directly, like so Joi.object(), Joi.string()...

AnayoOleru avatar Nov 23 '21 20:11 AnayoOleru

For anyone stumbling on this issue, this was implemented long ago with https://joi.dev/api/?v=17.5.0#types

Marsup avatar Dec 02 '21 10:12 Marsup