hapijs / joi

The most powerful data validation library for JS
Other
20.95k stars 1.51k forks source link

Is it possible to override or remove behavior from a field? #2929

Open meircarlos opened 1 year ago

meircarlos commented 1 year ago

Let's say I have the following schema:


const schema = Joi.object().keys({
  withDefault: Joi.string().default(3),
  array: Joi.array().items(1,2),
});

I wonder if it's possible to delete the default value from the withDefault field when extending it, for instance doing: schema.extract("withDefault").default() so that the default is reset to no default value.

And then for the array, I wonder if it's also possible to override the list of possible items, let's say instead of 1 and 2, I want the list to be 2, 3 and 4.

Marsup commented 1 year ago

There is a way, but it's really not advised. If you can plan ahead, you'd better use tailor to do that.

StefanOctavian commented 1 month ago

I found you can add .default(() => undefined)