Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequelize validation method

I'm trying to validate my Model but i miss something and i don't know what is it. It is my module and his validation for email.

module.exports = function(sequelize, DataTypes){
    return  sequelize.define("Scanner",
        {
        id      : {
            primaryKey      : true,
            autoIncrement   : true,
            type            : DataTypes.INTEGER
        },
        email   : {
            type    : DataTypes.STRING,
            isUnique :true,
            allowNull:false,
            validate:{
                isEmail : true
            }
        },
        pin     : {
            type : DataTypes.INTEGER
        }
    },{
        tableName       : 'scanner'
    });
};

When i'm trying to Find an object with parameters (pin + email) if i put this.email = ssdf.sdf , my query is launched and i would like to check first if my params are correct.

Scanner.prototype.getScannerByCredentials = function(callback){
    //Send only Field id and email
    _Scanner.find({ where: { email : this.email, pin :this.pin},attributes:['id','email'] }).success(function(scanner) {
        return callback(null, scanner);
    }).error(function(error){
        console.log(error);
        return callback(error, null);
    });
};

I tried with validate() method but i've got as error : Object [object Object] has no method 'validate' and when i'm made a console.log(_Scanner); i saw my function validate() so i don't know why that's doesn't work..

Scanner.prototype.getScannerByCredentials = function(callback){
    //Send only Field id and email
    _Scanner.find({ where: { email : this.email, pin :this.pin},attributes:['id','email'] }).validate().success(function(scanner) {
        return callback(null, scanner);
    }).error(function(error){
        console.log(error);
        return callback(error, null);
    });
};

I'm reading the docs and try a lot of thing founded on the net so if someone could explain to me what's wrong, it will be really nice. Thanks a lot in advance.

EDIT : FOUND !

My solution if you're interested :)

My Models :

module.exports = function(sequelize, DataTypes){
    return  sequelize.define("Scanner",
        {
        id      : {
            primaryKey      : true,
            autoIncrement   : true,
            type            : DataTypes.INTEGER,
            allowNull       : false
        },
        email   : {
            type    : DataTypes.STRING,
            isUnique :true,
            allowNull:false,
            validate:{
                isEmail : true
            }
        },
        pin     : {
            type : DataTypes.INTEGER
        }
    },{
        tableName       : 'scanner',
        classMethods:{
            isValid : function(objScanner) {
                return this.build(objScanner).validate() ? true : false ;
            }
        }
    });
};

My credentials Method :

Scanner.prototype.getScannerByCredentials = function(callback){


    //MODIF THIS PART / NEED TO IMPROVE
    var error = _Scanner.build({ email : this.email, pin : this.pin}).validate();

    if(error === null){
        _Scanner.find({ where: { email : this.email, pin :this.pin},attributes:['id','email'] }).success(function(scanner) {
            console.log(scanner);
            return callback(null, scanner);
        }).error(function(error){
            console.log(error);
            return callback(error, null);
        });
    }
    else
        return callback(error,null);
};

And my generic update method (bonus) :

Scanner.prototype.update= function(callback){
    var self = this;
    _Scanner.find(this.id).success(function(scannerFound){
        if(scannerFound){
            //Set old Values by New Values : only values changed
            scannerFound.dataValues = ormize(self,scannerFound.dataValues);

            //Check validation Fields before insert DB
            if(_Scanner.isValid(scannerFound)){
                scannerFound.save().success(function(){
                    return callback(true);
                }).error(function(error){
                    return callback(false);
                });
            }else
                return callback(false);
        }else
            return callback(false);
    }).error(function(error){
        console.log(error);
        return callback(false);
    });
};

If you have any advice with my code it will be grateful too :) Thank a lot in advance

like image 866
user2822320 Avatar asked Aug 13 '14 05:08

user2822320


Video Answer


1 Answers

What version do you use? This should have been fixed

The field should look like this.

isEmail: {msg: 'Reason'}
like image 185
Bernard Ojengwa Avatar answered Sep 28 '22 01:09

Bernard Ojengwa