Validating password / confirm password with Mongoose schema

前端 未结 7 2182
小蘑菇
小蘑菇 2020-12-08 17:15

I have a userSchema that looks like this:

var userSchema = new Schema({
    name: {
      type: String
    , required: true
    , validate: [val         


        
7条回答
  •  被撕碎了的回忆
    2020-12-08 17:35

    It's kind of late but for the sake of people having similar issues. i ran into a similar problem lately, and here was how i went about it; i used a library called joi

    const joi = require('joi');
         ...
    function validateUser(user){
      const schema = joi.object({
        username: joi.string().min(3).max(50).required(),
        email: joi.string().min(10).max(255).required().email(),
        password: joi.string().min(5).max(255).required(),
        password2: joi.string().valid(joi.ref('password')).required(),
      });
    
      return schema.validate(user);
    }
    
    exports.validate = validateUser;
    

提交回复
热议问题