diff --git a/services/loopback/common/models/client.js b/services/loopback/common/models/client.js index c9c7cec00..9a246fac1 100644 --- a/services/loopback/common/models/client.js +++ b/services/loopback/common/models/client.js @@ -40,12 +40,24 @@ module.exports = Self => { min: 3, max: 10 }); - var validateIban = require('../validations/validateIban'); - Self.validateBinded('iban', validateIban, { - message: 'The IBAN does not have the correct format', - allowNull: true, // FIXME: Ignored by loopback when it's false - allowBlank: true + Self.validateAsync('iban', ibanNeedsValidation, { + message: 'The IBAN does not have the correct format' }); + var validateIban = require('../validations/validateIban'); + async function ibanNeedsValidation(err, done) { + let filter = { + fields: ['code'], + where: {id: this.countryFk} + }; + let country = await Self.app.models.Country.findOne(filter); + let code = country ? country.code.toLowerCase() : null; + if (code === 'fr') + return done(); + + if (!validateIban(this.iban)) + err(); + done(); + } Self.validateAsync('fi', tinIsValid, { message: 'Invalid TIN'