Joi: TypeError: Joi.validate is not a function

Created on 24 Sep 2019  路  4Comments  路  Source: sideway/joi

Context

  • node version: v10.16.3
  • joi version: 14.3.1
  • environment (node, browser): node, safari
  • used with (hapi, standalone, ...): javascript
  • any other relevant information:

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

const Joi = require('@hapi/joi');

// set scheme for joi
const schema = Joi.object().keys({
name: Joi.string().min(6).max(30).required(),
birthyear: Joi.number().integer().min(1970).max(2019)
});

// data to validate
const dateToValidate = {
name: 'chris',
birthyear: 1971
}

// validate
const result = Joi.validate(dateToValidate, schema);

TypeError: Joi.validate is not a function
at Object. (/Users/tuts/javaScript/ValidationWithJoi/app.js:22:20)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

const schema = Joi.any()

Which result you had ?

TypeError: Joi.validate is not a function

What did you expect ?

compile without error

non issue

Most helpful comment

For the new version

const schema = Joi.object({ name: Joi.string() .min(6) .required(),
email: Joi.string() .min(6) .required() .email(),
password: Joi.string() .min(6) .required() });

const validation = schema.validate(req.body);
res.send(validation);

All 4 comments

Are you absolutely sure you're using joi 14 ? I'd bet not and if so it's a duplicate of #2125.

You fix it by changing joi.validate(request, validationSchema to validationSchema.validate(request As joi.validate() is no longer supported in v16. It is clearly documented in the API docs and release notes.

shareedit
answered Sep 23 at 6:11

Eran Hammer

For the new version

const schema = Joi.object({ name: Joi.string() .min(6) .required(),
email: Joi.string() .min(6) .required() .email(),
password: Joi.string() .min(6) .required() });

const validation = schema.validate(req.body);
res.send(validation);

For the new version

const schema = Joi.object({ name: Joi.string() .min(6) .required(),
email: Joi.string() .min(6) .required() .email(),
password: Joi.string() .min(6) .required() });

const validation = schema.validate(req.body);
res.send(validation);

Hey, it roks, thank you. @vahe-nikoghosyan

Was this page helpful?
0 / 5 - 0 ratings