mirror of
https://github.com/bitwarden/server.git
synced 2025-04-05 05:00:19 -05:00
Added Validator and ValidationResult
This commit is contained in:
parent
42774eaa17
commit
551d95368c
6
src/Core/AdminConsole/Shared/Validation/IValidator.cs
Normal file
6
src/Core/AdminConsole/Shared/Validation/IValidator.cs
Normal file
@ -0,0 +1,6 @@
|
||||
namespace Bit.Core.AdminConsole.Shared.Validation;
|
||||
|
||||
public interface IValidator<T>
|
||||
{
|
||||
public Task<ValidationResult<T>> ValidateAsync(T value);
|
||||
}
|
15
src/Core/AdminConsole/Shared/Validation/ValidationResult.cs
Normal file
15
src/Core/AdminConsole/Shared/Validation/ValidationResult.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using Bit.Core.Models.Commands;
|
||||
|
||||
namespace Bit.Core.AdminConsole.Shared.Validation;
|
||||
|
||||
public abstract record ValidationResult<T>;
|
||||
|
||||
public record Valid<T> : ValidationResult<T>
|
||||
{
|
||||
public T Value { get; init; }
|
||||
}
|
||||
|
||||
public record Invalid<T> : ValidationResult<T>
|
||||
{
|
||||
public IEnumerable<Error<T>> Errors { get; init; }
|
||||
}
|
@ -56,7 +56,7 @@ public class Partial<T> : CommandResult<T>
|
||||
}
|
||||
}
|
||||
|
||||
public record Error<T>(string Message, T ErroredValue);
|
||||
public abstract record Error<T>(string Message, T ErroredValue);
|
||||
|
||||
public record RecordNotFoundError<T>(string Message, T ErroredValue) : Error<T>(Message, ErroredValue)
|
||||
{
|
||||
@ -77,3 +77,13 @@ public record InsufficientPermissionsError<T>(string Message, T ErroredValue) :
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public record InvalidRequestError<T>(string Message, T ErroredValue) : Error<T>(Message, ErroredValue)
|
||||
{
|
||||
public const string Code = "Invalid Request";
|
||||
|
||||
public InvalidRequestError(T ErroredValue) : this(Code, ErroredValue)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
53
test/Core.Test/AdminConsole/Shared/IValidatorTests.cs
Normal file
53
test/Core.Test/AdminConsole/Shared/IValidatorTests.cs
Normal file
@ -0,0 +1,53 @@
|
||||
using Bit.Core.AdminConsole.Shared.Validation;
|
||||
using Bit.Core.Models.Commands;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Core.Test.AdminConsole.Shared;
|
||||
|
||||
public class IValidatorTests
|
||||
{
|
||||
public class TestClass
|
||||
{
|
||||
public string Name { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public class TestClassValidator : IValidator<TestClass>
|
||||
{
|
||||
public Task<ValidationResult<TestClass>> ValidateAsync(TestClass value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value.Name))
|
||||
{
|
||||
return Task.FromResult<ValidationResult<TestClass>>(new Invalid<TestClass>
|
||||
{
|
||||
Errors = [new InvalidRequestError<TestClass>(value)]
|
||||
});
|
||||
}
|
||||
|
||||
return Task.FromResult<ValidationResult<TestClass>>(new Valid<TestClass> { Value = value });
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_WhenSomethingIsInvalid_ReturnsInvalidWithError()
|
||||
{
|
||||
var example = new TestClass();
|
||||
|
||||
var result = await new TestClassValidator().ValidateAsync(example);
|
||||
|
||||
Assert.IsType<Invalid<TestClass>>(result);
|
||||
var invalidResult = result as Invalid<TestClass>;
|
||||
Assert.Equal(InvalidRequestError<TestClass>.Code, invalidResult.Errors.First().Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_WhenIsValid_ReturnsValid()
|
||||
{
|
||||
var example = new TestClass { Name = "Valid" };
|
||||
|
||||
var result = await new TestClassValidator().ValidateAsync(example);
|
||||
|
||||
Assert.IsType<Valid<TestClass>>(result);
|
||||
var validResult = result as Valid<TestClass>;
|
||||
Assert.Equal(example.Name, validResult.Value.Name);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user