Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doc as 2nd parameter to validation message function #12564

Closed
2 tasks done
vkarpov15 opened this issue Oct 17, 2022 · 0 comments 路 Fixed by #12651
Closed
2 tasks done

Add doc as 2nd parameter to validation message function #12564

vkarpov15 opened this issue Oct 17, 2022 · 0 comments 路 Fixed by #12651
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class
Milestone

Comments

@vkarpov15
Copy link
Collaborator

Prerequisites

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

馃殌 Feature Proposal

In #6523 we added the ability to set a validator's message property to a function:

    it('evaluate message function gh6523', function(done) {
      const s = mongoose.Schema({
        n: {
          type: String,
          // required: true,
          validate: {
            validator: function() {
              return false;
            },
            message: function(properties) {
              return 'fail ' + properties.value;
            }
          }
        }
      });
      const M = mongoose.model('gh6523', s);
      const m = new M({ n: 0 });

      m.validate(function(error) {
        assert.equal('fail 0', error.errors['n'].message);
        done();
      });
    });

However, there's no way to configure the return value based on the document being validated (see #12308 (reply in thread)). Would be great if you could do something like this:

            message: function(properties, doc) {
              return messages[doc.$locals.locale];
            }

Motivation

See above

Example

            message: function(properties, doc) {
              return messages[doc.$locals.locale];
            }
@vkarpov15 vkarpov15 added new feature This change adds new functionality, like a new method or class enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature labels Oct 17, 2022
@vkarpov15 vkarpov15 added this to the 6.8 milestone Oct 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant