fix: show error message for duplicate env var keys - #5251
Conversation
The Zod superRefine validation already catches duplicate keys but only marks the second occurrence. This change flags all rows with the same key so users can see which entries conflict. Also adds a reason tooltip to the disabled save button. Fixes unkeyed#5219
|
@Felipeness is attempting to deploy a commit to the Unkey Team on Vercel. A member of the Team first needs to authorize it. |
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughPropagates react-hook-form's trigger from EnvVarsForm to EnvVarRow to prompt re-validation on environment changes; switches duplicate-key validation to a grouped pass that reports all duplicate indices; updates save-state to include reason "Fix validation errors above" when form is invalid. Changes
Sequence Diagram(s)mermaid Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ 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 |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
ogzhanolguncu
left a comment
There was a problem hiding this comment.
Works great, we just have to trigger validation again and get rid of the stale error. This only happens when changes(changing variables' environment). Check out the video
Screen.Recording.2026-03-09.at.14.24.53.mov
Clear stale duplicate-key errors when a variable's environment is changed, since the compound key includes the environment ID. Addresses review feedback on unkeyed#5251
Felipeness
left a comment
There was a problem hiding this comment.
Thanks for catching that and for the video — it made the issue super clear! The root cause was that changing the environment didn't re-trigger the array-level superRefine validation, so the stale error stuck around. I've pushed a fix (trigger("envVars") on environment change) to force re-validation when the variable's environment is updated. Could you give it another look when you get a chance?
Merge upstream changes (RemoveButton import from unkeyed#5211) with our UseFormTrigger addition, keeping both imports.
useFieldArray.remove() does not re-trigger array-level superRefine
validation, so removing one of two duplicate rows left the remaining
row showing a stale "Duplicate key" error. Calling trigger("envVars")
after remove() forces re-validation and clears the error.
|
While auditing the code for this PR, I also noticed a pre-existing issue in the env vars component: the React Opened a separate small PR for that: #5262. Not 100% sure it causes visible issues in practice, but just trying to help clean things up! |
Summary
superRefinevalidation inschema.tsto mark all rows with duplicate keys (both first and subsequent occurrences), not just the second one. Previously, the first row with a duplicate key showed no error, leaving users confused about why the Save button was disabled."Fix validation errors above") so users understand why saving is blocked when validation errors exist.How it works
The original code used a
Map<string, number>to track the first seen index. When a duplicate was found, only the later row got an error viactx.addIssue. The first row was silently stored in the map and never flagged.The fix uses a
Map<string, number[]>to collect all indices per compound key (environmentId::key). After the grouping pass, any group with more than one entry gets errors added to every row in the group.Fixes #5219
Test plan