Mongoose: Validation fails when the required string field is empty string

Created on 19 Oct 2018  路  14Comments  路  Source: Automattic/mongoose

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

BUG

What is the current behavior?

required field with empty string "" throws validation error reason: Pathreasonis required.

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

import mongoose from 'mongoose';

const WatSchema = new mongoose.Schema({ 
  reason: {
    type: String,
    required: true,
  }
});


const Wat = mongoose.model('Wat', WatSchema);

// Wat validation failed: reason: Path `reason` is required.
await Wat.create({ reason: '' }).save();

What is the expected behavior?

Should never throw validation if typeof FieldType === typeof value.

Please mention your node.js, mongoose and MongoDB version.

Not sure

enhancement

All 14 comments

@pronebird Can you please try to set minimize (https://mongoosejs.com/docs/guide.html#minimize) to false to see if the problem maybe lies within that part? If an empty string is "minimized away" then the failing validation is correct because although you set the string, it would not be saved within the databse and therefore be invalid to the given validation. Would be interested in the result myself but can't test it myself at the moment.

Update: Regarding the fact that failing validation would be "correct" in that case: An empty string does, like an empty object or an empty array, carry no information. So it is the same as not having the attribute at all (you just need to check for its existence when quering). For edge-cases where you need the attributes to exist regardless of information/content,you should set minimize to false.

@mhombach minimize is about minimizing away empty objects, not empty strings.

Fixed in 5.4 branch along with #7186 . To opt in to the new behavior, do:

mongoose.Schema.String.checkRequired(v => v != null)

@vkarpov15 when it will be on production?

@coldner current ETA for 5.4 is December 14

I don't see any update on this:
https://github.com/Automattic/mongoose/blob/master/lib/schema/string.js#L114
v.length is still required to be non-zero.

I have an idea for a backward compatible solution: The checkRequired function will accept empty strings if the minlength is set to 0. This can be seen as if the default value for minlength is 1, and the user need to set it to 0 if he wants to accept empty strings
I have no idea how to implement this from outside by overriding the checkRequired function, because I don't know how to access the other schema options (like minlength). Surely it can be implemented from inside the library code.

@ozomer One-liner to configure Mongoose to accept '':

mongoose.Schema.Types.String.checkRequired(v => v != null);

We closed this because there's now a simple workaround, read more here. Allowing '' by default is a little too backwards breaking for us to release in a minor release.

Still not fixed in 5.6.9

@vkarpov15 It can't be backwards breaking, because nobody expects that empty string means null. So it won't break anybody's code. Moreover, where should I put this code?

@lcswillems someone relying on Mongoose to validate the contents of an HTML <input type="text"> tag in a form could reasonably rely on required to make sure the user actually entered something.

@vkarpov15 Okay, I see. But they misuse the "required" parameter. "required" means "must have a value". Empty string is a value, so it should not raise an error... "required" should be "notEmpty" in their case...

Moreover, the workaround mongoose.Schema.Types.String.checkRequired(v => v != null); doesn't work in a TypeScript project.

It does if your typescript bindings are up to date, so please open up an issue with the typescript bindings project you use.

Moreover, the workaround mongoose.Schema.Types.String.checkRequired(v => v != null); doesn't work in a TypeScript project.

You can do 
const Str = mongoose.Schema.Types.String as any;
Str.checkRequired(v => v != null);

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adamreisnz picture adamreisnz  路  3Comments

jeremyml picture jeremyml  路  3Comments

ghost picture ghost  路  3Comments

adamreisnz picture adamreisnz  路  3Comments

simonxca picture simonxca  路  3Comments