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

Enum object syntax does not work on a number path #10648

Closed
thiagokisaki opened this issue Aug 31, 2021 · 3 comments
Closed

Enum object syntax does not work on a number path #10648

thiagokisaki opened this issue Aug 31, 2021 · 3 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@thiagokisaki
Copy link
Contributor

thiagokisaki commented Aug 31, 2021

Do you want to request a feature or report a bug?
Report a bug.

What is the current behavior?
According to mongoose docs there are two ways to set a validator message: using array syntax or using object syntax.

Currently, the enum object syntax is not working on a number path.

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

const { Schema, model } = require('mongoose');

const reviewSchema = new Schema({
  comment: String,
  rating: {
    type: Number,
    enum: {
      values: [1, 2, 3, 4, 5],
      message: 'Rating must be an integer between 1 and 5',
    },
  },
});

const Review = model('Review', reviewSchema);

const review = new Review({ comment: 'Excellent!', rating: 5 });

console.log(review.validateSync());

Causes the error:

Error: Review validation failed: rating: `5` is not a valid enum value for path `rating`.

What is the expected behavior?
To be able to use object syntax to set an enum validator for numbers.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js: v14.17.3
Mongoose: v6.0.3
MongoDB: irrelevant

@thiagokisaki thiagokisaki changed the title Enum object syntax for a number path is not working Enum object syntax does not work on a number path Aug 31, 2021
@IslandRhythms IslandRhythms added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Sep 1, 2021
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented Sep 1, 2021

https://mongoosejs.com/docs/validation.html#built-in-validators

Only strings have the enum property. For Numbers it is min and max

@thiagokisaki
Copy link
Contributor Author

thiagokisaki commented Sep 1, 2021

@IslandRhythms IslandRhythms added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels Sep 1, 2021
@vkarpov15 vkarpov15 added this to the 6.0.5 milestone Sep 1, 2021
@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Sep 1, 2021
@IslandRhythms
Copy link
Collaborator

I'm getting the same error message

const mongoose = require('mongoose');
const { Schema, model } = mongoose;

const reviewSchema = new Schema({
  comment: String,
  rating: {
    type: Number,
    enum: {
      values: [1, 2, 3, 4, 5],
      message: 'Rating must be an integer between 1 and 5',
    },
  },
});

const Review = model('Review', reviewSchema);
async function test() {
    await mongoose.connect('mongodb://localhost:27017/test', {
        useNewUrlParser: true,
        useUnifiedTopology: true,
      });
      await mongoose.connection.dropDatabase();

      const review = new Review({ comment: 'Excellent!', rating: 5 });

    console.log(review.validateSync());
}

test();

vkarpov15 added a commit that referenced this issue Sep 2, 2021
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. docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

No branches or pull requests

3 participants