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

validator doesnt parse nested objects in formdata #3710

Open
darklight9811 opened this issue Nov 29, 2024 · 3 comments
Open

validator doesnt parse nested objects in formdata #3710

darklight9811 opened this issue Nov 29, 2024 · 3 comments
Labels
enhancement New feature or request.

Comments

@darklight9811
Copy link

What version of Hono are you using?

4.6.12

What runtime/platform is your app running on? (with version if possible)

bun 1.1.37

What steps can reproduce the bug?

  • Use this to parse the object into formdata on the frontend:
objectToFormData

This is compliant with zod-form-data

// biome-ignore lint/suspicious/noExplicitAny: <explanation>
export function appendFormData(data: any, root: string, formData: FormData) {
	if (data instanceof File) return formData.append(root, data);
	if (Array.isArray(data)) {
		for (let i = 0; i < data.length; i++) {
			appendFormData(data[i], `${root}[${i}]`, formData);
		}

		return;
	}
	if (data instanceof Date) {
		return formData.append(root, data.toISOString());
	}
	if (typeof data === "object" && data) {
		for (const key in data) {
			// biome-ignore lint/suspicious/noPrototypeBuiltins: <explanation>
			if (data.hasOwnProperty(key)) {
				if (root === "") appendFormData(data[key], key, formData);
				else appendFormData(data[key], `${root}.${key}`, formData);
			}
		}

		return;
	}
	if (data !== null && typeof data !== "undefined")
		return formData.append(root, data);
}

export function objectToFormData(obj: unknown, rootName = "") {
	const formData = new FormData();

	appendFormData(obj, rootName, formData);

	return formData;
}
  • Send the data to the backend
  • Parse it with validator:
app.post(
  '/posts',
  validator('form', (value, c) => {
    console.log(value);
  //...

What is the expected behavior?

Have deep nested objects sent like this:

FormData {
   name: "Test",
   img: File (170.37 KB) {
     name: "Purple.png",
     type: "image/png"
   },
   "address.id": "dXJuOm1ieHBvaTpkNjUyMjUxOC04NDMzLTRlNzQtYjM5MS1iOGRkZDZlZDg4ZTA",
   "address.label": "Testaccio market, 00153 Rome, Italy",
   "address.coords[0]": 12.47373157,
   "address.coords[1]": 41.87758156,
}

To be parsed like this:

{
   name: "Test",
   img: File (170.37 KB) {
     name: "Purple.png",
     type: "image/png"
   },
   "address": {
      "id": "dXJuOm1ieHBvaTpkNjUyMjUxOC04NDMzLTRlNzQtYjM5MS1iOGRkZDZlZDg4ZTA",
      "label": "Testaccio market, 00153 Rome, Italy",
      "coords": [12.47373157, 41.87758156],
    }
}

What do you see instead?

{
   name: "Test",
   img: File (170.37 KB) {
     name: "Purple.png",
     type: "image/png"
   },
   "address.id": "dXJuOm1ieHBvaTpkNjUyMjUxOC04NDMzLTRlNzQtYjM5MS1iOGRkZDZlZDg4ZTA",
   "address.label": "Testaccio market, 00153 Rome, Italy",
   "address.coords[0]": "12.47373157",
   "address.coords[1]": "41.87758156",
}

Additional information

Also noticed the number got cast into string

@yusukebe
Copy link
Member

Hi @darklight9811

Do you think this is a bug or a feature request?

@darklight9811
Copy link
Author

Hi @darklight9811

Do you think this is a bug or a feature request?

I think it fits both, since it's a limitation

@yusukebe
Copy link
Member

This is not a bug and is an expected behavior. We have not implemented this feature and I don't know we should do it. So, I'll tag this as an enhancement.

@yusukebe yusukebe added enhancement New feature or request. and removed triage labels Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request.
Projects
None yet
Development

No branches or pull requests

2 participants