forked from ardalis/Result
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update sample for FluentValidation Refactor Result to use ValidationErrors * cleaning up * projects compiled. * New version AspNetCore. Co-authored-by: Steve Smith <[email protected]>
- Loading branch information
Showing
24 changed files
with
317 additions
and
48 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
21 changes: 21 additions & 0 deletions
21
sample/Ardalis.Result.Sample.UnitTests/Ardalis.Result.Sample.UnitTests.csproj
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,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="5.10.3" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> | ||
<PackageReference Include="xunit" Version="2.4.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" /> | ||
<PackageReference Include="coverlet.collector" Version="1.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Ardalis.Sample.Core\Ardalis.Sample.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
32 changes: 32 additions & 0 deletions
32
sample/Ardalis.Result.Sample.UnitTests/ServiceTests/PersonServiceCreate.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,32 @@ | ||
using Ardalis.Sample.Core.Services; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Ardalis.Result.Sample.UnitTests.ServiceTests | ||
{ | ||
public class PersonServiceCreate | ||
{ | ||
[Fact] | ||
public void ReturnsInvalidResultGivenEmptyNames() | ||
{ | ||
var service = new PersonService(); | ||
|
||
var result = service.Create("", ""); | ||
|
||
result.Status.Should().Be(ResultStatus.Invalid); | ||
result.ValidationErrors.Count.Should().Be(2); | ||
} | ||
|
||
[Fact] | ||
public void ReturnsInvalidResultWith2ErrorsGivenSomeLongNameSurname() | ||
{ | ||
var service = new PersonService(); | ||
|
||
var result = service.Create("Steve", "SomeLongName"); | ||
|
||
result.Status.Should().Be(ResultStatus.Invalid); | ||
result.ValidationErrors.Count.Should().Be(2); | ||
} | ||
|
||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
sample/Ardalis.Result.Sample.UnitTests/ValidatorTests/PersonValidatorValidate.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,77 @@ | ||
using Ardalis.Sample.Core.Model; | ||
using Ardalis.Sample.Core.Validators; | ||
using FluentAssertions; | ||
using System; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace Ardalis.Result.Sample.UnitTests.ValidatorTests | ||
{ | ||
public class PersonValidatorValidate | ||
{ | ||
[Fact] | ||
public void ReturnsTrueGivenValidPerson() | ||
{ | ||
var person = new Person() | ||
{ | ||
Surname = "Testname", | ||
Forename = "Testname2" | ||
}; | ||
|
||
var validator = new PersonValidator(); | ||
|
||
var result = validator.Validate(person); | ||
|
||
Assert.True(result.IsValid); | ||
} | ||
|
||
[Fact] | ||
public void ReturnsFalseGivenPersonWithNoSurname() | ||
{ | ||
var person = new Person() | ||
{ | ||
Forename = "TestForename" | ||
}; | ||
|
||
var validator = new PersonValidator(); | ||
|
||
var result = validator.Validate(person); | ||
|
||
Assert.False(result.IsValid); | ||
Assert.True(result.Errors.Any()); | ||
} | ||
|
||
[Fact] | ||
public void ReturnsFalseGivenPersonWithNoForename() | ||
{ | ||
var person = new Person() | ||
{ | ||
Surname = "TestSurname", | ||
}; | ||
|
||
var validator = new PersonValidator(); | ||
|
||
var result = validator.Validate(person); | ||
|
||
Assert.False(result.IsValid); | ||
Assert.True(result.Errors.Any()); | ||
} | ||
|
||
[Fact] | ||
public void ReturnsFalseGivenPersonWithSomeLongNameForSurname() | ||
{ | ||
var person = new Person() | ||
{ | ||
Surname = "SomeLongName", | ||
Forename = "Steve" | ||
}; | ||
|
||
var validator = new PersonValidator(); | ||
|
||
var result = validator.Validate(person); | ||
|
||
result.IsValid.Should().BeFalse(); | ||
result.Errors.Should().HaveCount(2); | ||
} | ||
} | ||
} |
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
5 changes: 1 addition & 4 deletions
5
sample/Ardalis.Result.SampleWeb/WeatherForecastFeature/ForecastEndpoint.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
7 changes: 3 additions & 4 deletions
7
sample/Ardalis.Result.SampleWeb/WeatherForecastFeature/WeatherForecastController.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Ardalis.Sample.Core.Model | ||
{ | ||
public class Person | ||
{ | ||
public int Id { get; set; } | ||
public string Surname { get; set; } | ||
public string Forename { get; set; } | ||
|
||
public List<Person> Children { get; set; } | ||
|
||
public DateTime DateOfBirth { get; set; } | ||
} | ||
} |
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,66 @@ | ||
using Ardalis.Result; | ||
using Ardalis.Sample.Core.Model; | ||
using Ardalis.Sample.Core.Validators; | ||
using FluentValidation.Results; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Ardalis.Sample.Core.Services | ||
{ | ||
public class PersonService | ||
{ | ||
public Result<Person> Create(string firstName, string lastName) | ||
{ | ||
var person = new Person(); | ||
person.Forename = firstName; | ||
person.Surname = lastName; | ||
|
||
var validator = new PersonValidator(); | ||
|
||
var result = validator.Validate(person); | ||
if (!result.IsValid) | ||
{ | ||
return Result<Person>.Invalid(result.AsErrors()); | ||
} | ||
|
||
return Result<Person>.Success(person); | ||
} | ||
} | ||
|
||
} | ||
|
||
namespace FluentValidation.Results | ||
{ | ||
public static class FluentValidationResultExtensions | ||
{ | ||
public static List<ValidationError> AsErrors(this ValidationResult valResult) | ||
{ | ||
var resultErrors = new List<ValidationError>(); | ||
|
||
foreach (var valFailure in valResult.Errors) | ||
{ | ||
resultErrors.Add(new ValidationError() | ||
{ | ||
Severity = FromSeverity(valFailure.Severity), | ||
ErrorMessage = valFailure.ErrorMessage, | ||
Identifier = valFailure.PropertyName | ||
}) ; | ||
} | ||
|
||
return resultErrors; | ||
} | ||
|
||
public static ValidationSeverity FromSeverity(Severity severity) | ||
{ | ||
switch(severity) | ||
{ | ||
case Severity.Error: return ValidationSeverity.Error; | ||
case Severity.Warning: return ValidationSeverity.Warning; | ||
case Severity.Info: return ValidationSeverity.Info; | ||
default: throw new ArgumentOutOfRangeException(nameof(severity), "Unexpected Severity"); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
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,17 @@ | ||
using Ardalis.Sample.Core.Model; | ||
using FluentValidation; | ||
|
||
namespace Ardalis.Sample.Core.Validators | ||
{ | ||
public class PersonValidator : AbstractValidator<Person> | ||
{ | ||
public PersonValidator() | ||
{ | ||
RuleFor(x => x.Surname) | ||
.NotEmpty() | ||
.MaximumLength(10) | ||
.NotEqual("SomeLongName"); | ||
RuleFor(x => x.Forename).NotEmpty().WithMessage("Please specify a first name"); | ||
} | ||
} | ||
} |
Oops, something went wrong.