使用 joi 进行条件验证:如果属性值大于 4,则做一件事,否则做其他事情

问题描述

在 joi 中,我知道我可以做到:

.when('propertyName',{
  is: 4,then: Joi.number().required(),otherwise: Joi.number()
    .strip()
    .optional()
    .allow(''),})

但是,与其在 propertyName 等于 4 时触发“then”,不如在 propertyName 大于 4 时触发它。我该怎么做?请注意,propertyName 可以是任何浮点数,而不仅仅是整数。

解决方法

is: 4而不是is: Joi.number().greater(4)