-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from MrDave1999/refactor/issue-52
improve: Change error message format when validation fails from Fluent Validation
- Loading branch information
Showing
13 changed files
with
184 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
namespace SimpleResults.Tests.FluentValidation; | ||
|
||
public class FailedValidationTestCases : IEnumerable | ||
{ | ||
public IEnumerator GetEnumerator() | ||
{ | ||
yield return new object[] | ||
{ | ||
new Order | ||
{ | ||
Customer = string.Empty, | ||
Description = string.Empty, | ||
DeliveryAddress = default, | ||
Details = default | ||
}, | ||
new[] | ||
{ | ||
"'Customer' property failed validation. Error was: 'Customer' must not be empty.", | ||
"'Description' property failed validation. Error was: 'Description' must not be empty.", | ||
"'DeliveryAddress' property failed validation. Error was: 'Delivery Address' must not be empty.", | ||
"'Details' property failed validation. Error was: 'Details' must not be empty." | ||
} | ||
}; | ||
|
||
yield return new object[] | ||
{ | ||
new Order | ||
{ | ||
Customer = string.Empty, | ||
Description = string.Empty, | ||
DeliveryAddress = new Address | ||
{ | ||
Description = string.Empty, | ||
Postcode = string.Empty, | ||
Country = string.Empty | ||
}, | ||
Details = new List<OrderDetail> | ||
{ | ||
new() | ||
{ | ||
Product = string.Empty, | ||
Price = 0, | ||
Amount = 0 | ||
}, | ||
new() | ||
{ | ||
Product = string.Empty, | ||
Price = -1, | ||
Amount = -1 | ||
} | ||
} | ||
}, | ||
new[] | ||
{ | ||
"'Customer' property failed validation. Error was: 'Customer' must not be empty.", | ||
"'Description' property failed validation. Error was: 'Description' must not be empty.", | ||
"'DeliveryAddress.Description' property failed validation. Error was: 'Description' must not be empty.", | ||
"'DeliveryAddress.Postcode' property failed validation. Error was: 'Postcode' must not be empty.", | ||
"'DeliveryAddress.Country' property failed validation. Error was: 'Country' must not be empty.", | ||
"'Details[0].Product' property failed validation. Error was: 'Product' must not be empty.", | ||
"'Details[0].Price' property failed validation. Error was: 'Price' must be greater than '0'.", | ||
"'Details[0].Amount' property failed validation. Error was: 'Amount' must be greater than '0'.", | ||
"'Details[1].Product' property failed validation. Error was: 'Product' must not be empty.", | ||
"'Details[1].Price' property failed validation. Error was: 'Price' must be greater than '0'.", | ||
"'Details[1].Amount' property failed validation. Error was: 'Amount' must be greater than '0'." | ||
} | ||
}; | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
tests/FluentValidation/Validators/DeliveryAddressValidator.cs
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace SimpleResults.Tests.FluentValidation.Validators; | ||
|
||
public class Address | ||
{ | ||
public string Description { get; init; } | ||
public string Country { get; init; } | ||
public string Postcode { get; init; } | ||
} | ||
|
||
public class DeliveryAddressValidator : AbstractValidator<Address> | ||
{ | ||
public DeliveryAddressValidator() | ||
{ | ||
RuleFor(d => d.Description).NotEmpty(); | ||
RuleFor(d => d.Country).NotEmpty(); | ||
RuleFor(d => d.Postcode).NotEmpty(); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace SimpleResults.Tests.FluentValidation.Validators; | ||
|
||
public class OrderDetail | ||
{ | ||
public string Product { get; init; } | ||
public int Amount { get; init; } | ||
public double Price { get; init; } | ||
} | ||
|
||
public class OrderDetailValidator : AbstractValidator<OrderDetail> | ||
{ | ||
public OrderDetailValidator() | ||
{ | ||
RuleFor(o => o.Product).NotEmpty(); | ||
RuleFor(o => o.Amount).GreaterThan(0); | ||
RuleFor(o => o.Price).GreaterThan(0); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace SimpleResults.Tests.FluentValidation.Validators; | ||
|
||
public class Order | ||
{ | ||
public string Customer { get; init; } | ||
public string Description { get; init; } | ||
public Address DeliveryAddress { get; init; } | ||
public IEnumerable<OrderDetail> Details { get; init; } | ||
} | ||
|
||
public class OrderValidator : AbstractValidator<Order> | ||
{ | ||
public OrderValidator() | ||
{ | ||
RuleFor(o => o.Customer).NotEmpty(); | ||
RuleFor(o => o.Description).NotEmpty(); | ||
RuleFor(o => o.DeliveryAddress) | ||
.NotEmpty() | ||
.SetValidator(new DeliveryAddressValidator()); | ||
RuleFor(o => o.Details).NotEmpty(); | ||
RuleForEach(o => o.Details).SetValidator(new OrderDetailValidator()); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
tests/FluentValidation/PersonValidator.cs → ...tValidation/Validators/PersonValidator.cs
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