验证失败时的回退值

Fallback value when validation fails

(how) 是否可以在 joi 中指定当值与架构不匹配时使用的回退值?

伪代码:

Joi.string()
    .regex(/\d/)
    .fallback('0') // .fallback does not exist, but I wish it did

例如当值与正则表达式不匹配时,我想用后备

替换它

API docs 中,我发现了一些有前途的东西,但不适用于我的情况:例如any.default()string.replace

是的,如果您使用 Joi.compile()

Joi.compile([
  Joi.string().regex(/\d/),
  Joi.empty(Joi.any()).default('0'),
])

希望对您有所帮助!