admin管理员组

文章数量:1193328

I am a beginner and currently working my way through a node.js API authentication tutorial. All going well until having to refactor code into separate files and now I keep getting the following error "TypeError: schema.validate is not a function" when sending a JSON request via Postman, regardless if the request should fail the validation or not.

Here is my code for validation.js

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

//Register Validation - wrap it in a function as there will be multiple schemas
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.assert(data, schema);

}
// Login Validation

const loginValidation = data => {

    const schema = {
        
        email: Joi.string().min(6).required().email(),
        password: Joi.string().min(6).required()
    
        }
    
    return Joi.assert(data, schema);
    
    }

module.exports.registerValidation = registerValidation;
module.exports.loginValidation = loginValidation;

My auth.js code is like so

const router = require('express').Router();
const User = require('../model/User');
const { registerValidation } = require('../validation');

router.post('/register', async (req, res) => {

//Validate data before creating user

// const { error } = schema.validate(req.body);
const { error } = registerValidation(req.body);
if(error) return res.status(400).send(error.details[0].message);


    const user = new User({
        name: req.body.name,
        email: req.body.email,
        password: req.body.password,
        date: req.body.date
    });
    try {
        const savedUser = await user.save();
        res.send(savedUser);

    }catch(err){
        res.status(400).send(err);
    }

});

router.post('/login')

module.exports = router;

Can someone assist at all? The only thing I have changed is that I was using Joi.validate which is no longer valid according to the docs so I have updated it to Joi.assert.

Many thanks

I am a beginner and currently working my way through a node.js API authentication tutorial. All going well until having to refactor code into separate files and now I keep getting the following error "TypeError: schema.validate is not a function" when sending a JSON request via Postman, regardless if the request should fail the validation or not.

Here is my code for validation.js

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

//Register Validation - wrap it in a function as there will be multiple schemas
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.assert(data, schema);

}
// Login Validation

const loginValidation = data => {

    const schema = {
        
        email: Joi.string().min(6).required().email(),
        password: Joi.string().min(6).required()
    
        }
    
    return Joi.assert(data, schema);
    
    }

module.exports.registerValidation = registerValidation;
module.exports.loginValidation = loginValidation;

My auth.js code is like so

const router = require('express').Router();
const User = require('../model/User');
const { registerValidation } = require('../validation');

router.post('/register', async (req, res) => {

//Validate data before creating user

// const { error } = schema.validate(req.body);
const { error } = registerValidation(req.body);
if(error) return res.status(400).send(error.details[0].message);


    const user = new User({
        name: req.body.name,
        email: req.body.email,
        password: req.body.password,
        date: req.body.date
    });
    try {
        const savedUser = await user.save();
        res.send(savedUser);

    }catch(err){
        res.status(400).send(err);
    }

});

router.post('/login')

module.exports = router;

Can someone assist at all? The only thing I have changed is that I was using Joi.validate which is no longer valid according to the docs so I have updated it to Joi.assert.

Many thanks

Share Improve this question edited Jul 25, 2020 at 12:28 Ju_ asked Jul 25, 2020 at 12:21 Ju_Ju_ 751 gold badge1 silver badge9 bronze badges 2
  • Where did you refer to see that joi.validate is not valid? – Deekshith Hegde Commented Jul 25, 2020 at 12:36
  • I think I read it on GitHub which referred to the documents for version 16. I was getting “joi.validate is not a function” – Ju_ Commented Jul 25, 2020 at 12:45
Add a comment  | 

3 Answers 3

Reset to default 14

For anyone still getting "schema.validate is not a function":

When you declare the schema, make sure you declare Joi.object, like so:

const schema = Joi.object({
   username: Joi.string().etc
})

Use the below approach for validation as joi.validate is no longer supported

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)

Declare like this const schema = Joi.object({username: Joi.string().etc })

This thing works!!

Normally you are declaring like this:- const schema = {username: Joi.string().etc }

Instead of that declare like that
const schema = Joi.object({username: Joi.string().etc })
Note: In the above version of 17.0.0, there is no Joi.validate function so with above thing you have to use schema.validate(req.body)

It will work.

本文标签: