You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.1 KiB
66 lines
2.1 KiB
using FluentValidation;
|
|
using FluentValidation.Results;
|
|
using FluentValidation.Validators;
|
|
|
|
namespace Recyclarr.Common.FluentValidation;
|
|
|
|
public static class FluentValidationExtensions
|
|
{
|
|
// From: https://github.com/FluentValidation/FluentValidation/issues/1648
|
|
// ReSharper disable once UnusedMethodReturnValue.Global
|
|
public static IRuleBuilderOptions<T, TProperty?> SetNonNullableValidator<T, TProperty>(
|
|
this IRuleBuilder<T, TProperty?> ruleBuilder,
|
|
IValidator<TProperty> validator,
|
|
params string[] ruleSets)
|
|
{
|
|
var adapter = new NullableChildValidatorAdaptor<T, TProperty>(validator, validator.GetType())
|
|
{
|
|
RuleSets = ruleSets
|
|
};
|
|
|
|
return ruleBuilder.SetAsyncValidator(adapter);
|
|
}
|
|
|
|
private sealed class NullableChildValidatorAdaptor<T, TProperty> : ChildValidatorAdaptor<T, TProperty>,
|
|
IPropertyValidator<T, TProperty?>, IAsyncPropertyValidator<T, TProperty?>
|
|
{
|
|
public NullableChildValidatorAdaptor(IValidator<TProperty> validator, Type validatorType)
|
|
: base(validator, validatorType)
|
|
{
|
|
}
|
|
|
|
public override bool IsValid(ValidationContext<T> context, TProperty? value)
|
|
{
|
|
return base.IsValid(context, value!);
|
|
}
|
|
|
|
public override Task<bool> IsValidAsync(
|
|
ValidationContext<T> context,
|
|
TProperty? value,
|
|
CancellationToken cancellation)
|
|
{
|
|
return base.IsValidAsync(context, value!, cancellation);
|
|
}
|
|
}
|
|
|
|
public static IEnumerable<TSource> IsValid<TSource, TValidator>(
|
|
this IEnumerable<TSource> source,
|
|
TValidator validator,
|
|
Action<List<ValidationFailure>, TSource>? handleInvalid = null)
|
|
where TValidator : IValidator<TSource>
|
|
{
|
|
foreach (var s in source)
|
|
{
|
|
var result = validator.Validate(s);
|
|
if (result is {IsValid: true})
|
|
{
|
|
yield return s;
|
|
}
|
|
else
|
|
{
|
|
handleInvalid?.Invoke(result.Errors, s);
|
|
}
|
|
}
|
|
}
|
|
}
|