-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FluentValidation RuleSets and partial validation support (#45)
- Loading branch information
Showing
9 changed files
with
106 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,8 @@ namespace SharedModels | |
{ | ||
public class Person | ||
{ | ||
public string Name { get; set; } | ||
public string FirstName { get; set; } | ||
public string LastName { get; set; } | ||
public int? Age { get; set; } | ||
public string EmailAddress { get; set; } | ||
public Address Address { get; set; } = new Address(); | ||
|
@@ -15,24 +16,34 @@ public class PersonValidator : AbstractValidator<Person> | |
{ | ||
public PersonValidator() | ||
{ | ||
RuleFor(p => p.Name).NotEmpty().WithMessage("You must enter a name"); | ||
RuleFor(p => p.Name).MaximumLength(50).WithMessage("Name cannot be longer than 50 characters"); | ||
RuleFor(p => p.Age).NotNull().GreaterThanOrEqualTo(0).WithMessage("Age must be greater than 0"); | ||
RuleFor(p => p.Age).LessThan(150).WithMessage("Age cannot be greater than 150"); | ||
RuleFor(p => p.EmailAddress).NotEmpty().WithMessage("You must enter a email address"); | ||
RuleFor(p => p.EmailAddress).EmailAddress().WithMessage("You must provide a valid email address"); | ||
RuleSet("Names", () => | ||
{ | ||
RuleFor(p => p.FirstName) | ||
.NotEmpty().WithMessage("You must enter your first name") | ||
.MaximumLength(50).WithMessage("First name cannot be longer than 50 characters"); | ||
|
||
RuleFor(x => x.Name).MustAsync(async (name, cancellationToken) => await IsUniqueAsync(name)) | ||
.WithMessage("Name must be unique") | ||
.When(person => !string.IsNullOrEmpty(person.Name)); | ||
RuleFor(p => p.LastName) | ||
.NotEmpty().WithMessage("You must enter your last name") | ||
.MaximumLength(50).WithMessage("Last name cannot be longer than 50 characters"); | ||
}); | ||
|
||
RuleFor(p => p.Age) | ||
.NotNull().WithMessage("You must enter your age") | ||
.GreaterThanOrEqualTo(0).WithMessage("Age must be greater than 0") | ||
.LessThan(150).WithMessage("Age cannot be greater than 150"); | ||
|
||
RuleFor(p => p.EmailAddress) | ||
.NotEmpty().WithMessage("You must enter a email address") | ||
.EmailAddress().WithMessage("You must provide a valid email address") | ||
.MustAsync(async (email, cancellationToken) => await IsUniqueAsync(email)).WithMessage("Email address must be unique").When(p => !string.IsNullOrEmpty(p.EmailAddress)); | ||
|
||
RuleFor(p => p.Address).SetValidator(new AddressValidator()); | ||
} | ||
|
||
private async Task<bool> IsUniqueAsync(string name) | ||
private async Task<bool> IsUniqueAsync(string email) | ||
{ | ||
await Task.Delay(300); | ||
return name.ToLower() != "test"; | ||
return email.ToLower() != "[email protected]"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters