簡體   English   中英

Joi 驗證 - 如何根據數組值進行字段驗證?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM