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

Multipart/form-data nested request does not work #1138

Open
Teyik0 opened this issue Mar 23, 2025 · 2 comments
Open

Multipart/form-data nested request does not work #1138

Teyik0 opened this issue Mar 23, 2025 · 2 comments
Labels
enhancement New feature or request

Comments

@Teyik0
Copy link

Teyik0 commented Mar 23, 2025

What is the problem this feature would solve?

All is in the title. We need a way to handle this case out of the box.

For now we need to do several requests when having nested database model, this has a cost in term of performance and forces us to write more code and unnecessary queries to the database, whereas one unique query would be sufficient with this feature.

Here is a minimal reproduction that of course throw a validation error since Elysia is not handling this case.

import { treaty } from "@elysiajs/eden";
import { describe, expect, it } from "bun:test";
import { Elysia, t } from "elysia";

const app = new Elysia().post(
  "/",
  ({ body }) => {
    // Anyway what is the logic here, error is happening at validation lifecycle since 
    // Elysia is not parsing the body correctly in this case
    return body;
  },
  {
    body: t.Object({
      name: t.String(),
      // variants is the nested attribute that needs to be parsed correctly by Elysia, without this, it will work.
      variants: t.Array(
        t.Object({
          price: t.Number({ minimum: 0 }),
          weight: t.Number({ minimum: 0 }),
        }),
      ),
      // From the docs ->
      // By providing a file type, Elysia will automatically assume that the content-type is multipart/form-data.
      image: t.File({ type: "image" }),
    }),
  },
);

const api = treaty(app);

const testProduct = {
  name: "Test Product",
  variants: [
    {
      price: 10,
      weight: 100,
    },
  ],
  image: new File(["dummy content"], "image1.webp", { type: "image/webp" }),
};

describe("Product POST/", () => {
  it("should create a product using treaty", async () => {
    const { data, status, error } = await api.index.post(testProduct);
    console.log("Error Value:", error?.value);
    console.log("Error Status:", error?.status);
    expect(status).toBe(200);
    expect(data).toEqual(testProduct);
  });
});

What is the feature you are proposing to solve the problem?

I want Elysia to properly parse multipart/form-data requests when using nested body parameters. This should be the default behavior, as Elysia's type handling does not raise any issues when writing this code boilerplate.

What alternatives have you considered?

Doing multiple route, with multiple queries to my database.

@Teyik0 Teyik0 added the enhancement New feature or request label Mar 23, 2025
@Teyik0
Copy link
Author

Teyik0 commented Mar 25, 2025

Thanks to @SaltyAom help, found a workaround using t.ArrayString(), updated sample code below for people that had this problem.

Treaty will complain for a ts-error without the unknown but it work 👍

import { treaty } from "@elysiajs/eden";
import { describe, expect, it } from "bun:test";
import { Elysia, t } from "elysia";

const app = new Elysia().post(
  "/",
  ({ body }) => {
    return body;
  },
  {
    body: t.Object({
      name: t.String(),
      // t.String() become t.ArrayString()
      variants: t.ArrayString(
        t.Object({
          price: t.Number({ minimum: 0 }),
          weight: t.Number({ minimum: 0 }),
        }),
      ),
      image: t.File({ type: "image" }),
    }),
  },
);

const api = treaty(app);

const testProduct = {
  name: "Test Product",
  variants: [
    {
      price: 10,
      weight: 100,
    },
  ],
  image: new File(["dummy content"], "image1.webp", { type: "image/webp" }),
};

describe("Product POST/", () => {
  it("should create a product using treaty", async () => {
    const stringifiedVariants = JSON.stringify(testProduct.variants);

    const { data, status } = await api.index.post({
      name: testProduct.name,
      variants: stringifiedVariants as unknown as {
        price: number;
        weight: number;
      }[],
      image: testProduct.image,
    });

    expect(status).toBe(200);
    expect(data).toEqual(testProduct);
  });
});
``

@kravetsone
Copy link
Contributor

Oh looks good but also need an ObjectString huh

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