fix(schema): always pass raw string value to error validators, only trim to 30 chars for maxlength validator#16238
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a regression introduced by #15571 where string values in props.value were truncated for all validators, by ensuring validator props always carry the raw value and limiting truncation to maxlength error message formatting.
Changes:
- Stop truncating
validatorProperties.valueinSchemaType#doValidate()and#doValidateSync(). - Add targeted truncation logic for
maxlengthmessage formatting inSchemaString. - Add/extend tests asserting
err.errors.*.valueandproperties.valueremain untrimmed, including for custom validator message functions.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
lib/schemaType.js |
Removes global string truncation from validator properties so custom validators see full values. |
lib/schema/string.js |
Wraps maxlength validator message formatting to trim displayed value while keeping stored values raw. |
test/schema.validation.test.js |
Adds coverage to ensure custom validator message functions receive full string values (async + sync). |
test/errors.validation.test.js |
Adds assertions that maxlength validation errors keep full value/properties.value while message remains trimmed. |
hasezoey
approved these changes
Apr 23, 2026
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
hasezoey
reviewed
Apr 23, 2026
Comment on lines
+739
to
+741
| if (typeof msg === 'function') { | ||
| return MongooseError.ValidatorError.prototype.formatMessage(msg, props, doc); | ||
| } |
Collaborator
There was a problem hiding this comment.
Considering that the following if also added a typeof == "string" check and is followed by a unconditional call to formatMessage, this seems unnecessary; unless it is necessary for performance?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix #16236
Re: #15550
Re: #15571
Summary
Original bug report was about trimming long strings in the
maxlengthvalidator, but the fix in #15571 also leaks the 30 character max limit to user-specified validators, which is incorrect. With this PR, the validator props will always have the raw untrimmed value, and we will just apply the 30 character limit to the error message for themaxlengthvalidator.Examples