Provides FluentValidation extensions to validate first and last names using the strongly-typed FirstName and LastName value objects from PosInformatique.Foundations.People.
$ dotnet add package PosInformatique.Foundations.People.FluentValidationThis package provides FluentValidation extensions for validating first names and last names using the
FirstName and LastName value objects from PosInformatique.Foundations.People.
It simplifies the integration of robust name validation into your FluentValidation rules, ensuring that string properties conform to the defined business rules for first and last names.
You can install the package from NuGet:
dotnet add package PosInformatique.Foundations.People.FluentValidation
FirstName and LastName value objects
nullvalues are accepted (combine withNotNull()validator to forbid nulls)
FirstName and LastName within your validation logic.public class PersonValidator : AbstractValidator<Person>
{
public PersonValidator()
{
// FirstName must be a valid FirstName (e.g., "John", "Jean-Pierre")
// Null values are allowed by default. Use NotNull() to disallow.
RuleFor(x => x.FirstName).MustBeFirstName();
// Example with NotNull()
RuleFor(x => x.FirstName)
.NotNull()
.MustBeFirstName();
}
}
public class Person
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
}
// Usage
var validator = new PersonValidator();
// Valid
var result1 = validator.Validate(new Person { FirstName = "John", LastName = "DOE" }); // IsValid: true
// Invalid (contains invalid character)
var result2 = validator.Validate(new Person { FirstName = "John_123", LastName = "DOE" }); // IsValid: false
// Invalid (too long)
var result3 = validator.Validate(new Person { FirstName = new string('A', 51), LastName = "DOE" }); // IsValid: false
// Null (valid by default for MustBeFirstName)
var result4 = validator.Validate(new Person { FirstName = null, LastName = "DOE" }); // IsValid: true
// Null (invalid if NotNull() is used)
var validatorNotNull = new PersonValidator();
validatorNotNull.RuleFor(x => x.FirstName).NotNull().MustBeFirstName();
var result5 = validatorNotNull.Validate(new Person { FirstName = null, LastName = "DOE" }); // IsValid: false
public class PersonValidator : AbstractValidator<Person>
{
public PersonValidator()
{
// LastName must be a valid LastName (e.g., "DOE", "SMITH-JOHNSON")
// Null values are allowed by default. Use NotNull() to disallow.
RuleFor(x => x.LastName).MustBeLastName();
// Example with NotNull()
RuleFor(x => x.LastName)
.NotNull()
.MustBeLastName();
}
}
public class Person
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
}
// Usage
var validator = new PersonValidator();
// Valid
var result1 = validator.Validate(new Person { FirstName = "John", LastName = "DOE" }); // IsValid: true
// Invalid (contains invalid character)
var result2 = validator.Validate(new Person { FirstName = "John", LastName = "DOE_123" }); // IsValid: false
// Invalid (too long)
var result3 = validator.Validate(new Person { FirstName = "John", LastName = new string('A', 51) }); // IsValid: false
// Null (valid by default for MustBeLastName)
var result4 = validator.Validate(new Person { FirstName = "John", LastName = null }); // IsValid: true
// Null (invalid if NotNull() is used)
var validatorNotNull = new PersonValidator();
validatorNotNull.RuleFor(x => x.LastName).NotNull().MustBeLastName();
var result5 = validatorNotNull.Validate(new Person { FirstName = "John", LastName = null }); // IsValid: false