From 2a2667a2ecc5d9f6b6496327d9d7418f390c9bcb Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sun, 17 Sep 2023 23:45:31 -0700 Subject: [PATCH] Fixed: Don't allow quality profile to be created without all qualities (cherry picked from commit 32e1ae2f64827272d351991838200884876e52b4) --- .../Profiles/Quality/QualityItemsValidator.cs | 44 +++++++++++++++++++ .../Quality/QualityProfileController.cs | 2 + 2 files changed, 46 insertions(+) diff --git a/src/Radarr.Api.V3/Profiles/Quality/QualityItemsValidator.cs b/src/Radarr.Api.V3/Profiles/Quality/QualityItemsValidator.cs index 1d0bc491c..85ac65932 100644 --- a/src/Radarr.Api.V3/Profiles/Quality/QualityItemsValidator.cs +++ b/src/Radarr.Api.V3/Profiles/Quality/QualityItemsValidator.cs @@ -17,6 +17,8 @@ namespace Radarr.Api.V3.Profiles.Quality ruleBuilder.SetValidator(new ItemGroupIdValidator()); ruleBuilder.SetValidator(new UniqueIdValidator()); ruleBuilder.SetValidator(new UniqueQualityIdValidator()); + ruleBuilder.SetValidator(new AllQualitiesValidator()); + return ruleBuilder.SetValidator(new ItemGroupNameValidator()); } } @@ -151,4 +153,46 @@ namespace Radarr.Api.V3.Profiles.Quality return true; } } + + public class AllQualitiesValidator : PropertyValidator + { + protected override string GetDefaultMessageTemplate() => "Must contain all qualities"; + + protected override bool IsValid(PropertyValidatorContext context) + { + if (context.PropertyValue is not IList items) + { + return false; + } + + var qualityIds = new HashSet(); + + 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; + } + } } diff --git a/src/Radarr.Api.V3/Profiles/Quality/QualityProfileController.cs b/src/Radarr.Api.V3/Profiles/Quality/QualityProfileController.cs index 08cf7cc9d..ec11f896b 100644 --- a/src/Radarr.Api.V3/Profiles/Quality/QualityProfileController.cs +++ b/src/Radarr.Api.V3/Profiles/Quality/QualityProfileController.cs @@ -27,6 +27,7 @@ namespace Radarr.Api.V3.Profiles.Quality // TODO: Need to validate the Items to ensure groups have names and at no item has no name, no items and no 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(); @@ -34,6 +35,7 @@ namespace Radarr.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 &&