Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughAdds a pagination parser and constants for the public Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
src/app/api/prompts/route.ts (1)
22-33: Consider moving query validation to a small Zod schema for route consistency.The logic works, but this route family guideline prefers Zod-based request validation and would make this parser contract explicit and easier to evolve.
As per coding guidelines, "Use Zod for request validation in API routes".
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/app/api/prompts/route.ts` around lines 22 - 33, Replace the ad-hoc parsing in parsePublicPromptPagination with a small Zod schema that validates and coerce query values: define a Zod object schema (e.g., PublicPromptQuerySchema) with optional string/number fields for "page" and "perPage", apply transforms/coercions to positive integers and default to DEFAULT_PUBLIC_PROMPTS_PAGE and DEFAULT_PUBLIC_PROMPTS_PER_PAGE, then use schema.parse/coerce on Object.fromEntries(searchParams) inside parsePublicPromptPagination and compute perPage = Math.min(parsed.perPage, MAX_PUBLIC_PROMPTS_PER_PAGE); keep the same return shape ({ page, perPage }) and preserve the existing constants (DEFAULT_PUBLIC_PROMPTS_PAGE, DEFAULT_PUBLIC_PROMPTS_PER_PAGE, MAX_PUBLIC_PROMPTS_PER_PAGE).src/app/api/prompts/__tests__/route.test.ts (1)
5-18: Add malformed-number regression cases to lock parser intent.Please add cases like
"2.5"and"10abc"to ensure malformed values fall back to defaults rather than being partially parsed.➕ Suggested test additions
describe("parsePublicPromptPagination", () => { + it("defaults malformed numeric tokens", () => { + const params = new URLSearchParams({ page: "2.5", perPage: "10abc" }); + expect(parsePublicPromptPagination(params)).toEqual({ page: 1, perPage: 24 }); + }); + it("defaults invalid values", () => { const params = new URLSearchParams({ page: "0", perPage: "NaN" }); expect(parsePublicPromptPagination(params)).toEqual({ page: 1, perPage: 24 }); });🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/app/api/prompts/__tests__/route.test.ts` around lines 5 - 18, Add regression tests to lock parser intent: update src/app/api/prompts/__tests__/route.test.ts to include cases that pass malformed numeric strings to parsePublicPromptPagination so they fall back to defaults rather than being partially parsed. Specifically add a test calling parsePublicPromptPagination(new URLSearchParams({ page: "2.5", perPage: "50" })) and asserting { page: 1, perPage: 50 }, and another calling parsePublicPromptPagination(new URLSearchParams({ page: "2", perPage: "10abc" })) and asserting { page: 2, perPage: 24 } (use the existing default perPage 24 and clamp rules); reference the parsePublicPromptPagination function in your assertions.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/app/api/prompts/route.ts`:
- Around line 16-33: The current manual parser (parsePositiveInt) accepts
malformed numbers like "2.5" or "10abc" and page has no upper bound; replace
this with Zod validation inside parsePublicPromptPagination: define a Zod schema
(e.g., paginationSchema) that parses searchParams.get("page") and
searchParams.get("perPage") as integers (z.coerce.number().int().positive())
with safe defaults (DEFAULT_PUBLIC_PROMPTS_PAGE,
DEFAULT_PUBLIC_PROMPTS_PER_PAGE), enforce a max for perPage using
MAX_PUBLIC_PROMPTS_PER_PAGE, and enforce a sensible max for page (add or reuse a
MAX_PUBLIC_PROMPTS_PAGE constant) to prevent unbounded skips; then return {
page, perPage } from parsePublicPromptPagination using values validated by the
Zod schema and remove/replace parsePositiveInt usage.
---
Nitpick comments:
In `@src/app/api/prompts/__tests__/route.test.ts`:
- Around line 5-18: Add regression tests to lock parser intent: update
src/app/api/prompts/__tests__/route.test.ts to include cases that pass malformed
numeric strings to parsePublicPromptPagination so they fall back to defaults
rather than being partially parsed. Specifically add a test calling
parsePublicPromptPagination(new URLSearchParams({ page: "2.5", perPage: "50" }))
and asserting { page: 1, perPage: 50 }, and another calling
parsePublicPromptPagination(new URLSearchParams({ page: "2", perPage: "10abc"
})) and asserting { page: 2, perPage: 24 } (use the existing default perPage 24
and clamp rules); reference the parsePublicPromptPagination function in your
assertions.
In `@src/app/api/prompts/route.ts`:
- Around line 22-33: Replace the ad-hoc parsing in parsePublicPromptPagination
with a small Zod schema that validates and coerce query values: define a Zod
object schema (e.g., PublicPromptQuerySchema) with optional string/number fields
for "page" and "perPage", apply transforms/coercions to positive integers and
default to DEFAULT_PUBLIC_PROMPTS_PAGE and DEFAULT_PUBLIC_PROMPTS_PER_PAGE, then
use schema.parse/coerce on Object.fromEntries(searchParams) inside
parsePublicPromptPagination and compute perPage = Math.min(parsed.perPage,
MAX_PUBLIC_PROMPTS_PER_PAGE); keep the same return shape ({ page, perPage }) and
preserve the existing constants (DEFAULT_PUBLIC_PROMPTS_PAGE,
DEFAULT_PUBLIC_PROMPTS_PER_PAGE, MAX_PUBLIC_PROMPTS_PER_PAGE).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: c77fbe2a-088b-4b1c-8927-5c50c19f572d
📒 Files selected for processing (2)
src/app/api/prompts/__tests__/route.test.tssrc/app/api/prompts/route.ts
Closes #1129
Summary
page/perPagevalues on the public prompts APIperPagerequests to a public maximum of 100Why
The public
GET /api/promptsendpoint previously passed unbounded query params directly into Prisma pagination, which allowed oversized reads and payloads.Testing
Summary by CodeRabbit
Tests
Refactor