Files
mixtape/zero.Core/Validation/ZeroValidator.cs
T

30 lines
974 B
C#
Raw Normal View History

2021-11-19 16:11:12 +01:00
using FluentValidation;
using FluentValidation.Results;
2021-11-20 13:52:28 +01:00
namespace zero.Validation;
2021-11-19 16:11:12 +01:00
public class ZeroValidator<TInterface> : ZeroValidator<TInterface, TInterface>
{
}
public abstract class ZeroValidator<TInterface, TImplementation> : AbstractValidator<TImplementation>, IValidator<TInterface> where TImplementation : TInterface
{
public ValidationResult Validate(TInterface instance)
{
if (!(instance is TImplementation))
{
throw new ArgumentException($"Parameter {nameof(instance)} has to be of type {typeof(TImplementation)}.");
}
return base.Validate((TImplementation)instance);
}
public Task<ValidationResult> ValidateAsync(TInterface instance, CancellationToken cancellation = default)
{
if (!(instance is TImplementation))
{
throw new ArgumentException($"Parameter {nameof(instance)} has to be of type {typeof(TImplementation)}.");
}
return base.ValidateAsync((TImplementation)instance, cancellation);
}
}