如何解决如何Joi-允许空的日期字符串[Joi]
如标题所述,如何通过Joi验证允许空的Date-String。
添加时:
Date: ""
它得到了问题:message: ""Date" must be a number of milliseconds or valid date string"
具有此 Joi-Validation:
"Date": Joi.date().required().allow("").allow(null).options(validationErrors);
问题:如何通过Joi验证允许一个空的Date-String?
编辑:在添加.required()
,.default("")
Date: ""
或添加Cannot set parameter at row: 1. Wrong input for DATE type
确实会出现另一个错误>
解决方法
您只需从上面的代码中删除require()
。
"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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。