Joi: TypeError: Joi.validate is not a function

Created on 3 Oct 2019  路  12Comments  路  Source: sideway/joi

Hi,

I have the validation like this
`private validateInput(envConfig: EnvConfig): EnvConfig {
const envVarsSchema: Joi.ObjectSchema = Joi.object({
NODE_ENV: Joi.any()
.valid('development', 'production', 'test')
.default('development'),
PORT: Joi.number().default(3000),
API_AUTH_ENABLED: Joi.boolean().required(),
ELASTIC_URL:Joi.string().required(),
ELASTIC_USER:Joi.string().required(),
ELASTIC_PWD:Joi.string().required(),
});

const { error, value: validatedEnvConfig } = Joi.validate(envConfig,envVarsSchema);
if (error) {
  throw new Error(`Config validation error: ${error.message}`);
}
return validatedEnvConfig;

}`

I am getting error in below line, How to validate this schema and return error or value ?

const { error, value: validatedEnvConfig } = Joi.validate(envConfig,envVarsSchema);
^
TypeError: Joi.validate is not a function

follow instructions 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 12 comments

Seriously, use search. Duplicate of #2125 and #2145.

Hi Marsup, Sorry I didn't search the closed issues, What is the equivalent/alternate function for this validate?

validate on the schema itself.

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);

You can use assert instead of validate:

function validateUser(user) {
const schema = Joi.object({
username: Joi.string().min(6).max(50).required(),
email: Joi.string().min(6).max(255).required().email(),
password: Joi.string().min(6).max(255).required()
});

return Joi.assert(user, schema);

}

@Marsup can be a bit kinder to the community seeking help.

Sorry but I have little patience when things are one search away with the exact same words, also getting rid of the issue template shows some level of disrespect for maintainers. I don't think you realize the toll issues take on maintainers.

The problem is all of the articles and tutorials that I came across have it the old way, so many people new to Joi probably implement it using the old Joi.validate way copying from tutorials. Of course, then when I googled, I found this, so.. shrug

const schema = {
number: Joi.number().min(4).required()
};
const validate = Joi.validate(req.body, schema);

It is now:
const schema = Joi.object({
number: Joi.number().min(4).required()
});

const validation = schema.validate(req.body);
console.log(validation);

Changing the schema to joi.object is often overlooked, so @carlosag012 's example informative. thank you.

Simply you can check whether you have installed @hapi/joi not joi only
and you can use this approach for validation
const schema=Joi.object().keys({ username: Joi.string().min(5).max(50), email: Joi.string().required().email().min(5).max(255), password: Joi.string().required().min(5).max(1024), }); const validation = schema.validate(req.body); if(validation.error){ res.status(400).send(validation.error.details[0].message); return ; }

Was this page helpful?
0 / 5 - 0 ratings

Related issues

normancarcamo picture normancarcamo  路  3Comments

Taxi4you picture Taxi4you  路  3Comments

REBELinBLUE picture REBELinBLUE  路  3Comments

PaunPrashant picture PaunPrashant  路  3Comments

n-sviridenko picture n-sviridenko  路  3Comments