Skip to content

Add Zod validation to server actions and API chat route; replace root interfaces with Zod schemas#197

Merged
na-trium-144 merged 3 commits intomainfrom
copilot/add-zod-validation-server-actions
Mar 19, 2026
Merged

Add Zod validation to server actions and API chat route; replace root interfaces with Zod schemas#197
na-trium-144 merged 3 commits intomainfrom
copilot/add-zod-validation-server-actions

Conversation

Copy link
Contributor

Copilot AI commented Mar 18, 2026

app/actions/ server action parameters and the app/api/chat/ POST body lacked runtime validation. Types like MarkdownSection, ReplCommand, etc. were defined as bare interfaces, requiring a separate schema to validate — so the types themselves are rewritten as Zod schemas with z.output<typeof schema>.

Type definitions replaced with Zod schemas

  • app/lib/docs.tsinterface PagePathPagePathSchema, interface MarkdownSectionMarkdownSectionSchema. Also adds ReplacedRangeSchema and DynamicMarkdownSectionSchema (consolidated here from client files to make schemas server-importable).
  • packages/runtime/src/interface.tsReplOutputType, ReplOutput, UpdatedFile, ReplCommand interfaces all replaced with Zod schemas + z.output<> types. Added zod to the package's deps.
  • app/markdown/multiHighlight.tsx / pageContent.tsx — local interface ReplacedRange / interface DynamicMarkdownSection removed; re-export the types from @/lib/docs.
// Before
export interface MarkdownSection { file: string; id: SectionId; ... }

// After
export const MarkdownSectionSchema = z.object({
  file: z.string(),
  id: z.string().transform((s) => s as SectionId),
  ...
});
export type MarkdownSection = z.output<typeof MarkdownSectionSchema>;

