IbanNet

repository·main·Indexed 41 days ago

https://github.com/skwasjer/ibannet

A .NET library for validating, parsing, and constructing International Bank Account Numbers (IBAN). It provides a strongly-typed Iban primitive, an IIbanValidator for validation, and an IIbanParser for conversion. The library includes integrations for ASP.NET Core via DataAnnotations, FluentValidation, and dependency injection support for Autofac and Microsoft.Extensions.DependencyInjection. It supports 126 countries based on the SWIFT registry, with optional support for unofficial countries via the WikipediaRegistryProvider.

Tokens
2K
Snippets
9
Records
13
Agent score
100%

What's inside IbanNet

  1. Integrate IbanNet with Microsoft.Extensions.DependencyInjection

    main

    To use IbanNet within a .NET application using Microsoft.Extensions.DependencyInjection, install the IbanNet.DependencyInjection.ServiceProvider package and register the services in your service collection. This provides access to IBAN validation, parsing, registry management, and generation via dependency injection.

    // Basic registration
    services.AddIbanNet();
    
    // Registration with custom configuration
    services.AddIbanNet(opts => opts
        .UseRegistryProvider(new SwiftRegistryProvider())
        .WithRule<MyCustomRule>()
    );
  2. Validate IBANs using FluentValidation

    main

    To validate IBAN user input within a FluentValidation rule, use the Iban() extension method on a property rule. This method requires an instance of IIbanValidator to be passed into your validator's constructor.

    Implementation Steps

    1. Define your model containing the IBAN string.
    2. Create a validator inheriting from AbstractValidator<T>.
    3. Inject IIbanValidator into the constructor.
    4. Apply .Iban(ibanValidator) to the relevant property rule.
    public class InputModel
    {
        public string BackAccountNumber { get; set; }
    }
    
    public class InputModelValidator : AbstractValidator<InputModel>
    {
        public InputModelValidator(IIbanValidator ibanValidator)
        {
            RuleFor(x => x.BankAccountNumber).NotNull().Iban(ibanValidator);
        }
    }
  3. Run the IbanNet ASP.NET Core example web app

    main

    The IbanNet example web app demonstrates how to integrate IbanNet validation into different ASP.NET Core hosting models, including RazorPages, MVC controllers, and API controllers.

    To run the application, you must select one of the available Debug configurations in your IDE (such as Visual Studio) to choose which validation framework you want to test with IbanNet:

    1. Debug DataAnnotations: Tests integration with standard .NET DataAnnotations.
    2. Debug FluentValidation: Tests integration with the FluentValidation library.

    After selecting a configuration, run or debug the web application as usual.

    # Select a Debug-configuration and then run/debug the web application.
    # Options:
    # - Debug DataAnnotations
    # - Debug FluentValidation
  4. Enable unofficial countries via WikipediaRegistryProvider

    main
    By default, IbanNet only enables countries listed in the official SWIFT registry. To include extra (unofficial) countries sourced from Wikipedia (such as Angola, Benin, or Morocco), you must explicitly enable the WikipediaRegistryProvider in your configuration. Check the project documentation for the specific implementation steps to integrate this provider.
  5. Register IbanNet and FluentValidation in .NET Core

    main

    Before using the FluentValidation integration, you must register the IbanNet services and your validators in the .NET Core dependency injection container. Ensure you call AddIbanNet() to provide the necessary IIbanValidator implementation.

    services.AddIbanNet();
    services.AddTransient<IValidator<InputModel>, InputModelValidator>();
    services.AddFluentValidation();
  6. Validate IBAN input using IbanNet.DataAnnotations

    main

    Use the [Iban] attribute to enable automatic IBAN validation for properties within your models. This attribute is compatible with ASP.NET (Core) and Microsoft Data Annotations. When used in a controller, the validation logic is automatically triggered during model binding, and the result is accessible via ModelState.IsValid.

    public class InputModel
    {
        [Iban]
        [Required]
        public string BackAccountNumber { get; set; }
    }
    
    [ApiController]
    public class MyController : ControllerBase
    {
        [HttpPost]
        public ActionResult Save(InputModel model)
        {
            if (ModelState.IsValid)
            {
                // ..
            }
        }
    }
  7. Parse an IBAN using the Iban type and IIbanParser

    main

    To convert a string into a strongly-typed Iban object, use the IIbanParser interface. The IbanParser constructor typically accepts an IbanRegistry (such as IbanRegistry.Default). The TryParse method handles strings with or without spaces and returns a boolean indicating success. Once parsed, you can format the Iban object using different IbanFormat options, such as IbanFormat.Obfuscated.

    Iban iban;
    IIbanParser parser = new IbanParser(IbanRegistry.Default);
    bool success = parser.TryParse("NL91 ABNA 0417 1643 00", out iban);
    if (success)
    {
        Console.WriteLine(iban.ToString(IbanFormat.Obfuscated)); // XXXXXXXXXXXXXX4300
    }
  8. Validate an IBAN using IIbanValidator

    main

    You can validate an International Bank Account Number (IBAN) by implementing the IIbanValidator interface. Use the IbanValidator class to perform the validation and check the IsValid property of the returned ValidationResult object.

    IIbanValidator validator = new IbanValidator();
    ValidationResult validationResult = validator.Validate("NL91ABNA0417164300");
    if (validationResult.IsValid)
    {
        // ..
    }