32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
import { expect, test } from "bun:test";
|
|
import { createContext, defineEndpoint } from "../src/index.ts";
|
|
import { v } from "@wrnexus/validation";
|
|
|
|
const user = v.object({ name: v.string().min(2), email: v.string().email() });
|
|
const endpoint = defineEndpoint({
|
|
input: user,
|
|
output: user,
|
|
handler(input) {
|
|
return input;
|
|
},
|
|
});
|
|
|
|
test("typed endpoints unwrap official validation schemas and return bounded validation errors", async () => {
|
|
const request = new Request("https://example.test/api/user");
|
|
const ctx = createContext(request, new URL(request.url));
|
|
const invalid = await endpoint(ctx, { name: "A", email: "bad" });
|
|
expect(invalid.status).toBe(400);
|
|
expect(await invalid.json()).toEqual({
|
|
error: {
|
|
code: "VALIDATION_ERROR",
|
|
message: "Endpoint validation failed.",
|
|
details: {
|
|
name: "Must be at least 2 characters",
|
|
email: "Must be a valid email",
|
|
},
|
|
},
|
|
});
|
|
const valid = await endpoint(ctx, { name: "Ada", email: "ada@example.test" });
|
|
expect(await valid.json()).toEqual({ data: { name: "Ada", email: "ada@example.test" } });
|
|
});
|