`withValidator({ body })` rejects the multipart bodies that `useForm` and `action()` send for files
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 35/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- react, typescript
Hướng nghiên cứu
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.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- TypeScript
- Star
- 627
- Fork
- 66
- Merge trung bình
- 14 giờ 56 phút
- Pull request đã merge (30 ngày)
- 170
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của voidzero-dev/void
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
voidzero-dev/void#677 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 52/100
voidzero-dev/void#680 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
voidzero-dev/void#676 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 18/100
voidzero-dev/void#667 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
platform: interrupted install ignores a replacement runtime token and never updates deployed WorkersĐang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
voidzero-dev/void#666 ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của voidzero-dev/void
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
unocss/unocss#5421 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
needs-area
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Azure/typespec-azure#5936 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 Nửa ngày Mức phù hợp với người mới 72/100
vercel-labs/scriptc#2910 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
automation missing-model model-sync provider:fireworks-ai
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
anomalyco/models.dev#12458 ·
Maintainer thường phản hồi trong vòng 1 ngày