Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
warcayac opened this issue May 31, 2024 · 5 comments
Closed

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

warcayac opened this issue May 31, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@warcayac
Copy link

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

@warcayac warcayac added the bug Something isn't working label May 31, 2024
@david-plugge
Copy link

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

@warcayac
Copy link
Author

warcayac commented Jun 1, 2024

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

SaltyAom added a commit that referenced this issue Jun 9, 2024
@SaltyAom
Copy link
Member

SaltyAom commented Jun 9, 2024

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

@warcayac
Copy link
Author

warcayac commented Jun 9, 2024

it is working as expected, thanks.

@M-Gonzalo
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants