This 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- FluentValidation integration for phone number validation
- Extension method
MustBePhoneNumber()forstringproperties - Validation based on the core
PhoneNumber.IsValid()logic (E.164 format) nullvalues are considered valid by default (combine withNotNull()/NotEmpty()when needed)- Consistent validation rules across your application
- Validate incoming DTOs or commands that contain phone numbers as strings
- Ensure only valid E.164 phone numbers are accepted at the boundaries of your system
- Reuse the same validation logic used by the
PhoneNumbervalue object everywhere
using 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)
}
}- If
Mobileisnull, the rule passes. - If
Mobileis notnull, 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:
Mobileis notnullor empty.Mobilemust be a valid phone number in E.164 format.