How to fix ValidationError: User validation failed: email: Path `email` is required. in MongoDB

MongoDBBEGINNERMEDIUMUpdated Dec 22, 2025ยท 162 views

This Mongoose validation error occurs when attempting to save a document without providing a required email field. The error indicates that your schema defines email as required, but the field is missing or undefined in the data being saved.

Quick fix

Fixes ValidationError: User validation failed: email: Path `email` is required.

const userData = {
  username: 'john_doe',
  // email is missing!
};

console.log('Data being saved:', userData);

const user = new User(userData);
await user.save(); // Will throw validation error
  1. Verify the data being passed to the model
  2. Check your schema definition
  3. Verify request body parsing in Express
  4. Handle validation errors gracefully
mongodb โ€” error
const userData = {
username: 'john_doe',
// email is missing!
};
console.log('Data being saved:', userData);
const user = new User(userData);
await user.save(); // Will throw validation error
ValidationError: User validation failed: email: Path `email` is required.

What this error means

This error is thrown by Mongoose (the MongoDB ODM for Node.js) when you attempt to save a document that doesn't satisfy the schema's validation rules. Specifically, it means you're trying to create or update a User document without providing a value for the email field, which is marked as required in your schema definition. Mongoose registers validation as a pre('save') hook on every schema by default. When you call save() on a document, Mongoose validates all fields against the schema rules before writing to MongoDB. The required validator is one of Mongoose's built-in validators that checks whether a field has a value. Unlike other validators that skip undefined values, the required validator specifically checks for undefined, null, or missing fields. The error structure includes the model name (User), the field path (email), and the validation rule that failed (required). This helps you quickly identify which field needs to be provided when creating or updating documents.

Typical symptoms

  • Error thrown when calling save() or create() on a Mongoose model
  • Application crashes or API endpoints return 500 errors when creating users
  • POST requests fail with validation error messages
  • Document creation fails even though other required fields are provided
  • Error occurs during user registration or profile creation flows

Common causes

  1. The email field is not included in the data object when creating a new document
  2. The email property exists but is set to undefined, null, or an empty string
  3. Request body data is not being properly parsed or extracted (missing body-parser middleware)
  4. Frontend form is not sending the email field in the request payload
  5. Schema was updated to make email required, but existing code doesn't provide it
  6. Using findOneAndUpdate() with runValidators: true but not including email in the update object
  7. Destructuring or mapping data incorrectly, causing the email field to be omitted

How to fix "ValidationError: User validation failed: email: Path `email` is required."

1Verify the data being passed to the model

First, check what data you're actually passing when creating the document. Add logging before the save operation:

javascript
const userData = {
  username: 'john_doe',
  // email is missing!
};

console.log('Data being saved:', userData);

const user = new User(userData);
await user.save(); // Will throw validation error

Ensure the email field is present and has a valid value:

javascript
const userData = {
  username: 'john_doe',
  email: 'john@example.com' // Include required field
};

const user = new User(userData);
await user.save(); // Should succeed
2Check your schema definition

Review your Mongoose schema to confirm which fields are required:

javascript
const userSchema = new mongoose.Schema({
  username: { type: String, required: true },
  email: {
    type: String,
    required: true, // This means email MUST be provided
    unique: true
  },
  age: { type: Number } // Optional field
});

If the field shouldn't be required, update your schema:

javascript
const userSchema = new mongoose.Schema({
  email: {
    type: String,
    required: false, // Or omit 'required' entirely
    unique: true
  }
});
3Verify request body parsing in Express

If you're using Express, ensure you have body-parser middleware configured to extract request data:

javascript
const express = require('express');
const app = express();

// Add these middleware BEFORE your routes
app.use(express.json()); // Parse JSON bodies
app.use(express.urlencoded({ extended: true })); // Parse URL-encoded bodies

app.post('/api/users', async (req, res) => {
  console.log('Request body:', req.body); // Should show email

  const user = new User(req.body);
  await user.save();
  res.json(user);
});

