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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validateMany feature method #6910

Closed
Twois opened this issue Aug 22, 2018 · 3 comments · Fixed by #14434
Closed

Add validateMany feature method #6910

Twois opened this issue Aug 22, 2018 · 3 comments · Fixed by #14434
Labels
new feature This change adds new functionality, like a new method or class
Milestone

Comments

@Twois
Copy link

Twois commented Aug 22, 2018

Do you want to request a feature or report a bug?
Feature

What is the current behavior?
There is no way to run multiple validators on the shame field.

toySchema.path('name').validate(function(v) {
  if (v !== 'Turbo Man') {
    throw new Error('Need to get a Turbo Man for Christmas');
  }
  return true;
}, 'Name `{VALUE}` is not valid');

If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?
We should to be able to add multiple custom validators, and get back the result all of them.

I saw that there were some issue what reported the same, and the answer was, we can use mongoose-validator-all package. But that package have no more maintained for 1,5 years and anyway it should be supported by mongoose i think.

To prevent breaking changes, instead of overwrite the current validate system, just add a new one.

toySchema.path('password').validateMany([
function(v, f) {
  if (this.name == v) {    
   return {
        'passed': false,
         'message': `field [${f}] can be the same as name, value: [${v}]`
    };
  }
  return {
    'passed': true,
  };
},function(v, f) {
  if (this.confirmPw == v) {    //just for example
   return {
        'passed': false,
         'message': `field [${f}] have to be the same as password, confirmPw, password: [${this.confirmPw}, ${v}]`
    };
  }
  return {
    'passed': true,
  };
},
]);

Errors:

{
 "password": [{error Object}, {error Object}]
}

UI:
For be able to compare fields whit others e.g password, this should to contain the document itself as in pre hooks.

@vkarpov15
Copy link
Collaborator

I'm not sure I understand the problem here. You can multiple validate calls, schema.path().validate(validator1).validate(validator2) . Can you elaborate on your use case a bit?

@vkarpov15 vkarpov15 added the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label Aug 25, 2018
@Twois
Copy link
Author

Twois commented Aug 25, 2018

We can add multiple validate, but as I know it will break at the first failure, and my suggestion is, to add a new validate method what run all validator function on the fields, even if some of them failed.

Update:

sometimes we want to test the min length and is the string contain specific character e.g, password. In that case, what a cool thing could be, if we can show to the user the two error in one step, Now the validate show only the first error.

@vkarpov15
Copy link
Collaborator

We will consider adding a similar feature for a future release. Until then, you can use something like Ramda's compose function

@vkarpov15 vkarpov15 removed the needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity label Aug 26, 2018
@vkarpov15 vkarpov15 added this to the Parking Lot milestone Aug 26, 2018
@vkarpov15 vkarpov15 modified the milestones: Parking Lot, 8.2.2, 8.3 Mar 13, 2024
@hasezoey hasezoey added the new feature This change adds new functionality, like a new method or class label Mar 14, 2024
vkarpov15 added a commit that referenced this issue Mar 18, 2024
Add function `SchemaType.prototype.validateAll`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

3 participants