This 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- FluentValidation extension for first name and last name validation based on the business rules of the PosInformatique.Foundations.People package.
- Uses the same parsing and validation rules as the
FirstNameandLastNamevalue objects - Clear and consistent error messages
nullvalues are accepted (combine withNotNull()validator to forbid nulls)
- Validation: Ensure that user inputs for first and last names adhere to your domain's business rules.
- Type safety: Leverage the strong typing of
FirstNameandLastNamewithin your validation logic. - Consistency: Apply a single, robust name validation logic across all projects using FluentValidation.
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: falsepublic 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