koajs / joi-router

Configurable, input and output validated routing for koa

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Does not allow validate options

PosicubeBeege opened this issue · comments

I have a use case where I'd like to change the validation based on some external configuration value. Looking through Joi docs and other info, it seems that Joi allows a context value provided in an optional options object that can be used in Joi conditions. Looking through the below line, it seems there's no opportunity to pass this options object through the router to Joi.

const res = Joi.validate(request[prop], validate[prop]);

This is a problem for my use case. For the larger community / maintainers, does this seem valuable?

I would just submit a PR, but I see some potential issues. What if context or other options should change per validation entry? Would having some property of validate like options end up colliding with something else?

My initial thought is to allow each prop of validate to optionally accept a plain Javascript object in addition to a Joi object, and that plain object could then include an options prop. Thoughts?

As a minor aside/off-topic, how do folks usually deal with something like this where they need the change ASAP and don't have time to wait for a release but also don't want to use a non-npm fork of the library? I've never come up with a good way to handle that, and I'm sure someone else has figured it out.

Thanks folks. Loving this thing. It's been a real game-changer for my team.

commented

good point. my case is that I want all validation errors returned rather than only the first one, which can be achieved by setting abortEarly to false in Joi.validate options.

how about adding another entry in .route() options, say, "options" ("validateOptions" may be too long...), then just pass it to validateInput as the fourth argument? you don't even need to check it as Joi will do it.

@aheckmann, please close this issue it fixed with #119.

thanks!