我正在尝试使用JOI来检查日期是否是有效日期,并且将来也不是。我希望2001年11月31日会失败,因为没有11月31日..但是它过去了!
奇怪的是2001年11月32日失败了!知道是什么问题吗?我的测试代码如下
const joi = require('joi')
const moment = require('moment')
const schema = joi.object({
location: joi.string().strict().trim().min(1).required().error(() => {
return {
message: 'A location must be entered',
}
}),
incidentDate: joi.date().max(moment().format('YYYY-MM-DD')).required().error(() => {
return {
message: 'A date must be entered that is not in the future',
}
}),
})
const requestForm = {"dateOfIncident":{"day":"31","month":"11","year":"2001"},"location":"sdcds"}
const strDate = `${requestForm.dateOfIncident.year}-${requestForm.dateOfIncident.month}-${requestForm.dateOfIncident.day}`
requestForm.incidentDate = strDate
const joiErrors = joi.validate(requestForm, schema, { stripUnknown: true })
console.log(joiErrors)
答案 0 :(得分:0)
添加另一个.format
可以达到目的
incidentDate: joi.date().format('YYYY-MM-DD').max(moment().format('YYYY-MM-DD')).required().error(() => {
return {
message: 'A date must be entered in the correct format that is not in the future',
}
})