javascript Joi验证:如何在对象有效载荷中要求至少一个键

4dc9hkyq  于 2023-11-15  发布在  Java
关注(0)|答案(1)|浏览(127)

Joi在下面的验证中似乎没有遵守.or条件:

export const PAYLOAD = Joi.object().keys({
  PUSH_UP:        Joi.any(),
  INVERTED_ROW:   Joi.any(),
  DIP:            Joi.any(),
  CHIN_UP:        Joi.any(),
  PULL_UP:        Joi.any(),
  GOBLET_SQUAT:   Joi.any(),
  BACK_EXTENSION: Joi.any()
}).or('PUSH_UP', 'INVERTED_ROW', 'DIP', 'CHIN_UP', 'PULL_UP', 'GOBLET_SQUAT', 'BACK_EXTENSION')

字符串
传入的payload应该包含一个或多个对象键。在这种情况下,payload如下:

{
  "BACK_EXTENSION": {
    "reps": 30,
    "exerciseId": 7
  },
  "PUSH_UP": {
    "reps": 1,
    "exerciseId": 1
  }
}


回复:

ValidationError: "INVERTED_ROW" is required


基于文档(https://joi.dev/api/?v = 17. 9. 1 #objecttorpineers-options),我希望这能工作.任何想法?谢谢!

w8f9ii69

w8f9ii691#

正如你提到的“一个或多个”。所以我假设你不在乎哪一个键是存在的。

export const PAYLOAD = Joi.object().keys({
  PUSH_UP:        Joi.any(),
  INVERTED_ROW:   Joi.any(),
  DIP:            Joi.any(),
  CHIN_UP:        Joi.any(),
  PULL_UP:        Joi.any(),
  GOBLET_SQUAT:   Joi.any(),
  BACK_EXTENSION: Joi.any()
}).min(1);

字符串

相关问题