elysiajs / elysia

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

Loose/less-effort body validation #840

Open hyoretsu opened 1 month ago

hyoretsu commented 1 month ago

What version of Elysia is running?

1.1.13

What platform is your computer?

Microsoft Windows NT 10.0.22631.0 x64

What steps can reproduce the bug?

Use this validation in a route

body: t.Union([
    t.Object({
        bookmarked: t.Optional(t.Boolean()),
        link: t.Optional(t.String({ format: "uri" })),
        timeSpent: t.Optional(t.String()),
        when: t.Optional(t.Date()),
    }),
    t.Object({
        bookmarked: t.Optional(t.Boolean()),
        mediaId: t.String(),
        number: t.Integer(),
        timeSpent: t.Optional(t.String()),
        when: t.Optional(t.Date()),
    }),
    t.Object({
        bookmarked: t.Optional(t.Boolean()),
        mediaId: t.Optional(t.String()),
        timeSpent: t.Optional(t.String()),
        when: t.Optional(t.Date()),
    }),
])

And provide this body:

{
"mediaId": "610132145125381",
"number": 1127,
"timeSpent": "PT4M35S",
"when":"2024-09-20T22:05:00.000-03:00"
}

What is the expected behavior?

This shouldn't be an edge case and it should recognize the body as the second type.

What do you see instead?

It recognizes the body as the first type and simply deletes mediaId and number from the body. They exist in ctx.body during onTransform;

Additional information

No response

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

No response

hyoretsu commented 1 month ago

If I add { additionalProperties: false }, to the first field it works.

kravetsone commented 1 month ago

Can u try it with plain typebox?