koajs / joi-router

Configurable, input and output validated routing for koa
MIT License
450 stars 96 forks source link

Does not allow validate options #106

Closed PosicubeBeege closed 3 years ago

PosicubeBeege commented 4 years ago

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.

https://github.com/koajs/joi-router/blob/b7f940c81a3084e3c81eb3244593e27bdd24b927/joi-router.js#L481

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.

mdpx commented 4 years ago

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.

3imed-jaberi commented 3 years ago

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

aheckmann commented 3 years ago

thanks!