Mongoose: Mongoose Boolean validation not working if a String is provided

Created on 16 Jun 2016  路  1Comment  路  Source: Automattic/mongoose

Given the following schema with a Boolean field:

var UserSchema = new Schema({
  _id: {
    type: Number
  },
  enabled: {
    type: Boolean,
    default: false
  }
});

When trying to save a document, if I supply a random string instead of a Boolean value or "Boolean string" (e.g. "false" or "true" instead of just false/true) for enabled field, the document is still being saved properly, with the Boolean field set to true.

Expected behavior: save operation should throw a ValidationError.

backwards-breaking enhancement

Most helpful comment

Yeah unfortunately this is backwards breaking, mongoose will cast anything into a boolean and this is expected behavior. Will make this change for the next major release.

>All comments

Yeah unfortunately this is backwards breaking, mongoose will cast anything into a boolean and this is expected behavior. Will make this change for the next major release.

Was this page helpful?
0 / 5 - 0 ratings