From bb44fbc362ebe7abc4c3934057ade51dd820f59c Mon Sep 17 00:00:00 2001 From: Bogdan Date: Thu, 9 May 2024 20:09:45 +0300 Subject: [PATCH] New: Root folder exists validation for import lists Also moved the AppendArgument to avoid cases like `Invalid Path: '{path}'`. --- .../Validation/Paths/PathValidator.cs | 9 ++----- .../Paths/RootFolderExistsValidator.cs | 25 +++++++++++++++++++ .../Validation/Paths/RootFolderValidator.cs | 4 +-- .../ImportLists/ImportListController.cs | 13 +++++++--- 4 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 src/NzbDrone.Core/Validation/Paths/RootFolderExistsValidator.cs diff --git a/src/NzbDrone.Core/Validation/Paths/PathValidator.cs b/src/NzbDrone.Core/Validation/Paths/PathValidator.cs index 13da8a937..6bf9af49e 100644 --- a/src/NzbDrone.Core/Validation/Paths/PathValidator.cs +++ b/src/NzbDrone.Core/Validation/Paths/PathValidator.cs @@ -19,14 +19,9 @@ public class PathValidator : PropertyValidator protected override bool IsValid(PropertyValidatorContext context) { - if (context.PropertyValue == null) - { - return false; - } + context.MessageFormatter.AppendArgument("path", context.PropertyValue?.ToString()); - context.MessageFormatter.AppendArgument("path", context.PropertyValue.ToString()); - - return context.PropertyValue.ToString().IsPathValid(PathValidationType.CurrentOs); + return context.PropertyValue != null && context.PropertyValue.ToString().IsPathValid(PathValidationType.CurrentOs); } } } diff --git a/src/NzbDrone.Core/Validation/Paths/RootFolderExistsValidator.cs b/src/NzbDrone.Core/Validation/Paths/RootFolderExistsValidator.cs new file mode 100644 index 000000000..8b4c4e7a0 --- /dev/null +++ b/src/NzbDrone.Core/Validation/Paths/RootFolderExistsValidator.cs @@ -0,0 +1,25 @@ +using FluentValidation.Validators; +using NzbDrone.Common.Extensions; +using NzbDrone.Core.RootFolders; + +namespace NzbDrone.Core.Validation.Paths +{ + public class RootFolderExistsValidator : PropertyValidator + { + private readonly IRootFolderService _rootFolderService; + + public RootFolderExistsValidator(IRootFolderService rootFolderService) + { + _rootFolderService = rootFolderService; + } + + protected override string GetDefaultMessageTemplate() => "Root folder '{path}' does not exist"; + + protected override bool IsValid(PropertyValidatorContext context) + { + context.MessageFormatter.AppendArgument("path", context.PropertyValue?.ToString()); + + return context.PropertyValue == null || _rootFolderService.All().Exists(r => r.Path.PathEquals(context.PropertyValue.ToString())); + } + } +} diff --git a/src/NzbDrone.Core/Validation/Paths/RootFolderValidator.cs b/src/NzbDrone.Core/Validation/Paths/RootFolderValidator.cs index 7f3db8ed3..95067cd6f 100644 --- a/src/NzbDrone.Core/Validation/Paths/RootFolderValidator.cs +++ b/src/NzbDrone.Core/Validation/Paths/RootFolderValidator.cs @@ -17,13 +17,13 @@ public RootFolderValidator(IRootFolderService rootFolderService) protected override bool IsValid(PropertyValidatorContext context) { + context.MessageFormatter.AppendArgument("path", context.PropertyValue?.ToString()); + if (context.PropertyValue == null) { return true; } - context.MessageFormatter.AppendArgument("path", context.PropertyValue.ToString()); - return !_rootFolderService.All().Exists(r => r.Path.PathEquals(context.PropertyValue.ToString())); } } diff --git a/src/Radarr.Api.V3/ImportLists/ImportListController.cs b/src/Radarr.Api.V3/ImportLists/ImportListController.cs index 4e39eeafb..f41bfab08 100644 --- a/src/Radarr.Api.V3/ImportLists/ImportListController.cs +++ b/src/Radarr.Api.V3/ImportLists/ImportListController.cs @@ -12,13 +12,18 @@ public class ImportListController : ProviderControllerBase c.RootFolderPath).IsValidPath(); SharedValidator.RuleFor(c => c.MinimumAvailability).NotNull(); - SharedValidator.RuleFor(c => c.QualityProfileId).ValidId(); - SharedValidator.RuleFor(c => c.QualityProfileId).SetValidator(qualityProfileExistsValidator); + + SharedValidator.RuleFor(c => c.RootFolderPath).Cascade(CascadeMode.Stop) + .IsValidPath() + .SetValidator(rootFolderExistsValidator); + + SharedValidator.RuleFor(c => c.QualityProfileId).Cascade(CascadeMode.Stop) + .ValidId() + .SetValidator(qualityProfileExistsValidator); } } }