Validation added

  • app/api/chat/route.tstype ChatParams replaced by ChatParamsSchema (composed from all sub-schemas above); POST body is now validated via safeParse, returning HTTP 400 on invalid input.
  • app/actions/deleteChat.ts / app/actions/getRedirectFromChat.tschatId parameter validated with z.string().uuid() before use.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • workers.cloudflare.com
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node node node_modules/.bin/next lint (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.

- Replace `interface MarkdownSection`, `interface PagePath` in docs.ts
  with Zod schemas + `z.output<typeof schema>` types
- Add `ReplacedRangeSchema` and `DynamicMarkdownSectionSchema` to docs.ts,
  moving the type definitions out of the client-only pageContent.tsx
- Remove `interface ReplacedRange` from multiHighlight.tsx; re-export
  `ReplacedRange` type from @/lib/docs
- Remove `interface DynamicMarkdownSection` from pageContent.tsx; re-export
  `DynamicMarkdownSection` type from @/lib/docs
- Replace `ReplOutputType` union, `ReplOutput`, `UpdatedFile`, `ReplCommand`
  interfaces in packages/runtime/src/interface.ts with Zod schemas +
  `z.output<typeof schema>` types; add zod dependency to runtime package
- Replace `type ChatParams` in route.ts with `ChatParamsSchema` + Zod
  validation of POST request body (returns 400 on invalid input)
- Add `z.string().uuid()` validation to deleteChat and getRedirectFromChat
  server action parameters

Co-authored-by: na-trium-144 <100704180+na-trium-144@users.noreply.github.com>
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Mar 18, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
my-code f759e87 Commit Preview URL

Branch Preview URL
Mar 19 2026, 02:16 PM

Copilot AI changed the title [WIP] Add Zod validation for ServerAction parameters Add Zod validation to server actions and API chat route; replace root interfaces with Zod schemas Mar 18, 2026
Copilot AI requested a review from na-trium-144 March 18, 2026 13:22
@na-trium-144
Copy link
Contributor

@copilot

  const parsed = chatIdSchema.safeParse(chatId);
  if (!parsed.success) {
    throw new Error(parsed.error.issues.map((e) => e.message).join(", "));
  }

これは const parsed = chatIdSchema.parse(chatId); (エラー時にはZodErrorがthrowされる) と書いた方がシンプルだと思ったのですが、何が違う?

Copy link
Contributor Author

Copilot AI commented Mar 19, 2026

@na-trium-144 The model claude-sonnet-4.6 is not available for your account. This can happen if the model was disabled by your organization's policy or if your Copilot plan doesn't include access to it.

You can try again without specifying a model (just @copilot) to use the default, or choose a different model from the model picker.

If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: b3a0cf5f-611a-4591-a903-2e07980acf6b

Copy link
Contributor Author

Copilot AI commented Mar 19, 2026

@na-trium-144 The model claude-sonnet-4.6 is not available for your account. This can happen if the model was disabled by your organization's policy or if your Copilot plan doesn't include access to it.

You can try again without specifying a model (just @copilot) to use the default, or choose a different model from the model picker.

If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: 078caba8-8e39-400c-8e51-87bb82c37e7b

Copy link
Contributor Author

Copilot AI commented Mar 19, 2026

@na-trium-144 The model claude-sonnet-4.6 is not available for your account. This can happen if the model was disabled by your organization's policy or if your Copilot plan doesn't include access to it.

You can try again without specifying a model (just @copilot) to use the default, or choose a different model from the model picker.

If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: b3a0cf5f-611a-4591-a903-2e07980acf6b

Copy link
Contributor Author

Copilot AI commented Mar 19, 2026

@na-trium-144 The model claude-sonnet-4.6 is not available for your account. This can happen if the model was disabled by your organization's policy or if your Copilot plan doesn't include access to it.

You can try again without specifying a model (just @copilot) to use the default, or choose a different model from the model picker.

If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: 078caba8-8e39-400c-8e51-87bb82c37e7b

Copy link
Contributor Author

Copilot AI commented Mar 19, 2026

@na-trium-144 The model claude-sonnet-4.6 is not available for your account. This can happen if the model was disabled by your organization's policy or if your Copilot plan doesn't include access to it.

You can try again without specifying a model (just @copilot) to use the default, or choose a different model from the model picker.

If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: b3a0cf5f-611a-4591-a903-2e07980acf6b

Copy link
Contributor Author

Copilot AI commented Mar 19, 2026

@na-trium-144 The model claude-sonnet-4.6 is not available for your account. This can happen if the model was disabled by your organization's policy or if your Copilot plan doesn't include access to it.

You can try again without specifying a model (just @copilot) to use the default, or choose a different model from the model picker.

If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: 078caba8-8e39-400c-8e51-87bb82c37e7b

Copy link
Contributor Author

Copilot AI commented Mar 19, 2026

@na-trium-144 The model claude-sonnet-4.6 is not available for your account. This can happen if the model was disabled by your organization's policy or if your Copilot plan doesn't include access to it.

You can try again without specifying a model (just @copilot) to use the default, or choose a different model from the model picker.

If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: b3a0cf5f-611a-4591-a903-2e07980acf6b

Copy link
Contributor Author

Copilot AI commented Mar 19, 2026

@na-trium-144 The model claude-sonnet-4.6 is not available for your account. This can happen if the model was disabled by your organization's policy or if your Copilot plan doesn't include access to it.

You can try again without specifying a model (just @copilot) to use the default, or choose a different model from the model picker.

If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: 078caba8-8e39-400c-8e51-87bb82c37e7b

Copy link
Contributor Author

Copilot AI commented Mar 19, 2026

@na-trium-144 The model claude-sonnet-4.6 is not available for your account. This can happen if the model was disabled by your organization's policy or if your Copilot plan doesn't include access to it.

You can try again without specifying a model (just @copilot) to use the default, or choose a different model from the model picker.

If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: b3a0cf5f-611a-4591-a903-2e07980acf6b

Copy link
Contributor Author

Copilot AI commented Mar 19, 2026

@na-trium-144 The model claude-sonnet-4.6 is not available for your account. This can happen if the model was disabled by your organization's policy or if your Copilot plan doesn't include access to it.

You can try again without specifying a model (just @copilot) to use the default, or choose a different model from the model picker.

If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: 078caba8-8e39-400c-8e51-87bb82c37e7b

Copy link
Contributor Author

Copilot AI commented Mar 19, 2026

@na-trium-144 The model claude-sonnet-4.6 is not available for your account. This can happen if the model was disabled by your organization's policy or if your Copilot plan doesn't include access to it.

You can try again without specifying a model (just @copilot) to use the default, or choose a different model from the model picker.

If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: b3a0cf5f-611a-4591-a903-2e07980acf6b

Copy link
Contributor Author

Copilot AI commented Mar 19, 2026

@na-trium-144 The model claude-sonnet-4.6 is not available for your account. This can happen if the model was disabled by your organization's policy or if your Copilot plan doesn't include access to it.

You can try again without specifying a model (just @copilot) to use the default, or choose a different model from the model picker.

If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: 078caba8-8e39-400c-8e51-87bb82c37e7b

@na-trium-144
Copy link
Contributor

@copilot

  const parsed = chatIdSchema.safeParse(chatId);
  if (!parsed.success) {
    throw new Error(parsed.error.issues.map((e) => e.message).join(", "));
  }

これは const parsed = chatIdSchema.parse(chatId); (エラー時にはZodErrorがthrowされる) と書いた方がシンプルだと思ったのですが、何が違う?

Copy link
Contributor Author

Copilot AI commented Mar 19, 2026

@copilot

  const parsed = chatIdSchema.safeParse(chatId);

...

違いは主に2点です。parse は失敗時に ZodError をそのまま throw し、safeParse{ success: false, error } を返すので、こちらで Error(message) 形式に整形して投げられます。今回の実装では呼び出し側へ返る例外メッセージ形式を明示的にそろえるために safeParse + 手動 throw を使っています。

@copilot

...

おっしゃる通り、シンプルさだけなら parse で書けます。違いは、parse だと ZodError がそのまま伝播し、safeParse だと失敗時の文言・形式をこちらで統一できる点です。ここでは throw new Error(issues.join(", ")) にして呼び出し側へ返るメッセージを一定にする意図で safeParse を選んでいます。

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • workers.cloudflare.com
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node node node_modules/.bin/next lint (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@na-trium-144 na-trium-144 marked this pull request as ready for review March 19, 2026 14:19
@na-trium-144 na-trium-144 merged commit 28504a9 into main Mar 19, 2026
8 of 9 checks passed
@na-trium-144 na-trium-144 deleted the copilot/add-zod-validation-server-actions branch March 19, 2026 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

server functionにバリデーション

2 participants