Skip to content

IbanNet.FluentValidation

skwasjer edited this page Feb 28, 2020 · 10 revisions

The IbanNet.FluentValidation package provides FluentValidation support to validate IBAN user input.

Example model

Assuming we have a string property for the IBAN.

public class InputModel
{
    public string BackAccountNumber { get; set; }
}

Create a new validator

using FluentValidation;
using IbanNet;
using IbanNet.FluentValidation;

public class InputModelValidator : AbstractValidator<InputModel>
{
    public InputModelValidator(IIbanValidator ibanValidator)
    {
        RuleFor(x => x.BankAccountNumber).Iban(ibanValidator);
    }
}

Register the validator

Make sure to register:

  • IbanNet
  • the validator
  • FluentValidation

Not required

The .Iban() validator extension method does not require a property to be set, and as such a null value will be validated successfully. An empty string property however IS considered invalid. To disallow null values chain with the built-in FluentValidation .NotNull() extension.

.NET Core example

services.AddIbanNet();
services.AddTransient<IValidator<InputModel>, InputModelValidator>()
services.AddFluentValidation();