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) if (!valid)
throw new Error(`Invalid value`); 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 {*} value The value
* @param {Object} conf The validation configuration * @param {Object} conf The validation configuration
*/ */
export function checkNull(value, conf) { export function checkNull(value, conf) {
if (value === '' && !conf.allowBlank) { if (conf.allowBlank === false && value === '')
throw new Error(`Value can't be blank`); 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`); 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' message: 'No se puede cambiar la forma de pago si no hay comercial asignado'
}); });
function hasSalesMan(err) { function hasSalesMan(err) {
if(this.payMethod && !this.salesPerson) err(); if(this.payMethod && !this.salesPerson)
}; err();
}
Client.validateAsync('payMethodFk', hasIban, { Client.validateAsync('payMethodFk', hasIban, {
message: 'El método de pago seleccionado requiere que se especifique el IBAN' 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) { function hasIban(err, done) {
let iban = this.iban; let iban = this.iban;
let PayMethod = Client.app.models.PayMethod; let PayMethod = Client.app.models.PayMethod;
PayMethod.findById(this.payMethodFk, function (_, instance){ PayMethod.findById(this.payMethodFk, function (_, instance) {
if (instance.ibanRequired && !iban) if (instance && instance.ibanRequired && !iban)
err(); err();
done(); done();
}); });
}; }
// Hooks // Hooks
Client.observe('before save', function(ctx, next) { Client.observe('before save', function(ctx, next) {
if(ctx.instance) { if (ctx.instance) {
if (!ctx.instance.dueDay){ if (!ctx.instance.dueDay)
ctx.instance.dueDay = 5; ctx.instance.dueDay = 5;
}
next(); next();
} } else {
else { Client.findById(ctx.where.id, function(err, instance) {
Client.findById(ctx.where.id, if (instance
function(err, item) { && instance.payMethodFk != ctx.data.payMethodFk
if (!err) { && instance.dueDay == ctx.data.dueDay)
if (item.payMethod != ctx.data.payMethod && item.dueDay == ctx.data.dueDay) {
ctx.data.dueDay = 5; ctx.data.dueDay = 5;
}
}
next(); next();
} });
);
} }
}); });