This package provides a FluentValidation extension for validating email addresses using the EmailAddress value object.
It ensures that only valid RFC 5322 compliant email addresses are accepted when validating string properties.
You can install the package from NuGet:
dotnet add package PosInformatique.Foundations.EmailAddresses.FluentValidationThis package depends on the base package PosInformatique.Foundations.EmailAddresses.
- FluentValidation extension for email address validation
- Uses the same parsing and validation rules as the EmailAddress value object
- Clear and consistent error messages
nullvalues are accepted (combine withNotNull()validator to forbid nulls)
using FluentValidation;
public class User
{
public string Email { get; set; }
}
public class UserValidator : AbstractValidator<User>
{
public UserValidator()
{
RuleFor(u => u.Email).MustBeEmailAddress();
}
}var validator = new UserValidator();
// Valid, because null is ignored
var result1 = validator.Validate(new User { Email = null });
Console.WriteLine(result1.IsValid); // True
// Valid, because it's a valid email
var result2 = validator.Validate(new User { Email = "alice@company.com" });
Console.WriteLine(result2.IsValid); // True
// Invalid, because it's not a valid email
var result3 = validator.Validate(new User { Email = "not-an-email" });
Console.WriteLine(result3.IsValid); // Falsepublic class UserValidator : AbstractValidator<User>
{
public UserValidator()
{
RuleFor(u => u.Email)
.NotEmpty() // Disallow null and empty
.MustBeEmailAddress();
}
}