elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
9.09k stars 193 forks source link

t.Literal type is ignored in favor of string during validation #661

Open wavpro opened 1 month ago

wavpro commented 1 month ago

What version of Elysia.JS is running?

1.0.22

What platform is your computer?

Microsoft Windows NT 10.0.19045.0 x64

What steps can reproduce the bug?

import { Elysia, t } from 'elysia'

new Elysia()
   .guard(
      {
        params: t.Object({
            id: t.Numeric(),
        }),
        query: t.Object({
          size: t.Union([
            t.Literal(512),
            t.Literal(128),
            t.Literal(64)
          ])
       })
    },
    (app) => app
       .get('/tracks/:id/cover', async ({ params: { id }, query: { size } }) => {
          return "Hello world!";
       })
    )
 .listen(3000);

This applies to both queries and params, showcased as query in the code above.

What is the expected behavior?

It should find the type that the t.Literal is, and cast the param/query to that before trying to compare to it.

What do you see instead?

I get this error when trying to access the page:

{
  "type": "validation",
  "on": "query",
  "property": "/size",
  "message": "Expected union value",
  "expected": {
    "size": 512
  },
  "found": {
    "size": "128"
  },
  "errors": [
    {
      "type": 62,
      "schema": {
        "anyOf": [
          {
            "const": 512,
            "type": "number"
          },
          {
            "const": 128,
            "type": "number"
          },
          {
            "const": 64,
            "type": "number"
          }
        ]
      },
      "path": "/size",
      "value": "128",
      "message": "Expected union value"
    }
  ]
}

Additional information

No response