FluentValidation integration for the PhoneNumber value object, providing dedicated validators and rules to ensure E.164 compliant phone numbers.
$ dotnet add package PosInformatique.Foundations.PhoneNumbers.FluentValidationThis package provides FluentValidation integration for the PhoneNumber value object
from PosInformatique.Foundations.PhoneNumbers.
It adds a dedicated validator and extension method to validate that string properties contain valid phone numbers
in E.164 format, using the same parsing and validation logic as the core PhoneNumber type.
You can install the package from NuGet:
dotnet add package PosInformatique.Foundations.PhoneNumbers.FluentValidation
MustBePhoneNumber() for string propertiesPhoneNumber.IsValid() logic (E.164 format)null values are considered valid by default (combine with NotNull() / NotEmpty() when needed)PhoneNumber value object everywhereusing FluentValidation;
public sealed class ContactDto
{
public string Name { get; set; } = default!;
public string? Mobile { get; set; }
}
public sealed class ContactDtoValidator : AbstractValidator<ContactDto>
{
public ContactDtoValidator()
{
RuleFor(x => x.Mobile)
.MustBePhoneNumber(); // Validates Mobile as an E.164 phone number (or null)
}
}
Mobile is null, the rule passes.Mobile is not null, it must be a valid phone number in E.164 format, otherwise validation fails with the default message:
"'Mobile' must be a valid phone number in E.164 format."If you want to make the phone number mandatory, combine MustBePhoneNumber() with standard FluentValidation rules:
public sealed class RequiredContactDtoValidator : AbstractValidator<ContactDto>
{
public RequiredContactDtoValidator()
{
RuleFor(x => x.Mobile)
.NotEmpty()
.MustBePhoneNumber();
}
}
This enforces:
Mobile is not null or empty.Mobile must be a valid phone number in E.164 format.