Files
mixtape/zero.Core/Validation/ZeroValidator.cs
T
2022-01-24 00:08:44 +01:00

29 lines
973 B
C#

using FluentValidation;
using FluentValidation.Results;
namespace zero.Validation;
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);
}
}