Enhance Joi Error Messages for User-Friendliness with Optional `{ format: true }` Feature
- Dominant language
- JavaScript
- Stars
- 21.2k
- Forks
- 1.5k
- Avg merge
- 4h 57m
- Merged PRs (30d)
- 14
Description
### Runtime
Node.js
### Runtime version
Node.js 22 (LTS)
### Module version
Joi v17.x
### Used with
Standalone and Express.js applications
### Any other relevant information
- Developers currently have to manually override or wrap Joi's default error messages to create user-friendly feedback.
- The technical error messages not only clutter the UI but may also inadvertently leak sensitive validation details.
- **Examples of current error messages:**
- \`"password" with value "pass" fails to match the required pattern: /^[a-zA-Z0-9]{8,30}$/\`
- \`"mixedField" must be one of [string, number]\`
- \`"settings.notifications" is required\`
- \`"tree.child.child.child.name" is required\`
### What problem are you trying to solve?
**What problem are you trying to solve?**
- **User Experience:** End users see cryptic and technical messages that are hard to understand, leading to a poor overall user experience.
- **Developer Overhead:** Developers are forced to write custom error-handling logic for every field and scenario, increasing both development time and potential for inconsistency.
- **Security Concerns:** Technical error messages may expose underlying validation logic or sensitive details that should remain internal.
- **Maintainability:** A built-in, standardized approach to formatting error messages would reduce redundancy and improve code clarity across projects.
### Do you have a new or modified API suggestion to solve the problem?
- **Feature Request:** Introduce an optional parameter `{ format: true }` in the Joi validation options.
- **Behavior when enabled:**
- **Simplify messages:** Strip out technical details and present clear, concise messages that can be safely shown directly to end users.
- **Consistency:** Automatically apply friendly formatting for all types of validation errors (e.g., pattern mismatches, required fields, type errors).
- **Example transformation:**
- **Before:** \`"password" with value "pass" fails to match the required pattern: /^[a-zA-Z0-9]{8,30}$/\`
- **After:** \`Password must be 8-30 characters long and contain only letters and numbers.\`
- **Before:**\`"mixedField" must be one of [string, number]\`
- **After:**"Mixed Field must be either a text or a number."
- **Before:** \`"settings.notifications" is required\`
- **After:**"Notification settings are required."
- **Before:** \`"tree.child.child.child.name" is required\`
- **After:** "Name is required."
- **Impact:**
- **Enhanced UX:** Streamlined error messages would lead to a better front-end experience.
- **Reduced Developer Hassle:** Eliminates the need for repetitive custom error message coding, allowing developers to focus on core functionality.
- **Security Improvement:** Minimizes exposure of internal validation patterns and logic.
I am ready to contribute a PR to implement this feature and collaborate on refining the approach. Looking forward to your feedback!
Contributor guide
Assessment
This issue has not been assessed yet.