Errores en validación IBAN solucionados

This commit is contained in:
Juan Ferrer Toribio 2017-05-26 14:39:36 +02:00
parent 51f112fc02
commit 3d71f454a0
3 changed files with 61 additions and 34 deletions

View File

@ -22,7 +22,7 @@ export const validators = {
} else if (conf.min) {
throw new Error(`Value should have at least ${conf.min} characters`);
} else {
throw new Error(`Value should have at most ${conf.min} characters`);
throw new Error(`Value should have at most ${conf.max} characters`);
}
}
},
@ -99,9 +99,9 @@ export function validate(value, conf) {
* @param {Object} conf The validation configuration
*/
export function checkNull(value, conf) {
if (typeof value === 'undefined' || value === '') {
if (!conf.allowBlank)
throw new Error(`Value can't be blank`);
} else if (value === null && !conf.allowNull)
console.log(conf);
if (value === '' && !conf.allowBlank) {
throw new Error(`Value can't be blank`);
} else if (value == null && !conf.allowNull)
throw new Error(`Value can't be null`);
}

View File

@ -1,43 +1,71 @@
module.exports = function(Client) {
require("../scopes/client/card.js")(Client);
require("../scopes/client/activate.js")(Client);
require("../scopes/client/addresses.js")(Client);
// Methods
require('../scopes/client/card.js')(Client);
require('../scopes/client/activate.js')(Client);
require('../scopes/client/addresses.js')(Client);
// Validations
Client.validatesUniquenessOf('name', {message: 'El nombre debe ser único'});
Client.validatesUniquenessOf('fi', {message: 'El NIF/CIF debe ser único'});
Client.validatesPresenceOf('socialName', {message: 'Debe especificarse la razón social'});
Client.validatesFormatOf('postcode', {allowNull: true, with: /^\d+$/, message: 'El código postal solo debe contener números'});
Client.validatesLengthOf('postcode', {allowNull: true, min: 3, max: 10});
Client.validatesFormatOf('email', {allowNull: true, with: /^[\w|\.|\-]+@\w[\w|\.|\-]*\w$/, message: 'Correo electrónico inválido'});
Client.validatesLengthOf('iban', {allowNull: true, allowBlank: true, max: 23});
/* vfalco. A MIRAR ya que no funciona el validar campos de diferentes modelos.
Client.validateAsync('payMethodFk', hasIban, {message: 'Tiene que rellenar el Iban'});
function hasIban(err, done){
let iban = this.iban;
let PayMethod = Client.app.models.PayMethod;
PayMethod.findById(this.payMethodFk, function (finderr, instance){
if (instance.ibanRequired && !iban)
err();
else
done();
});
};
*/
Client.validatesUniquenessOf('name', {
message: 'El nombre debe ser único'
});
Client.validatesUniquenessOf('fi', {
message: 'El NIF/CIF debe ser único'
});
Client.validatesPresenceOf('socialName', {
message: 'Debe especificarse la razón social'
});
Client.validatesFormatOf('postcode', {
message: 'El código postal solo debe contener números',
allowNull: true,
with: /^\d+$/
});
Client.validatesFormatOf('email', {
message: 'Correo electrónico inválido',
allowNull: true,
with: /^[\w|\.|\-]+@\w[\w|\.|\-]*\w$/
});
Client.validatesLengthOf('postcode', {
allowNull: true,
min: 3, max: 10
});
Client.validatesLengthOf('iban', {
allowNull: true,
allowBlank: true,
max: 23
});
Client.validate('payMethod', hasCC, {message: 'Introduzca el iban del cliente'});
Client.validate('payMethod', hasCC, {
message: 'El método de pago seleccionado requiere que se especifique IBAN del cliente'
});
function hasCC(err) {
if (this.payMethod == 2 && !this.iban) err();
};
Client.validate('payMethod', hasSalesMan, {message: 'No se puede cambiar la forma de pago si no hay comercial asignado'});
Client.validate('payMethod', hasSalesMan, {
message: 'No se puede cambiar la forma de pago si no hay comercial asignado'
});
function hasSalesMan(err) {
if(this.payMethod && !this.salesPerson) err();
};
Client.validatesLengthOf('iban', {allowNull: true, min: 23, max: 23});
// vfalco. A mirar ya que no funciona el validar campos de diferentes modelos.
Client.validateAsync('payMethodFk', hasIban, {
message: 'Tiene que rellenar el IBAN'
});
function hasIban(err, done) {
let iban = this.iban;
let PayMethod = Client.app.models.PayMethod;
PayMethod.findById(this.payMethodFk, function (_, instance){
if (instance.ibanRequired && !iban)
err();
done();
});
};
// Hooks
Client.observe('before save', function(ctx, next) {
@ -61,7 +89,6 @@ module.exports = function(Client) {
}
});
// Basic filter
Client.installMethod('filter', filterClients);

View File

@ -12,11 +12,11 @@
"cors": "^2.5.2",
"helmet": "^1.3.0",
"i18n": "^0.8.3",
"loopback": "^2.38.0",
"loopback": "^2.38.3",
"loopback-boot": "^2.6.5",
"loopback-component-explorer": "^2.7.0",
"loopback-connector-mysql": "^3.0.0",
"loopback-datasource-juggler": "^2.54.0",
"loopback-datasource-juggler": "^2.54.1",
"serve-favicon": "^2.0.1",
"strong-error-handler": "^1.2.1"
},