-
Notifications
You must be signed in to change notification settings - Fork 0
Refine Project Update Input Validation #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughThis change adds title validation to the updateProject logic by trimming whitespace and enforcing a maximum length of 200 characters, returning BAD_REQUEST errors for empty or oversized titles. Tests are updated to validate these constraints using result-based assertions instead of exception-throwing patterns. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/actions/projects/updateProject/logic.test.ts (1)
107-107: Pre-existing bug: Mismatched project ID in assertion.The test input uses
id: 'project-123'(line 97), but the assertion on line 107 expectsid: 'project-100'. This appears to be a pre-existing bug in the test that should be corrected.🔎 Proposed fix
expect(prisma.project.updateMany).toHaveBeenCalledWith({ where: { - id: 'project-100', + id: 'project-123', updatedAt: new Date('2025-01-01T09:00:00.000Z'), }, data: { title: 'New Title' }, });
🧹 Nitpick comments (2)
src/actions/projects/updateProject/logic.test.ts (1)
116-127: Good test coverage, but consider adding a trimming verification test.The test correctly verifies that whitespace-only titles are rejected. However, there's no test that verifies the trimming behavior itself (e.g.,
' Valid Title 'should be trimmed to'Valid Title'and saved correctly).🔎 Suggested additional test case
it('should trim whitespace from title before saving', async () => { (prisma.project.update as jest.Mock).mockResolvedValue({ ...mockProject, title: 'Trimmed Title' }); const input = { id: 'project-123', title: ' Trimmed Title ' }; const result = await updateProject(input); expect(result.success).toBe(true); expect(prisma.project.update).toHaveBeenCalledWith({ where: { id: 'project-123' }, data: { title: 'Trimmed Title' }, // Verify trimmed value is passed }); });src/actions/projects/updateProject/logic.ts (1)
16-22: Verify schema constraints and address PR description inconsistency.The validation logic looks correct, but there are a few concerns:
- Schema verification needed: Line 16 calls
title.trim()without a null/undefined check. Iftitlecan be null or undefined, this will throw a runtime error.- PR description inconsistency: The PR description mentions validating both "project name and description," but only title validation is present. Is description validation missing, or is the PR description outdated?
- Magic number: The 200-character limit is hardcoded. Consider extracting it to a named constant for maintainability.
Run the following script to verify the schema definition:
#!/bin/bash # Description: Check UpdateProjectInput schema to verify title is required/non-nullable # Search for UpdateProjectInput schema definition ast-grep --pattern 'export const UpdateProjectInputSchema = $$$' # Also search for the type definition rg -n "UpdateProjectInput" --type=ts -C5🔎 Proposed refactor to extract magic number
+const MAX_TITLE_LENGTH = 200; + export async function updateProject(input: UpdateProjectInput): Promise<Result<Project>> { const { id, title, lastUpdatedAt } = input; try { let updatedProject: Project; const trimmedTitle = title.trim(); if (trimmedTitle.length === 0) { return error('Title is required', ErrorCodes.BAD_REQUEST); } - if (trimmedTitle.length > 200) { + if (trimmedTitle.length > MAX_TITLE_LENGTH) { return error('Title is too long', ErrorCodes.BAD_REQUEST); }
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (2)
src/actions/projects/updateProject/logic.test.ts(1 hunks)src/actions/projects/updateProject/logic.ts(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/actions/projects/updateProject/logic.test.ts (2)
src/actions/projects/updateProject/logic.ts (1)
updateProject(10-69)src/lib/prisma.ts (1)
prisma(14-18)
src/actions/projects/updateProject/logic.ts (2)
src/lib/result.ts (1)
error(38-44)src/lib/prisma.ts (1)
prisma(14-18)
🔇 Additional comments (2)
src/actions/projects/updateProject/logic.ts (1)
31-31: LGTM: Consistent use of trimmed title.The trimmed title is correctly used in both the optimistic locking path (updateMany) and the standard update path, ensuring consistent behavior across both branches.
Also applies to: 54-54
src/actions/projects/updateProject/logic.test.ts (1)
129-141: LGTM: Title length validation test is thorough.The test correctly verifies that titles exceeding 200 characters are rejected with appropriate error handling and no database calls.
Overview: This PR addresses validation for the project update logic, enhancing data integrity and standardizing error responses.
Changes
src/actions/projects/updateProject/logic.tsto remove extraneous whitespace.src/actions/projects/updateProject/logic.ts.Summary by CodeRabbit
Bug Fixes
Tests
✏️ Tip: You can customize this high-level summary in your review settings.