Api Validation failed

When I try to validate the user it does not works.‘Validate register input’ does not helps me to validate the user.How can i fix it?

const router= express.Router();
const User= require('../../Models/User');
const bcrypt = require('bcryptjs');
const keys = require('../../config/keys');
const jwt  = require('jsonwebtoken');
const passport =require ('passport');
const validateRegisterInput =require('../../validation/register');
//const ValidateLoginInput = require ('../../validation/login');

router.get('/test', (req,res) =>{
    res.json({msg:"This is a user route"});
});

router.get("/current", passport.authenticate ("jwt",{session: false}),
 (res,req) =>{
   res.send(req.user);
 }
) 

router.post('/register', (req, res) => {
  const { errors, isValid } = validateRegisterInput(req.body);

  if (!isValid) {
    return res.status(400).json(errors);
  }

  User.findOne({ email: req.body.email })
    .then(user => {
      if (user) {
        // Use the validations to send the error
        errors.email = 'Email already exists';
        return res.status(400).json(errors);
      } else {
        const newUser = new User({
          name: req.body.name,
          email: req.body.email,
          password: req.body.password
        })
      }
    })
}) 

Hello there.

Do you have a question?

If so, please edit your post to include it.

The more information you give us, the more likely we are to be able to help.

@JeremyLT When I try to validate the user in postman it does not validate it. How can I makes my code work.

@JeremyLT Here is my repo https://github.com/naima-shk/Twitter-Clone

Your validator is incorrect, you’re checking if isEmpty and negating the result, i.e. if value is not empty it returns false which you negate to true and if statement executes.

I didn’t really look much at it but I did see one thing that doesn’t look right.

register.js

return {
    errors,
    inValid:  Object.keys(errors).length === 0
}

users.js

const { errors, isValid } = validateRegisterInput(req.body);

@jenovs Can you elaborate on it? please send me the edited code, if possible.

@lasjorg So What will be the solution for this?

@jenovs @lasjorg When I try ''console.log(req.body) " inside the post request the output is in the attached image.

  const { errors, isValid } = validateRegisterInput(req.body);
  console.log(req.body);
  if (!isValid) {
    return res.status(400).json(errors);
  }

  User.findOne({ email: req.body.email })
    .then(user => {
      if (user) {
        // Use the validations to send the error
        errors.email = 'Email already exists';
        return res.status(400).json(errors);
      } else {
        const newUser = new User({
          name: req.body.name,
          email: req.body.email,
          password: req.body.password
        })
      }
    })
})

Look at the property name on the return object and the variable name.

inValid
isValid

The function name isEmpty communicates the intended usage. Please do read the docs for the package.

@lasjorg I have changed inValid to isValid But does not work for me. I’m unable to fix it eventually.

Is there any function we can use aside for inValid?

@Chrstnslvdr I have no idea about that. Can you please help?