Repository navigation
feat: add FormDefinition validation to update controller and export Z… - #78
Conversation
…od error formatter
|
Warning Review limit reached
Next review available in: 18 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthrough
updateForm Validation and Error Handling
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
apps/api/src/controllers/form.controller.ts (1)
178-201: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick winPersist the parsed result, not the raw
definition.
FormDefinitionSchema.safeParse(definition)normalizes the payload (notablyversionvia.default("1.0")), butparsed.datais discarded and the rawdefinitionis written tofieldson Line 201. As a result, Zod-applied defaults are never persisted — this is the same reason thevalidatemiddleware reassignsreq.body = result.data. A client omittingversionwould store an un-versioned definition, defeating the schema-versioning intent.🛠️ Proposed fix to persist normalized data
if (definition !== undefined) { const parsed = FormDefinitionSchema.safeParse(definition); if (!parsed.success) { res.status(400).json({ success: false, message: "Validation failed", errors: formatZodErrors(parsed.error), }); return; } + // use the normalized value so defaults (e.g. version) are persisted + definition = parsed.data; }Note: this requires
definitionto be mutable (e.g. destructure into alet).🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@apps/api/src/controllers/form.controller.ts` around lines 178 - 201, Persist the normalized schema result from FormDefinitionSchema.safeParse in form.controller's update flow instead of writing the raw definition object to prisma.form.update. After parsing definition, use parsed.data for the fields assignment so Zod defaults like version are preserved, and make definition mutable if needed by the existing destructuring in the controller before building the update payload.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@apps/api/src/controllers/form.controller.ts`:
- Around line 178-201: Persist the normalized schema result from
FormDefinitionSchema.safeParse in form.controller's update flow instead of
writing the raw definition object to prisma.form.update. After parsing
definition, use parsed.data for the fields assignment so Zod defaults like
version are preserved, and make definition mutable if needed by the existing
destructuring in the controller before building the update payload.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 9f6e9b24-353e-4324-9692-902f0862c276
📒 Files selected for processing (2)
apps/api/src/controllers/form.controller.tsapps/api/src/middleware/validate.ts
📜 Review details
🔇 Additional comments (2)
apps/api/src/middleware/validate.ts (1)
39-45: LGTM!apps/api/src/controllers/form.controller.ts (1)
221-232: LGTM!
Basharkhan7776
left a comment
There was a problem hiding this comment.
Just added controler, merging
Summary
Implements the
[Block] Edit/[id] APIsby refining existing endpoints and adding explicit defensive validation.Changes Made
GET /api/v1/forms/:idhandles drafts correctly (returns form data byuserIdregardless ofpublishedstate).PATCH /api/v1/forms/:idproperly accepts updates to theForm.definitionJSON.formatZodErrorshelper from validation middleware for reuse.FormDefinitionSchema.safeParse()validation inside theupdateFormcontroller as defense-in-depth before touching the database.updateFormby properly trapping the PrismaP2025error if the form is deleted between the ownership check and the update.Testing
bun turbo lintpasses (0 errors, 0 warnings).Related Issues
Checklist