HTTP Actions the best way to validate request body
I am using HTTP Actions and the
request param is unknown. There is no documented way to validate request or how to use built-in validator to validate object shape inside of actions. I am wondering what is the best way to handle this.7 Replies
Thanks for posting in <#1088161997662724167>.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.
- Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
- Use search.convex.dev to search Docs, Stack, and Discord all at once.
- Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI.
- Avoid tagging staff unless specifically instructed.
Thank you!
Please share an example from your code. The HTTP action function should look something like:
yes, given this action when I have
const body = await request.json() , body is undefined how could I validate it using convex validator? or do i need to create zod schemas sepoerately and use zod to validate?There's still a lot that you haven't shared about your actual setup, and without that it's tough to know what's happening. Could you please share code snippets of the relevant pieces? E.g. the
httpAction function definition, the http.route definition that targets that function, and even an example request to the endpoint.Take this snippet:
With this setup is there a way to validate the request body with convex without relying on something like zod? such as use convex built-in validator?
observe that
result is unknown
That's really odd that the request isn't being typed correctly. I've never seen this issue.
When you hover over
httpAction what does it show?
As for using the built-in Convex validator, that only works with Convex functions as far as I know, plus it's only a Typescript validator. To validate the data coming in via an HTTP request, you'll need to use Zod or something similar that validates the data at runtime.