[英]Joi validation - how to make field validation based on the array value?
我一直在嘗試實現以下邏輯:
if methods array includes object { type: 0 }
then
成績字段是必需的
else
成績字段是可選的
const methodTypeSchema = Joi.object({
type: Joi.number(),
});
export const schema = Joi.object({
methods: Joi.array().items(methodTypeSchema),
grade: Joi.string().when('methods', {
is: Joi.array().has(Joi.object({ type: 0 })),
then: Joi.required(),
otherwise: Joi.optional(),
}),
});
我將不勝感激。
嗯,現在它似乎工作了......我的錯誤是我使用了一些自定義函數而不是默認的Joi.string()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.