[英]How to Joi-Allow an empty Date String [Joi]
正如標題所述,如何通過 Joi 驗證允許空日期字符串。
添加時:
Date: ""
它得到問題: message: ""Date" must be a number of milliseconds or valid date string"
使用此Joi 驗證:
"Date": Joi.date().required().allow("").allow(null).options(validationErrors);
問題:如何通過 Joi 驗證允許空日期字符串?
編輯:通過刪除: .required()
和或添加.default("")
我確實得到另一個錯誤,當添加Date: ""
, Cannot set parameter at row: 1. Wrong input for DATE type
您可以簡單地從上面的代碼中刪除required()
。
"Date": Joi.date().allow("").allow(null).options(validationErrors);
有效: new Date()
或""
(空字符串)
joi
版本17.2.1
const joi = require('joi');
const schema = joi.object().keys({
"Date": joi.alternatives([
joi.date(),
joi.string().valid('')
]).required()
}).required();
// success
const value = {
"Date": "",
};
// success
const value = {
"Date": new Date(),
};
// error
const value = {
"Date": null,
};
// error
const value = {
};
// error
const value = {
"Date": "invalid string"
};
const report = schema.validate(value);
console.log(report.error);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.