feat(validate): use ajv-errors plugin for schema error messages - #83
feat(validate): use ajv-errors plugin for schema error messages#83alltheseas wants to merge 1 commit into
Conversation
Schemata schemas already embed errorMessage keywords (592 occurrences across 245 schemas). Previously Sherlock stripped these, re-extracted them via codegen into error-messages.ts, then matched AJV schema paths back to those messages at runtime. This was a workaround for not having the ajv-errors plugin. Now ajv-errors handles errorMessage natively: - Remove stripErrorMessages() — schemas keep their errorMessage fields - Remove enrichErrors() — no more manual path matching - Remove BASE_ERROR_MESSAGES/KIND_ERROR_MESSAGES import - Add unwrapErrorKeywords() to preserve original AJV keyword for DB grouping Net: ~40 lines of workaround removed, ~6 lines added. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
📝 WalkthroughWalkthroughAdded Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 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.
🧹 Nitpick comments (1)
src/validate/engine.ts (1)
96-102: Add defensive check when accessing nested keyword.If
err.params.errors[0]exists but lacks akeywordproperty,err.keywordwould be set toundefined, which could cause unexpected behavior in downstream DB grouping/filtering.🛡️ Proposed defensive check
function unwrapErrorKeywords(errors: ErrorObject[]): void { for (const err of errors) { - if (err.keyword === 'errorMessage' && err.params?.errors?.length) { - err.keyword = err.params.errors[0].keyword; + if (err.keyword === 'errorMessage' && err.params?.errors?.length) { + const originalKeyword = err.params.errors[0].keyword; + if (originalKeyword) { + err.keyword = originalKeyword; + } } } }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/validate/engine.ts` around lines 96 - 102, In unwrapErrorKeywords, avoid setting err.keyword to undefined by adding a defensive check that verifies err.params?.errors?.[0]?.keyword is defined before assigning; if the nested keyword is missing, skip the assignment (or preserve the original err.keyword / set a safe fallback) so downstream grouping/filtering that relies on keyword won't receive undefined. Reference: function unwrapErrorKeywords and the err.params.errors[0] access.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/validate/engine.ts`:
- Around line 96-102: In unwrapErrorKeywords, avoid setting err.keyword to
undefined by adding a defensive check that verifies
err.params?.errors?.[0]?.keyword is defined before assigning; if the nested
keyword is missing, skip the assignment (or preserve the original err.keyword /
set a safe fallback) so downstream grouping/filtering that relies on keyword
won't receive undefined. Reference: function unwrapErrorKeywords and the
err.params.errors[0] access.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 9f2d8516-6ea0-4c92-9e7e-46eebcb0d055
⛔ Files ignored due to path filters (1)
package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (2)
package.jsonsrc/validate/engine.ts
Summary
errorMessagekeywords (592 occurrences across 245 schemas)error-messages.ts, then matched AJV schema paths back at runtime — a workaround for not having theajv-errorspluginajv-errorshandleserrorMessagenatively: ~40 lines of workaround removed, ~6 lines addedunwrapErrorKeywords()so DB grouping/filtering is unaffectedWhat changed in
src/validate/engine.tsstripErrorMessages()— recursive delete oferrorMessagefrom schemasajvErrors(ajv)— one-line plugin initenrichErrors()— manual codegen-based schemaPath matchingunwrapErrorKeywords()— restore original keyword from ajv-errors wrapperBASE_ERROR_MESSAGES/KIND_ERROR_MESSAGESimportWhy
User feedback: improving error messages with
errorMessageshould be the canonical approach. The schemas already have the data — this PR lets AJV use it directly instead of stripping and rebuilding it.error-messages.tsis now dead code (no imports). Can be cleaned up separately along with the codegen--errorsflag.Test plan
npm run build— compiles cleanlynpm run scan— validate events and confirm error messages appear correctlyerrorMessagesource)sherlock report --by-erroroutput for human-readable messages🤖 Generated with Claude Code
Summary by CodeRabbit