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

Normalize doesn't seem to do anything with aot: false #1020

Open
danieljvdm opened this issue Jan 17, 2025 · 3 comments
Open

Normalize doesn't seem to do anything with aot: false #1020

danieljvdm opened this issue Jan 17, 2025 · 3 comments
Labels
bug Something isn't working

Comments

@danieljvdm
Copy link

danieljvdm commented Jan 17, 2025

What version of Elysia is running?

1.2.10

What steps can reproduce the bug?

In a cloudflare worker environment with aot: false, the default behavior of normalizing responses seems to be broken.

For:

const TestType = t.Object({
  foo: t.String(),
});

const route = new Elysia({ aot: false })
  .get(
    '/:id',
    async () => {


      const test = {
        foo: 'bar',
        baz: 'qux',
      };

      return test;
    },
    {
      response: TestType,
    },
  );

We get:

{
  "type": "validation",
  "on": "response",
  "summary": "Property 'baz' should not be provided",
  "property": "/baz",
  "message": "Unexpected property",
  "expected": {
    "foo": ""
  },
  "found": {
    "foo": "bar",
    "baz": "qux"
  },
  "errors": [
    {
      "type": 42,
      "schema": {
        "type": "object",
        "properties": {
          "foo": {
            "type": "string"
          }
        },
        "required": [
          "foo"
        ],
        "additionalProperties": false
      },
      "path": "/baz",
      "value": "qux",
      "message": "Unexpected property",
      "errors": [],
      "summary": "Property 'baz' should not be provided"
    }
  ]
}
@danieljvdm danieljvdm added the bug Something isn't working label Jan 17, 2025
@danieljvdm
Copy link
Author

If we import import { Value } from '@sinclair/typebox/value';

We can manually clean the response and things work as expected.

const cleanedRecipe = Value.Clean(RecipeDetailDTO, recipe);

@danieljvdm
Copy link
Author

Another incredibly strange datapoint.... I have this route handler in the same Elysia instance as the above example (though the real code was swapped out for the foo/bar example)

  .get(
    '',
    async ({ query, recipeService }) => {
      const search = query.search;

      const recipes = await recipeService.getRecipes({
        searchQuery: search,
        filter: { idIn: query.idSearch ? query.idIn ?? [] : undefined },
      });
      return recipes;
    },
    {
      response: RecipeListDTO,
      query: t.Optional(
        t.Object({
          search: t.Optional(t.String()),
          idSearch: t.Optional(t.Boolean()),
          idIn: t.Optional(t.Array(t.String())),
        }),
      ),
    },
  )

In this code, I don't get a validation error on recipes (even though it's returning way more data than the DTO is asking for) and it also isn't normalized... literally nothing happens. And this is in the same instance as I get a validation error on my too-big object...

@yvvki
Copy link

yvvki commented Jan 19, 2025

This behavior is prevalent on the html plugin too where the autoDetect option, which triggers mapResponses, would not do anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants