Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

zod (#281)

Maximilian Deichmannfcee0eb9743b
diff --git a/src/__tests__/zod.servertest.ts b/src/__tests__/zod.servertest.ts
new file mode 100644
index 0000000000..18a2daa72c
--- /dev/null
+++ b/src/__tests__/zod.servertest.ts
@@ -0,0 +1,26 @@
+import { paginationZod } from "@/src/utils/zod";
+import { ZodError } from "zod";
+
+// Create test cases
+describe("Pagination Zod Schema", () => {
+ it("should validate valid input", () => {
+ const pageResult = paginationZod.page.parse("2");
+ const limitResult = paginationZod.limit.parse("20");
+
+ expect(pageResult).toBe(2);
+ expect(limitResult).toBe(20);
+ });
+
+ it("should handle empty values", () => {
+ const pageResult = paginationZod.page.parse("");
+ const limitResult = paginationZod.limit.parse("");
+
+ expect(pageResult).toBe(1);
+ expect(limitResult).toBe(50);
+ });
+
+ it("should handle invalid input", () => {
+ expect(() => paginationZod.page.parse("abc")).toThrowError(ZodError);
+ expect(() => paginationZod.limit.parse("abc")).toThrowError(ZodError);
+ });
+});
diff --git a/src/utils/zod.ts b/src/utils/zod.ts
index 339e407551..1c8909e34c 100644
--- a/src/utils/zod.ts
+++ b/src/utils/zod.ts
@@ -39,9 +39,12 @@ export const jsonSchema: z.ZodType<Json> = z.lazy(() =>
);
export const paginationZod = {
- page: z.preprocess((x) => (x === "" ? undefined : x), z.number().default(1)),
+ page: z.preprocess(
+ (x) => (x === "" ? undefined : x),
+ z.coerce.number().default(1),
+ ),
limit: z.preprocess(
(x) => (x === "" ? undefined : x),
- z.number().lte(100).default(50),
+ z.coerce.number().lte(100).default(50),
),
};