-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove Joi from types #4497
base: master
Are you sure you want to change the base?
Remove Joi from types #4497
Conversation
The PR makes sense to me, but applying it on my project causes a whole bunch of errors along the lines of |
Thank for the feedback. Unfortunately, this will likely take a bit of work to adapt to. Specifying a validator is required to type such simple validation objects, since it can no longer default to Joi. You would need to use one of the above methods, or explicit casting, to make your server.route<ReqRefDefaults, typeof Joi>({
…
}) Of course, you can still just wrap it as suggested, though that would still lose the typings on any |
Considering the number of times a user would call |
But you shouldn't declare it at the route level, but at the server level as in the "registration" section above. If you are passing the I'm not sure what you mean by declaration merging? Do you mean to expect people to merge a declare module '@hapi/hapi' {
interface DefaultValidator extends Joi.Root {}
} This will cause problems for independent plugins, which will either pollute the main project validator, or have to do without. |
I'm torn, you're certainly right, but declaring independent plugins as I do, all the plugins will have to be changed to something like: const plugin: Plugin<{}> = {
register(server: Server<{}, typeof Joi>) {
}
}; It doesn't look like a good DX but I'm not sure there's a better sane choice. |
I am not sure, but this might also fix #4414. Currently the solution mentioned in that issue works with raw JS, but with TS (which is the reason you would use zod in the first place) it doesn't work and shows type errors everywhere. |
This PR removes the explicit import of
Joi
in the typings. As it is, any typescript users that use Hapi, will need to add a dependency of Joi, just to resolve the typings.The explicit dependency is replaced by a generic validator that can be set on the
Server
object, or inferred from the config. This has the additional advantage, that other validators will be able to be typed instead of assuming that Joi is used.The new types will require a bit of work to adapt to for projects that use
server.validate()
.This fixes #4491.
Definition
The base Joi object already implement this interface.
Registration
Explicit:
Inferred from config:
Using the new
validator()
returned value:For plugins
Explicit:
Using the new
validator()
returned value:Usage
Once registered, the validator typings are used to help type the validation options:
It can be overriden at the route level:
It also allows custom inline validators:
It is also used to validate rules:
As it currently is, pre-compiled Joi schemas continue to work, even with no registered validator (though any options won't be validated):
FYI, it's possible that this could be revised to extract the allowed options from the passed Joi object.