Mongoose validation: required : false, validate : regex, issues with empty values

19,223

Solution 1

You may try with a custom validator as they are only triggered when there is a value on a given key because the key selection for custom validation is done via path() :

var user = new Schema({

  // ...
  phone    : { type: String }, // using default - required:false
  // ...

});

// Custom validation
user.path('phone').validate(function (value) {

  // Your validation code here, should return bool

}, 'Some error message');

Have a look at this question: Why Mongoose doesn't validate empty document?

This will also effectively prevent the document to be persisted to the DB if validation fails, unless you handle the error accordingly.

BonusTip: Try to approach custom validations in a simplistic way, for example try to avoid loops when possible and avoid using libraries like lodash or underscore for in my experience I've seen that these may have a significant performance cost when working with lots of transactions.

Solution 2

I think your regex is failing validation on empty string which should in this case be valid since this field is not required. Why don't you try this regex:

/^$|^\d{10}$/ 

This will match an empty string or 10 digits.

Solution 3

You can simply check if the value entered exists (not null or undefined). If it exists, then test the regex:

var user = new Schema(
{ 
    _id      : { type: String, required: true },
    name     : { type: String, required: true},
    phone    : { type: String,/*not required by default**/ 
                 validate: {
                     validator: function(v) {
                         var re = /^\d{10}$/;
                         return (!v || !v.trim().length) || re.test(v)
                     },
                     message: 'Provided phone number is invalid.'
                 }
    },
    password : { type: String },
    added    : { type: Date,    default: Date.now },
},
{collection : 'users'}
);
Share:
19,223

Related videos on Youtube

Israel Cruz
Author by

Israel Cruz

Updated on September 23, 2022

Comments

  • Israel Cruz
    Israel Cruz over 1 year

    I get this message from Mongoose validation:

    'Validator failed for path phone with value ``'

    That shouldn't happen since phone is not required.

    Here's my model schema:

    var user = new Schema(
    { 
        _id      : { type: String, required: true },
        name     : { type: String, required: true},
        phone    : { type: String, required: false, validate: /^\d{10}$/ },
        password : { type: String },
        added    : { type: Date,    default: Date.now },
    },
    {collection : 'users'}
    );
    

    It seems that mongoose's validation fails when i use required: false and set validate property up. If I change it to:

    phone    : { type: String, required: false},
    

    Everything goes right, why is that? What am I doing wrong?

  • Israel Cruz
    Israel Cruz about 10 years
    Well actually I don't want mongoose to store an empty value, It should not be stored at all and not trigger a validation error either
  • Stepan Grigoryan
    Stepan Grigoryan about 10 years
    Then why don't you make the phone field required?
  • Israel Cruz
    Israel Cruz about 10 years
    I don't want it to be required, it is an optional field, users aren't forced to fill it, but if they do provide a phone, then I want mongoose to validate it, does that make sense?