Prerequisites
Mongoose version
=8.18
Node.js version
24
MongoDB server version
7.x
Typescript version (if applicable)
No response
Description
Based on some digging, it appears that as of Mongoose v8.18, all props.value strings are truncated at 30 characters after the change in #15571 that trimmed strings. The text of the PR seems to indicate that the intent of the change was to trim strings for maxLength validators, but the implementation affects all validators including custom validators.
Steps to Reproduce
Here is a simple schema that validates a property is a valid URL:
const schema = new Schema({
url: {
type: String,
validate: {
validator: function(val) {
return validateUrlWithProtocol(val) // assume some lib function that does what we want
},
message: (props) => {
// this could contain _any_ logic that needs to manipulate the failing value
return `${props.value} is not a valid url`
}
}
}
});
Expected Behavior
- for an valid input string
https://hello.short-enough.com (31 chars), this functions as expected
- for an invalid input string
hello.short-enough.com (23 chars), this functions as expected
- for an invalid input string
long-long-long.short-enough.com (32 chars), the message field can no longer perform any business logic on the preserved input value since props.value is truncated to long-long-long.short-enough.co...
The previous behavior allowed the message function to perform business logic on the full input value unchanged.
Prerequisites
Mongoose version
Node.js version
24
MongoDB server version
7.x
Typescript version (if applicable)
No response
Description
Based on some digging, it appears that as of Mongoose v8.18, all
props.valuestrings are truncated at 30 characters after the change in #15571 that trimmed strings. The text of the PR seems to indicate that the intent of the change was to trim strings formaxLengthvalidators, but the implementation affects all validators including custom validators.Steps to Reproduce
Here is a simple schema that validates a property is a valid URL:
Expected Behavior
https://hello.short-enough.com(31 chars), this functions as expectedhello.short-enough.com(23 chars), this functions as expectedlong-long-long.short-enough.com(32 chars), the message field can no longer perform any business logic on the preserved input value sinceprops.valueis truncated tolong-long-long.short-enough.co...The previous behavior allowed the
messagefunction to perform business logic on the full input value unchanged.