Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9090845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:28:13+00:00 2026-06-16T22:28:13+00:00

var userSchema = Schema({ email: { type: String, unique: true, match: /^[a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,4}$/, lowercase: true,

  • 0
var userSchema = Schema({
  email: {
    type: String,
    unique: true,
    match: /^[a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,4}$/,
    lowercase: true,
    trim: true
  },
  nickname: {
    type: String,
    trim: true,
    required: true
  },
  password: { type: String, required: true },
  url: { type: String, trim: true, default: '' },
  role: {
    type: String,
    enum: [ 'admin', 'reader' ],
    default: 'reader'
  },
  about: { type: String, trim: true },
  created: { type: Date, default: Date.now, required: true }
});

I’d like to customize this with something more user friendly, But I don’t know how to set a custom error message for required, unique index and enum failure. How can I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:28:15+00:00Added an answer on June 16, 2026 at 10:28 pm

    I don’t think you can override the error message with the required option. If you need to customize the message in the schema, instead of at the point of the save, I’d think your best bet would be to write a custom validator, like this:

    function createdValidator(created) {
      return (created !== undefined && created !== null);
    }
    
    ....
    
    new Schema({created: { type: Date, default: Date.now, validate: [createdValidator, "custom error message"] } });
    

    Update:

    According to the documentation here, you can do multiple validations like this:

    var multiple = [
      { validator: createdValidator, msg: "custom error message" },
      { validator: function(created){ return true;}, msg: "You'll never see me" }
    ];
    
    new Schema({created: { type: Date, validate: multiple }});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to following code: var UserSchema = mongoose.Schema({ email: { type: String, unique:
I have this code var UserSchema = new Schema({ Username: {type: String, index: true},
Given the following schema: var UserSchema = new Schema({ , email : { type:
I have the following simple shema: var userSchema = new Schema({ name : String,
This is the Schema: var userschema = new mongoose.Schema({ user: String, messages: [{ title:
This is my schema: var userschema = new mongoose.Schema({ user: String, imagen: [{ title:
In mongoose I've got this model: var userschema = new mongoose.Schema({ user: String, following:
I have MongooseJS schema as follow: var UserSchema = new Schema({ name : String,
I've got the following schemas: var userSchema = new Schema({ firstName: String, lastName: String,
This is my Schema: var userschema = new mongoose.Schema({ user: String, follow: [String], imagen:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.