-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[PM-15621] Add support for handling multiple CommandResult validators. #5475
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
๏ปฟusing Bit.Core.Models.Commands; | ||
|
||
namespace Bit.Core.Validators; | ||
|
||
public static class CommandResultValidator | ||
{ | ||
public static CommandResult ExecuteValidators(Func<CommandResult>[] validators) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Run synchronous validators, then run async validators" is probably going to be a pretty common pattern. I suggest a combined interface, like |
||
{ | ||
foreach (var validator in validators) | ||
{ | ||
var result = validator(); | ||
|
||
if (result is not Success) | ||
{ | ||
return result; | ||
} | ||
} | ||
|
||
return new Success(); | ||
} | ||
|
||
public static async Task<CommandResult> ExecuteValidatorAsync(Func<Task<CommandResult>>[] validators) | ||
{ | ||
foreach (var validator in validators) | ||
{ | ||
var result = await validator(); | ||
|
||
if (result is not Success) | ||
{ | ||
return result; | ||
} | ||
} | ||
|
||
return new Success(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
๏ปฟusing Bit.Core.Models.Commands; | ||
using Bit.Core.Validators; | ||
using Xunit; | ||
|
||
namespace Bit.Core.Test.Validators; | ||
|
||
public class CommandResultValidatorTests | ||
{ | ||
[Fact] | ||
public void ExecuteValidators_AllSuccess_ReturnsSuccess() | ||
{ | ||
// Arrange | ||
var validators = new Func<CommandResult>[] | ||
{ | ||
() => new Success(), | ||
() => new Success(), | ||
() => new Success() | ||
}; | ||
|
||
// Act | ||
var result = CommandResultValidator.ExecuteValidators(validators); | ||
|
||
// Assert | ||
Assert.IsType<Success>(result); | ||
} | ||
|
||
public static IEnumerable<object[]> TestCases() | ||
{ | ||
yield return new object[] | ||
{ | ||
new Func<CommandResult>[] | ||
{ | ||
() => new Failure("First failure"), | ||
() => new Success(), | ||
() => new Failure("Second failure"), | ||
} | ||
}; | ||
yield return new object[] | ||
{ | ||
new Func<CommandResult>[] | ||
{ | ||
() => new Success(), | ||
() => new Failure("First failure"), | ||
() => new Failure("Second failure"), | ||
} | ||
}; | ||
yield return new object[] | ||
{ | ||
new Func<CommandResult>[] | ||
{ | ||
() => new Success(), | ||
() => new Success(), | ||
() => new Failure("First failure"), | ||
} | ||
}; | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(TestCases))] | ||
public void ExecuteValidators_WhenValidatorFails_ReturnsFirstFailure(Func<CommandResult>[] validators) | ||
{ | ||
// Act | ||
var result = CommandResultValidator.ExecuteValidators(validators); | ||
|
||
// Assert | ||
Assert.IsType<Failure>(result); | ||
Assert.Equal(["First failure"], ((Failure)result).ErrorMessages); | ||
} | ||
|
||
[Fact] | ||
public async Task ExecuteValidatorAsync_AllSuccess_ReturnsSuccess() | ||
{ | ||
// Arrange | ||
var validators = new Func<Task<CommandResult>>[] | ||
{ | ||
async () => await Task.FromResult(new Success()), | ||
async () => await Task.FromResult(new Success()), | ||
async () => await Task.FromResult(new Success()) | ||
}; | ||
|
||
// Act | ||
var result = await CommandResultValidator.ExecuteValidatorAsync(validators); | ||
|
||
// Assert | ||
Assert.IsType<Success>(result); | ||
} | ||
|
||
public static IEnumerable<object[]> AsyncTestCases() | ||
{ | ||
yield return new object[] | ||
{ | ||
new Func<Task<CommandResult>>[] | ||
{ | ||
async () => await Task.FromResult(new Failure("First failure")), | ||
async () => await Task.FromResult(new Success()), | ||
async () => await Task.FromResult(new Failure("Second failure")), | ||
} | ||
}; | ||
yield return new object[] | ||
{ | ||
new Func<Task<CommandResult>>[] | ||
{ | ||
async () => await Task.FromResult(new Success()), | ||
async () => await Task.FromResult(new Failure("First failure")), | ||
async () => await Task.FromResult(new Failure("Second failure")), | ||
} | ||
}; | ||
yield return new object[] | ||
{ | ||
new Func<Task<CommandResult>>[] | ||
{ | ||
async () => await Task.FromResult(new Success()), | ||
async () => await Task.FromResult(new Success()), | ||
async () => await Task.FromResult(new Failure("First failure")), | ||
} | ||
}; | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(AsyncTestCases))] | ||
public async Task ExecuteValidatorAsync_WhenValidatorFails_ReturnsFirstFailure(Func<Task<CommandResult>>[] validators) | ||
{ | ||
// Act | ||
var result = await CommandResultValidator.ExecuteValidatorAsync(validators); | ||
|
||
// Assert | ||
Assert.IsType<Failure>(result); | ||
Assert.Equal(["First failure"], ((Failure)result).ErrorMessages); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know you two have discussed this so I may be out of the loop, but
CommandResultValidator
is an odd name to me. You're validating the inputs of the command, not the result.CommandRequestValidator
, or justCommandValidator
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jimmy and I talked about this and we are POC'ing some stuff now about this.
Mostly around the difference between Commands and Validators. There are a few more POC PRs we're putting together to try to get some common types out for everyone to look at.