Without these middleware, req.body will be undefined, and no fields will be passed to your model.

4Handle validation errors gracefully

Implement proper error handling to catch validation errors and return helpful messages:

javascript
app.post('/api/users', async (req, res) => {
  try {
    const user = new User(req.body);
    await user.save();
    res.status(201).json(user);
  } catch (error) {
    if (error.name === 'ValidationError') {
      // Extract validation error messages
      const errors = Object.keys(error.errors).map(key => ({
        field: key,
        message: error.errors[key].message
      }));

      return res.status(400).json({
        error: 'Validation failed',
        details: errors
      });
    }

    res.status(500).json({ error: 'Internal server error' });
  }
});

This returns a user-friendly error response:

json
{
  "error": "Validation failed",
  "details": [
    {
      "field": "email",
      "message": "Path `email` is required."
    }
  ]
}
5Fix update operations with validation

When using update methods with runValidators, ensure required fields are included:

javascript
// This will fail if email is required
await User.findOneAndUpdate(
  { _id: userId },
  { username: 'new_username' }, // email not included
  { runValidators: true } // Validates the update
);

// Solution 1: Include all required fields
await User.findOneAndUpdate(
  { _id: userId },
  {
    username: 'new_username',
    email: 'existing@email.com' // Include required field
  },
  { runValidators: true }
);

// Solution 2: Use $set to update only specific fields
await User.findOneAndUpdate(
  { _id: userId },
  { $set: { username: 'new_username' } },
  { runValidators: false } // Don't validate required fields for partial updates
);

Advanced notes

Custom Validation Messages:
You can customize the error message for required fields in your schema:

javascript
const userSchema = new mongoose.Schema({
  email: {
    type: String,
    required: [true, 'Email address is required for user registration'],
    lowercase: true,
    trim: true
  }
});

Conditional Required Fields:
You can make fields conditionally required based on other field values:

javascript
const userSchema = new mongoose.Schema({
  accountType: { type: String, enum: ['personal', 'business'] },
  email: {
    type: String,
    required: function() {
      return this.accountType === 'business'; // Only required for business accounts
    }
  }
});

Pre-save Hooks for Default Values:
You can set default values in pre-save hooks if a required field might be computed:

javascript
userSchema.pre('save', function(next) {
  if (!this.email && this.username) {
    this.email = `${this.username}@example.com`; // Generate default email
  }
  next();
});

Debugging Validation Errors:
Access detailed validation error information:

javascript
try {
  await user.save();
} catch (error) {
  if (error.name === 'ValidationError') {
    console.log('Validation errors:', error.errors);
    // error.errors.email.kind === 'required'
    // error.errors.email.path === 'email'
    // error.errors.email.message === 'Path `email` is required.'
  }
}

Performance Consideration:
Validation runs on every save() by default. For bulk operations or performance-critical paths, you can skip validation:

javascript
await user.save({ validateBeforeSave: false }); // Skip all validation

However, use this with caution as it bypasses all schema validation rules, not just required field checks.

Frequently asked questions

What does "ValidationError: User validation failed: email: Path `email` is requir" mean?
js) when you attempt to save a document that doesn't satisfy the schema's validation rules. Specifically, it means you're trying to create or update a User document without providing a value for the email field, which is marked as required in your schema definition. Mongoose registers validation as a pre('save') hook on every schema by default.
How do I fix "ValidationError: User validation failed: email: Path `email` is requir"?
This Mongoose validation error occurs when attempting to save a document without providing a required email field. The error indicates that your schema defines email as required, but the field is missing or undefined in the data being saved.
What causes "ValidationError: User validation failed: email: Path `email` is requir"?
The email field is not included in the data object when creating a new document The email property exists but is set to undefined, null, or an empty string Request body data is not being properly parsed or extracted (missing body-parser middleware)
Is the "ValidationError: User validation failed: email: Path `email` is requir" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading