node.js - 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())
感谢您的时间
解决方案
推荐阅读
- sql-server - SSRS SharePoint 报表生成器和重复事件
- javascript - 许多值被添加到数组 onChange (INPUT)
- python-3.x - python 中 ops.EagerTensor(value, ctx.device_name, dtype) 的运行时错误
- java - 从 Playstore 下载我的 Android 应用程序时崩溃,但是当我从 Android Studio 将它运行到任何设备时它可以正常工作
- java - 在 for 循环中创建多个变量“name + i”(JAVA)
- r - 基于重复模式的子集数据框
- c++ - llvm::BranchInst::getSuccessor(unsigned int) 断言失败?
- python - 使用python返回excel中两个不同文件中两列之间的差异
- swift - 预览层不受 UIView 限制
- excel - 有没有办法用excel文件+vba的路径文件夹名称保存pdf文件?