elysiajs/elysia

Custom validation error message returns as string

Opened this issue · 0 comments

What version of Elysia is running?

1.2.2

What platform is your computer?

Linux 5.15.133.1-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

import { Elysia, t } from "elysia";

const app = new Elysia()
  .post("/works", ({ body: { name } }) => ({ hello: name }), {
    body: t.Object({
      name: t.String({ minLength: 1 })
    })
  })
  .post("/bug", ({ body: { name } }) => ({ hello: name }), {
    body: t.Object({
      name: t.String({ minLength: 1, error: "bug :(" })
    })
  })
  .listen(3000);

What is the expected behavior?

Posting
{name: ""}
to the /works endpoint returns the typebox validation failed object with the content-type headers of the response application/json. I would expect the /bug endpoint to do the same except substitute my custom error message.

{
  "type": "validation",
  "on": "body",
  "summary": "Expected string length greater or equal to 1",
  "property": "/name",
  "message": "Expected string length greater or equal to 1",
  "expected": {
    "name": " "
  },
  "found": {
    "name": ""
  },
  "errors": [
    {
      "summary": "Expected string length greater or equal to 1",
      "type": 52,
      "schema": {
        "minLength": 1,
        "type": "string"
      },
      "path": "/name",
      "value": "",
      "message": "Expected string length greater or equal to 1",
      "errors": []
    }
  ]
}

What do you see instead?

Posting
{name: ""}
to the /bug endpoint it returns just the custom error message as a string without any of the other typebox validation information and the response headers are still application/json.

bug: (

Additional information

No response

Have you try removing the node_modules and bun.lockb and try again yet?

Yes