-
Notifications
You must be signed in to change notification settings - Fork 192
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
Hono/Zod-OpenAPI question: how to validate a multi-field form data #906
Comments
It does not support the type import { createRoute, z, OpenAPIHono } from '@hono/zod-openapi'
const route = createRoute({
method: 'post',
path: '/books',
request: {
body: {
content: {
'multipart/form-data': {
schema: z.object({
formValue: z.string()
})
},
'application/json': {
schema: z.object({
jsonValue: z.string()
})
}
}
}
},
responses: {
200: {
description: 'Success message'
}
}
})
const app = new OpenAPIHono()
app.openapi(route, (c) => {
const validatedBody = c.req.valid('form')
return c.json(validatedBody)
})
const form = new FormData()
form.append('formValue', 'foo')
const res = await app.request('/books', {
method: 'POST',
body: form
})
const data = await res.json()
console.log(data) // foo |
Hey @yusukebe The solution I found so far is to preprocess the data and deserialize the JSON object before validation, but I still have to use the |
@abdurahmanshiine Thank you for the explanation.
It's not expected usage, so I think this middleware may not fully support that use case. It's difficult to investigate if I don't know the route definition that you did. But again, it's not expected usage. |
@yusukebe |
How about this? Perhaps you may validate import { createRoute, z, OpenAPIHono } from '@hono/zod-openapi'
const route = createRoute({
method: 'post',
path: '/',
request: {
body: {
content: {
'multipart/form-data': {
schema: z.object({
file: z.instanceof(File),
message: z.string()
})
}
},
required: true
}
},
responses: {
200: {
description: 'Success uploading'
}
}
})
const app = new OpenAPIHono()
app.openapi(route, (c) => {
const data = c.req.valid('form')
// const filedata = data.file
return c.json({ message: data.message })
})
export default app |
Alright, let me try this out |
@abdurahmanshiine I just got this to work using:
The Obviously I'm uploading an array of files, so I need the array option; if you're just uploading one file, you can probably use the part in the "or". |
Hey @TimMensch |
Mine works: c.req.valid("form").files This shows up as I'm using TypeScript 5.6.2. |
Hey @yusukebe, I think the issue is arising from this type |
Code: import { createRoute, z, OpenAPIHono } from '@hono/zod-openapi'
const route = createRoute({
method: 'post',
path: '/',
request: {
body: {
content: {
'multipart/form-data': {
schema: z.object({
message: z.string()
})
}
}
}
},
responses: {
200: {
description: 'success!'
}
}
})
const app = new OpenAPIHono()
app.openapi(route, (c) => {
const data = c.req.valid('form')
return c.json({ message: data.message })
}) |
Hey there,
I'm using this package, and I have a request that's of type
multipart/form-data
. The request body contains two keys, one is a JSON data, and the other one is aFile
data. I want to validate the JSON data only after parsing it as JSON, but I realized there is nomultipart/form-data
option, and now I have no idea how to go about this.This is my code for reference:
Any help is appreciated
The text was updated successfully, but these errors were encountered: