Closed rmzaoo closed 5 months ago
🧪 A prerelease is available for testing 🧪
You can install this latest build in your project with:
npm install --save https://prerelease-registry.devprod.cloudflare.dev/itty-router-openapi/runs/8122549266/npm-package-itty-router-openapi-109
Or you can immediately run this with npx
:
npx https://prerelease-registry.devprod.cloudflare.dev/itty-router-openapi/runs/8122549266/npm-package-itty-router-openapi-109
Hey @rmzaoo
if you are just trying to receive unknown properties in your endpoint request body, you should be able to do so by turning the property raiseUnknownParameters
to false
when creating your router, like this
const router = OpenAPIRouter({
raiseUnknownParameters: false,
});
Did i understand your question right?
@G4brym I tried that but it didn't work. I tried to debug why and i found out that that property is commented on the handler creation (src/openapi.ts:190). Also, even when using that property, validation still runs (safeParse function on src/route.ts:252) and unknown properties are still removed from the body data
@G4brym Any news about it?
@rmzaoo for you to receive any parameters in the reques body, you will need to use Zod Catchall Here's an example endpoint to showcase this
export class TestBody extends OpenAPIRoute {
static schema: OpenAPIRouteSchema = {
requestBody: z.object({}).catchall(z.any()),
}
async handle(
request: Request,
env: any,
context: any,
data: any,
) {
return data
}
}
router.post('/test', TestBody)
Then you will be able to send any parameters inside the body like this:
@G4brym Okey perfect, that's good.
Another thing, for some reason in route.ts:210
the if (queryParams)
is true and an empty object is added to the unvalidatedData['query']
. Further down it conflicts with the validationSchema.
I solved this by putting onlyschema.request?.query
in the if, could you take a look at this?
(i used your example to test)
@G4brym Again about skipvalidation, after a few tests the parameters passed in such as path, query and header also do their validation. A little bit more context about the project: This was implemented itty-router originally, and I'm currently migrating to itty-router-openapi to have propper documentation for the API. I already do my own validations in the handlers, that's why I don't want to be limited to the validation errors that zod presents before reaching the request handler. For these validations to be mandatory would result in validating my request 2 times and skipvalidation allows me to proceed without validating the entire schema again.
I would be ok with not merging this change, but, do you have any suggestion to skip it not only for body but for path, query and headers too?
@rmzaoo sorry for the delay, I'm currently out of the office, I will only be able to merge this early January, meanwhile you can use this branch preview build by running this command npm install --save https://prerelease-registry.devprod.cloudflare.dev/itty-router-openapi/runs/7290903179/npm-package-itty-router-openapi-109
Is this PR going to be merged?
I modified the code to skip the parse validation function and allow unknown parameters in the request body. This was done because we couldn’t find any other solution. Did I miss something in the documentation? Is there a way, with the current implementation, to allow unknown parameters in the request body structure without skipping the parse validation function?