Validation with `Numeric` & `Number` options doesn't work
Closed this issue · 2 comments
bogeychan commented
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
im new here, but can you tell me what different numeric and number?
bogeychan commented
@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"
}