elysiajs / elysia

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

"default" option is not being applied on validation #664

Closed warcayac closed 3 months ago

warcayac commented 4 months ago

What version of Elysia.JS is running?

1.0.22

What platform is your computer?

Linux 6.9.2-arch1-1 x86_64 unknown

What steps can reproduce the bug?

export const apiUsersRoutes = new Elysia({prefix:'/users'})
  .post(
    '/',
    async ({body}) => {
      const result = await users.addOne(body);
      return result;
    },
    {
      body: t.Object({
        username: t.String(),
        password: t.String(),
        email: t.Optional(t.String({format: 'email'})),
        isSuperuser: t.Boolean({default: false}),
      })
    }
  )

then try to send a POST request with this body:

{ "username": "admin", "password": "123456789", "email": "admin@chesu.org" }

What is the expected behavior?

body is accepted with default value applied to "isSuperuser" property

What do you see instead?

image

If I send this body: { "username": "admin", "password": "123456789", "email": "admin@chesu.org", "isSuperuser": false }

then it is accepted and processed.

Additional information

No response

david-plugge commented 4 months ago

Workaround: Wrap it into optional and pass false as a second parameter

warcayac commented 4 months ago

@david-plugge it doesn't work, isSuperuser is ignored and never is assigned the default value.

SaltyAom commented 3 months ago

7d0e29b published on 1.0.23 including test case. Let me know if it works on your end.

warcayac commented 3 months ago

it is working as expected, thanks.

M-Gonzalo commented 3 months ago

Oh I think I'm relying on this bug for my app to work as intended. I'm not experienced with Typebox so when I saw that default values would get translated to examples in Swagger, I started using them everywhere. But if they can actually permeate to the DB, I should go and update everything at once lol