enum validation breaks removing values by assignment of undefined #967

Closed
aadamowski opened this Issue Jun 17, 2012 · 4 comments

Comments

Projects
None yet
4 participants
@aadamowski

It seems that removing an enum property value (even explicitly made optional through required: false) by assigning undefined to it is impossible because it triggers validation error, e.g.:

var schema = new mongoose.Schema({
  'type':{ type:String, required: false, enum:['type1', 'type2']}
});

...
schema.model.findById(..., function(err, persistentObject) {
  ...
  persistentObject.type = undefined;
  persistentObject.save(....);
  ...
});

results in:

ValidationError: Validator "enum" failed for path type
@aheckmann

This comment has been minimized.

Show comment Hide comment
@aheckmann

aheckmann Jun 18, 2012

Collaborator

thanks for the report

Collaborator

aheckmann commented Jun 18, 2012

thanks for the report

@aheckmann

This comment has been minimized.

Show comment Hide comment
@aheckmann

aheckmann Jun 18, 2012

Collaborator

we'll apply this fix to the 3.x branch since its a behavior change. for 2.x the work-around is to allow nulls in your enum and assign it null; not exactly the same thing but close.

Collaborator

aheckmann commented Jun 18, 2012

we'll apply this fix to the 3.x branch since its a behavior change. for 2.x the work-around is to allow nulls in your enum and assign it null; not exactly the same thing but close.

@aheckmann aheckmann closed this in 91f4d85 Jun 18, 2012

@sivatumma

This comment has been minimized.

Show comment Hide comment
@sivatumma

sivatumma Aug 4, 2015

All possible tricks including taking latest mongoose 4.1.0 didn't work for me.

defaultLife:{
    required:false,
    type:String,
    enum:"REGULAR,DAY,WEEK,MONTH".split(","),
    default:"REGULAR"
}

So I modified it to

defaultLife:{
    required:false,
    type:String,
    enum:",REGULAR,DAY,WEEK,MONTH".split(","),
    default:"REGULAR"
}

to work. See that , I had to put ?!.

All possible tricks including taking latest mongoose 4.1.0 didn't work for me.

defaultLife:{
    required:false,
    type:String,
    enum:"REGULAR,DAY,WEEK,MONTH".split(","),
    default:"REGULAR"
}

So I modified it to

defaultLife:{
    required:false,
    type:String,
    enum:",REGULAR,DAY,WEEK,MONTH".split(","),
    default:"REGULAR"
}

to work. See that , I had to put ?!.

@vkarpov15

This comment has been minimized.

Show comment Hide comment
@vkarpov15

vkarpov15 Aug 4, 2015

Collaborator

Hmm @sivatumma can you show me how you're using this schema?

Collaborator

vkarpov15 commented Aug 4, 2015

Hmm @sivatumma can you show me how you're using this schema?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment