Skip to content

value passed in props to the message for a custom validator are truncated as of 8.18/#15571 #16236

Description

@hearthsinger

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions