我正在构建 Node/Express API 并使用 Joi 进行验证。这是一个很棒的包并且非常有用。然而,我们已经厌倦了做这样的事情:
const mySchema = joi.object({
thing1: joi.string().required(),
thing2: joi.string().required(),
thing3: joi.string().required(),
thing4: joi.string().required(),
thing5: joi.string().required(),
}).required();
我们希望默认情况下需要一切,并手动调用.可选
来覆盖它。事实上,这似乎是一个明智的默认设置 - 但现在先把它放在一边。
有办法实现吗?
最佳答案
您可以使用presence
选项将字段设为默认必填。示例:
const mySchema = joi.object({
thing1: joi.string(),
thing2: joi.string(),
thing3: joi.string(),
thing4: joi.string(),
thing5: joi.string(),
}).options({ presence: 'required' }).required();
关于javascript - Joi - 默认设置所需的一切?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55949766/