Skip to content
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

Cannot validate JSON in query params #144

Closed
lnogol opened this issue Feb 15, 2023 · 2 comments
Closed

Cannot validate JSON in query params #144

lnogol opened this issue Feb 15, 2023 · 2 comments

Comments

@lnogol
Copy link

lnogol commented Feb 15, 2023

I have this in the api def:

parameters: [
  { name: "filter", type: "Query", schema: z.object({ ... }) }
]

and using @zodios/react to call the endpoint. the object gets correctly serialized on the client (JSON.stringify -> encodeURIComponent) and TS inference also works fine, but when I actually call the endpoint, it returns a validation error:

{
  "context": "query.filter",
  "error": [
    {
      "code": "invalid_type",
      "expected": "object",
      "received": "string",
      "path": [],
      "message": "Expected object, received string"
    }
  ]
}

it looks like the query is not deserialized back into an object before validation. I found this might be taking care of that, but it doesn't pass the conditions apparently

using an ugly workaround for now

app.use((req, res, next) => {
	try {
		if (req.query.filter) {
			req.query.filter = JSON.parse(req.query.filter);
		}
	} catch (err) {
		next();
	}
	next();
});
@ecyrbe
Copy link
Owner

ecyrbe commented Feb 15, 2023

yes, this should be doable easilly actually. take care of it now.
Thanks for the report.

@ecyrbe
Copy link
Owner

ecyrbe commented Feb 15, 2023

This is now fixed in version 10.5.1 of @zodios/express. thanks again for the report.

@ecyrbe ecyrbe closed this as completed Feb 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants