elysiajs / elysia

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

schema validation on query: cannot use Union or Intersect #848

Open mtt-artis opened 3 weeks ago

mtt-artis commented 3 weeks ago

What version of Elysia is running?

1.1.16

What platform is your computer?

Linux 6.5.11-linuxkit aarch64 unknown (bun dev container on mcbook air m1))

What steps can reproduce the bug?

const server = new Elysia().get('/', ({ query }) => {
    return query;
}, {
    type: 'json',
    query: t.Union([
        t.Object({
            q1: t.String(),
            q2: t.Optional(t.String()),
        }),
        t.Object({
            q1: t.Optional(t.String()),
            q2: t.String()
        })
    ]),
}).listen(3000);

console.log(await Promise.all([
    fetch("http://localhost:3000?q1=v").then(r => r.text()),
    fetch("http://localhost:3000?q2=v").then(r => r.text()),
    fetch("http://localhost:3000?q1=v&q2=v").then(r => r.text()),
    fetch("http://localhost:3000?q1=v&q2=v&q3=v").then(r => r.text()),
    fetch("http://localhost:3000").then(r => r.text()),
    fetch("http://localhost:3000?q3").then(r => r.text()),
]));

server.stop()

What is the expected behavior?

query to be type as { q2?: string | undefined; q1: string; } | { q1?: string | undefined; q2: string; }

What do you see instead?

ts error on the query schema key

Type 'TUnion<[TObject<{ q1: TString; q2: TOptional<TString>; }>, TObject<{ q1: TOptional<TString>; q2: TString; }>]>' is not assignable to type 'never'.ts(2322)

Additional information

Vs code info

Version: 1.93.1 (Universal) Commit: 38c31bc77e0dd6ae88a4e9cc93428cc27a56ba40 Date: 2024-09-11T17:20:05.685Z (1 wk ago) Electron: 30.4.0 ElectronBuildId: 10073054 Chromium: 124.0.6367.243 Node.js: 20.15.1 V8: 12.4.254.20-electron.0 OS: Darwin arm64 22.3.0

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

yes

K4leri commented 5 days ago

Intersect in runtime works fine and make validation errors as it should. I dont check the same for Union type