Errores en validación solucionados

This commit is contained in:
Juan Ferrer Toribio 2017-05-26 17:05:33 +02:00
parent daaf73a143
commit 025bd4224e
2 changed files with 19 additions and 26 deletions

View File

@ -56,9 +56,6 @@ export const validators = {
if (!valid)
throw new Error(`Invalid value`);
},
uniqueness: function() {
// TODO: Implement me
}
};
@ -93,14 +90,14 @@ export function validate(value, conf) {
}
/**
* Checks if value satisfies a not null validation.
* Checks if value satisfies a blank or not null validation.
*
* @param {*} value The value
* @param {Object} conf The validation configuration
*/
export function checkNull(value, conf) {
if (value === '' && !conf.allowBlank) {
if (conf.allowBlank === false && value === '')
throw new Error(`Value can't be blank`);
} else if (value == null && !conf.allowNull)
else if (conf.allowNull === false && value == null)
throw new Error(`Value can't be null`);
}

View File

@ -41,8 +41,9 @@ module.exports = function(Client) {
message: 'No se puede cambiar la forma de pago si no hay comercial asignado'
});
function hasSalesMan(err) {
if(this.payMethod && !this.salesPerson) err();
};
if(this.payMethod && !this.salesPerson)
err();
}
Client.validateAsync('payMethodFk', hasIban, {
message: 'El método de pago seleccionado requiere que se especifique el IBAN'
@ -50,33 +51,28 @@ module.exports = function(Client) {
function hasIban(err, done) {
let iban = this.iban;
let PayMethod = Client.app.models.PayMethod;
PayMethod.findById(this.payMethodFk, function (_, instance){
if (instance.ibanRequired && !iban)
PayMethod.findById(this.payMethodFk, function (_, instance) {
if (instance && instance.ibanRequired && !iban)
err();
done();
});
};
}
// Hooks
Client.observe('before save', function(ctx, next) {
if(ctx.instance) {
if (!ctx.instance.dueDay){
if (ctx.instance) {
if (!ctx.instance.dueDay)
ctx.instance.dueDay = 5;
}
next();
}
else {
Client.findById(ctx.where.id,
function(err, item) {
if (!err) {
if (item.payMethod != ctx.data.payMethod && item.dueDay == ctx.data.dueDay) {
ctx.data.dueDay = 5;
}
}
next();
}
);
} else {
Client.findById(ctx.where.id, function(err, instance) {
if (instance
&& instance.payMethodFk != ctx.data.payMethodFk
&& instance.dueDay == ctx.data.dueDay)
ctx.data.dueDay = 5;
next();
});
}
});