Tengo este validador en mi ruta de registro :
router.post( "/register", // username must be an email body("username") .isLength({ min: 4 }) .withMessage("username must be at least 4 chars long") .isLength({ max: 12 }) .withMessage(" username must be less than 12 chars long") .exists() .withMessage("username is required") .trim() .matches(/^[A-Za-z0-9\_]+$/) .withMessage("username must be alphanumeric only") .escape(), body("email") .isEmail() .normalizeEmail() .withMessage("Invalid Email") .exists(), body("password") .isLength({ min: 5 }) .withMessage("password must be at least 5 chars long") .isLength({ max: 30 }) .withMessage("password must be at max 30 chars long") .matches(/\d/) .withMessage("password must contain a number") .exists(), (req, res) => { // Finds the validation errors in this request and wraps them in an object with handy functions const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({ errors: errors.array() }); } const username = Utils.Clean_input(req.body.username); const email = Utils.Clean_input(req.body.email); const password = Utils.Clean_input(req.body.password); const salthash = Utils.genPassword(password); const salt = salthash.salt; const hash = salthash.hash; const newuser = new User({ username: username, email: email, hash: hash, salt: salt, }); try { newuser.save((err, data) => { if (data) { res.status(201).json({ status: 201, sucess: true, message: "Register Success, verify your email and login", }); } else { res.status(400).json({ err }); } }); } catch (error) { res.status(500).json({ errors: "Server is down try again later" }); } } );¿Puedo poner esos métodos de validación en una función personalizada en otro archivo .js e importarlos? Si es así, ¿cómo?
ejemplo: router.post("/register",validation_fucntion,(res,req){..}) , la función de validación maneja la validación de otro archivo .js que se importa al proyecto
Express middleware también se puede declarar en una matriz para su reutilización. Entonces puede crear la función de validation que devuelve una matriz con los validadores.
P.ej
app.ts :
import express from 'express'; import { body, validationResult } from 'express-validator'; function UserRegisterValidation() { return [ body('username') .isLength({ min: 4 }) .withMessage('username must be at least 4 chars long') .isLength({ max: 12 }) .withMessage(' username must be less than 12 chars long') .exists() .withMessage('username is required') .trim() .matches(/^[A-Za-z0-9\_]+$/) .withMessage('username must be alphanumeric only') .escape(), body('email').isEmail().normalizeEmail().withMessage('Invalid Email').exists(), body('password') .isLength({ min: 5 }) .withMessage('password must be at least 5 chars long') .isLength({ max: 30 }) .withMessage('password must be at max 30 chars long') .matches(/\d/) .withMessage('password must contain a number') .exists(), ]; } const app = express(); const port = 3000; app.use(express.json()); app.post('/register', UserRegisterValidation(), (req, res) => { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({ errors: errors.array() }); } res.sendStatus(200); }); app.listen(port, () => console.log('HTTP server started at port 3000'));Prueba:
⚡ curl -i -X POST -H "Content-Type: application/json" -d '{"username": "teresa teng"}' http://localhost:3000/register HTTP/1.1 400 Bad Request X-Powered-By: Express Content-Type: application/json; charset=utf-8 Content-Length: 295 ETag: W/"127-wBnvezB+j1d3j/nM62Y93ii4FtM" Date: Thu, 09 Dec 2021 02:49:12 GMT Connection: keep-alive Keep-Alive: timeout=5 {"errors":[{"msg":"Invalid Email","param":"email","location":"body"},{"msg":"password must be at least 5 chars long","param":"password","location":"body"},{"msg":"password must contain a number","param":"password","location":"body"},{"msg":"Invalid value","param":"password","location":"body"}]}% ⚡ curl -i -X POST -H "Content-Type: application/json" -d '{"username": "teresa teng", "password": "password123", "email": "test@gmail.com"}' http://localhost:3000/register HTTP/1.1 200 OK X-Powered-By: Express Content-Type: text/plain; charset=utf-8 Content-Length: 2 ETag: W/"2-nOO9QiTIwXgNtWtBJezz8kv3SLc" Date: Thu, 09 Dec 2021 02:45:04 GMT Connection: keep-alive Keep-Alive: timeout=5 OK%