我使用express-validator来检查我的帖子字段,我的一个字段必须是小数或空.我使用了这个架构: request.checkBody({ 'product_price': { optional: true, isDecimal: { errorMessage: 'The product price must be a decimal
request.checkBody({ 'product_price': { optional: true, isDecimal: { errorMessage: 'The product price must be a decimal' } } })
这个Schema的问题是如果product_price为空则不验证我的帖子,即使是“optional:true”.
您需要为可选项设置checkFalsy
选项以允许定义但空的值:
request.checkBody({ 'product_price': { optional: { options: { checkFalsy: true } }, isDecimal: { errorMessage: 'The product price must be a decimal' } } });
编辑:自发布此答案后文档已被移动,现在可以找到here.