Skip to content

Commit

Permalink
Version 3.0.0 (ardalis#34)
Browse files Browse the repository at this point in the history
* 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
ShadyNagy and ardalis authored Aug 21, 2020
1 parent 5817b87 commit 61c4019
Show file tree
Hide file tree
Showing 24 changed files with 317 additions and 48 deletions.
9 changes: 8 additions & 1 deletion Ardalis.Result.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ardalis.Sample.Core", "samp
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ardalis.Result.SampleWeb.FunctionalTests", "sample\Ardalis.Result.SampleWeb.FunctionalTests\Ardalis.Result.SampleWeb.FunctionalTests.csproj", "{CE817D81-37E9-4BAA-92E6-8602303FF772}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ardalis.Result.AspNetCore", "src\Ardalis.Result.AspNetCore\Ardalis.Result.AspNetCore.csproj", "{B92FA989-EAD7-4542-BC51-F02DEF9C34B3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ardalis.Result.AspNetCore", "src\Ardalis.Result.AspNetCore\Ardalis.Result.AspNetCore.csproj", "{B92FA989-EAD7-4542-BC51-F02DEF9C34B3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ardalis.Result.Sample.UnitTests", "sample\Ardalis.Result.Sample.UnitTests\Ardalis.Result.Sample.UnitTests.csproj", "{84E62AF8-95DA-43BC-95FD-A19103C50949}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -51,6 +53,10 @@ Global
{B92FA989-EAD7-4542-BC51-F02DEF9C34B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B92FA989-EAD7-4542-BC51-F02DEF9C34B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B92FA989-EAD7-4542-BC51-F02DEF9C34B3}.Release|Any CPU.Build.0 = Release|Any CPU
{84E62AF8-95DA-43BC-95FD-A19103C50949}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{84E62AF8-95DA-43BC-95FD-A19103C50949}.Debug|Any CPU.Build.0 = Debug|Any CPU
{84E62AF8-95DA-43BC-95FD-A19103C50949}.Release|Any CPU.ActiveCfg = Release|Any CPU
{84E62AF8-95DA-43BC-95FD-A19103C50949}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -62,6 +68,7 @@ Global
{58143C42-9722-40E3-89A0-492D13862686} = {FA061DC6-61B7-401F-87A4-D338B396CCD9}
{CE817D81-37E9-4BAA-92E6-8602303FF772} = {FA061DC6-61B7-401F-87A4-D338B396CCD9}
{B92FA989-EAD7-4542-BC51-F02DEF9C34B3} = {865A74CD-F478-4AA5-AFA5-6C26FB38B849}
{84E62AF8-95DA-43BC-95FD-A19103C50949} = {FA061DC6-61B7-401F-87A4-D338B396CCD9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7CD0ED8C-3B1C-4F16-8B8D-3D8F1A8F1A5A}
Expand Down
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>
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);
}

}
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

<ItemGroup>
<PackageReference Include="Ardalis.ApiEndpoints" Version="1.0.0" />
<PackageReference Include="Ardalis.Result.AspNetCore" Version="2.1.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.4.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.4.1" />

</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Ardalis.Result.AspNetCore\Ardalis.Result.AspNetCore.csproj" />
<ProjectReference Include="..\Ardalis.Sample.Core\Ardalis.Sample.Core.csproj" />
</ItemGroup>

Expand Down
4 changes: 3 additions & 1 deletion sample/Ardalis.Result.SampleWeb/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Ardalis.Sample.Core.Services;
using Ardalis.Sample.Core;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand All @@ -23,7 +24,8 @@ public void ConfigureServices(IServiceCollection services)
services.AddControllers();
services.AddRazorPages();

services.AddSwaggerGen(c => {
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
c.EnableAnnotations();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using Ardalis.ApiEndpoints;
using Ardalis.Result.AspNetCore;
using Ardalis.Sample.Core;
using Ardalis.Sample.Core.DTOs;
using Ardalis.Sample.Core.Model;
using Ardalis.Sample.Core.Services;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Ardalis.Result.SampleWeb.WeatherForecastFeature
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System.Collections.Generic;
using Ardalis.Result.AspNetCore;
using Ardalis.Sample.Core;
using Ardalis.Result.AspNetCore;
using Ardalis.Sample.Core.DTOs;
using Ardalis.Sample.Core.Model;
using Ardalis.Sample.Core.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Swashbuckle.AspNetCore.Annotations;
using System.Collections.Generic;

namespace Ardalis.Result.SampleWeb.WeatherForecastFeature
{
Expand Down
8 changes: 6 additions & 2 deletions sample/Ardalis.Sample.Core/Ardalis.Sample.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ardalis.Result" Version="2.0.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.6.0" />
<PackageReference Include="FluentValidation" Version="9.1.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Ardalis.Result\Ardalis.Result.csproj" />
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions sample/Ardalis.Sample.Core/Model/Person.cs
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; }
}
}
2 changes: 1 addition & 1 deletion sample/Ardalis.Sample.Core/Model/WeatherForecast.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;

namespace Ardalis.Sample.Core.Model
{
{
public class WeatherForecast
{
public DateTime Date { get; set; }
Expand Down
66 changes: 66 additions & 0 deletions sample/Ardalis.Sample.Core/Services/PersonService.cs
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");
}
}
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Collections.Generic;
using System.Linq;

namespace Ardalis.Sample.Core
namespace Ardalis.Sample.Core.Services
{
public class WeatherService
{
Expand All @@ -21,8 +21,11 @@ public Result<IEnumerable<WeatherForecast>> GetForecast(ForecastRequestDto model
// validate model
if (model.PostalCode.Length > 10)
{
return Result<IEnumerable<WeatherForecast>>.Invalid(new Dictionary<string, string[]> {
{ "PostalCode", new[]{"PostalCode cannot exceed 10 characters."} }
return Result<IEnumerable<WeatherForecast>>.Invalid(new List<ValidationError> {
new ValidationError
{
Identifier = "PostalCode",
ErrorMessage = "PostalCode cannot exceed 10 characters." }
});
}

Expand All @@ -42,11 +45,11 @@ public Result<IEnumerable<WeatherForecast>> GetForecast(ForecastRequestDto model
var rng = new Random();
return new Result<IEnumerable<WeatherForecast>>(Enumerable.Range(1, 5)
.Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray());
}
}
Expand Down
17 changes: 17 additions & 0 deletions sample/Ardalis.Sample.Core/Validators/PersonValidator.cs
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");
}
}
}
Loading

0 comments on commit 61c4019

Please sign in to comment.