TmTron
TmTron

Reputation: 19411

Fallback value when validation fails

(how) is it possible in joi to specify a fallback value that is used when the value does not match a schema?

pseudocode:

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

e.g. when the value does not match the regex, I want to replace it with the fallback

In the API docs, I found some promising stuff, but not usable for my case: e.g. any.default(), string.replace

Upvotes: 0

Views: 194

Answers (1)

Yes it is if you use Joi.compile().

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

Hope it helps!

Upvotes: 1

Related Questions