From 8ee649b9d0ae228efeb454a7c03d212d414cd658 Mon Sep 17 00:00:00 2001 From: Juan Date: Tue, 27 Feb 2018 22:50:14 +0100 Subject: [PATCH] Bug with empty value validations fixed --- client/core/src/lib/validator.js | 6 ++++-- services/loopback/common/models/client.js | 4 +++- services/loopback/common/validations/validateDni.js | 6 +++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/client/core/src/lib/validator.js b/client/core/src/lib/validator.js index 9f8d6d53f..e1a3dd1ed 100644 --- a/client/core/src/lib/validator.js +++ b/client/core/src/lib/validator.js @@ -72,9 +72,11 @@ export function validateAll(value, validations) { export function validate(value, conf) { let validator = validators[conf.validation]; try { - checkNull(value, conf); + let isEmpty = value == null || value === ''; - if (validator) // && value != null ?? + if (isEmpty) + checkNull(value, conf); + if (validator && (!isEmpty || conf.validation == 'presence')) validator(value, conf); } catch (e) { let message = conf.message ? conf.message : e.message; diff --git a/services/loopback/common/models/client.js b/services/loopback/common/models/client.js index 2b21ccfa3..a6391e91e 100644 --- a/services/loopback/common/models/client.js +++ b/services/loopback/common/models/client.js @@ -43,7 +43,9 @@ module.exports = function(Self) { var validateIban = require('../validations/validateIban'); Self.validateBinded('iban', validateIban, { - message: 'El iban no tiene el formato correcto' + message: 'El iban no tiene el formato correcto', + allowNull: true, // FIXME: Ignored by loopback when it's false + allowBlank: true }); let validateDni = require('../validations/validateDni'); diff --git a/services/loopback/common/validations/validateDni.js b/services/loopback/common/validations/validateDni.js index c30890c6e..150603b7f 100644 --- a/services/loopback/common/validations/validateDni.js +++ b/services/loopback/common/validations/validateDni.js @@ -1,6 +1,6 @@ -module.exports = fiWithCountry => { - if (fiWithCountry == null) - return true; +module.exports = function(fiWithCountry) { + if (fiWithCountry == null) return true; + if (typeof fiWithCountry != 'string') return false; fiWithCountry = fiWithCountry.toUpperCase();