David J
David J

Reputation: 1098

Mongoose Validate Not Called

I'm following the mongoose documentation out of the book and I cannot get even the most simple validator to work. What am I missing? This validator which I assume would fail 100% succeeds 100% and I can insert any userid I want. In this I am simply trying to see if it's called and insertion always passes when I expect it to fail. I have tried many variations and examples I've seen online and in all cases the validator seem not to be called.

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const profileSchema = new Schema({
created: {
    type: Date,
    default: Date.now
    },
userid: { 
    type: Schema.Types.ObjectId, 
    ref: 'user',
    validate: {
        validator: val => { return false; },
        message: 'something'
    }
});
const Profile = mongoose.model('profile',profileSchema);

Upvotes: 2

Views: 548

Answers (1)

David J
David J

Reputation: 1098

This is a known issue during update. I didn't recognize the way I had my code structured that I was doing an update. I had an 'add' function which called findOneAndUpdate(). Thanks for the good questions which helped me find my problem.

See the answer: Mongoose .update() does not trigger validation checking

Upvotes: 1

Related Questions