joi icon indicating copy to clipboard operation
joi copied to clipboard

Custom extension questions regarding mutateRegister/mutateRebuild

Open Birkbjo opened this issue 2 years ago • 0 comments

Support plan

  • is this issue currently blocking your project? (yes/no): no
  • is this issue affecting a production system? (yes/no): no

Context

  • node version: v14.17.1
  • module version: 17.4.2,
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): hapi
  • any other relevant information: Creating a custom extension, for a simple query-filter (eg. Joi.filter().operator().value()

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```

Birkbjo avatar Mar 23 '23 20:03 Birkbjo