From c8e73457ac667bae57629be34741658a4ce47210 Mon Sep 17 00:00:00 2001 From: Joren Broekema Date: Thu, 3 Dec 2020 16:21:32 +0100 Subject: [PATCH 1/4] feat(input-iban): add IsNotCountryIBAN validator Co-authored-by: andrei19021 --- .changeset/two-buses-glow.md | 5 +++ packages/input-iban/README.md | 22 +++++++++++++ packages/input-iban/index.js | 2 +- packages/input-iban/src/validators.js | 36 +++++++++++++++++++++ packages/input-iban/test/validators.test.js | 11 ++++++- packages/input-iban/translations/bg.js | 15 +++++++++ packages/input-iban/translations/cs.js | 15 +++++++++ packages/input-iban/translations/de.js | 15 +++++++++ packages/input-iban/translations/en.js | 15 +++++++++ packages/input-iban/translations/es.js | 15 +++++++++ packages/input-iban/translations/fr.js | 15 +++++++++ packages/input-iban/translations/hu.js | 15 +++++++++ packages/input-iban/translations/it.js | 15 +++++++++ packages/input-iban/translations/nl.js | 15 +++++++++ packages/input-iban/translations/pl.js | 15 +++++++++ packages/input-iban/translations/ro.js | 15 +++++++++ packages/input-iban/translations/ru.js | 15 +++++++++ packages/input-iban/translations/sk.js | 15 +++++++++ packages/input-iban/translations/uk.js | 15 +++++++++ packages/input-iban/translations/zh.js | 15 +++++++++ 20 files changed, 299 insertions(+), 2 deletions(-) create mode 100644 .changeset/two-buses-glow.md diff --git a/.changeset/two-buses-glow.md b/.changeset/two-buses-glow.md new file mode 100644 index 000000000..ddffad865 --- /dev/null +++ b/.changeset/two-buses-glow.md @@ -0,0 +1,5 @@ +--- +'@lion/input-iban': minor +--- + +add country blacklisting validator diff --git a/packages/input-iban/README.md b/packages/input-iban/README.md index d380cb4f0..286aadbc3 100644 --- a/packages/input-iban/README.md +++ b/packages/input-iban/README.md @@ -90,3 +90,25 @@ export const countryRestrictions = () => html` Demo instructions: you can use NL20 INGB 0001 2345 67 `; ``` + +### Blacklisted Country + +By default, we validate the input to ensure the IBAN is valid. +To get the default feedback message for this default validator, use `loadDefaultFeedbackMessages` from `@lion/form-core`. + +In the example below, we show how to use an additional validator that blocks IBANs from certain countries. + +```js preview-story +export const blacklistedCountry = () => html` + +
+ Demo instructions: Try RO 89 RZBR 6997 3728 4864 5577 and watch it fail +`; +``` diff --git a/packages/input-iban/index.js b/packages/input-iban/index.js index 9405bd43a..4e430deae 100644 --- a/packages/input-iban/index.js +++ b/packages/input-iban/index.js @@ -1,4 +1,4 @@ export { LionInputIban } from './src/LionInputIban.js'; export { formatIBAN } from './src/formatters.js'; export { parseIBAN } from './src/parsers.js'; -export { IsIBAN, IsCountryIBAN } from './src/validators.js'; +export { IsIBAN, IsCountryIBAN, IsNotCountryIBAN } from './src/validators.js'; diff --git a/packages/input-iban/src/validators.js b/packages/input-iban/src/validators.js index 28f398c9b..8593472f1 100644 --- a/packages/input-iban/src/validators.js +++ b/packages/input-iban/src/validators.js @@ -153,3 +153,39 @@ export class IsCountryIBAN extends IsIBAN { return localize.msg('lion-validate+iban:error.IsCountryIBAN', data); } } + +export class IsNotCountryIBAN extends IsIBAN { + static get validatorName() { + return 'IsNotCountryIBAN'; + } + + /** + * @param {?} [value] + * @returns {Boolean} + */ + execute(value) { + const notIBAN = super.execute(value); + if (value.slice(0, 2) === this.param) { + return true; + } + if (notIBAN) { + return true; + } + return false; + } + + /** + * @param {object} [data] + * @param {*} [data.modelValue] + * @param {string} [data.fieldName] + * @param {*} [data.params] + * @param {string} [data.type] + * @param {Object.} [data.config] + * @param {string} [data.name] + * @returns {Promise} + */ + static async getMessage(data) { + await loadTranslations(); + return localize.msg('lion-validate+iban:error.IsNotCountryIBAN', data); + } +} diff --git a/packages/input-iban/test/validators.test.js b/packages/input-iban/test/validators.test.js index 44caec309..0b20d5ce5 100644 --- a/packages/input-iban/test/validators.test.js +++ b/packages/input-iban/test/validators.test.js @@ -1,6 +1,6 @@ import { expect } from '@open-wc/testing'; -import { IsIBAN, IsCountryIBAN } from '../src/validators.js'; +import { IsIBAN, IsCountryIBAN, IsNotCountryIBAN } from '../src/validators.js'; import '../lion-input-iban.js'; @@ -19,4 +19,13 @@ describe('IBAN validation', () => { expect(nlValidator.execute('DE89370400440532013000')).to.be.true; expect(nlValidator.execute('foo')).to.be.true; }); + + it('provides IsNotCountryIBAN to prevent IBANs from specific countries', () => { + const nlValidator = new IsNotCountryIBAN('NL'); + const deValidator = new IsNotCountryIBAN('DE'); + expect(nlValidator.execute('NL17INGB0002822608')).to.be.true; + expect(deValidator.execute('DE89370400440532013000')).to.be.true; + expect(nlValidator.execute('DE89370400440532013000')).to.be.false; + expect(deValidator.execute('NL17INGB0002822608')).to.be.false; + }); }); diff --git a/packages/input-iban/translations/bg.js b/packages/input-iban/translations/bg.js index d57af8633..6fbd20db9 100644 --- a/packages/input-iban/translations/bg.js +++ b/packages/input-iban/translations/bg.js @@ -16,5 +16,20 @@ export default { 'RO {Румънски}\n' + 'other {{params}}\n' + '} {fieldName}.', + IsNotCountryIBAN: + '{params, select,\n' + + 'AT {Австрийски}\n' + + 'BE {Белгийски}\n' + + 'CZ {Чешки}\n' + + 'DE {Немски}\n' + + 'ES {Испански}\n' + + 'FR {Френски}\n' + + 'HU {Унгарски}\n' + + 'IT {Италиански}\n' + + 'NL {Нидерландски}\n' + + 'PL {Полски}\n' + + 'RO {Румънски}\n' + + 'other {{params}}\n' + + '} {fieldName} не е позволено.', }, }; diff --git a/packages/input-iban/translations/cs.js b/packages/input-iban/translations/cs.js index ec50c8059..56ce59f19 100644 --- a/packages/input-iban/translations/cs.js +++ b/packages/input-iban/translations/cs.js @@ -16,5 +16,20 @@ export default { 'RO {Rumun}\n' + 'other {{params}}\n' + '} {fieldName}.', + IsNotCountryIBAN: + '{params, select,\n' + + 'AT {Rakušan}\n' + + 'BE {Belgičan}\n' + + 'CZ {Čech}\n' + + 'DE {Němec}\n' + + 'ES {Španěl}\n' + + 'FR {Francouz}\n' + + 'HU {Maďar}\n' + + 'IT {Ital}\n' + + 'NL {Holanďan}\n' + + 'PL {Polák}\n' + + 'RO {Rumun}\n' + + 'other {{params}}\n' + + '} {fieldName} není povoleno.', }, }; diff --git a/packages/input-iban/translations/de.js b/packages/input-iban/translations/de.js index 388d1718d..c8a49f079 100644 --- a/packages/input-iban/translations/de.js +++ b/packages/input-iban/translations/de.js @@ -16,5 +16,20 @@ export default { 'RO {Rumänisch}\n' + 'other {{params}}\n' + '} {fieldName} ein.', + IsNotCountryIBAN: + '{params, select,\n' + + 'AT {Österreichisch}\n' + + 'BE {Belgisch}\n' + + 'CZ {Tschechisch}\n' + + 'DE {Deutsch}\n' + + 'ES {Spanisch}\n' + + 'FR {Französisch}\n' + + 'HU {Ungarisch}\n' + + 'IT {Italienisch}\n' + + 'NL {Niederländisch}\n' + + 'PL {Polnisch}\n' + + 'RO {Rumänisch}\n' + + 'other {{params}}\n' + + '} {fieldName} ist nicht erlaubt.', }, }; diff --git a/packages/input-iban/translations/en.js b/packages/input-iban/translations/en.js index 8357f20c6..4515a0a78 100644 --- a/packages/input-iban/translations/en.js +++ b/packages/input-iban/translations/en.js @@ -17,4 +17,19 @@ export default { 'other {{params}}\n' + '} {fieldName}.', }, + 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.', }; diff --git a/packages/input-iban/translations/es.js b/packages/input-iban/translations/es.js index d1b48c57d..2d236c789 100644 --- a/packages/input-iban/translations/es.js +++ b/packages/input-iban/translations/es.js @@ -16,5 +16,20 @@ export default { 'RO {Rumano}\n' + 'other {{params}}\n' + '}.', + IsNotCountryIBAN: + '{fieldName} {params, select,\n' + + 'AT {Austriaco}\n' + + 'BE {Belga}\n' + + 'CZ {Checo}\n' + + 'DE {Alemán}\n' + + 'ES {Español}\n' + + 'FR {Francés}\n' + + 'HU {Húngaro}\n' + + 'IT {Italiano}\n' + + 'NL {Neerlandés}\n' + + 'PL {Polaco}\n' + + 'RO {Rumano}\n' + + 'other {{params}}\n' + + '} no se permite.', }, }; diff --git a/packages/input-iban/translations/fr.js b/packages/input-iban/translations/fr.js index 9447c4044..94c212dd8 100644 --- a/packages/input-iban/translations/fr.js +++ b/packages/input-iban/translations/fr.js @@ -16,5 +16,20 @@ export default { 'RO {roumain}\n' + 'other {{params}}\n' + '} valide.', + IsNotCountryIBAN: + '{fieldName} {params, select,\n' + + 'AT {autrichien}\n' + + 'BE {belge}\n' + + 'CZ {tchèque}\n' + + 'DE {allemand}\n' + + 'ES {espagnol}\n' + + 'FR {français}\n' + + 'HU {hongrois}\n' + + 'IT {italien}\n' + + 'NL {néerlandais}\n' + + 'PL {polonais}\n' + + 'RO {roumain}\n' + + 'other {{params}}\n' + + "} n'est pas autorisé.", }, }; diff --git a/packages/input-iban/translations/hu.js b/packages/input-iban/translations/hu.js index d8b1bf34c..2b8a8123d 100644 --- a/packages/input-iban/translations/hu.js +++ b/packages/input-iban/translations/hu.js @@ -16,5 +16,20 @@ export default { 'RO {Román}\n' + 'other {{params}}\n' + '} {fieldName} értéket.', + IsNotCountryIBAN: + '{params, select,\n' + + 'AT {Osztrák}\n' + + 'BE {Belga}\n' + + 'CZ {Cseh}\n' + + 'DE {Német}\n' + + 'ES {Spanyol}\n' + + 'FR {Francia}\n' + + 'HU {Magyar}\n' + + 'IT {Olasz}\n' + + 'NL {Holland}\n' + + 'PL {Lengyel}\n' + + 'RO {Román}\n' + + 'other {{params}}\n' + + '} {fieldName} nem engedélyezett', }, }; diff --git a/packages/input-iban/translations/it.js b/packages/input-iban/translations/it.js index 3eac1ab3e..b1d79b545 100644 --- a/packages/input-iban/translations/it.js +++ b/packages/input-iban/translations/it.js @@ -16,5 +16,20 @@ export default { 'RO {Rumeno}\n' + 'other {{params}}\n' + '}.', + IsNotCountryIBAN: + '{fieldName} {params, select,\n' + + 'AT {Austriaco}\n' + + 'BE {Belga}\n' + + 'CZ {Ceco}\n' + + 'DE {Tedesco}\n' + + 'ES {Spagnolo}\n' + + 'FR {Francese}\n' + + 'HU {Ungherese}\n' + + 'IT {Italiano}\n' + + 'NL {Olandese}\n' + + 'PL {Polacco}\n' + + 'RO {Rumeno}\n' + + 'other {{params}}\n' + + '} non è permesso', }, }; diff --git a/packages/input-iban/translations/nl.js b/packages/input-iban/translations/nl.js index c2974e41c..360c47ea7 100644 --- a/packages/input-iban/translations/nl.js +++ b/packages/input-iban/translations/nl.js @@ -16,5 +16,20 @@ export default { 'RO {Roemeense}\n' + 'other {{params}}\n' + '} {fieldName} in.', + IsNotCountryIBAN: + '{params, select,\n' + + 'AT {Oostenrijkse}\n' + + 'BE {Belgische}\n' + + 'CZ {Tsjechische}\n' + + 'DE {Duitse}\n' + + 'ES {Spaanse}\n' + + 'FR {Franse}\n' + + 'HU {Hongaarse}\n' + + 'IT {Italiaanse}\n' + + 'NL {Nederlandse}\n' + + 'PL {Poolse}\n' + + 'RO {Roemeense}\n' + + 'other {{params}}\n' + + '} {fieldName} is niet toegestaan', }, }; diff --git a/packages/input-iban/translations/pl.js b/packages/input-iban/translations/pl.js index 22b2d6b29..33039a654 100644 --- a/packages/input-iban/translations/pl.js +++ b/packages/input-iban/translations/pl.js @@ -16,5 +16,20 @@ export default { 'RO {Rumuński}\n' + 'other {{params}}\n' + '} {fieldName}.', + IsNotCountryIBAN: + '{params, select,\n' + + 'AT {Austriacki}\n' + + 'BE {Belgijski}\n' + + 'CZ {Czeski}\n' + + 'DE {Niemiecki}\n' + + 'ES {Hiszpański}\n' + + 'FR {Francuski}\n' + + 'HU {Węgierski}\n' + + 'IT {Włoski}\n' + + 'NL {Holenderski}\n' + + 'PL {Polski}\n' + + 'RO {Rumuński}\n' + + 'other {{params}}\n' + + '} {fieldName} nie jest dozwolone', }, }; diff --git a/packages/input-iban/translations/ro.js b/packages/input-iban/translations/ro.js index c2be4d085..1b5efc4b0 100644 --- a/packages/input-iban/translations/ro.js +++ b/packages/input-iban/translations/ro.js @@ -16,5 +16,20 @@ export default { 'RO {românesc}\n' + 'other {{params}}\n' + '} valid(ă).', + IsNotCountryIBAN: + '{fieldName} {params, select,\n' + + 'AT {austriac}\n' + + 'BE {belgian}\n' + + 'CZ {ceh}\n' + + 'DE {german}\n' + + 'ES {spaniol}\n' + + 'FR {francez}\n' + + 'HU {maghiar}\n' + + 'IT {italian}\n' + + 'NL {olandez}\n' + + 'PL {polonez}\n' + + 'RO {românesc}\n' + + 'other {{params}}\n' + + '} nu este permis', }, }; diff --git a/packages/input-iban/translations/ru.js b/packages/input-iban/translations/ru.js index f6e3d2863..9f2fb1c02 100644 --- a/packages/input-iban/translations/ru.js +++ b/packages/input-iban/translations/ru.js @@ -16,5 +16,20 @@ export default { 'RO {Румынский}\n' + 'other {{params}}\n' + '} {fieldName}.', + IsNotCountryIBAN: + '{params, select,\n' + + 'AT {Австрийский}\n' + + 'BE {Бельгийский}\n' + + 'CZ {Чешский}\n' + + 'DE {Немецкий}\n' + + 'ES {Испанский}\n' + + 'FR {Французский}\n' + + 'HU {Венгерский}\n' + + 'IT {Итальянский}\n' + + 'NL {Нидерландский}\n' + + 'PL {Польский}\n' + + 'RO {Румынский}\n' + + 'other {{params}}\n' + + '} {fieldName} не допускается', }, }; diff --git a/packages/input-iban/translations/sk.js b/packages/input-iban/translations/sk.js index 5a9666020..72686b6ec 100644 --- a/packages/input-iban/translations/sk.js +++ b/packages/input-iban/translations/sk.js @@ -16,5 +16,20 @@ export default { 'RO {rumunský}\n' + 'other {{params}}\n' + '} kód {fieldName}.', + IsNotCountryIBAN: + '{params, select,\n' + + 'AT {rakúsky}\n' + + 'BE {belgický}\n' + + 'CZ {český}\n' + + 'DE {nemecký}\n' + + 'ES {španielsky}\n' + + 'FR {francúzsky}\n' + + 'HU {maďarský}\n' + + 'IT {taliansky}\n' + + 'NL {holandský}\n' + + 'PL {poľský}\n' + + 'RO {rumunský}\n' + + 'other {{params}}\n' + + '} {fieldName} nie je povolené', }, }; diff --git a/packages/input-iban/translations/uk.js b/packages/input-iban/translations/uk.js index 407b98375..c8fdbc5fa 100644 --- a/packages/input-iban/translations/uk.js +++ b/packages/input-iban/translations/uk.js @@ -16,5 +16,20 @@ export default { 'RO {румунська}\n' + 'other {{params}}\n' + '} {fieldName}.', + IsNotCountryIBAN: + '{params, select,\n' + + 'AT {австрійський}\n' + + 'BE {бельгійський}\n' + + 'CZ {чеський}\n' + + 'DE {німецький}\n' + + 'ES {іспанський}\n' + + 'FR {французький}\n' + + 'HU {угорський}\n' + + 'IT {італійський}\n' + + 'NL {голландський}\n' + + 'PL {польський}\n' + + 'RO {румунська}\n' + + 'other {{params}}\n' + + '} {fieldName} не дозволено', }, }; diff --git a/packages/input-iban/translations/zh.js b/packages/input-iban/translations/zh.js index a33889edc..e5036dbd2 100644 --- a/packages/input-iban/translations/zh.js +++ b/packages/input-iban/translations/zh.js @@ -16,5 +16,20 @@ export default { 'RO {罗马尼亚}\n' + '另一个 {{params}}\n' + '} {fieldName}。', + IsNotCountryIBAN: + '{params, select,\n' + + 'AT {奥}\n' + + 'BE {比利时的}\n' + + 'CZ {捷克}\n' + + 'DE {德语}\n' + + 'ES {西班牙语}\n' + + 'FR {法国}\n' + + 'HU {匈牙利}\n' + + 'IT {意大利}\n' + + 'NL {荷兰人}\n' + + 'PL {抛光}\n' + + 'RO {罗马尼亚}\n' + + '另一个 {{params}}\n' + + '} {fieldName} 不允許', }, }; From 1d52585a6e944aca0d147d4d29583222a3d77a9f Mon Sep 17 00:00:00 2001 From: Joren Broekema Date: Thu, 3 Dec 2020 17:01:20 +0100 Subject: [PATCH 2/4] chore: capitalize some translations as they are start of sentence --- packages/input-iban/translations/hu.js | 2 +- packages/input-iban/translations/it.js | 2 +- packages/input-iban/translations/nl.js | 2 +- packages/input-iban/translations/pl.js | 2 +- packages/input-iban/translations/ro.js | 2 +- packages/input-iban/translations/ru.js | 2 +- packages/input-iban/translations/sk.js | 24 ++++++++++++------------ packages/input-iban/translations/uk.js | 24 ++++++++++++------------ packages/input-iban/translations/zh.js | 2 +- 9 files changed, 31 insertions(+), 31 deletions(-) diff --git a/packages/input-iban/translations/hu.js b/packages/input-iban/translations/hu.js index 2b8a8123d..f9c46e527 100644 --- a/packages/input-iban/translations/hu.js +++ b/packages/input-iban/translations/hu.js @@ -30,6 +30,6 @@ export default { 'PL {Lengyel}\n' + 'RO {Román}\n' + 'other {{params}}\n' + - '} {fieldName} nem engedélyezett', + '} {fieldName} nem engedélyezett.', }, }; diff --git a/packages/input-iban/translations/it.js b/packages/input-iban/translations/it.js index b1d79b545..aebd7518e 100644 --- a/packages/input-iban/translations/it.js +++ b/packages/input-iban/translations/it.js @@ -30,6 +30,6 @@ export default { 'PL {Polacco}\n' + 'RO {Rumeno}\n' + 'other {{params}}\n' + - '} non è permesso', + '} non è permesso.', }, }; diff --git a/packages/input-iban/translations/nl.js b/packages/input-iban/translations/nl.js index 360c47ea7..bcfabe1a6 100644 --- a/packages/input-iban/translations/nl.js +++ b/packages/input-iban/translations/nl.js @@ -30,6 +30,6 @@ export default { 'PL {Poolse}\n' + 'RO {Roemeense}\n' + 'other {{params}}\n' + - '} {fieldName} is niet toegestaan', + '} {fieldName} is niet toegestaan.', }, }; diff --git a/packages/input-iban/translations/pl.js b/packages/input-iban/translations/pl.js index 33039a654..ce3be3864 100644 --- a/packages/input-iban/translations/pl.js +++ b/packages/input-iban/translations/pl.js @@ -30,6 +30,6 @@ export default { 'PL {Polski}\n' + 'RO {Rumuński}\n' + 'other {{params}}\n' + - '} {fieldName} nie jest dozwolone', + '} {fieldName} nie jest dozwolone.', }, }; diff --git a/packages/input-iban/translations/ro.js b/packages/input-iban/translations/ro.js index 1b5efc4b0..8c94daf78 100644 --- a/packages/input-iban/translations/ro.js +++ b/packages/input-iban/translations/ro.js @@ -30,6 +30,6 @@ export default { 'PL {polonez}\n' + 'RO {românesc}\n' + 'other {{params}}\n' + - '} nu este permis', + '} nu este permis.', }, }; diff --git a/packages/input-iban/translations/ru.js b/packages/input-iban/translations/ru.js index 9f2fb1c02..d89476063 100644 --- a/packages/input-iban/translations/ru.js +++ b/packages/input-iban/translations/ru.js @@ -30,6 +30,6 @@ export default { 'PL {Польский}\n' + 'RO {Румынский}\n' + 'other {{params}}\n' + - '} {fieldName} не допускается', + '} {fieldName} не допускается.', }, }; diff --git a/packages/input-iban/translations/sk.js b/packages/input-iban/translations/sk.js index 72686b6ec..9e5e7ca4c 100644 --- a/packages/input-iban/translations/sk.js +++ b/packages/input-iban/translations/sk.js @@ -18,18 +18,18 @@ export default { '} kód {fieldName}.', IsNotCountryIBAN: '{params, select,\n' + - 'AT {rakúsky}\n' + - 'BE {belgický}\n' + - 'CZ {český}\n' + - 'DE {nemecký}\n' + - 'ES {španielsky}\n' + - 'FR {francúzsky}\n' + - 'HU {maďarský}\n' + - 'IT {taliansky}\n' + - 'NL {holandský}\n' + - 'PL {poľský}\n' + - 'RO {rumunský}\n' + + 'AT {Rakúsky}\n' + + 'BE {Belgický}\n' + + 'CZ {Český}\n' + + 'DE {Nemecký}\n' + + 'ES {Španielsky}\n' + + 'FR {Francúzsky}\n' + + 'HU {Maďarský}\n' + + 'IT {Taliansky}\n' + + 'NL {Holandský}\n' + + 'PL {Poľský}\n' + + 'RO {Rumunský}\n' + 'other {{params}}\n' + - '} {fieldName} nie je povolené', + '} {fieldName} nie je povolené.', }, }; diff --git a/packages/input-iban/translations/uk.js b/packages/input-iban/translations/uk.js index c8fdbc5fa..583d6d2a5 100644 --- a/packages/input-iban/translations/uk.js +++ b/packages/input-iban/translations/uk.js @@ -18,18 +18,18 @@ export default { '} {fieldName}.', IsNotCountryIBAN: '{params, select,\n' + - 'AT {австрійський}\n' + - 'BE {бельгійський}\n' + - 'CZ {чеський}\n' + - 'DE {німецький}\n' + - 'ES {іспанський}\n' + - 'FR {французький}\n' + - 'HU {угорський}\n' + - 'IT {італійський}\n' + - 'NL {голландський}\n' + - 'PL {польський}\n' + - 'RO {румунська}\n' + + 'AT {Австрійський}\n' + + 'BE {Бельгійський}\n' + + 'CZ {Чеський}\n' + + 'DE {Німецький}\n' + + 'ES {Iспанський}\n' + + 'FR {Французький}\n' + + 'HU {Угорський}\n' + + 'IT {Iталійський}\n' + + 'NL {Голландський}\n' + + 'PL {Польський}\n' + + 'RO {Румунська}\n' + 'other {{params}}\n' + - '} {fieldName} не дозволено', + '} {fieldName} не дозволено.', }, }; diff --git a/packages/input-iban/translations/zh.js b/packages/input-iban/translations/zh.js index e5036dbd2..f39bdee38 100644 --- a/packages/input-iban/translations/zh.js +++ b/packages/input-iban/translations/zh.js @@ -30,6 +30,6 @@ export default { 'PL {抛光}\n' + 'RO {罗马尼亚}\n' + '另一个 {{params}}\n' + - '} {fieldName} 不允許', + '} {fieldName} 不允許。', }, }; From a45064696db677853212b7f0fafbdc11d43c4750 Mon Sep 17 00:00:00 2001 From: Joren Broekema Date: Thu, 3 Dec 2020 18:24:37 +0100 Subject: [PATCH 3/4] chore: allow array for IsNotCountryIBAN --- packages/input-iban/README.md | 8 +++--- packages/input-iban/src/validators.js | 28 ++++++++++++++----- packages/input-iban/test/validators.test.js | 17 +++++++++++- packages/input-iban/translations/bg.js | 4 +-- packages/input-iban/translations/cs.js | 4 +-- packages/input-iban/translations/de.js | 4 +-- packages/input-iban/translations/en.js | 30 ++++++++++----------- packages/input-iban/translations/es.js | 4 +-- packages/input-iban/translations/fr.js | 4 +-- packages/input-iban/translations/hu.js | 4 +-- packages/input-iban/translations/it.js | 4 +-- packages/input-iban/translations/nl.js | 4 +-- packages/input-iban/translations/pl.js | 4 +-- packages/input-iban/translations/ro.js | 4 +-- packages/input-iban/translations/ru.js | 4 +-- packages/input-iban/translations/sk.js | 4 +-- packages/input-iban/translations/uk.js | 4 +-- packages/input-iban/translations/zh.js | 4 +-- 18 files changed, 85 insertions(+), 54 deletions(-) diff --git a/packages/input-iban/README.md b/packages/input-iban/README.md index 286aadbc3..d4a5db9be 100644 --- a/packages/input-iban/README.md +++ b/packages/input-iban/README.md @@ -8,7 +8,7 @@ Its purpose is to provide a way for users to fill in an IBAN (International Bank ```js script import { html } from 'lit-html'; import { loadDefaultFeedbackMessages } from '@lion/validate-messages'; -import { IsCountryIBAN } from './src/validators.js'; +import { IsCountryIBAN, IsNotCountryIBAN } from './src/validators.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. +You can pass a single string value, or an array of strings. + ```js preview-story export const blacklistedCountry = () => html` diff --git a/packages/input-iban/src/validators.js b/packages/input-iban/src/validators.js index 8593472f1..41d0d4c2b 100644 --- a/packages/input-iban/src/validators.js +++ b/packages/input-iban/src/validators.js @@ -124,7 +124,7 @@ export class IsCountryIBAN extends IsIBAN { } /** - * @param {?} [value] + * @param {string} value * @returns {Boolean} */ execute(value) { @@ -160,18 +160,25 @@ export class IsNotCountryIBAN extends IsIBAN { } /** - * @param {?} [value] + * @param {string} value * @returns {Boolean} */ execute(value) { + let isInvalid = false; 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) { - return true; + isInvalid = true; } - return false; + return isInvalid; } /** @@ -186,6 +193,13 @@ export class IsNotCountryIBAN extends IsIBAN { */ static async getMessage(data) { 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); } } diff --git a/packages/input-iban/test/validators.test.js b/packages/input-iban/test/validators.test.js index 0b20d5ce5..2c6e6e07e 100644 --- a/packages/input-iban/test/validators.test.js +++ b/packages/input-iban/test/validators.test.js @@ -9,6 +9,7 @@ describe('IBAN validation', () => { const validator = new IsIBAN(); expect(validator.execute('NL17INGB0002822608')).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', () => { @@ -17,7 +18,7 @@ describe('IBAN validation', () => { expect(nlValidator.execute('NL17INGB0002822608')).to.be.false; expect(deValidator.execute('DE89370400440532013000')).to.be.false; 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', () => { @@ -28,4 +29,18 @@ describe('IBAN validation', () => { expect(nlValidator.execute('DE89370400440532013000')).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; + }); }); diff --git a/packages/input-iban/translations/bg.js b/packages/input-iban/translations/bg.js index 6fbd20db9..e4ac23695 100644 --- a/packages/input-iban/translations/bg.js +++ b/packages/input-iban/translations/bg.js @@ -17,7 +17,7 @@ export default { 'other {{params}}\n' + '} {fieldName}.', IsNotCountryIBAN: - '{params, select,\n' + + '{userSuppliedCountryCode, select,\n' + 'AT {Австрийски}\n' + 'BE {Белгийски}\n' + 'CZ {Чешки}\n' + @@ -29,7 +29,7 @@ export default { 'NL {Нидерландски}\n' + 'PL {Полски}\n' + 'RO {Румънски}\n' + - 'other {{params}}\n' + + 'other {{userSuppliedCountryCode}}\n' + '} {fieldName} не е позволено.', }, }; diff --git a/packages/input-iban/translations/cs.js b/packages/input-iban/translations/cs.js index 56ce59f19..440785373 100644 --- a/packages/input-iban/translations/cs.js +++ b/packages/input-iban/translations/cs.js @@ -17,7 +17,7 @@ export default { 'other {{params}}\n' + '} {fieldName}.', IsNotCountryIBAN: - '{params, select,\n' + + '{userSuppliedCountryCode, select,\n' + 'AT {Rakušan}\n' + 'BE {Belgičan}\n' + 'CZ {Čech}\n' + @@ -29,7 +29,7 @@ export default { 'NL {Holanďan}\n' + 'PL {Polák}\n' + 'RO {Rumun}\n' + - 'other {{params}}\n' + + 'other {{userSuppliedCountryCode}}\n' + '} {fieldName} není povoleno.', }, }; diff --git a/packages/input-iban/translations/de.js b/packages/input-iban/translations/de.js index c8a49f079..3b3e60e05 100644 --- a/packages/input-iban/translations/de.js +++ b/packages/input-iban/translations/de.js @@ -17,7 +17,7 @@ export default { 'other {{params}}\n' + '} {fieldName} ein.', IsNotCountryIBAN: - '{params, select,\n' + + '{userSuppliedCountryCode, select,\n' + 'AT {Österreichisch}\n' + 'BE {Belgisch}\n' + 'CZ {Tschechisch}\n' + @@ -29,7 +29,7 @@ export default { 'NL {Niederländisch}\n' + 'PL {Polnisch}\n' + 'RO {Rumänisch}\n' + - 'other {{params}}\n' + + 'other {{userSuppliedCountryCode}}\n' + '} {fieldName} ist nicht erlaubt.', }, }; diff --git a/packages/input-iban/translations/en.js b/packages/input-iban/translations/en.js index 4515a0a78..f02792778 100644 --- a/packages/input-iban/translations/en.js +++ b/packages/input-iban/translations/en.js @@ -16,20 +16,20 @@ export default { 'RO {Romanian}\n' + 'other {{params}}\n' + '} {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.', }; diff --git a/packages/input-iban/translations/es.js b/packages/input-iban/translations/es.js index 2d236c789..cfd2fef8c 100644 --- a/packages/input-iban/translations/es.js +++ b/packages/input-iban/translations/es.js @@ -17,7 +17,7 @@ export default { 'other {{params}}\n' + '}.', IsNotCountryIBAN: - '{fieldName} {params, select,\n' + + '{fieldName} {userSuppliedCountryCode, select,\n' + 'AT {Austriaco}\n' + 'BE {Belga}\n' + 'CZ {Checo}\n' + @@ -29,7 +29,7 @@ export default { 'NL {Neerlandés}\n' + 'PL {Polaco}\n' + 'RO {Rumano}\n' + - 'other {{params}}\n' + + 'other {{userSuppliedCountryCode}}\n' + '} no se permite.', }, }; diff --git a/packages/input-iban/translations/fr.js b/packages/input-iban/translations/fr.js index 94c212dd8..87bc00c4d 100644 --- a/packages/input-iban/translations/fr.js +++ b/packages/input-iban/translations/fr.js @@ -17,7 +17,7 @@ export default { 'other {{params}}\n' + '} valide.', IsNotCountryIBAN: - '{fieldName} {params, select,\n' + + '{fieldName} {userSuppliedCountryCode, select,\n' + 'AT {autrichien}\n' + 'BE {belge}\n' + 'CZ {tchèque}\n' + @@ -29,7 +29,7 @@ export default { 'NL {néerlandais}\n' + 'PL {polonais}\n' + 'RO {roumain}\n' + - 'other {{params}}\n' + + 'other {{userSuppliedCountryCode}}\n' + "} n'est pas autorisé.", }, }; diff --git a/packages/input-iban/translations/hu.js b/packages/input-iban/translations/hu.js index f9c46e527..1fd205995 100644 --- a/packages/input-iban/translations/hu.js +++ b/packages/input-iban/translations/hu.js @@ -17,7 +17,7 @@ export default { 'other {{params}}\n' + '} {fieldName} értéket.', IsNotCountryIBAN: - '{params, select,\n' + + '{userSuppliedCountryCode, select,\n' + 'AT {Osztrák}\n' + 'BE {Belga}\n' + 'CZ {Cseh}\n' + @@ -29,7 +29,7 @@ export default { 'NL {Holland}\n' + 'PL {Lengyel}\n' + 'RO {Román}\n' + - 'other {{params}}\n' + + 'other {{userSuppliedCountryCode}}\n' + '} {fieldName} nem engedélyezett.', }, }; diff --git a/packages/input-iban/translations/it.js b/packages/input-iban/translations/it.js index aebd7518e..b87a32711 100644 --- a/packages/input-iban/translations/it.js +++ b/packages/input-iban/translations/it.js @@ -17,7 +17,7 @@ export default { 'other {{params}}\n' + '}.', IsNotCountryIBAN: - '{fieldName} {params, select,\n' + + '{fieldName} {userSuppliedCountryCode, select,\n' + 'AT {Austriaco}\n' + 'BE {Belga}\n' + 'CZ {Ceco}\n' + @@ -29,7 +29,7 @@ export default { 'NL {Olandese}\n' + 'PL {Polacco}\n' + 'RO {Rumeno}\n' + - 'other {{params}}\n' + + 'other {{userSuppliedCountryCode}}\n' + '} non è permesso.', }, }; diff --git a/packages/input-iban/translations/nl.js b/packages/input-iban/translations/nl.js index bcfabe1a6..edd102eba 100644 --- a/packages/input-iban/translations/nl.js +++ b/packages/input-iban/translations/nl.js @@ -17,7 +17,7 @@ export default { 'other {{params}}\n' + '} {fieldName} in.', IsNotCountryIBAN: - '{params, select,\n' + + '{userSuppliedCountryCode, select,\n' + 'AT {Oostenrijkse}\n' + 'BE {Belgische}\n' + 'CZ {Tsjechische}\n' + @@ -29,7 +29,7 @@ export default { 'NL {Nederlandse}\n' + 'PL {Poolse}\n' + 'RO {Roemeense}\n' + - 'other {{params}}\n' + + 'other {{userSuppliedCountryCode}}\n' + '} {fieldName} is niet toegestaan.', }, }; diff --git a/packages/input-iban/translations/pl.js b/packages/input-iban/translations/pl.js index ce3be3864..303269a59 100644 --- a/packages/input-iban/translations/pl.js +++ b/packages/input-iban/translations/pl.js @@ -17,7 +17,7 @@ export default { 'other {{params}}\n' + '} {fieldName}.', IsNotCountryIBAN: - '{params, select,\n' + + '{userSuppliedCountryCode, select,\n' + 'AT {Austriacki}\n' + 'BE {Belgijski}\n' + 'CZ {Czeski}\n' + @@ -29,7 +29,7 @@ export default { 'NL {Holenderski}\n' + 'PL {Polski}\n' + 'RO {Rumuński}\n' + - 'other {{params}}\n' + + 'other {{userSuppliedCountryCode}}\n' + '} {fieldName} nie jest dozwolone.', }, }; diff --git a/packages/input-iban/translations/ro.js b/packages/input-iban/translations/ro.js index 8c94daf78..bec11d061 100644 --- a/packages/input-iban/translations/ro.js +++ b/packages/input-iban/translations/ro.js @@ -17,7 +17,7 @@ export default { 'other {{params}}\n' + '} valid(ă).', IsNotCountryIBAN: - '{fieldName} {params, select,\n' + + '{fieldName} {userSuppliedCountryCode, select,\n' + 'AT {austriac}\n' + 'BE {belgian}\n' + 'CZ {ceh}\n' + @@ -29,7 +29,7 @@ export default { 'NL {olandez}\n' + 'PL {polonez}\n' + 'RO {românesc}\n' + - 'other {{params}}\n' + + 'other {{userSuppliedCountryCode}}\n' + '} nu este permis.', }, }; diff --git a/packages/input-iban/translations/ru.js b/packages/input-iban/translations/ru.js index d89476063..5986d9f57 100644 --- a/packages/input-iban/translations/ru.js +++ b/packages/input-iban/translations/ru.js @@ -17,7 +17,7 @@ export default { 'other {{params}}\n' + '} {fieldName}.', IsNotCountryIBAN: - '{params, select,\n' + + '{userSuppliedCountryCode, select,\n' + 'AT {Австрийский}\n' + 'BE {Бельгийский}\n' + 'CZ {Чешский}\n' + @@ -29,7 +29,7 @@ export default { 'NL {Нидерландский}\n' + 'PL {Польский}\n' + 'RO {Румынский}\n' + - 'other {{params}}\n' + + 'other {{userSuppliedCountryCode}}\n' + '} {fieldName} не допускается.', }, }; diff --git a/packages/input-iban/translations/sk.js b/packages/input-iban/translations/sk.js index 9e5e7ca4c..f568fecbe 100644 --- a/packages/input-iban/translations/sk.js +++ b/packages/input-iban/translations/sk.js @@ -17,7 +17,7 @@ export default { 'other {{params}}\n' + '} kód {fieldName}.', IsNotCountryIBAN: - '{params, select,\n' + + '{userSuppliedCountryCode, select,\n' + 'AT {Rakúsky}\n' + 'BE {Belgický}\n' + 'CZ {Český}\n' + @@ -29,7 +29,7 @@ export default { 'NL {Holandský}\n' + 'PL {Poľský}\n' + 'RO {Rumunský}\n' + - 'other {{params}}\n' + + 'other {{userSuppliedCountryCode}}\n' + '} {fieldName} nie je povolené.', }, }; diff --git a/packages/input-iban/translations/uk.js b/packages/input-iban/translations/uk.js index 583d6d2a5..2842bc3a7 100644 --- a/packages/input-iban/translations/uk.js +++ b/packages/input-iban/translations/uk.js @@ -17,7 +17,7 @@ export default { 'other {{params}}\n' + '} {fieldName}.', IsNotCountryIBAN: - '{params, select,\n' + + '{userSuppliedCountryCode, select,\n' + 'AT {Австрійський}\n' + 'BE {Бельгійський}\n' + 'CZ {Чеський}\n' + @@ -29,7 +29,7 @@ export default { 'NL {Голландський}\n' + 'PL {Польський}\n' + 'RO {Румунська}\n' + - 'other {{params}}\n' + + 'other {{userSuppliedCountryCode}}\n' + '} {fieldName} не дозволено.', }, }; diff --git a/packages/input-iban/translations/zh.js b/packages/input-iban/translations/zh.js index f39bdee38..1d6a1e76e 100644 --- a/packages/input-iban/translations/zh.js +++ b/packages/input-iban/translations/zh.js @@ -17,7 +17,7 @@ export default { '另一个 {{params}}\n' + '} {fieldName}。', IsNotCountryIBAN: - '{params, select,\n' + + '{userSuppliedCountryCode, select,\n' + 'AT {奥}\n' + 'BE {比利时的}\n' + 'CZ {捷克}\n' + @@ -29,7 +29,7 @@ export default { 'NL {荷兰人}\n' + 'PL {抛光}\n' + 'RO {罗马尼亚}\n' + - '另一个 {{params}}\n' + + '另一个 {{userSuppliedCountryCode}}\n' + '} {fieldName} 不允許。', }, }; From 983bb2fd239eaf11da9380d9528da2dca29c66e5 Mon Sep 17 00:00:00 2001 From: Joren Broekema Date: Thu, 3 Dec 2020 19:02:40 +0100 Subject: [PATCH 4/4] fix: display IsIBAN message for country validators if Unparseable --- packages/input-iban/src/validators.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/input-iban/src/validators.js b/packages/input-iban/src/validators.js index 41d0d4c2b..efa958b19 100644 --- a/packages/input-iban/src/validators.js +++ b/packages/input-iban/src/validators.js @@ -1,7 +1,7 @@ /* eslint-disable max-classes-per-file */ import { localize } from '@lion/localize'; -import { Validator } from '@lion/form-core'; +import { Unparseable, Validator } from '@lion/form-core'; import { isValidIBAN } from 'ibantools'; let loaded = false; @@ -150,7 +150,10 @@ export class IsCountryIBAN extends IsIBAN { */ static async getMessage(data) { await loadTranslations(); - return localize.msg('lion-validate+iban:error.IsCountryIBAN', data); + // 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) + : localize.msg('lion-validate+iban:error.IsCountryIBAN', data); } } @@ -196,10 +199,12 @@ export class IsNotCountryIBAN extends IsIBAN { const _data = { ...data, userSuppliedCountryCode: - typeof data?.modelValue === 'string' - ? data?.modelValue.slice(0, 2) - : data?.modelValue.viewValue.slice(0, 2), + typeof data?.modelValue === 'string' ? data?.modelValue.slice(0, 2) : '', }; - return localize.msg('lion-validate+iban:error.IsNotCountryIBAN', _data); + + // 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) + : localize.msg('lion-validate+iban:error.IsNotCountryIBAN', _data); } }