Skip to content

Instantly share code, notes, and snippets.

@suhas86
Created July 5, 2020 06:44
Show Gist options
  • Save suhas86/5b0ebfbce6c57d6d54cf75481e2dca83 to your computer and use it in GitHub Desktop.
Save suhas86/5b0ebfbce6c57d6d54cf75481e2dca83 to your computer and use it in GitHub Desktop.
import express, { Request, Response } from "express";
import { body, check, validationResult } from "express-validator";
const router = express.Router();
router.post(
"/api/users/signup",
[
body("firstName")
.trim()
.not()
.isEmpty()
.withMessage("First Name is required"),
body("email").isEmail().withMessage("Email must be valid"),
body("password")
.trim()
.isLength({ min: 6 })
.withMessage("Password must be minimum of 6 characters"),
check("password").custom((val, { req }) => {
if (val !== req.body.confirmPassword) {
throw new Error("Passwords don't match");
} else {
return val;
}
}),
],
async (req: Request, res: Response) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).send(errors.array());
}
return res.send({});
}
);
export { router as SignupRoute };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment