4

I have the following model using mongoose:

var Usuarios = function (){
  var crypt = require('./crypt_helper'),
      _model,
      _findByEmail,
      _findById,
      _findByRememberToken,
      _schema,
      validadores = {}

  validadores.notBlank = function(val){
    return !(/^\s*$/.test(val));
  }

  validadores.email = function(val){
    return /^[\w+\-.]+@[a-z\d\-.]+\.[a-z]+$/i.test(val);
  }

  validadores.unique = function(field, message){
    _schema.path(field).validate(function(val, fn){
      var query = {}
      query[field] = val;
      _model.find(query).exec(function (err, vals) {
        fn(err || vals.length === 0);
      });
    }, message);
  };

  var _schema = new mongoose.Schema({
    nombres: {
      type : String,
      required: true,
      validate: [validadores.notBlank, 'no debe estar en blanco']
    },
    apellidos: {
      type : String,
      required: true,
      validate: [validadores.notBlank, 'no debe estar en blanco']
    },
    email: {
      type : String,
      index: {
        unique: true,
        required : true
      },
      //required: true,
      lowercase: true, trim: true,
      validate: [validadores.email, 'no es un correo electrónico válido']
    },
    password_digest: {
      type : String,
      required: true,
      validate: [validadores.notBlank, 'no debe estar en blanco']
    },
    remember_token: String,
    token: String,
    fecha_token: Date,
    permisos: [String],
    tipo_id: String,
    documento: {
      type : String,
    //  required: true,
      validate: [validadores.notBlank, 'no debe estar en blanco']
    }
  });
  _schema.virtual('password').set(function(pw){
    this.password_digest = crypt.cryptPassword(pw);
  });

  _schema.virtual('avatar').get(function(){
    var gravatar_id = crypto.createHash('md5').update(this.email).digest('hex');
    return "https://secure.gravatar.com/avatar/" + gravatar_id + '?s=' + s
  });

  _schema.pre('save', function (next) {
    this.remember_token = crypt.token();
    next();
  });
  _model = mongoose.model('usuarios', _schema);

  validadores.unique('email', 'Correo electrónico ya registrado');
  validadores.unique('documento', 'Número de documento ya registrado');

  _findByEmail = function(email, success, fail) {
    _model.findOne({'email': email}).exec(function(err, doc) {
      if(err){
        fail(err);
      } else {
        success(doc);
      }
    });
  };

  _findByRememberToken = function (rt, success, fail) {
    _model.findOne({'remember_token': rt}).exec(function(err, doc) {
      if(err){
        fail(err);
      } else {
        if(doc) {
          var u = doc.toJSON();

          delete u['password_digest'];
          delete u['remember_token'];
          delete u['fecha_token'];
          delete u['token'];

          success(u);
        } else {
          success();
        }
      }
    });
  };

  // Encontrar por ID
  _findById = function (id, success, fail) {
    _model.findOne({'_id': id}).exec(function(err, doc) {
      if(err){
        fail(err);
      } else {
        if (doc){
          var u = doc.toJSON();

          delete u['password_digest'];
          delete u['remember_token'];
          delete u['fecha_token'];
          delete u['token'];

          success(u);
        } else {
          success();
        }
      }
    });
  };

  return {
    model: _model,
    schema: _schema,
    findById: _findById,
    findByEmail: _findByEmail,
    findByRememberToken: _findByRememberToken
  };
}();

'password' field is a virtual attribute, 'password_digest' is saved in the database instead. I want to validate in the model the strengh of the password. At least I want to verify than it is, say, 6 chars long. How can I do that?

4

0 回答 0