Skip to content

fix(schema): always pass raw string value to error validators, only trim to 30 chars for maxlength validator#16238

Merged
vkarpov15 merged 2 commits into
8.xfrom
vkarpov15/gh-16236
Apr 23, 2026
Merged

fix(schema): always pass raw string value to error validators, only trim to 30 chars for maxlength validator#16238
vkarpov15 merged 2 commits into
8.xfrom
vkarpov15/gh-16236

Conversation

@vkarpov15
Copy link
Copy Markdown
Collaborator

Fix #16236
Re: #15550
Re: #15571

Summary

Original bug report was about trimming long strings in the maxlength validator, 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 the maxlength validator.

Examples

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.value in SchemaType#doValidate() and #doValidateSync().
  • Add targeted truncation logic for maxlength message formatting in SchemaString.
  • Add/extend tests asserting err.errors.*.value and properties.value remain 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.

Comment thread lib/schema/string.js
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@vkarpov15 vkarpov15 merged commit 86037f3 into 8.x Apr 23, 2026
107 of 109 checks passed
@hasezoey hasezoey deleted the vkarpov15/gh-16236 branch April 23, 2026 14:51
Comment thread lib/schema/string.js
Comment on lines +739 to +741
if (typeof msg === 'function') {
return MongooseError.ValidatorError.prototype.formatMessage(msg, props, doc);
}
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.

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?

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