[英]express-validator isAfter is always false
我正在嘗試驗證某個 endDate 是否在 startDate 之后。 我已經嘗試了我發現的和能想到的一切,但沒有任何效果。 我嘗試過的一些例子:
check('endDate').isAfter(new Date('startDate')).withMessage('End date of lab must be valid and after start date')
check('endDate').isAfter(new Date('startDate').toDateString()).withMessage('End date of lab must be valid and after start date')
check('endDate').isAfter('startDate').withMessage('End date of lab must be valid and after start date')
check('endDate').isAfter(new Date('' + 'startDate').toDateString()).withMessage('End date of lab must be valid and after start date')
文檔說 isAfter() 需要一個字符串。
這是一個簡單的示例:
例如,定義startDate
var startDate = new Date(2019, 11, 22); // or var startDate = new Date();
然后,您可以檢查所需的日期
check('endDate').isAfter(new Date(startDate).toDateString()).withMessage('End date of lab must be valid and after start date')
如果startDate
也是輸入字段,則可以使用自定義驗證程序:
check('endDate').custom((value, { req }) => {
if(new Date(value) <= new Date(req.body.startDate)) {
throw new Error ('End date of lab must be valid and after start date');
}
return true;
})
query("endDate").notEmpty().custom((v, { req }) => v > req.query?.startDate)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.