Skip to content
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions apps/example-todo-app/pages/api/todo/form-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { z } from "zod"
import { v4 as uuidv4 } from "uuid"
import { HttpException } from "nextlove"

export const formData = z.object({
id: z.string().uuid().optional().default(uuidv4()),
title: z.string(),
completed: z.boolean().optional().default(false),
})
export const formData = z
.object({
id: z.string().uuid().optional().default(uuidv4()),
title: z.string(),
completed: z.boolean().optional().default(false),
})
.optional()

export const route_spec = checkRouteSpec({
methods: ["POST"],
Expand Down
28 changes: 27 additions & 1 deletion apps/example-todo-app/tests/api/todo/form-add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import test from "ava"
import { TODO_ID } from "tests/fixtures"
import getTestServer from "tests/fixtures/get-test-server"
import { v4 as uuidv4 } from "uuid"
import { formData } from "pages/api/todo/form-add"
import { withRouteSpec } from "lib/middlewares"
import { z } from "zod"

test("POST /todo/form-add", async (t) => {
const { axios } = await getTestServer(t)
Expand All @@ -23,3 +24,28 @@ test("POST /todo/form-add", async (t) => {

t.is(successfulRes.status, 200)
})

test("Valid formData object passes validation", (t) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to actually call and endpoint

const validFormData = {
clear_sandbox_state: "clear_sandbox_state",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
clear_sandbox_state: "clear_sandbox_state",
completed: true,

}

withRouteSpec({
auth: "support",
methods: ["GET", "POST"],
queryParams: z.object({
workspace_id: z.string(),
}),
formData: z.object({
clear_sandbox_state: z.literal("clear_sandbox_state"),
}),
} as const)

const validationResult = z
.object({
clear_sandbox_state: z.literal("clear_sandbox_state"),
})
.safeParse(validFormData)

t.true(validationResult.success)
})
Loading