ardalis / Result

A result abstraction that can be mapped to HTTP response codes if needed.
MIT License
866 stars 107 forks source link

closes #161 Auto-Evaluation of the Status property when Errors ou ValidationErrors are added to the result. #162

Closed Ewerton closed 8 months ago

Ewerton commented 9 months ago

Auto-evaluate the Status property when Errors ou ValidationErrors are added to the result. It only works when the result is created as Ok(), Error() ou Invalid().

The following unit test describes the functioning of this PR

 [Fact]
 public void ErrorAndValidationErrorChanged()
 {
     Result result = Result.Success(); // Created as Ok
     result.IsSuccess.Should().Be(true);
     result.Status.Should().Be(ResultStatus.Ok);

     // Adding a validation error
     result.ValidationErrors.Add(new ValidationError("Validation Error 1"));
     result.IsSuccess.Should().Be(false);
     result.Status.Should().Be(ResultStatus.Invalid); // <- Status changed to Invalid

     // Adding an Error
     result.Errors.Add("Error 1");
     result.IsSuccess.Should().Be(false);
     result.Status.Should().Be(ResultStatus.Error); // <- Status changed to Error

     // clearing the errors (still remain one Validation Error, so it's Invalid)
     result.Errors.Clear();
     result.IsSuccess.Should().Be(false);
     result.Status.Should().Be(ResultStatus.Invalid); // <- Status changed to Invalid

     // clearing the validation errors. It should revert back to the initial status
     result.ValidationErrors.Clear();
     result.IsSuccess.Should().Be(true);
     result.Status.Should().Be(ResultStatus.Ok); // <- reverted back to its initial state
 }
ardalis commented 8 months ago

I'll bring this in separately since it's breaking change.

github-actions[bot] commented 8 months ago

Code Coverage

Package Line Rate Branch Rate Complexity Health
Ardalis.Result.AspNetCore 54% 60% 159
Ardalis.Result.Sample.Core 38% 38% 63
Ardalis.Result.SampleWeb 66% 50% 43
Ardalis.Result 38% 24% 137
Ardalis.Result.FluentValidation 0% 0% 6
Summary 48% (497 / 1041) 45% (114 / 256) 408