如何使用express-validator在单独的文件中实施验证 [英] How to implement validation in a separate file using express-validator

查看:363
本文介绍了如何使用express-validator在单独的文件中实施验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用express-validator来验证req.body,然后再发送将数据插入postgres的发布请求.

I am trying to use express-validator to validate the req.body before sending a post request to insert data to postgres.

我有一个路由文件,控制器文件,并且我想在一个名为validate.js的文件中进行验证.同时,我安装了express-validator,并在server.js中将其导入.我遇到的其他资源似乎在包含插入数据逻辑的函数中实现了验证.

I have a route file, controller file and I want to carryout validation in a file called validate.js. Meanwhile, I have installed express-validator and in my server.js I have imported it. Other resources I come across seem to implement the validation in the function that contains the logic for inserting the data.

//server.js
....
import expressValidator from 'express-validator';

...
app.use(bodyParser.urlencoded({ extended: false }));
app.use(expressValidator);

//route.js
import express from 'express';
import usersController from './controller';

const router = express.Router();

router.post('/createuser', usersController.createUser);

//controller.js
createUser(req, res){
 ...
const { firstName, lastName, email, password } = req.body;
//code to insert user details to the database
}

//validator.js
import { check } from 'express-validator/check';

module.exports = [check('email').isEmail()];

我希望在一个名为validateator.js的文件中实现验证,例如,在插入数据库之前先验证电子邮件

I expect to implemet the validation in a file called validator.js to, say, validate the email before inserting to the database

推荐答案

这是我使用express-validator的方式.我有一个文件validator.js,其中有许多路由的验证逻辑.例如:

validator.js

Here is the way i use express-validator. I have a file validator.js where i have validation logic for many routes. For example:

validator.js

const { check } = require('express-validator/check');

exports.createUser = [check('email').isEmail()];

exports.anotherRoute = [// check data];

exports.doSomethingElse = [// check data];

现在在您的路由文件中,您需要validator.js文件
const validator = require("./validator"); // or where your file is located
并使用您想要的验证逻辑作为中间件.例如:

route.js

Now in your route file you require the validator.js file
const validator = require("./validator"); // or where your file is located
and use the validation logic you want as a middleware. For example:

route.js

//

router.post('/createuser', validator.createUser, usersController.createUser);

最后,在需要validationResult之后,您必须在控制器内部检查在验证期间创建的可能错误.

controller.js

Last, inside your controller you have to check for possible errors created during validation, after requiring validationResult.

controller.js

const { validationResult } = require('express-validator/check');


exports.createUser(req, res) {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
        return res.status(422).json({ errors: errors.array() });
    }
    // do stuff here.

}

此外,您不必在server.js文件中使用app.use(expressValidator);

Also, you don't have to use app.use(expressValidator); in your server.js file

这篇关于如何使用express-validator在单独的文件中实施验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