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

[PM-15621] Add support for handling multiple CommandResult validators. #5475

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
36 changes: 36 additions & 0 deletions src/Core/Validators/CommandResultValidator.cs
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
Copy link
Member

@eliykat eliykat Mar 10, 2025

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 just CommandValidator?

Copy link
Contributor

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.

{
public static CommandResult ExecuteValidators(Func<CommandResult>[] validators)
Copy link
Member

Choose a reason for hiding this comment

The 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 ExecuteValidatorsAsync(Func<CommandResult>[], Func<Task<CommandResult>>[]) which can just handle it all for you. That itself would be asynchronous but doesn't matter as long as it's guaranteed to run synchronous validators first.

{
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();
}
}
130 changes: 130 additions & 0 deletions test/Core.Test/Validators/CommandResultValidatorTests.cs
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);
}
}
Loading