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
Advertisement
Answer
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);