Generic middleware validator
Hello, I'm trying to write a middleware in my app that handles validating the incoming request body. It currently looks like this
The return type means that in my handler
body
comes through with the correct type definition given by the validator, e.g.
This has worked nicely so far, but unfortunately with larger schemas I'm getting a Type instantiation is excessively deep and possibly infinite
error when calling validateBody
.
I've read answers to similar questions here and here, and tried out some of the suggestions, but I haven't been able to come up with a solution so far.
Is there any way to get this working as expected, or is it not possible with the current limitations of Typescript? Am I safer just casting body
in the handler
to the expected type (since at that point it has passed validation and I know that it conforms to the expected type)?
Any help or suggestions would be greatly appreciated, thanks!6 Replies
Can you post the relevant types that are resulting in the errors?
It seems like it would be some combination of
use
, handler
, MiddlewareObj
and ValidatedBody
.
You should not have to cast just because a schema is large unless it's so big arktype has a problem inferring it directlySure. I've moved the call outside of the middleware chain i.e.
and I'm still seeing the error so I think we can rule out
use
/handler
interfering with it. Here are the other types
Here's a CodeSandbox link to the ark schemas and their usageCould you create a repo from this so I can see it locally? I'm not seeing errors in the codesanbox, just a bunch of stuff falling back to
any
https://github.com/bgribben/type-issue
I think that should show the issue, thanks for looking into this
This might be an issue with the middy library, or at least how I'm using it. One call to
use
seems to impact the other e.g.
in this instance, commenting out the handleCreatePet
gets rid of the Type instantiation is excessively deep and possibly infinite
error in handleUpdatePet
.
I have no idea how they could impact each other, gonna do some digging to see if I'm even using the library correctly 🙃
Hey @ssalbdivad I've updated https://github.com/bgribben/type-issue to use my own recursive type for the middleware
and I'm still seeing the issue with the following usage
The strange thing is if I move handleUpdatePet
above handleCreatePet
, the error disappears
At a glance, would you know why the order of the calls impacts the output of TS?
Apologies for the direct ping, but if you get a second to check out the error in the repo, it would be greatly appreciated. Thanks!I'm not totally sure what's going on internally here, but changing your validate signature to the following seems to resolve the issue:
Unfortunately, this requires using the internal
distill
API for now, but I will likely expose it in an upcoming release.
You'll also need to update your module
settings in tsconfig to something like:
so you can correctly resolve package.json exports, and likely add "type": "module"
to package.json since ArkType only exports esm.Thank you!