Skip to content

fix: trim long strings in minLength and maxLength error messages and display the string length - #15571

Merged
vkarpov15 merged 3 commits into
8.18from
vkarpov15/gh-15550
Aug 5, 2025
Merged

fix: trim long strings in minLength and maxLength error messages and display the string length#15571
vkarpov15 merged 3 commits into
8.18from
vkarpov15/gh-15550

Conversation

@vkarpov15

Copy link
Copy Markdown
Collaborator

Fix #15550

Summary

minLength and maxLength validators currently display the full string output, which can be problematic in the case of very long strings. This PR trims the output if more than 30 chars, and also displays the length of the string.

Examples

@vkarpov15 vkarpov15 added this to the 8.18 milestone Aug 4, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves the error messages for string length validation by trimming very long strings and displaying the actual string length. The change addresses issue #15550 where long strings in validation error messages could be problematic for readability.

  • Updated error message format to include string length for minLength and maxLength validators
  • Added logic to trim strings longer than 30 characters in error messages
  • Added test coverage for the new error message format with string length

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
lib/error/messages.js Updated minlength and maxlength error message templates to include {LENGTH} placeholder
lib/schemaType.js Added logic to set length property and trim long strings in validator properties for both sync and async validation
test/errors.validation.test.js Added test case to verify custom error messages can use the new {LENGTH} placeholder
test/model.test.js Updated existing test expectation to match new error message format with length

Comment thread lib/schemaType.js
Comment thread lib/schemaType.js

@hasezoey hasezoey left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, maybe add a test to know that >30 characters is actually shortened?

@vkarpov15
vkarpov15 merged commit 505bfc5 into 8.18 Aug 5, 2025
74 checks passed
@hasezoey
hasezoey deleted the vkarpov15/gh-15550 branch August 6, 2025 09:09
pull Bot pushed a commit to edisplay/mongoose that referenced this pull request May 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants