简体   繁体   English

Joi 验证 - 如何根据数组值进行字段验证?

[英]Joi validation - how to make field validation based on the array value?

I've been trying to implement the following logic:我一直在尝试实现以下逻辑:

if methods array includes object { type: 0 }

then grade field is requires then成绩字段是必需的

else grade field is optional 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(),
  }),
});

I'll be grateful for the help.我将不胜感激。

hm, now it seems working... my mistake was that instead of default Joi.string() I used some custom functions嗯,现在它似乎工作了......我的错误是我使用了一些自定义函数而不是默认的Joi.string()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM