Provides a FluentValidation extension to validate email addresses using the strongly-typed EmailAddress value object (RFC 5322 compliant).
$ dotnet add package PosInformatique.Foundations.EmailAddresses.FluentValidationThis 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.FluentValidation
This package depends on the base package PosInformatique.Foundations.EmailAddresses.
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); // False
public class UserValidator : AbstractValidator<User>
{
public UserValidator()
{
RuleFor(u => u.Email)
.NotEmpty() // Disallow null and empty
.MustBeEmailAddress();
}
}