`withValidator({ body })` rejects the multipart bodies that `useForm` and `action()` send for files
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 35/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 活躍
- 技術堆疊
- react, typescript
研究方向
Start in runtime/validator.ts, where the body slot calls c.req.json(), and in objectToFormData() in pages/navigation-engine.ts, which encodes the multipart fields. The open design question is whether non-file values should be coerced in the schema or sent as one JSON part, so settle that before editing. Done means a multipart action with a File reaches the handler with a typed body and fills form.errors on schema failure, while JSON requests behave as before.
由索引模型根據 Issue 內容生成。
描述
Summary
When the data holds a File, Blob or FileList, useForm and action() send it as multipart/form-data (hasFiles() → objectToFormData() in pages/navigation-engine.ts). withValidator({ body }) only calls c.req.json() (runtime/validator.ts), so it answers 400 {"error":"Invalid JSON body"} before the handler runs. Void's client and its validator don't agree on the body format.
The file uploads guide avoids this by skipping withValidator and calling c.req.parseBody(). That gives up what the validator gives every other action:
- a typed
bodyin the handler, form.errorsfilled from the schema's issues,useFormdata typed from the schema (without a validator it falls back toRecord<string, unknown>).
Reproduction
// pages/photos.server.ts
import { defineHandler } from 'void';
import { z } from 'zod';
export const action = defineHandler.withValidator({
body: z.object({ title: z.string().min(1), photo: z.instanceof(File) }),
})(async (c, { body }) => {
// never reached when a file is attached
});
// pages/photos.tsx: the upload form from the guide
const form = useForm('/photos', { title: '', photo: null as File | null });
Pick a file and submit. The client posts FormData, and the action answers 400 {"error":"Invalid JSON body"} without running the handler. form.errors stays {}, and form.error is a generic VoidActionError: Action failed with 400 Bad Request. With no file, the client sends JSON, and the validator fills form.errors as expected.
Proposal
- Read form bodies in the
bodyslot. Formultipart/form-dataandapplication/x-www-form-urlencoded, read the body withc.req.formData()instead ofc.req.json(), and passFilevalues through to the schema. - Undo
objectToFormData()'s encoding, so the schema sees the shape the client sent:a[b]back into nested objects,a[0]anda[]back into arrays. Otherwise a nested field arrives as a flat'profile[name]'key. - Keep scalar types, or document that they're lost. The client sends booleans as
'1'/'0', numbers as strings,nullas''and dates as ISO strings, so a schema written for the JSON body fails on the multipart one. Either document that the schema needs coercion, or have the client send the non-file values as one JSON part and only the files as separate parts, so the server can rebuild the exact object and one schema works for both encodings.
Prior art
- Hono, which Void builds on:
validator('form', …)validatesparseBody()output, alongsidevalidator('json', …). - Next.js: server actions receive
FormData, files included, and libraries like Conform andzod-form-datavalidate it with a schema. - SvelteKit: Superforms validates
request.formData(), files included, with the same schema as the client.
Workaround
// pages/photos.server.ts
import { defineHandler, ValidationError } from 'void/handler';
import { z } from 'zod';
const photoSchema = z.object({ title: z.string().min(1), photo: z.instanceof(File) });
export const action = defineHandler(async (c) => {
const result = photoSchema.safeParse(await c.req.parseBody());
if (!result.success) {
throw new ValidationError(
Object.fromEntries(result.error.issues.map((issue) => [issue.path.join('.'), issue.message])),
);
}
// result.data is typed here, but useForm's data and errors aren't
});
Environment
void1.5.0@void/react1.5.0
- 主要語言
- TypeScript
- 星號
- 627
- 分支
- 66
- 平均合併
- 14 小時 56 分鐘
- 30 天內合併 PR
- 170
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
voidzero-dev/void 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 62/100
voidzero-dev/void#677 ·
維護者通常 1 天內回覆
-
難度 3/5 1-2 天 新手友好度 52/100
voidzero-dev/void#680 ·
維護者通常 1 天內回覆
-
難度 4/5 3-5 天 新手友好度 25/100
voidzero-dev/void#676 ·
維護者通常 1 天內回覆
-
難度 5/5 一週以上 新手友好度 18/100
voidzero-dev/void#667 ·
維護者通常 1 天內回覆
-
platform: interrupted install ignores a replacement runtime token and never updates deployed Workers未關閉
難度 4/5 3-5 天 新手友好度 35/100
voidzero-dev/void#666 ·
維護者通常 1 天內回覆
查看 voidzero-dev/void 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 72/100
維護者通常 1 天內回覆
-
ready-for-agent
難度 2/5 1-3 小時 新手友好度 72/100
LucasSantana-Dev/Lucky#2951 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 75/100
danielmiessler/LifeOS#2567 ·
維護者通常 9 天內回覆
-
refactor(react-pi): classify base64 images with core's parseBase64可能已有人在做 @jimmyallinai 今天認領。 未關閉maintenance
難度 2/5 1-3 小時 新手友好度 62/100
assistant-ui/assistant-ui#10293 · 2 則留言 · 1 個 reaction ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 70/100
brijeshb42/monaco-vim#175 ·