Skip to content

t.Union() ignores subsequent type parsing when encountering an empty t.Object({}) #1783

@lycpan233

Description

@lycpan233

What version of Elysia is running?

elysia@1.4.27

What platform is your computer?

Darwin 25.2.0 arm64 arm

What environment are you using

Bun 1.3.10

Are you using dynamic mode?

using default

What steps can reproduce the bug?

import axios from 'axios'
import Elysia, { t } from 'elysia'
;(async () => {
  const app = new Elysia()
    .get(
      '/',
      () => {
        return {
          status: 200,
          message: 'Hello World',
          list: [
            {
              command: 'idle' as const,
              actions: {},
            },
            {
              command: 'doSomething' as const,
              actions: {
                doSomething: 'doSomething',
              },
            },
          ],
        }
      },
      {
        response: t.Object({
          status: t.Number(),
          message: t.String(),
          list: t.Array(
            t.Union([
              t.Object({
                command: t.Union([t.Literal('idle'), t.Literal('doSomething')]),
                actions: t.Object({}),
              }),
              t.Object({
                command: t.Union([t.Literal('idle'), t.Literal('doSomething')]),
                actions: t.Object({
                  doSomething: t.String(),
                }),
              }),
            ])
          ),
        }),
      }
    )
    .listen(3333)

  const res = await axios.get('http://localhost:3333')
  console.log(JSON.stringify(res.data, null, 2))
// {
//   "status": 200,
//   "message": "Hello World",
//   "list": [
//     { "command": "idle", "actions": {} },
//     { "command": "doSomething", "actions": {} } // doSomething is missing here!
//   ]
// }

  process.exit(0)
})()

The second element in the list (index 1) is not being output correctly. It seems the parser stops at the first empty t.Object({}) inside the t.Union and ignores subsequent, more specific schemas.

  list: t.Array(
    t.Union([
      t.Object({
        command: t.Union([t.Literal('idle'), t.Literal('doSomething')]),
        actions: t.Object({
          doSomething: t.String(),
        }),
      }),
      t.Object({
        command: t.Union([t.Literal('idle'), t.Literal('doSomething')]),
        actions: t.Object({}),
      }),
    ])
  ),

The issue is resolved after swapping the order of elements within t.Union(). This indicates that the validation logic is short-circuiting on the first matching schema.

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

I've traced the issue back to a specific commit in version v1.4.20. While I'm not entirely sure which original bug this commit was intended to fix, it has introduced a regression where t.Union no longer correctly parses subsequent schemas if it encounters an empty object first.

#1649

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

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions