feat(input-iban): allow to specify countries in lowercase

This commit is contained in:
sdonose 2021-04-15 13:53:01 +02:00 committed by Thijs Louisse
parent 749a4a4bf2
commit 0fc206bfd6
3 changed files with 32 additions and 11 deletions

View file

@ -0,0 +1,5 @@
---
'@lion/input-iban': patch
---
allow to specify countries in lowercase

View file

@ -124,12 +124,12 @@ export class IsCountryIBAN extends IsIBAN {
} }
/** /**
* @param {string} value * @param {string} modelValue
* @returns {Boolean} * @returns {Boolean}
*/ */
execute(value) { execute(modelValue) {
const notIBAN = super.execute(value); const notIBAN = super.execute(modelValue);
if (value.slice(0, 2) !== this.param) { if (modelValue.slice(0, 2) !== this.param.toUpperCase()) {
return true; return true;
} }
if (notIBAN) { if (notIBAN) {
@ -163,19 +163,20 @@ export class IsNotCountryIBAN extends IsIBAN {
} }
/** /**
* @param {string} value * @param {string} modelValue
* @returns {Boolean} * @returns {Boolean}
*/ */
execute(value) { execute(modelValue) {
let isInvalid = false; let isInvalid = false;
const notIBAN = super.execute(value); const notIBAN = super.execute(modelValue);
if (typeof this.param === 'string') { if (typeof this.param === 'string') {
if (value.slice(0, 2) === this.param) { if (String(modelValue.slice(0, 2)) === this.param.toUpperCase()) {
isInvalid = true; isInvalid = true;
} }
} else if (Array.isArray(this.param)) { } 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) { if (notIBAN) {
@ -201,7 +202,6 @@ export class IsNotCountryIBAN extends IsIBAN {
userSuppliedCountryCode: userSuppliedCountryCode:
typeof data?.modelValue === 'string' ? data?.modelValue.slice(0, 2) : '', typeof data?.modelValue === 'string' ? data?.modelValue.slice(0, 2) : '',
}; };
// If modelValue is Unparseable, the IsIBAN message is the more appropriate feedback // If modelValue is Unparseable, the IsIBAN message is the more appropriate feedback
return data?.modelValue instanceof Unparseable return data?.modelValue instanceof Unparseable
? localize.msg('lion-validate+iban:error.IsIBAN', _data) ? localize.msg('lion-validate+iban:error.IsIBAN', _data)

View file

@ -43,4 +43,20 @@ describe('IBAN validation', () => {
expect(deValidator.execute('DE89370400440532013000')).to.be.true; expect(deValidator.execute('DE89370400440532013000')).to.be.true;
expect(deValidator.execute('SK3112000000198742637541')).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;
});
}); });