fix: display IsIBAN message for country validators if Unparseable

This commit is contained in:
Joren Broekema 2020-12-03 19:02:40 +01:00
parent a45064696d
commit 983bb2fd23

View file

@ -1,7 +1,7 @@
/* eslint-disable max-classes-per-file */ /* eslint-disable max-classes-per-file */
import { localize } from '@lion/localize'; import { localize } from '@lion/localize';
import { Validator } from '@lion/form-core'; import { Unparseable, Validator } from '@lion/form-core';
import { isValidIBAN } from 'ibantools'; import { isValidIBAN } from 'ibantools';
let loaded = false; let loaded = false;
@ -150,7 +150,10 @@ export class IsCountryIBAN extends IsIBAN {
*/ */
static async getMessage(data) { static async getMessage(data) {
await loadTranslations(); 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 = { const _data = {
...data, ...data,
userSuppliedCountryCode: userSuppliedCountryCode:
typeof data?.modelValue === 'string' typeof data?.modelValue === 'string' ? data?.modelValue.slice(0, 2) : '',
? data?.modelValue.slice(0, 2)
: data?.modelValue.viewValue.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);
} }
} }