lykmapipo / sails-hook-validation

Custom validation error messages for sails model with i18n support
104 stars 29 forks source link

Errors in error.invalidAttributes but error.Errors is undefined #16

Closed tkiethanom closed 8 years ago

tkiethanom commented 9 years ago

My model looks liks this

module.exports = {
    types: {
        password: function(password){
            if(password != null){
                return password === this.confirm_password;
            }
            else{
                return true;
            }
        }
    },
    attributes: {
        id: {
            type: 'int',
            autoIncrement: true,
            primaryKey: true
        },
        email: {
            type: 'email',
        },
        password: {
            type: 'string',
            required: true,
            password: true
        },
        active: {
            type: 'boolean'
        },
        last_login_date: {
            type: 'datetime'
        }
    },
    validationMessages : {
        password: {
            password: 'Passwords do not match',
        },
        email: {
            email: 'testing',
        }
    },
};

In my controller I'm doing this

Admin.update({id: req.body.id}, saveData, function(err, result){                            
    console.log(err.invalidAttributes);
    console.log(err.Errors);                                
});

In my console the err.invalidAttributes has

{ password: [ { rule: 'password', message: '"password" validation rule failed for input: \'fasdf\'' } ] }

while the err.Errors is undefined.

Am I missing something? I ran npm install --save sails-hook-validation

My sails version is 0.11

tkiethanom commented 9 years ago

I've read that you need to have a matching validtionMessage for each attribute so I've simplified my model to

module.exports = {

    attributes: {
        test: {
            number: true
        }
    },
    validationMessages : {
        test: {
            number: 'test message',
        }
    },

};

Still the same results, the err.Errors is undefined but the err.invalidAttributes is populated.

lykmapipo commented 9 years ago

@tkiethanom

May you please check this spec and then this one. In the first case wihich cover your scenario error.Errors exist and in the second scenario if you update a model with empty hash sails will not update your data and thus no validation will be run.

You can check this model for clean way to define your custom messages.

Hope it helps.