Skip to content

Commit

Permalink
Merge branch 'main' into radekwojpl2/critical-error-result
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalis authored Sep 12, 2023
2 parents c8c2375 + 2dfc6f3 commit 6152cfe
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
<PackageReleaseNotes>PR 132. Add Result.Conflict</PackageReleaseNotes>
<AssemblyName>Ardalis.Result.AspNetCore</AssemblyName>
<Version>7.1.0</Version>
<TargetFrameworks>net7.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework) == 'net7.0'">
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageReleaseNotes>PR 132. Add Result.Conflict</PackageReleaseNotes>
<AssemblyName>Ardalis.Result.FluentValidation</AssemblyName>
<Version>7.1.0</Version>
<TargetFrameworks>net7.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/Ardalis.Result/Ardalis.Result.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageReleaseNotes>PR 132. Add Result.Conflict</PackageReleaseNotes>
<AssemblyName>Ardalis.Result</AssemblyName>
<Version>7.1.0</Version>
<TargetFrameworks>net7.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<None Include="icon.png" Pack="true" Visible="false" PackagePath="" />
Expand Down
10 changes: 10 additions & 0 deletions src/Ardalis.Result/Result.Void.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ public static Result ErrorWithCorrelationId(string correlationId, params string[
};
}

/// <summary>
/// Represents the validation error that prevents the underlying service from completing.
/// </summary>
/// <param name="validationError">The validation error encountered</param>
/// <returns>A Result</returns>
public new static Result Invalid(ValidationError validationError)
{
return new Result(ResultStatus.Invalid) { ValidationErrors = { validationError } };
}

/// <summary>
/// Represents validation errors that prevent the underlying service from completing.
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions src/Ardalis.Result/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ public static Result<T> Error(params string[] errorMessages)
return new Result<T>(ResultStatus.Error) { Errors = errorMessages };
}

/// <summary>
/// Represents a validation error that prevents the underlying service from completing.
/// </summary>
/// <param name="validationError">The validation error encountered</param>
/// <returns>A Result<typeparamref name="T"/></returns>
public static Result<T> Invalid(ValidationError validationError)
{
return new Result<T>(ResultStatus.Invalid) { ValidationErrors = { validationError } };
}

/// <summary>
/// Represents validation errors that prevent the underlying service from completing.
/// </summary>
Expand Down
10 changes: 9 additions & 1 deletion tests/Ardalis.Result.UnitTests/ResultConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,18 @@ public void InitializedIsSuccessFalseForForbiddenFactoryCall()
Assert.False(result.IsSuccess);
}

[Fact]
public void InitializedIsSuccessFalseForInvalidListFactoryCall()
{
var result = Result<object>.Invalid(new List<ValidationError>());

Assert.False(result.IsSuccess);
}

[Fact]
public void InitializedIsSuccessFalseForInvalidFactoryCall()
{
var result = Result<object>.Invalid(null);
var result = Result<object>.Invalid(new ValidationError());

Assert.False(result.IsSuccess);
}
Expand Down
20 changes: 18 additions & 2 deletions tests/Ardalis.Result.UnitTests/ResultVoidConstructor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using FluentAssertions;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
Expand Down Expand Up @@ -73,7 +72,7 @@ public void InitializesErrorResultWithCorrelationIdWithFactoryMethod()
}

[Fact]
public void InitializesInvalidResultWithFactoryMethod()
public void InitializesInvalidResultWithMultipleValidationErrorsWithFactoryMethod()
{
var validationErrors = new List<ValidationError>
{
Expand All @@ -98,6 +97,23 @@ public void InitializesInvalidResultWithFactoryMethod()
result.ValidationErrors.Should().ContainEquivalentOf(new ValidationError { ErrorMessage = "PostalCode cannot exceed 10 characters", Identifier = "postalCode" });
}

[Fact]
public void InitializesInvalidResultWithSingleValidationErrorWithFactoryMethod()
{
var validationError = new ValidationError
{
Identifier = "name",
ErrorMessage = "Name is required"
};

var result = Result.Invalid(validationError);

Assert.Null(result.Value);
Assert.Equal(ResultStatus.Invalid, result.Status);

result.ValidationErrors.Should().ContainEquivalentOf(new ValidationError { ErrorMessage = "Name is required", Identifier = "name" });
}

[Fact]
public void InitializesNotFoundResultWithFactoryMethod()
{
Expand Down
14 changes: 13 additions & 1 deletion tests/Ardalis.Result.UnitTests/ResultVoidMap.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using System.Collections.Generic;
using Xunit;

namespace Ardalis.Result.UnitTests
Expand Down Expand Up @@ -48,10 +49,21 @@ public void ShouldProduceForbidden()
actual.Value.Should().BeNull();
}

[Fact]
public void ShouldProduceInvalidWithEmptyList()
{
var result = Result.Invalid(new List<ValidationError>());

var actual = result.Map(_ => "This should be ignored");

actual.Status.Should().Be(ResultStatus.Invalid);
actual.Value.Should().BeNull();
}

[Fact]
public void ShouldProduceInvalid()
{
var result = Result.Invalid(new());
var result = Result.Invalid(new ValidationError());

var actual = result.Map(_ => "This should be ignored");

Expand Down
20 changes: 19 additions & 1 deletion tests/Ardalis.Result.UnitTests/ResultVoidToResultOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void ConvertFromErrorResultOfUnit(params string[] errors)
}

[Fact]
public void ConvertFromInvalidResultOfUnit()
public void ConvertFromInvalidResultOfUnitWithValidationErrorList()
{
var validationErrors = new List<ValidationError>
{
Expand All @@ -49,6 +49,24 @@ public void ConvertFromInvalidResultOfUnit()
result.ValidationErrors.Should().ContainEquivalentOf(new ValidationError { ErrorMessage = "PostalCode cannot exceed 10 characters", Identifier = "postalCode" });
}

[Fact]
public void ConvertFromInvalidResultOfUnitWithValidationError()
{
var validationError = new ValidationError
{
Identifier = "name",
ErrorMessage = "Name is required"
};

var result = DoBusinessOperationExample<object>(Result.Invalid(validationError));

Assert.Null(result.Value);
Assert.Equal(ResultStatus.Invalid, result.Status);

result.Status.Should().Be(ResultStatus.Invalid);
result.ValidationErrors.Should().ContainEquivalentOf(new ValidationError { ErrorMessage = "Name is required", Identifier = "name" });
}

[Fact]
public void ConvertFromNotFoundResultOfUnit()
{
Expand Down

0 comments on commit 6152cfe

Please sign in to comment.