From 0fc206bfd6aac1ca94e097cbc38f2b0a7112af15 Mon Sep 17 00:00:00 2001 From: sdonose Date: Thu, 15 Apr 2021 13:53:01 +0200 Subject: [PATCH] feat(input-iban): allow to specify countries in lowercase --- .changeset/curvy-suns-cross.md | 5 +++++ packages/input-iban/src/validators.js | 22 ++++++++++----------- packages/input-iban/test/validators.test.js | 16 +++++++++++++++ 3 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 .changeset/curvy-suns-cross.md diff --git a/.changeset/curvy-suns-cross.md b/.changeset/curvy-suns-cross.md new file mode 100644 index 000000000..e7a72966c --- /dev/null +++ b/.changeset/curvy-suns-cross.md @@ -0,0 +1,5 @@ +--- +'@lion/input-iban': patch +--- + +allow to specify countries in lowercase diff --git a/packages/input-iban/src/validators.js b/packages/input-iban/src/validators.js index 6f167845b..1538e65aa 100644 --- a/packages/input-iban/src/validators.js +++ b/packages/input-iban/src/validators.js @@ -124,12 +124,12 @@ export class IsCountryIBAN extends IsIBAN { } /** - * @param {string} value + * @param {string} modelValue * @returns {Boolean} */ - execute(value) { - const notIBAN = super.execute(value); - if (value.slice(0, 2) !== this.param) { + execute(modelValue) { + const notIBAN = super.execute(modelValue); + if (modelValue.slice(0, 2) !== this.param.toUpperCase()) { return true; } if (notIBAN) { @@ -163,19 +163,20 @@ export class IsNotCountryIBAN extends IsIBAN { } /** - * @param {string} value + * @param {string} modelValue * @returns {Boolean} */ - execute(value) { + execute(modelValue) { let isInvalid = false; - const notIBAN = super.execute(value); - + const notIBAN = super.execute(modelValue); if (typeof this.param === 'string') { - if (value.slice(0, 2) === this.param) { + if (String(modelValue.slice(0, 2)) === this.param.toUpperCase()) { isInvalid = true; } } else if (Array.isArray(this.param)) { - isInvalid = this.param.some(country => value.slice(0, 2) === country); + isInvalid = this.param.some( + country => String(modelValue.slice(0, 2)) === country.toUpperCase(), + ); } if (notIBAN) { @@ -201,7 +202,6 @@ export class IsNotCountryIBAN extends IsIBAN { userSuppliedCountryCode: typeof data?.modelValue === 'string' ? data?.modelValue.slice(0, 2) : '', }; - // If modelValue is Unparseable, the IsIBAN message is the more appropriate feedback return data?.modelValue instanceof Unparseable ? localize.msg('lion-validate+iban:error.IsIBAN', _data) diff --git a/packages/input-iban/test/validators.test.js b/packages/input-iban/test/validators.test.js index eb72ed666..b4f6ada8a 100644 --- a/packages/input-iban/test/validators.test.js +++ b/packages/input-iban/test/validators.test.js @@ -43,4 +43,20 @@ describe('IBAN validation', () => { expect(deValidator.execute('DE89370400440532013000')).to.be.true; expect(deValidator.execute('SK3112000000198742637541')).to.be.true; }); + + it('allows providing lower cased country isos', () => { + const nlValidator = new IsCountryIBAN('nl'); + expect(nlValidator.execute('NL17INGB0002822608')).to.be.false; + expect(nlValidator.execute('DE89370400440532013000')).to.be.true; + + const deValidator = new IsNotCountryIBAN('de'); + expect(deValidator.execute('DE89370400440532013000')).to.be.true; + expect(deValidator.execute('NL17INGB0002822608')).to.be.false; + + const deskValidator = new IsNotCountryIBAN(['de', 'sk']); + expect(deskValidator.execute('NL17INGB0002822608')).to.be.false; + expect(deskValidator.execute('FR1420041010050500013M02606')).to.be.false; + expect(deskValidator.execute('DE89370400440532013000')).to.be.true; + expect(deskValidator.execute('SK3112000000198742637541')).to.be.true; + }); });