forked from Lilly-Protocol/lily-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate-params-query.test.ts
More file actions
72 lines (63 loc) · 2.57 KB
/
Copy pathvalidate-params-query.test.ts
File metadata and controls
72 lines (63 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { describe, it, expect } from "vitest";
import express from "express";
import request from "supertest";
import { z } from "zod";
import { validateParams, validateQuery } from "../src/common/http/validate.middleware";
import { errorHandler } from "../src/common/http/error.middleware";
describe("validateParams and validateQuery helpers (issue #86)", () => {
const createAppWithValidation = () => {
const app = express();
app.get(
"/items/:id",
validateParams(z.object({ id: z.string().uuid() })),
(req, res) => {
res.json({ success: true, data: { id: req.params.id } });
},
);
app.get(
"/search",
validateQuery(z.object({ q: z.string().min(1), limit: z.coerce.number().int().positive().optional() })),
(req, res) => {
const vq = (req as Request & { validatedQuery?: { q: string; limit?: number } }).validatedQuery!; res.json({ success: true, data: { q: vq.q, limit: vq.limit } });
},
);
app.use(errorHandler);
return app;
};
it("should accept valid params and replace req.params with parsed data", async () => {
const id = "550e8400-e29b-41d4-a716-446655440000";
const app = createAppWithValidation();
const res = await request(app).get(`/items/${id}`);
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.data.id).toBe(id);
});
it("should reject invalid params with 400 envelope", async () => {
const app = createAppWithValidation();
const res = await request(app).get("/items/not-a-uuid");
expect(res.status).toBe(400);
expect(res.body.success).toBe(false);
expect(res.body.message).toBe("Request validation failed");
});
it("should accept valid query and coerce types", async () => {
const app = createAppWithValidation();
const res = await request(app).get("/search?q=test&limit=10");
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
expect(res.body.data.q).toBe("test");
expect(res.body.data.limit).toBe(10);
expect(typeof res.body.data.limit).toBe("number");
});
it("should reject missing required query params with 400", async () => {
const app = createAppWithValidation();
const res = await request(app).get("/search");
expect(res.status).toBe(400);
expect(res.body.success).toBe(false);
});
it("should reject invalid coerced query values with 400", async () => {
const app = createAppWithValidation();
const res = await request(app).get("/search?q=test&limit=-5");
expect(res.status).toBe(400);
expect(res.body.success).toBe(false);
});
});