From 025bd4224eb32eabded4e6d813f1ae700800755b Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Fri, 26 May 2017 17:05:33 +0200 Subject: [PATCH] =?UTF-8?q?Errores=20en=20validaci=C3=B3n=20solucionados?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/core/src/lib/validator.js | 9 +++---- services/client/common/models/Client.js | 36 +++++++++++-------------- 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/client/core/src/lib/validator.js b/client/core/src/lib/validator.js index 9c9670aa25..79a2e7f952 100644 --- a/client/core/src/lib/validator.js +++ b/client/core/src/lib/validator.js @@ -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`); } diff --git a/services/client/common/models/Client.js b/services/client/common/models/Client.js index 91f5f7bbd7..bc21385765 100644 --- a/services/client/common/models/Client.js +++ b/services/client/common/models/Client.js @@ -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(); + }); } });