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

Nested t.Integer() validation too loose #1011

Open
and-rose opened this issue Jan 12, 2025 · 2 comments · May be fixed by #1017
Open

Nested t.Integer() validation too loose #1011

and-rose opened this issue Jan 12, 2025 · 2 comments · May be fixed by #1017
Labels
bug Something isn't working

Comments

@and-rose
Copy link

and-rose commented Jan 12, 2025

What version of Elysia is running?

1.2.10

What platform is your computer?

Darwin 24.1.0 arm64 arm

What steps can reproduce the bug?

wrote some tests for test/validator/body.test.ts to investigate t.Integer() validation that was passing through in my project. Found that it wasn't validating my integers at all and was only really checking there were numbers.

	it('rejects malformed integer from array object', async () => {
		const app = new Elysia().post('/', ({ body }) => body, {
			body: t.Array(
				t.Object({
					name: t.String(),
					job: t.String(),
					trait: t.Optional(t.String()),
					age: t.Integer(),
					rank: t.Integer()
				})
			)
		})
		const res = await app.handle(
			post('/', [
				{
					name: 'sucrose',
					job: 'alchemist',
					age: 16.4,
					rank: 4
				}
			])
		)

		expect(res.status).toBe(422)
	})

	it('rejects malformed integer directly in array', async () => {
		const app = new Elysia().post('/', ({ body }) => body, {
			body: t.Array(t.Integer())
		})
		const res = await app.handle(post('/', [1, 2, 3, 4.2]))

		expect(res.status).toBe(422)
	})

found that they fail and pass through a 200. Seems like integer support is new #953 and this may have been an oversight?

What is the expected behavior?

Elysia should validate the nested integer and return a 422

What do you see instead?

Elysia passes through the non-integer to deeper layers, unvalidated.

Additional information

No response

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

yes

@and-rose and-rose added the bug Something isn't working label Jan 12, 2025
@nxht
Copy link

nxht commented Jan 16, 2025

I think it's due to using t.Number() as union in the pull request mentioned.

https://github.com/elysiajs/elysia/pull/953/files#r1893933305
It should be fixed by changing the union type to Type.Integer as the comment said.

@and-rose and-rose linked a pull request Jan 16, 2025 that will close this issue
@Vadym-Balan
Copy link

I've passed error function inside t.Number() and it works.

  duration: t.Number({
    minimum: 1,
    maximum: 5,
    error() {
      return "Duration should be between 1 and 5";
    },
  }),

https://elysiajs.com/essential/validation.html#error-is-called-per-field

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

Successfully merging a pull request may close this issue.

3 participants