admin管理员组文章数量:1332360
I'm using the suggested V4 syntax of express-validator in a Node.js project:
const { check, validationResult } = require('express-validator/check');
const { matchedData } = require('express-validator/filter');
and
app.post('/users/add/',[
check('first_name')
.isLength({ min: 1 }).withMessage('A first name is required')],
(req, res, next) => {
var errors = validationResult(req);
if (errors) {
..etc
Note that I am using the modern syntax and do not have the following code (as per the express-validator README.md file):
const expressValidator = require('express-validator');
app.use(expressValidator());
How do I trim blank spaces from the input field before running the validation?
I'm using the suggested V4 syntax of express-validator in a Node.js project:
const { check, validationResult } = require('express-validator/check');
const { matchedData } = require('express-validator/filter');
and
app.post('/users/add/',[
check('first_name')
.isLength({ min: 1 }).withMessage('A first name is required')],
(req, res, next) => {
var errors = validationResult(req);
if (errors) {
..etc
Note that I am using the modern syntax and do not have the following code (as per the express-validator README.md file):
const expressValidator = require('express-validator');
app.use(expressValidator());
How do I trim blank spaces from the input field before running the validation?
Share Improve this question asked Sep 14, 2017 at 7:48 SteveCSteveC 6877 silver badges12 bronze badges 1- As of v4.1.1, sanitization is only available in the legacy APIs, but please open an issue in the GitHub tracker requesting support in the new APIs. – gustavohenke Commented Sep 14, 2017 at 11:55
2 Answers
Reset to default 6You should use .trim()
before your .check()
like this,
check('first_name').trim().isLength({ min: 1 }).withMessage('A first name is required')]
Hope this helps!
The following work around may help, though I think the best solution would be to support sanitation in a fluent manner. The downside of my work around is that the processing of input is now in two places.
Add the required sanitation functions to requires clause:
const { trim } = require('express-validator').validator
Adjust the request processing:
app.post('/users/add/', (req, res, next) => { req.body.first_name = trim(req.body.first_name); req.body.last_name = trim(req.body.last_name); req.body.email = trim(req.body.email); next(); }, [check('first_name') .isLength({ min: 1 }).withMessage('A first name is required')], (req, res, next) => { var errors = validationResult(req); if (errors) { ..etc
本文标签: javascriptTrim input values before expressvalidator in NodeJsStack Overflow
版权声明:本文标题:javascript - Trim input values before express-validator in Node.Js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742311861a2451029.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论