Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for single validation error with Result.Invalid #144

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 prevent the underlying service from completing.
ardalis marked this conversation as resolved.
Show resolved Hide resolved
/// </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
Loading