Home > Blockchain >  I am getting "Joi.validate is not a function" when I try and send a request through postma
I am getting "Joi.validate is not a function" when I try and send a request through postma

Time:09-22

Hello I am pretty new to node.js and postman. I am having a problem when I try to send a body raw message from postman I get a TypeError in my terminal saying Joi.validate is not a function. I'm not sure what my problem is any help would be appreciated thanks in advance.

 //Validation
    const Joi = require('joi');
    
    //Register Validation
    const registerValidation = data  => {
    const schema = {
        name: Joi.string()
            .min(6)
            .required(),
        email: Joi.string()
            .min(6)
            .required()
            .email(),
        password: Joi.string()
            .min(6)
            .required()
        };
        return Joi.validate(data, schema);
    };
    
    //Login Validation
    const loginValidation = data  => {
        const schema = {
            email: Joi.string()
                .min(6)
                .required()
                .email(),
            password: Joi.string()
                .min(6)
                .required()
                
            };
            return Joi.validate(data, schema);
        };
    
    module.exports.registerValidation = registerValidation;
    module.exports.loginValidation = loginValidation;

CodePudding user response:

Validation is part of the schema you've created with Joi and not a method on the library itself.

Check the docs for it:

//Validation
const Joi = require('joi');
    
    //Register Validation
    const registerValidation = data  => {
        const schema = Joi.object({
            name: Joi.string()
                .min(6)
                .required(),
            email: Joi.string()
                .min(6)
                .required()
                .email(),
            password: Joi.string()
                .min(6)
                .required()
        });
        return schema.validate(data);
    };
    
    //Login Validation
    const loginValidation = data  => {
        const schema = Joi.object({
            email: Joi.string()
                .min(6)
                .required()
                .email(),
            password: Joi.string()
                .min(6)
                .required()
            };
            return schema.validate(data;
        });
    
    module.exports.registerValidation = registerValidation;
    module.exports.loginValidation = loginValidation;

One thing to keep in mind is that you could define your schemas outside the arrow functions to increase performance.

  • Related