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.
32 lines
1.1 KiB
32 lines
1.1 KiB
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using FluentValidation;
|
|
using FluentValidation.Validators;
|
|
|
|
namespace Readarr.Api.V1.Profiles.Quality
|
|
{
|
|
public static class QualityCutoffValidator
|
|
{
|
|
public static IRuleBuilderOptions<T, int> ValidCutoff<T>(this IRuleBuilder<T, int> ruleBuilder)
|
|
{
|
|
return ruleBuilder.SetValidator(new ValidCutoffValidator<T>());
|
|
}
|
|
}
|
|
|
|
public class ValidCutoffValidator<T> : PropertyValidator
|
|
{
|
|
protected override string GetDefaultMessageTemplate() => "Cutoff must be an allowed quality or group";
|
|
|
|
protected override bool IsValid(PropertyValidatorContext context)
|
|
{
|
|
var cutoff = (int)context.PropertyValue;
|
|
dynamic instance = context.ParentContext.InstanceToValidate;
|
|
var items = instance.Items as IList<QualityProfileQualityItemResource>;
|
|
|
|
var cutoffItem = items?.SingleOrDefault(i => (i.Quality == null && i.Id == cutoff) || i.Quality?.Id == cutoff);
|
|
|
|
return cutoffItem is { Allowed: true };
|
|
}
|
|
}
|
|
}
|