Express Validator - 根据参数值有条件地使字段成为必需

问题描述

我在将以下验证链合二为一时遇到问题。当然,这不是一个大问题,但我想知道如何解决

仅当名为 type 的变量的值为“FINAL”时才需要字段文档,否则它应该是可选的且可为空的。

我想优化的工作解决方案:

body('documents')
  .isArray()
  .optional({ nullable: true })
  .withMessage('documents should be an array'),body('documents')
  .if(body('type').equals('FINAL'))
  .isArray()
  .withMessage('documents should be an array'),

我认为可行的方法

body('documents')
  .isArray()
  .if(body('type').not().equals('FINAL'))
  .optional({ nullable: true })
  .withMessage('documents should be an array'),

也试过.if(body('type').equals('FINAL').not())

感谢您的时间

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)