hapijs / joi

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

Custom extension questions regarding mutateRegister/mutateRebuild #2934

Open Birkbjo opened 1 year ago

Birkbjo commented 1 year ago

Support plan

Context

How can we help?

I've successfully managed to create a custom extension for Joi to handle my queryFilter-objects. It works beatifully, but I just want to be sure that I'm doing it right - I haven't seen any weird behavior, since my usecase is pretty simple.

I've looked a lot of the internal code, and tried to follow how .keys() and .array().items() work. I just have a question regarding mutateRegister and mutateRebuild; basically, when do I need to call these? I'm using a custom term to store "overrides" for the value-schema if operators are passed, inspired by Object.pattern()-code. Do I need to call mutateRegister() for the schema stored in the "term"? Do I need to mutateRebuild in the method()?

My extension basically parses a filter like version=lte:2.26.1 to an object I use internally. It is used like this


const versionFilterSchema = Joi.filter()
    operator(Joi.string().valid('eq', 'in', 'lte'))
   .value(Joi.string())
   .value(Joi.string().pattern(<someVersionRegex>), { operators: ['lte'] }) //apply pattern only for lte```