-
Notifications
You must be signed in to change notification settings - Fork 33
Options
The default options are fine for most use cases. IbanNet however does have a limited set of options:
IbanNet is by default using rules as defined by the SWIFT registry to validate strictly according to the ISO 13616 standard. SWIFT is the official registration authority for national IBAN formats.
Disclaimer: IbanNet is not endorsed or sponsored by SWIFT, or in any way affiliated. This library merely uses the specification as provided publicly by SWIFT.
The (default) registry containing the SWIFT specification can be used in code by accessing the static IbanRegistry.Default
instance. It represents a dictionary of specifications keyed by country code.
IbanNet also supplies a registry provider based on data from Wikipedia. The patterns defined on Wikipedia however are less exact in comparison to SWIFT, and some information is missing. This library only offers this provider as an alternative or supplement, and should only be used in cases where you really need have support for countries that are listed on Wikipedia, but not officially acknowledged by SWIFT.
Configure (any) additional providers through the options, after adding the primary SwiftRegistryProvider
:
var validator = new IbanValidator(new IbanValidatorOptions
{
Registry = new IbanRegistry
{
Providers =
{
new SwiftRegistryProvider(), // Preferred
new WikipediaRegistryProvider() // If country not available in SWIFT, use Wikipedia.
}
}
});
If multiple providers are registered, IbanNet will pick the ruleset for a specific country from the first provider that supports the given country.
If you wish to extend or override validation patterns defined by the built-in providers, implement IIbanRegistryProvider
. For examples of such an implementation, you can look at the source code of the built-in providers.
To apply additional validation logic, implement IIbanValidationRule
and a custom error result. Custom rules will always be executed 'after' built-in validation has succeeded, so in that respect you can be sure the IBAN is valid when the rule executes. What extra logic is applied is up to you.
Use cases could be to verify banks, branch codes, etc.:
class MyCustomEror : ErrorResult
{
public MyCustomEror()
: base($"Something was wrong.")
{
}
}
class MyCustomRule : IIbanValidationRule
{
public ValidationRuleResult Validate(ValidationRuleContext context)
{
if (context.Value == "xxx")
{
// Return custom error if not valid.
return new MyCystomError();
}
// Success.
return ValidationRuleResult.Success;
}
}
Register the rule through the options:
var validator = new IbanValidator(new IbanValidatorOptions
{
Rules =
{
new MyCustomRule()
}
});
IbanNet also has a few built-in additional rules that are not active by default, but you can register in the same way:
-
AcceptCountryRule
accepts IBAN's from specific countries and rejects all others. -
RejectCountryRule
rejects IBAN's from specific countries and accepts all others.