chore: allow array for IsNotCountryIBAN

This commit is contained in:
Joren Broekema 2020-12-03 18:24:37 +01:00
parent 1d52585a6e
commit a45064696d
18 changed files with 85 additions and 54 deletions

View file

@ -8,7 +8,7 @@ Its purpose is to provide a way for users to fill in an IBAN (International Bank
```js script ```js script
import { html } from 'lit-html'; import { html } from 'lit-html';
import { loadDefaultFeedbackMessages } from '@lion/validate-messages'; import { loadDefaultFeedbackMessages } from '@lion/validate-messages';
import { IsCountryIBAN } from './src/validators.js'; import { IsCountryIBAN, IsNotCountryIBAN } from './src/validators.js';
import './lion-input-iban.js'; import './lion-input-iban.js';
@ -98,11 +98,13 @@ To get the default feedback message for this default validator, use `loadDefault
In the example below, we show how to use an additional validator that blocks IBANs from certain countries. In the example below, we show how to use an additional validator that blocks IBANs from certain countries.
You can pass a single string value, or an array of strings.
```js preview-story ```js preview-story
export const blacklistedCountry = () => html` export const blacklistedCountry = () => html`
<lion-input-iban <lion-input-iban
.modelValue=${'RO89RZBR6997372848645577'} .modelValue=${'DE89370400440532013000'}
.validators=${([new IsNotCountryIBAN('RO')], [new IsNotCountryIBAN('ES')])} .validators=${[new IsNotCountryIBAN(['RO', 'NL'])]}
name="iban" name="iban"
label="IBAN" label="IBAN"
></lion-input-iban> ></lion-input-iban>

View file

@ -124,7 +124,7 @@ export class IsCountryIBAN extends IsIBAN {
} }
/** /**
* @param {?} [value] * @param {string} value
* @returns {Boolean} * @returns {Boolean}
*/ */
execute(value) { execute(value) {
@ -160,18 +160,25 @@ export class IsNotCountryIBAN extends IsIBAN {
} }
/** /**
* @param {?} [value] * @param {string} value
* @returns {Boolean} * @returns {Boolean}
*/ */
execute(value) { execute(value) {
let isInvalid = false;
const notIBAN = super.execute(value); const notIBAN = super.execute(value);
if (value.slice(0, 2) === this.param) {
return true; if (typeof this.param === 'string') {
if (value.slice(0, 2) === this.param) {
isInvalid = true;
}
} else if (Array.isArray(this.param)) {
isInvalid = this.param.some(country => value.slice(0, 2) === country);
} }
if (notIBAN) { if (notIBAN) {
return true; isInvalid = true;
} }
return false; return isInvalid;
} }
/** /**
@ -186,6 +193,13 @@ export class IsNotCountryIBAN extends IsIBAN {
*/ */
static async getMessage(data) { static async getMessage(data) {
await loadTranslations(); await loadTranslations();
return localize.msg('lion-validate+iban:error.IsNotCountryIBAN', data); const _data = {
...data,
userSuppliedCountryCode:
typeof data?.modelValue === 'string'
? data?.modelValue.slice(0, 2)
: data?.modelValue.viewValue.slice(0, 2),
};
return localize.msg('lion-validate+iban:error.IsNotCountryIBAN', _data);
} }
} }

View file

@ -9,6 +9,7 @@ describe('IBAN validation', () => {
const validator = new IsIBAN(); const validator = new IsIBAN();
expect(validator.execute('NL17INGB0002822608')).to.be.false; expect(validator.execute('NL17INGB0002822608')).to.be.false;
expect(validator.execute('DE89370400440532013000')).to.be.false; expect(validator.execute('DE89370400440532013000')).to.be.false;
expect(validator.execute('foo')).to.be.true;
}); });
it('provides IsCountryIBAN to limit IBANs from specific countries', () => { it('provides IsCountryIBAN to limit IBANs from specific countries', () => {
@ -17,7 +18,7 @@ describe('IBAN validation', () => {
expect(nlValidator.execute('NL17INGB0002822608')).to.be.false; expect(nlValidator.execute('NL17INGB0002822608')).to.be.false;
expect(deValidator.execute('DE89370400440532013000')).to.be.false; expect(deValidator.execute('DE89370400440532013000')).to.be.false;
expect(nlValidator.execute('DE89370400440532013000')).to.be.true; expect(nlValidator.execute('DE89370400440532013000')).to.be.true;
expect(nlValidator.execute('foo')).to.be.true; expect(deValidator.execute('NL17INGB0002822608')).to.be.true;
}); });
it('provides IsNotCountryIBAN to prevent IBANs from specific countries', () => { it('provides IsNotCountryIBAN to prevent IBANs from specific countries', () => {
@ -28,4 +29,18 @@ describe('IBAN validation', () => {
expect(nlValidator.execute('DE89370400440532013000')).to.be.false; expect(nlValidator.execute('DE89370400440532013000')).to.be.false;
expect(deValidator.execute('NL17INGB0002822608')).to.be.false; expect(deValidator.execute('NL17INGB0002822608')).to.be.false;
}); });
it('accepts an array for IsNotCountryIBAN to prevent IBANs from multiple countries', () => {
const nlValidator = new IsNotCountryIBAN(['NL', 'FR']);
const deValidator = new IsNotCountryIBAN(['DE', 'SK']);
expect(nlValidator.execute('NL17INGB0002822608')).to.be.true;
expect(nlValidator.execute('FR1420041010050500013M02606')).to.be.true;
expect(nlValidator.execute('DE89370400440532013000')).to.be.false;
expect(nlValidator.execute('SK3112000000198742637541')).to.be.false;
expect(deValidator.execute('NL17INGB0002822608')).to.be.false;
expect(deValidator.execute('FR1420041010050500013M02606')).to.be.false;
expect(deValidator.execute('DE89370400440532013000')).to.be.true;
expect(deValidator.execute('SK3112000000198742637541')).to.be.true;
});
}); });

View file

@ -17,7 +17,7 @@ export default {
'other {{params}}\n' + 'other {{params}}\n' +
'} {fieldName}.', '} {fieldName}.',
IsNotCountryIBAN: IsNotCountryIBAN:
'{params, select,\n' + '{userSuppliedCountryCode, select,\n' +
'AT {Австрийски}\n' + 'AT {Австрийски}\n' +
'BE {Белгийски}\n' + 'BE {Белгийски}\n' +
'CZ {Чешки}\n' + 'CZ {Чешки}\n' +
@ -29,7 +29,7 @@ export default {
'NL {Нидерландски}\n' + 'NL {Нидерландски}\n' +
'PL {Полски}\n' + 'PL {Полски}\n' +
'RO {Румънски}\n' + 'RO {Румънски}\n' +
'other {{params}}\n' + 'other {{userSuppliedCountryCode}}\n' +
'} {fieldName} не е позволено.', '} {fieldName} не е позволено.',
}, },
}; };

View file

@ -17,7 +17,7 @@ export default {
'other {{params}}\n' + 'other {{params}}\n' +
'} {fieldName}.', '} {fieldName}.',
IsNotCountryIBAN: IsNotCountryIBAN:
'{params, select,\n' + '{userSuppliedCountryCode, select,\n' +
'AT {Rakušan}\n' + 'AT {Rakušan}\n' +
'BE {Belgičan}\n' + 'BE {Belgičan}\n' +
'CZ {Čech}\n' + 'CZ {Čech}\n' +
@ -29,7 +29,7 @@ export default {
'NL {Holanďan}\n' + 'NL {Holanďan}\n' +
'PL {Polák}\n' + 'PL {Polák}\n' +
'RO {Rumun}\n' + 'RO {Rumun}\n' +
'other {{params}}\n' + 'other {{userSuppliedCountryCode}}\n' +
'} {fieldName} není povoleno.', '} {fieldName} není povoleno.',
}, },
}; };

View file

@ -17,7 +17,7 @@ export default {
'other {{params}}\n' + 'other {{params}}\n' +
'} {fieldName} ein.', '} {fieldName} ein.',
IsNotCountryIBAN: IsNotCountryIBAN:
'{params, select,\n' + '{userSuppliedCountryCode, select,\n' +
'AT {Österreichisch}\n' + 'AT {Österreichisch}\n' +
'BE {Belgisch}\n' + 'BE {Belgisch}\n' +
'CZ {Tschechisch}\n' + 'CZ {Tschechisch}\n' +
@ -29,7 +29,7 @@ export default {
'NL {Niederländisch}\n' + 'NL {Niederländisch}\n' +
'PL {Polnisch}\n' + 'PL {Polnisch}\n' +
'RO {Rumänisch}\n' + 'RO {Rumänisch}\n' +
'other {{params}}\n' + 'other {{userSuppliedCountryCode}}\n' +
'} {fieldName} ist nicht erlaubt.', '} {fieldName} ist nicht erlaubt.',
}, },
}; };

View file

@ -16,20 +16,20 @@ export default {
'RO {Romanian}\n' + 'RO {Romanian}\n' +
'other {{params}}\n' + 'other {{params}}\n' +
'} {fieldName}.', '} {fieldName}.',
IsNotCountryIBAN:
'{userSuppliedCountryCode, select,\n' +
'AT {Austrian}\n' +
'BE {Belgian}\n' +
'CZ {Czech}\n' +
'DE {German}\n' +
'ES {Spanish}\n' +
'FR {French}\n' +
'HU {Hungarian}\n' +
'IT {Italian}\n' +
'NL {Dutch}\n' +
'PL {Polish}\n' +
'RO {Romanian}\n' +
'other {{userSuppliedCountryCode}}\n' +
'} {fieldName} is not allowed.',
}, },
IsNotCountryIBAN:
'{params, select,\n' +
'AT {Austrian}\n' +
'BE {Belgian}\n' +
'CZ {Czech}\n' +
'DE {German}\n' +
'ES {Spanish}\n' +
'FR {French}\n' +
'HU {Hungarian}\n' +
'IT {Italian}\n' +
'NL {Dutch}\n' +
'PL {Polish}\n' +
'RO {Romanian}\n' +
'other {{params}}\n' +
'} {fieldName} is not allowed.',
}; };

View file

@ -17,7 +17,7 @@ export default {
'other {{params}}\n' + 'other {{params}}\n' +
'}.', '}.',
IsNotCountryIBAN: IsNotCountryIBAN:
'{fieldName} {params, select,\n' + '{fieldName} {userSuppliedCountryCode, select,\n' +
'AT {Austriaco}\n' + 'AT {Austriaco}\n' +
'BE {Belga}\n' + 'BE {Belga}\n' +
'CZ {Checo}\n' + 'CZ {Checo}\n' +
@ -29,7 +29,7 @@ export default {
'NL {Neerlandés}\n' + 'NL {Neerlandés}\n' +
'PL {Polaco}\n' + 'PL {Polaco}\n' +
'RO {Rumano}\n' + 'RO {Rumano}\n' +
'other {{params}}\n' + 'other {{userSuppliedCountryCode}}\n' +
'} no se permite.', '} no se permite.',
}, },
}; };

View file

@ -17,7 +17,7 @@ export default {
'other {{params}}\n' + 'other {{params}}\n' +
'} valide.', '} valide.',
IsNotCountryIBAN: IsNotCountryIBAN:
'{fieldName} {params, select,\n' + '{fieldName} {userSuppliedCountryCode, select,\n' +
'AT {autrichien}\n' + 'AT {autrichien}\n' +
'BE {belge}\n' + 'BE {belge}\n' +
'CZ {tchèque}\n' + 'CZ {tchèque}\n' +
@ -29,7 +29,7 @@ export default {
'NL {néerlandais}\n' + 'NL {néerlandais}\n' +
'PL {polonais}\n' + 'PL {polonais}\n' +
'RO {roumain}\n' + 'RO {roumain}\n' +
'other {{params}}\n' + 'other {{userSuppliedCountryCode}}\n' +
"} n'est pas autorisé.", "} n'est pas autorisé.",
}, },
}; };

View file

@ -17,7 +17,7 @@ export default {
'other {{params}}\n' + 'other {{params}}\n' +
'} {fieldName} értéket.', '} {fieldName} értéket.',
IsNotCountryIBAN: IsNotCountryIBAN:
'{params, select,\n' + '{userSuppliedCountryCode, select,\n' +
'AT {Osztrák}\n' + 'AT {Osztrák}\n' +
'BE {Belga}\n' + 'BE {Belga}\n' +
'CZ {Cseh}\n' + 'CZ {Cseh}\n' +
@ -29,7 +29,7 @@ export default {
'NL {Holland}\n' + 'NL {Holland}\n' +
'PL {Lengyel}\n' + 'PL {Lengyel}\n' +
'RO {Román}\n' + 'RO {Román}\n' +
'other {{params}}\n' + 'other {{userSuppliedCountryCode}}\n' +
'} {fieldName} nem engedélyezett.', '} {fieldName} nem engedélyezett.',
}, },
}; };

View file

@ -17,7 +17,7 @@ export default {
'other {{params}}\n' + 'other {{params}}\n' +
'}.', '}.',
IsNotCountryIBAN: IsNotCountryIBAN:
'{fieldName} {params, select,\n' + '{fieldName} {userSuppliedCountryCode, select,\n' +
'AT {Austriaco}\n' + 'AT {Austriaco}\n' +
'BE {Belga}\n' + 'BE {Belga}\n' +
'CZ {Ceco}\n' + 'CZ {Ceco}\n' +
@ -29,7 +29,7 @@ export default {
'NL {Olandese}\n' + 'NL {Olandese}\n' +
'PL {Polacco}\n' + 'PL {Polacco}\n' +
'RO {Rumeno}\n' + 'RO {Rumeno}\n' +
'other {{params}}\n' + 'other {{userSuppliedCountryCode}}\n' +
'} non è permesso.', '} non è permesso.',
}, },
}; };

View file

@ -17,7 +17,7 @@ export default {
'other {{params}}\n' + 'other {{params}}\n' +
'} {fieldName} in.', '} {fieldName} in.',
IsNotCountryIBAN: IsNotCountryIBAN:
'{params, select,\n' + '{userSuppliedCountryCode, select,\n' +
'AT {Oostenrijkse}\n' + 'AT {Oostenrijkse}\n' +
'BE {Belgische}\n' + 'BE {Belgische}\n' +
'CZ {Tsjechische}\n' + 'CZ {Tsjechische}\n' +
@ -29,7 +29,7 @@ export default {
'NL {Nederlandse}\n' + 'NL {Nederlandse}\n' +
'PL {Poolse}\n' + 'PL {Poolse}\n' +
'RO {Roemeense}\n' + 'RO {Roemeense}\n' +
'other {{params}}\n' + 'other {{userSuppliedCountryCode}}\n' +
'} {fieldName} is niet toegestaan.', '} {fieldName} is niet toegestaan.',
}, },
}; };

View file

@ -17,7 +17,7 @@ export default {
'other {{params}}\n' + 'other {{params}}\n' +
'} {fieldName}.', '} {fieldName}.',
IsNotCountryIBAN: IsNotCountryIBAN:
'{params, select,\n' + '{userSuppliedCountryCode, select,\n' +
'AT {Austriacki}\n' + 'AT {Austriacki}\n' +
'BE {Belgijski}\n' + 'BE {Belgijski}\n' +
'CZ {Czeski}\n' + 'CZ {Czeski}\n' +
@ -29,7 +29,7 @@ export default {
'NL {Holenderski}\n' + 'NL {Holenderski}\n' +
'PL {Polski}\n' + 'PL {Polski}\n' +
'RO {Rumuński}\n' + 'RO {Rumuński}\n' +
'other {{params}}\n' + 'other {{userSuppliedCountryCode}}\n' +
'} {fieldName} nie jest dozwolone.', '} {fieldName} nie jest dozwolone.',
}, },
}; };

View file

@ -17,7 +17,7 @@ export default {
'other {{params}}\n' + 'other {{params}}\n' +
'} valid(ă).', '} valid(ă).',
IsNotCountryIBAN: IsNotCountryIBAN:
'{fieldName} {params, select,\n' + '{fieldName} {userSuppliedCountryCode, select,\n' +
'AT {austriac}\n' + 'AT {austriac}\n' +
'BE {belgian}\n' + 'BE {belgian}\n' +
'CZ {ceh}\n' + 'CZ {ceh}\n' +
@ -29,7 +29,7 @@ export default {
'NL {olandez}\n' + 'NL {olandez}\n' +
'PL {polonez}\n' + 'PL {polonez}\n' +
'RO {românesc}\n' + 'RO {românesc}\n' +
'other {{params}}\n' + 'other {{userSuppliedCountryCode}}\n' +
'} nu este permis.', '} nu este permis.',
}, },
}; };

View file

@ -17,7 +17,7 @@ export default {
'other {{params}}\n' + 'other {{params}}\n' +
'} {fieldName}.', '} {fieldName}.',
IsNotCountryIBAN: IsNotCountryIBAN:
'{params, select,\n' + '{userSuppliedCountryCode, select,\n' +
'AT {Австрийский}\n' + 'AT {Австрийский}\n' +
'BE {Бельгийский}\n' + 'BE {Бельгийский}\n' +
'CZ {Чешский}\n' + 'CZ {Чешский}\n' +
@ -29,7 +29,7 @@ export default {
'NL {Нидерландский}\n' + 'NL {Нидерландский}\n' +
'PL {Польский}\n' + 'PL {Польский}\n' +
'RO {Румынский}\n' + 'RO {Румынский}\n' +
'other {{params}}\n' + 'other {{userSuppliedCountryCode}}\n' +
'} {fieldName} не допускается.', '} {fieldName} не допускается.',
}, },
}; };

View file

@ -17,7 +17,7 @@ export default {
'other {{params}}\n' + 'other {{params}}\n' +
'} kód {fieldName}.', '} kód {fieldName}.',
IsNotCountryIBAN: IsNotCountryIBAN:
'{params, select,\n' + '{userSuppliedCountryCode, select,\n' +
'AT {Rakúsky}\n' + 'AT {Rakúsky}\n' +
'BE {Belgický}\n' + 'BE {Belgický}\n' +
'CZ {Český}\n' + 'CZ {Český}\n' +
@ -29,7 +29,7 @@ export default {
'NL {Holandský}\n' + 'NL {Holandský}\n' +
'PL {Poľský}\n' + 'PL {Poľský}\n' +
'RO {Rumunský}\n' + 'RO {Rumunský}\n' +
'other {{params}}\n' + 'other {{userSuppliedCountryCode}}\n' +
'} {fieldName} nie je povolené.', '} {fieldName} nie je povolené.',
}, },
}; };

View file

@ -17,7 +17,7 @@ export default {
'other {{params}}\n' + 'other {{params}}\n' +
'} {fieldName}.', '} {fieldName}.',
IsNotCountryIBAN: IsNotCountryIBAN:
'{params, select,\n' + '{userSuppliedCountryCode, select,\n' +
'AT {Австрійський}\n' + 'AT {Австрійський}\n' +
'BE {Бельгійський}\n' + 'BE {Бельгійський}\n' +
'CZ {Чеський}\n' + 'CZ {Чеський}\n' +
@ -29,7 +29,7 @@ export default {
'NL {Голландський}\n' + 'NL {Голландський}\n' +
'PL {Польський}\n' + 'PL {Польський}\n' +
'RO {Румунська}\n' + 'RO {Румунська}\n' +
'other {{params}}\n' + 'other {{userSuppliedCountryCode}}\n' +
'} {fieldName} не дозволено.', '} {fieldName} не дозволено.',
}, },
}; };

View file

@ -17,7 +17,7 @@ export default {
'另一个 {{params}}\n' + '另一个 {{params}}\n' +
'} {fieldName}。', '} {fieldName}。',
IsNotCountryIBAN: IsNotCountryIBAN:
'{params, select,\n' + '{userSuppliedCountryCode, select,\n' +
'AT {奥}\n' + 'AT {奥}\n' +
'BE {比利时的}\n' + 'BE {比利时的}\n' +
'CZ {捷克}\n' + 'CZ {捷克}\n' +
@ -29,7 +29,7 @@ export default {
'NL {荷兰人}\n' + 'NL {荷兰人}\n' +
'PL {抛光}\n' + 'PL {抛光}\n' +
'RO {罗马尼亚}\n' + 'RO {罗马尼亚}\n' +
'另一个 {{params}}\n' + '另一个 {{userSuppliedCountryCode}}\n' +
'} {fieldName} 不允許。', '} {fieldName} 不允許。',
}, },
}; };