Fixed: Don't allow quality profile to be created without all qualities

Closes #6005
This commit is contained in:
Mark McDowall 2023-09-17 23:45:31 -07:00
parent 5ff254b646
commit 32e1ae2f64
2 changed files with 47 additions and 1 deletions

View File

@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using FluentValidation;
using FluentValidation.Validators;
@ -17,6 +17,8 @@ namespace Sonarr.Api.V3.Profiles.Quality
ruleBuilder.SetValidator(new ItemGroupIdValidator<T>());
ruleBuilder.SetValidator(new UniqueIdValidator<T>());
ruleBuilder.SetValidator(new UniqueQualityIdValidator<T>());
ruleBuilder.SetValidator(new AllQualitiesValidator<T>());
return ruleBuilder.SetValidator(new ItemGroupNameValidator<T>());
}
}
@ -151,4 +153,46 @@ namespace Sonarr.Api.V3.Profiles.Quality
return true;
}
}
public class AllQualitiesValidator<T> : PropertyValidator
{
protected override string GetDefaultMessageTemplate() => "Must contain all qualities";
protected override bool IsValid(PropertyValidatorContext context)
{
if (context.PropertyValue is not IList<QualityProfileQualityItemResource> items)
{
return false;
}
var qualityIds = new HashSet<int>();
foreach (var item in items)
{
if (item.Id > 0)
{
foreach (var quality in item.Items)
{
qualityIds.Add(quality.Quality.Id);
}
}
else
{
qualityIds.Add(item.Quality.Id);
}
}
var allQualityIds = NzbDrone.Core.Qualities.Quality.All;
foreach (var quality in allQualityIds)
{
if (!qualityIds.Contains(quality.Id))
{
return false;
}
}
return true;
}
}
}

View File

@ -25,6 +25,7 @@ namespace Sonarr.Api.V3.Profiles.Quality
SharedValidator.RuleFor(c => c.Cutoff).ValidCutoff();
SharedValidator.RuleFor(c => c.Items).ValidItems();
SharedValidator.RuleFor(c => c.FormatItems).Must(items =>
{
var all = _formatService.All().Select(f => f.Id).ToList();
@ -32,6 +33,7 @@ namespace Sonarr.Api.V3.Profiles.Quality
return all.Except(ids).Empty();
}).WithMessage("All Custom Formats and no extra ones need to be present inside your Profile! Try refreshing your browser.");
SharedValidator.RuleFor(c => c).Custom((profile, context) =>
{
if (profile.FormatItems.Where(x => x.Score > 0).Sum(x => x.Score) < profile.MinFormatScore &&