as the title states, how to allow an empty Date-String through Joi validation.
When adding:
Date: ""
it gets the Issue : message: ""Date" must be a number of milliseconds or valid date string"
with this Joi-Validation:
"Date": Joi.date().required().allow("").allow(null).options(validationErrors);
Question : How can an empty Date-String be allowed through Joi validation?
EDIT : By removing: .required()
and or adding .default("")
i do get another error, when adding Date: ""
, Cannot set parameter at row: 1. Wrong input for DATE type
您可以简单地从上面的代码中删除required()
。
"Date": Joi.date().allow("").allow(null).options(validationErrors);
Valid: Either new Date()
or ""
(empty string)
joi
version 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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.