title: How to make password validation in NodeJS with Mongoose
body: <p>I have registration form with username, mail, password and password2. I want to verify passwords that they actually match. I verify practically everything in Mongoose Scheme but I cannot find any useful information in documentation how to grab password2 without actually saving it to database. (I have function to crypt password which runs only before saving)</p>
  
  <pre><code>    const userSchema = new mongoose.Schema({
      username: {
          type: String,
          unique: true,
          required: true,
          trim: true,
          validate(value) {
              if (!validator.isAlphanumeric(value , 'pl-PL')) {
                  throw new Error('Name cannot contain special characters.')
              }
          }
      },
      email: {
          type: String,
          unique: true,
          required: true,
          trim: true,
          lowercase: true,
          validate(value) {
              if (!validator.isEmail(value)) {
                  throw new Error('Email is invalid')
              }
          }
      },
      password: {
          type: String,
          required: true, 
          validate(value) {
              console.log(value)
              if(value !== this.password2) {
                  throw new Error("Passwords don't match. Try again.")
              }
  
              if(value.length &lt; 8) {
                  throw new Error("Passwords is too short. At least 8 characters.")
              }
          }
      },
      tokens: [{
          token: {
              type: String,
              required: true
          }
      }]
      })
  </code></pre>