elysiajs / elysia

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

Validation with `Numeric` & `Number` options doesn't work #855

Closed bogeychan closed 1 month ago

bogeychan commented 1 month ago

What version of Elysia is running?

1.1.16

What platform is your computer?

WSL Ubuntu

What steps can reproduce the bug?

// test.test.ts
import { describe, it, expect } from "bun:test";
import { Elysia, t } from "elysia";

const numberApp = new Elysia()
  .onError(({ code }) => code)
  .get("/:entityType", ({ params: { entityType } }) => entityType, {
    params: t.Object({
      entityType: t.Number({
        minimum: 0,
        maximum: 3,
        multipleOf: 1,
      }),
    }),
  });

const numericApp = new Elysia()
  .onError(({ code }) => code)
  .get("/:entityType", ({ params: { entityType } }) => entityType, {
    params: t.Object({
      entityType: t.Numeric({
        minimum: 0,
        maximum: 3,
        multipleOf: 1,
      }),
    }),
  });

async function expectValidResponse(response: Response) {
  expect(response.status).toBe(422);
  const body = await response.text();
  expect(body).not.toBe("UNKNOWN");
  expect(body).not.toBe("999");
}

describe("Elysia", () => {
  it("Number", async () => {
    const response = await numberApp.handle(
      new Request("http://localhost/999")
    );
    await expectValidResponse(response);
  });

  it("Numeric", async () => {
    const response = await numericApp.handle(
      new Request("http://localhost/999")
    );
    await expectValidResponse(response);
  });
});

What is the expected behavior?

all tests pass

What do you see instead?

all tests fail

Additional information

reported on discord

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

yes

nursyah21 commented 1 month ago

im new here, but can you tell me what different numeric and number?

bogeychan commented 1 month ago

@nursyah21 if you have this app:

import { Elysia, t } from "elysia";

new Elysia()
  .post("/", ({ body }) => body, {
    body: t.Object({
      age: t.Number(),
    }),
  })
  .listen(3000);

Only this works with Numeric because it allows both string and number values:

{
    "age": "42"
}