I've got route file
import express, { Request, Response } from "express";
import * as authController from "../controllers/authController";
import {
validateUserRegistration,
} from "../validate/validationParams";
const router = express.Router();
router.post("/register", validateUserRegistration, authController.register);
export default router;
validationParams are in seperate file
import { check } from "express-validator";
export const validateUserRegistration = [
check("firstName", "First Name is required").isString(),
check("lastName", "Last Name is required").isString(),
check("email", "Email is required").isEmail(),
check("password", "Password with 6 or more characters required").isLength({
min: 6,
}),
];
and the main logic is in authController
import { validationResult } from "express-validator";
import catchAsync from "../utils/catchAsync";
import User from "../models/user";
export const register = catchAsync(async (req, res, next) => {
console.log("register");
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({
message: errors.array(),
});
} else {
console.log("errors are empty");
}
let user = await User.findOne({ email: req.body.email });
if (user) return res.status(400).json({ message: "User already exists" });
user = new User(req.body);
await user.save();
return res.status(201).send({ message: "User registered successfully" });
});
Unfortunately validator is not validating at all. when I run false req I get no errors. Is it possible to make it work properly or I must put everything in one file where it works fine ?