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.
40 lines
1.4 KiB
40 lines
1.4 KiB
using FluentValidation;
|
|
using FluentValidation.Validators;
|
|
|
|
namespace Common.Extensions;
|
|
|
|
public static class FluentValidationExtensions
|
|
{
|
|
// From: https://github.com/FluentValidation/FluentValidation/issues/1648
|
|
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 Task<bool> IsValidAsync(ValidationContext<T> context, TProperty? value,
|
|
CancellationToken cancellation)
|
|
{
|
|
return base.IsValidAsync(context, value!, cancellation);
|
|
}
|
|
|
|
public override bool IsValid(ValidationContext<T> context, TProperty? value)
|
|
{
|
|
return base.IsValid(context, value!);
|
|
}
|
|
}
|
|
}
|