Skip to content

Commit

Permalink
Merge pull request ardalis#29 from ShadyNagy/master
Browse files Browse the repository at this point in the history
IResult.ValidationErrors be Dictionary<string, string[]> to allow multiple errors for the same key
  • Loading branch information
ardalis authored Aug 20, 2020
2 parents de07267 + 1cb558e commit 7ab3f47
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 18 deletions.
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.0" />
<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: 2 additions & 2 deletions sample/Ardalis.Sample.Core/WeatherService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public Result<IEnumerable<WeatherForecast>> GetForecast(ForecastRequestDto model
// validate model
if (model.PostalCode.Length > 10)
{
return Result<IEnumerable<WeatherForecast>>.Invalid(new Dictionary<string, string> {
{ "PostalCode", "PostalCode cannot exceed 10 characters." }
return Result<IEnumerable<WeatherForecast>>.Invalid(new Dictionary<string, string[]> {
{ "PostalCode", new[]{"PostalCode cannot exceed 10 characters."} }
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Ardalis.Result" Version="1.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
</ItemGroup>

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

</Project>
6 changes: 4 additions & 2 deletions src/Ardalis.Result.AspNetCore/ResultExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc;

namespace Ardalis.Result.AspNetCore
{
Expand All @@ -22,7 +24,7 @@ public static ActionResult<T> ToActionResult<T>(this ControllerBase controller,
{
foreach (var error in result.ValidationErrors)
{
controller.ModelState.AddModelError(error.Key, error.Value);
controller.ModelState.AddModelError(error.Key, string.Join(Environment.NewLine, error.Value));
}
return controller.BadRequest(controller.ModelState);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

Expand All @@ -18,7 +19,7 @@ public override void OnActionExecuted(ActionExecutedContext context)
{
foreach (var error in result.ValidationErrors)
{
(context.Controller as ControllerBase).ModelState.AddModelError(error.Key, error.Value);
(context.Controller as ControllerBase).ModelState.AddModelError(error.Key, string.Join(Environment.NewLine, error.Value));
}

context.Result = controller.BadRequest(controller.ModelState);
Expand Down
6 changes: 3 additions & 3 deletions src/Ardalis.Result/Ardalis.Result.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
Expand All @@ -12,8 +12,8 @@
<Summary>A simple package to implement the Result pattern for returning from services.</Summary>
<RepositoryUrl>https://github.com/ardalis/result</RepositoryUrl>
<PackageTags>result pattern web api aspnetcore mvc</PackageTags>
<PackageReleaseNotes>Update the icon</PackageReleaseNotes>
<Version>1.1.1</Version>
<PackageReleaseNotes>IResult.ValidationErrors be message string[].</PackageReleaseNotes>
<Version>1.1.2</Version>
<AssemblyName>Ardalis.Result</AssemblyName>
<PackageIconUrl>https://user-images.githubusercontent.com/782127/82077419-64085e00-96ad-11ea-9c7a-0f17ccf0bda6.png</PackageIconUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
Expand Down
2 changes: 1 addition & 1 deletion src/Ardalis.Result/IResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface IResult
{
ResultStatus Status { get; }
IEnumerable<string> Errors { get; }
Dictionary<string, string> ValidationErrors { get; }
Dictionary<string, string[]> ValidationErrors { get; }
Type ValueType { get; }
Object GetValue();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Ardalis.Result/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Type ValueType
}
public ResultStatus Status { get; } = ResultStatus.Ok;
public IEnumerable<string> Errors { get; private set; } = new List<string>();
public Dictionary<string, string> ValidationErrors { get; private set;} = new Dictionary<string, string>();
public Dictionary<string, string[]> ValidationErrors { get; private set;} = new Dictionary<string, string[]>();

public object GetValue()
{
Expand All @@ -44,7 +44,7 @@ public static Result<T> Error(params string[] errorMessages)
return new Result<T>(ResultStatus.Error) { Errors = errorMessages };
}

public static Result<T> Invalid(Dictionary<string, string> validationErrors)
public static Result<T> Invalid(Dictionary<string, string[]> validationErrors)
{
return new Result<T>(ResultStatus.Invalid) { ValidationErrors = validationErrors };
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Ardalis.Result.UnitTests/ResultConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ public void InitializesStatusToErrorAndSetsErrorMessageGivenErrorFactoryCall()
[Fact]
public void InitializesStatusToInvalidAndSetsErrorMessagesGivenInvalidFactoryCall()
{
var validationErrors = new Dictionary<string, string>
var validationErrors = new Dictionary<string, string[]>
{
{ "name", "Name is required"},
{ "postalCode", "PostalCode cannot exceed 10 characters"}
{ "name", new []{"Name is required"}},
{ "postalCode", new []{"PostalCode cannot exceed 10 characters"}}
};
// TODO: Support duplicates of the same key with multiple errors
var result = Result<object>.Invalid(validationErrors);

Assert.Equal(ResultStatus.Invalid, result.Status);
Assert.Equal("Name is required", result.ValidationErrors.Values.First());
Assert.Equal("PostalCode cannot exceed 10 characters", result.ValidationErrors.Values.Last());
Assert.Equal(new []{"Name is required"}, result.ValidationErrors.Values.First());
Assert.Equal(new[] { "PostalCode cannot exceed 10 characters"}, result.ValidationErrors.Values.Last());
}

[Fact]
Expand Down

0 comments on commit 7ab3f47

Please sign in to comment.