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

Bug on validateSync Method #13159

Closed
1 of 2 tasks
jogueji opened this issue Mar 13, 2023 · 0 comments · Fixed by #13167
Closed
1 of 2 tasks

Bug on validateSync Method #13159

jogueji opened this issue Mar 13, 2023 · 0 comments · Fixed by #13167
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@jogueji
Copy link

jogueji commented Mar 13, 2023

Prerequisites

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

Mongoose version

7.0.1

Node.js version

18.13.0

MongoDB server version

6.0.4

Typescript version (if applicable)

4.9.5

Description

On the method validateSync, you can provide the paths to validate, if you provide a path that corresponds to an array, and the item's values of that array are wrong, it won't throw an error, because you need to provide in the path the index of the value you want to validate.

Steps to Reproduce

Create a model like this:

import { model, Schema } from "mongoose";

const schema = new Schema({
  permissions: [{type: String, enum: ['users', 'anotherPermission']}]
});

const Model = model('Model', UserSchema);

Then create an object with invalid values on permissions:

const a = {
  permissions: ["avocado"]
}

Then validate the object with the model you previously created in these two different ways and see the errors:

const document = new model(a);
const error = document.validateSync();
const error1 = document.validateSync(["permissions.0"]);
const error2 = document.validateSync(["permissions"]);

In the first one, and the second one, you should be able to get an error, and in the third one, you don't get any error.

Expected Behavior

It is clear that there is an error in the Object that we created previously, but you only are available to get in two of three scenarios, but for me, we should get the error in the three scenarios, when we specify a path that we want to validate, the method should be able to validate all the nested object in that path.

@vkarpov15 vkarpov15 added this to the 7.0.2 milestone Mar 13, 2023
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Mar 13, 2023
vkarpov15 added a commit that referenced this issue Mar 13, 2023
…path to `validate()` and `validateSync()` `pathsToValidate` param

Fix #13159
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Mar 13, 2023
vkarpov15 added a commit that referenced this issue Mar 14, 2023
Validate array elements when passing array path to `validateSync()` in `pathsToValidate`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
2 participants