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

  • Home
  • SEARCH
  • 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 9034755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:34:17+00:00 2026-06-16T08:34:17+00:00

I have a userSchema that looks like this: var userSchema = new Schema({ name:

  • 0

I have a userSchema that looks like this:

var userSchema = new Schema({
    name: {
      type: String
    , required: true
    , validate: [validators.notEmpty, 'Name is empty']
    }
  , username: {
      type: String
    , required: true
    , validate: [validators.notEmpty, 'Username is empty']
    }
  , email: {
      type: String
    , required: true
    , validate: [
        { validator: validators.notEmpty, msg: 'Email is empty' }
      , { validator: validators.isEmail, msg: 'Invalid email' }
      ]
    }
  , salt: String
  , hash: String
});

All of my validation is happening in the schema so far, and I’m wondering how to go about achieving this with password validation. The user inputs the password into two fields, and the model should check that they are the same as each other.

Does this kind of validation belong in the schema? I’m new to this sort of validation.

How should I validate the passwords?

  • 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-16T08:34:19+00:00Added an answer on June 16, 2026 at 8:34 am

    I eventually discovered that you can use a combination of virtual paths and the invalidate function to achieve this, as shown in this gist, for the very same purpose of matching passwords: https://gist.github.com/1350041

    To quote directly:

    CustomerSchema.virtual('password')
    .get(function() {
      return this._password;
    })
    .set(function(value) {
      this._password = value;
      var salt = bcrypt.gen_salt_sync(12);
      this.passwordHash = bcrypt.encrypt_sync(value, salt);
    });
    
    CustomerSchema.virtual('passwordConfirmation')
    .get(function() {
      return this._passwordConfirmation;
    })
    .set(function(value) {
      this._passwordConfirmation = value;
    });
    
    CustomerSchema.path('passwordHash').validate(function(v) {
      if (this._password || this._passwordConfirmation) {
        if (!val.check(this._password).min(6)) {
          this.invalidate('password', 'must be at least 6 characters.');
        }
        if (this._password !== this._passwordConfirmation) {
          this.invalidate('passwordConfirmation', 'must match confirmation.');
        }
      }
    
      if (this.isNew && !this._password) {
        this.invalidate('password', 'required');
      }
    }, null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code var UserSchema = new Schema({ Username: {type: String, index: true},
I have the following simple shema: var userSchema = new Schema({ name : String,
I'm giving up. I have a Schema defined like this: var UserSchema = new
I have MongooseJS schema as follow: var UserSchema = new Schema({ name : String,
This is my schema: var userschema = new mongoose.Schema({ user: String, imagen: [{ title:
This is the Schema: var userschema = new mongoose.Schema({ user: String, messages: [{ title:
In mongoose I've got this model: var userschema = new mongoose.Schema({ user: String, following:
I have to following code: var UserSchema = mongoose.Schema({ email: { type: String, unique:
Let's say I have the following schema var userSchema = new Schema({ name :
I have the following schema in mongoose: UserSchema = new Schema username: {type: String,

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.