express-validator isDate 和 isISO8601 始终为 false

express-validator isDate and isISO8601 are always false

我正在尝试验证日期。我已经尽我所能,但我还没有找到解决办法。输入 {"dob": "2002-10-02"}

'use strict'

var validator = require('validator');
var controller = {

    create: (req,res) =>{

        //pick parameters
        var parameters =  req.body;
     
        //validator
        
        try {
        
            //not working (always returns false)
            //var validate_dob = validator.isDate(parameters.dob + '');
            //also not working (always returns false)
            //var validate_dob = validator.isISO8601(parameters.dob + '');

            

        } catch (error) {
            
            return res.status(400).send({
                message: error
            });
        }
      }
}

在你的问题中你提到了标签 express-validator,但在你的中间件中你使用了纯 validator.

这里我将使用 express-validator 库(6.6.0 版)作为示例。使用验证主体参数(登录名和密码)。但是您可以理解并从验证器列表中为您的日期选择验证。 Reference.

server/validators/login.validator.js

const { body, validationResult } = require('express-validator');

exports.validationBodyRules = [
    body('login', 'login is required').exists(),
    body('password', 'password is required').exists(),
    body('login', 'login is required').notEmpty(),
    body('password', 'password is required').notEmpty()
];

exports.checkRules = (req, res, next) => {
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
        return res.status(400).json({ errors: errors.array() });
    }
    next();
};

这是路由文件

server/routes/login.route.js

const router = require('express').Router();
const loginService = require('../controllers/login.controller');
const loginValidator = require('../validators/login.validator');

router.post('/login', loginValidator.validationBodyRules, loginValidator.checkRules, loginService.hashPassword, loginService.lookupLogin, loginService.logEmployee);

module.exports = router;

server/controllers/login.controller.js

const postgres = require('../../lib/postgres');
const jwt = require('jsonwebtoken');
const crypto = require('crypto');

exports.logEmployee = (req, res) => {
    res.status(200).json({ token: 'Bearer ' + jwt.sign(req.employee, process.env.SECRET, { expiresIn: 1800 }) });//expires in 1800 seconds
    res.end();
};

exports.hashPassword = (req, res, next) => {
    crypto.scrypt(req.body.password.toString(), 'salt', 256, (err, derivedKey) => {
        if (err) {
            return res.status(500).json({ errors: [{ location: req.path, msg: 'Could not do login', param: req.params.id }] });
        }
        req.body.kdfResult = derivedKey.toString('hex');
        next();
    });
};

exports.lookupLogin = (req, res, next) => {
    const sql = 'SELECT e.employee_id, e.login FROM employee e WHERE e.login= AND e.password = ';
    postgres.query(sql, [req.body.login, req.body.kdfResult], (err, result) => {
        if (err) {
            return res.status(500).json({ errors: [{ location: req.path, msg: 'Could not do login', param: req.params.id }] });
        }
        if (result.rows.length === 0) {
            return res.status(404).json({ errors: [{ location: req.path, msg: 'User or password does not match', param: req.params.id }] });
        }
        req.employee = result.rows[0];
        next();
    });
};

但是你可以使用这里的想法来使用日期验证器。

如果您需要更完整的示例,请告诉我。