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 validation issue in subdocuments #3521

Closed
tusbar opened this issue Oct 30, 2015 · 1 comment
Closed

Enum validation issue in subdocuments #3521

tusbar opened this issue Oct 30, 2015 · 1 comment
Milestone

Comments

@tusbar
Copy link
Contributor

tusbar commented Oct 30, 2015

Hey,

var mongoose = require('mongoose'); // mongoose@4.2.3

var s = new mongoose.Schema({
    array: [
        {
            string: {
                type: String,
                enum: [
                    'foo',
                    'bar',
                    'baz'
                ]
            }
        }
    ]
});

var Cool = mongoose.model('Cool', s);

it('should fail', function (done) {
    mongoose.connect('localhost/cool', function () {
        var cool = new Cool();
        var entry = cool.array.create({
            string: 'hello?'
        });
        cool.array.push(entry);

        cool.save(function (err) {
            expect(err).to.be.ok;
            done();
        });
    });
});

The enum validation is not performed, and the string property is saved with garbage value.
Not sure if related to #3511, since validation works if I use a required instead of the enum.

@vkarpov15 vkarpov15 added this to the 4.2.5 milestone Oct 30, 2015
tusbar added a commit to tusbar/mongoose that referenced this issue Nov 3, 2015
@tusbar tusbar mentioned this issue Nov 3, 2015
tusbar added a commit to tusbar/mongoose that referenced this issue Nov 3, 2015
vkarpov15 added a commit that referenced this issue Nov 3, 2015
@vkarpov15
Copy link
Collaborator

Fix will be in 4.2.5, thanks for the repro, that was very helpful 🍻.

In the future, I'd recommend avoiding using .create(), it's unnecessary, you can just pass a POJO to .push() and get the same result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants