From 88e3f8626254de839ea1e969c30f0be03ea7f178 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Sat, 15 Apr 2023 05:38:43 +0300 Subject: [PATCH] Fixed: Migrate to FluentValidation 9 --- src/NzbDrone.Common/Extensions/UrlExtensions.cs | 13 +------------ .../Clients/rTorrent/RTorrentDirectoryValidator.cs | 12 ++++++------ src/NzbDrone.Core/Prowlarr.Core.csproj | 2 +- .../Validation/FolderChmodValidator.cs | 3 ++- src/NzbDrone.Core/Validation/FolderValidator.cs | 5 +---- .../Validation/Paths/FileExistsValidator.cs | 3 ++- .../Validation/Paths/FolderWritableValidator.cs | 3 ++- .../Validation/Paths/MappedNetworkDriveValidator.cs | 10 +++------- .../Validation/Paths/PathExistsValidator.cs | 3 ++- src/NzbDrone.Core/Validation/Paths/PathValidator.cs | 5 +---- .../Validation/Paths/StartupFolderValidator.cs | 3 ++- .../Validation/Paths/SystemFolderValidator.cs | 5 +---- src/NzbDrone.Core/Validation/UrlValidator.cs | 5 +---- .../Prowlarr.Test.Common.csproj | 2 +- src/Prowlarr.Api.V1/Config/HostConfigController.cs | 2 +- src/Prowlarr.Api.V1/Prowlarr.Api.V1.csproj | 2 +- src/Prowlarr.Http/Prowlarr.Http.csproj | 2 +- src/Prowlarr.Http/REST/ResourceValidator.cs | 10 ++-------- .../Validation/EmptyCollectionValidator.cs | 5 +---- .../Validation/NetImportSyncIntervalValidator.cs | 12 ++---------- .../Validation/RssSyncIntervalValidator.cs | 12 ++---------- 21 files changed, 36 insertions(+), 83 deletions(-) diff --git a/src/NzbDrone.Common/Extensions/UrlExtensions.cs b/src/NzbDrone.Common/Extensions/UrlExtensions.cs index e44843838..d71cfec15 100644 --- a/src/NzbDrone.Common/Extensions/UrlExtensions.cs +++ b/src/NzbDrone.Common/Extensions/UrlExtensions.cs @@ -16,18 +16,7 @@ namespace NzbDrone.Common.Extensions return false; } - Uri uri; - if (!Uri.TryCreate(path, UriKind.Absolute, out uri)) - { - return false; - } - - if (!uri.IsWellFormedOriginalString()) - { - return false; - } - - return true; + return Uri.TryCreate(path, UriKind.Absolute, out var uri) && uri.IsWellFormedOriginalString(); } } } diff --git a/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentDirectoryValidator.cs b/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentDirectoryValidator.cs index 4b0e6f38a..e2889f7c8 100644 --- a/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentDirectoryValidator.cs +++ b/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentDirectoryValidator.cs @@ -16,12 +16,12 @@ namespace NzbDrone.Core.Download.Clients.rTorrent public RTorrentDirectoryValidator(PathExistsValidator pathExistsValidator, MappedNetworkDriveValidator mappedNetworkDriveValidator) { - RuleFor(c => c.Directory).Cascade(CascadeMode.StopOnFirstFailure) - .IsValidPath() - .SetValidator(mappedNetworkDriveValidator) - .SetValidator(pathExistsValidator) - .When(c => c.Directory.IsNotNullOrWhiteSpace()) - .When(c => c.Host == "localhost" || c.Host == "127.0.0.1"); + RuleFor(c => c.Directory).Cascade(CascadeMode.Stop) + .IsValidPath() + .SetValidator(mappedNetworkDriveValidator) + .SetValidator(pathExistsValidator) + .When(c => c.Directory.IsNotNullOrWhiteSpace()) + .When(c => c.Host == "localhost" || c.Host == "127.0.0.1"); } } } diff --git a/src/NzbDrone.Core/Prowlarr.Core.csproj b/src/NzbDrone.Core/Prowlarr.Core.csproj index d52623517..4632eb940 100644 --- a/src/NzbDrone.Core/Prowlarr.Core.csproj +++ b/src/NzbDrone.Core/Prowlarr.Core.csproj @@ -15,7 +15,7 @@ - + diff --git a/src/NzbDrone.Core/Validation/FolderChmodValidator.cs b/src/NzbDrone.Core/Validation/FolderChmodValidator.cs index 3e90bf9fa..5677526af 100644 --- a/src/NzbDrone.Core/Validation/FolderChmodValidator.cs +++ b/src/NzbDrone.Core/Validation/FolderChmodValidator.cs @@ -8,11 +8,12 @@ namespace NzbDrone.Core.Validation private readonly IDiskProvider _diskProvider; public FolderChmodValidator(IDiskProvider diskProvider) - : base("Must contain a valid Unix permissions octal") { _diskProvider = diskProvider; } + protected override string GetDefaultMessageTemplate() => "Must contain a valid Unix permissions octal"; + protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) diff --git a/src/NzbDrone.Core/Validation/FolderValidator.cs b/src/NzbDrone.Core/Validation/FolderValidator.cs index 9eedeb459..6f88c9009 100644 --- a/src/NzbDrone.Core/Validation/FolderValidator.cs +++ b/src/NzbDrone.Core/Validation/FolderValidator.cs @@ -5,10 +5,7 @@ namespace NzbDrone.Core.Validation { public class FolderValidator : PropertyValidator { - public FolderValidator() - : base("Invalid Path") - { - } + protected override string GetDefaultMessageTemplate() => "Invalid Path"; protected override bool IsValid(PropertyValidatorContext context) { diff --git a/src/NzbDrone.Core/Validation/Paths/FileExistsValidator.cs b/src/NzbDrone.Core/Validation/Paths/FileExistsValidator.cs index 9adb200aa..02e2aef81 100644 --- a/src/NzbDrone.Core/Validation/Paths/FileExistsValidator.cs +++ b/src/NzbDrone.Core/Validation/Paths/FileExistsValidator.cs @@ -8,11 +8,12 @@ namespace NzbDrone.Core.Validation.Paths private readonly IDiskProvider _diskProvider; public FileExistsValidator(IDiskProvider diskProvider) - : base("File does not exist") { _diskProvider = diskProvider; } + protected override string GetDefaultMessageTemplate() => "File does not exist"; + protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) diff --git a/src/NzbDrone.Core/Validation/Paths/FolderWritableValidator.cs b/src/NzbDrone.Core/Validation/Paths/FolderWritableValidator.cs index c58b5a273..6838b3ea6 100644 --- a/src/NzbDrone.Core/Validation/Paths/FolderWritableValidator.cs +++ b/src/NzbDrone.Core/Validation/Paths/FolderWritableValidator.cs @@ -9,11 +9,12 @@ namespace NzbDrone.Core.Validation.Paths private readonly IDiskProvider _diskProvider; public FolderWritableValidator(IDiskProvider diskProvider) - : base($"Folder is not writable by user {Environment.UserName}") { _diskProvider = diskProvider; } + protected override string GetDefaultMessageTemplate() => $"Folder is not writable by user {Environment.UserName}"; + protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) diff --git a/src/NzbDrone.Core/Validation/Paths/MappedNetworkDriveValidator.cs b/src/NzbDrone.Core/Validation/Paths/MappedNetworkDriveValidator.cs index 48e3da21e..2476800ce 100644 --- a/src/NzbDrone.Core/Validation/Paths/MappedNetworkDriveValidator.cs +++ b/src/NzbDrone.Core/Validation/Paths/MappedNetworkDriveValidator.cs @@ -14,12 +14,13 @@ namespace NzbDrone.Core.Validation.Paths private static readonly Regex DriveRegex = new Regex(@"[a-z]\:\\", RegexOptions.Compiled | RegexOptions.IgnoreCase); public MappedNetworkDriveValidator(IRuntimeInfo runtimeInfo, IDiskProvider diskProvider) - : base("Mapped Network Drive and Windows Service") { _runtimeInfo = runtimeInfo; _diskProvider = diskProvider; } + protected override string GetDefaultMessageTemplate() => "Mapped Network Drive and Windows Service"; + protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) @@ -46,12 +47,7 @@ namespace NzbDrone.Core.Validation.Paths var mount = _diskProvider.GetMount(path); - if (mount != null && mount.DriveType == DriveType.Network) - { - return false; - } - - return true; + return mount is not { DriveType: DriveType.Network }; } } } diff --git a/src/NzbDrone.Core/Validation/Paths/PathExistsValidator.cs b/src/NzbDrone.Core/Validation/Paths/PathExistsValidator.cs index 77398b5e8..91493482f 100644 --- a/src/NzbDrone.Core/Validation/Paths/PathExistsValidator.cs +++ b/src/NzbDrone.Core/Validation/Paths/PathExistsValidator.cs @@ -8,11 +8,12 @@ namespace NzbDrone.Core.Validation.Paths private readonly IDiskProvider _diskProvider; public PathExistsValidator(IDiskProvider diskProvider) - : base("Path does not exist") { _diskProvider = diskProvider; } + protected override string GetDefaultMessageTemplate() => "Path does not exist"; + protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) diff --git a/src/NzbDrone.Core/Validation/Paths/PathValidator.cs b/src/NzbDrone.Core/Validation/Paths/PathValidator.cs index c546d3e79..fe0b496ed 100644 --- a/src/NzbDrone.Core/Validation/Paths/PathValidator.cs +++ b/src/NzbDrone.Core/Validation/Paths/PathValidator.cs @@ -14,10 +14,7 @@ namespace NzbDrone.Core.Validation.Paths public class PathValidator : PropertyValidator { - public PathValidator() - : base("Invalid Path") - { - } + protected override string GetDefaultMessageTemplate() => "Invalid Path"; protected override bool IsValid(PropertyValidatorContext context) { diff --git a/src/NzbDrone.Core/Validation/Paths/StartupFolderValidator.cs b/src/NzbDrone.Core/Validation/Paths/StartupFolderValidator.cs index 158a9ca31..d2cd03780 100644 --- a/src/NzbDrone.Core/Validation/Paths/StartupFolderValidator.cs +++ b/src/NzbDrone.Core/Validation/Paths/StartupFolderValidator.cs @@ -9,11 +9,12 @@ namespace NzbDrone.Core.Validation.Paths private readonly IAppFolderInfo _appFolderInfo; public StartupFolderValidator(IAppFolderInfo appFolderInfo) - : base("Path cannot be an ancestor of the start up folder") { _appFolderInfo = appFolderInfo; } + protected override string GetDefaultMessageTemplate() => "Path cannot be an ancestor of the start up folder"; + protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) diff --git a/src/NzbDrone.Core/Validation/Paths/SystemFolderValidator.cs b/src/NzbDrone.Core/Validation/Paths/SystemFolderValidator.cs index d8f3a7f15..6b891394e 100644 --- a/src/NzbDrone.Core/Validation/Paths/SystemFolderValidator.cs +++ b/src/NzbDrone.Core/Validation/Paths/SystemFolderValidator.cs @@ -6,10 +6,7 @@ namespace NzbDrone.Core.Validation.Paths { public class SystemFolderValidator : PropertyValidator { - public SystemFolderValidator() - : base("Is {relationship} system folder {systemFolder}") - { - } + protected override string GetDefaultMessageTemplate() => "Is {relationship} system folder {systemFolder}"; protected override bool IsValid(PropertyValidatorContext context) { diff --git a/src/NzbDrone.Core/Validation/UrlValidator.cs b/src/NzbDrone.Core/Validation/UrlValidator.cs index 844ab80f2..8588b4848 100644 --- a/src/NzbDrone.Core/Validation/UrlValidator.cs +++ b/src/NzbDrone.Core/Validation/UrlValidator.cs @@ -14,10 +14,7 @@ namespace NzbDrone.Core.Validation public class UrlValidator : PropertyValidator { - public UrlValidator() - : base("Invalid Url") - { - } + protected override string GetDefaultMessageTemplate() => "Invalid Url"; protected override bool IsValid(PropertyValidatorContext context) { diff --git a/src/NzbDrone.Test.Common/Prowlarr.Test.Common.csproj b/src/NzbDrone.Test.Common/Prowlarr.Test.Common.csproj index 1f680a255..5ed567e15 100644 --- a/src/NzbDrone.Test.Common/Prowlarr.Test.Common.csproj +++ b/src/NzbDrone.Test.Common/Prowlarr.Test.Common.csproj @@ -4,7 +4,7 @@ - + diff --git a/src/Prowlarr.Api.V1/Config/HostConfigController.cs b/src/Prowlarr.Api.V1/Config/HostConfigController.cs index bed2a5c54..12fcfda3a 100644 --- a/src/Prowlarr.Api.V1/Config/HostConfigController.cs +++ b/src/Prowlarr.Api.V1/Config/HostConfigController.cs @@ -51,7 +51,7 @@ namespace Prowlarr.Api.V1.Config SharedValidator.RuleFor(c => c.SslPort).NotEqual(c => c.Port).When(c => c.EnableSsl); SharedValidator.RuleFor(c => c.SslCertPath) - .Cascade(CascadeMode.StopOnFirstFailure) + .Cascade(CascadeMode.Stop) .NotEmpty() .IsValidPath() .SetValidator(fileExistsValidator) diff --git a/src/Prowlarr.Api.V1/Prowlarr.Api.V1.csproj b/src/Prowlarr.Api.V1/Prowlarr.Api.V1.csproj index fb87b706f..55b761157 100644 --- a/src/Prowlarr.Api.V1/Prowlarr.Api.V1.csproj +++ b/src/Prowlarr.Api.V1/Prowlarr.Api.V1.csproj @@ -3,7 +3,7 @@ net6.0 - + diff --git a/src/Prowlarr.Http/Prowlarr.Http.csproj b/src/Prowlarr.Http/Prowlarr.Http.csproj index 8cfc4f4b2..9d9bcbb95 100644 --- a/src/Prowlarr.Http/Prowlarr.Http.csproj +++ b/src/Prowlarr.Http/Prowlarr.Http.csproj @@ -3,7 +3,7 @@ net6.0 - + diff --git a/src/Prowlarr.Http/REST/ResourceValidator.cs b/src/Prowlarr.Http/REST/ResourceValidator.cs index de81340bb..5a4b71f61 100644 --- a/src/Prowlarr.Http/REST/ResourceValidator.cs +++ b/src/Prowlarr.Http/REST/ResourceValidator.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Linq.Expressions; using FluentValidation; using FluentValidation.Internal; -using FluentValidation.Resources; using Prowlarr.Http.ClientSchema; namespace Prowlarr.Http.REST @@ -15,7 +14,7 @@ namespace Prowlarr.Http.REST { var rule = new PropertyRule(fieldListAccessor.GetMember(), c => GetValue(c, fieldListAccessor.Compile(), fieldName), null, () => CascadeMode.Continue, typeof(TProperty), typeof(TResource)); rule.PropertyName = fieldName; - rule.DisplayName = new StaticStringSource(fieldName); + rule.SetDisplayName(fieldName); AddRule(rule); return new RuleBuilder(rule, this); @@ -25,12 +24,7 @@ namespace Prowlarr.Http.REST { var resource = fieldListAccessor((TResource)container).SingleOrDefault(c => c.Name == fieldName); - if (resource == null) - { - return null; - } - - return resource.Value; + return resource?.Value; } } } diff --git a/src/Prowlarr.Http/Validation/EmptyCollectionValidator.cs b/src/Prowlarr.Http/Validation/EmptyCollectionValidator.cs index 14c74e796..d017d91e7 100644 --- a/src/Prowlarr.Http/Validation/EmptyCollectionValidator.cs +++ b/src/Prowlarr.Http/Validation/EmptyCollectionValidator.cs @@ -6,10 +6,7 @@ namespace Prowlarr.Http.Validation { public class EmptyCollectionValidator : PropertyValidator { - public EmptyCollectionValidator() - : base("Collection Must Be Empty") - { - } + protected override string GetDefaultMessageTemplate() => "Collection Must Be Empty"; protected override bool IsValid(PropertyValidatorContext context) { diff --git a/src/Prowlarr.Http/Validation/NetImportSyncIntervalValidator.cs b/src/Prowlarr.Http/Validation/NetImportSyncIntervalValidator.cs index 68f929c7c..ff2461114 100644 --- a/src/Prowlarr.Http/Validation/NetImportSyncIntervalValidator.cs +++ b/src/Prowlarr.Http/Validation/NetImportSyncIntervalValidator.cs @@ -4,10 +4,7 @@ namespace Prowlarr.Http.Validation { public class ImportListSyncIntervalValidator : PropertyValidator { - public ImportListSyncIntervalValidator() - : base("Must be between 10 and 1440 or 0 to disable") - { - } + protected override string GetDefaultMessageTemplate() => "Must be between 10 and 1440 or 0 to disable"; protected override bool IsValid(PropertyValidatorContext context) { @@ -23,12 +20,7 @@ namespace Prowlarr.Http.Validation return true; } - if (value >= 10 && value <= 1440) - { - return true; - } - - return false; + return value is >= 10 and <= 1440; } } } diff --git a/src/Prowlarr.Http/Validation/RssSyncIntervalValidator.cs b/src/Prowlarr.Http/Validation/RssSyncIntervalValidator.cs index ccdc1de92..5137664c1 100644 --- a/src/Prowlarr.Http/Validation/RssSyncIntervalValidator.cs +++ b/src/Prowlarr.Http/Validation/RssSyncIntervalValidator.cs @@ -4,10 +4,7 @@ namespace Prowlarr.Http.Validation { public class RssSyncIntervalValidator : PropertyValidator { - public RssSyncIntervalValidator() - : base("Must be between 10 and 120 or 0 to disable") - { - } + protected override string GetDefaultMessageTemplate() => "Must be between 10 and 120 or 0 to disable"; protected override bool IsValid(PropertyValidatorContext context) { @@ -23,12 +20,7 @@ namespace Prowlarr.Http.Validation return true; } - if (value >= 10 && value <= 120) - { - return true; - } - - return false; + return value is >= 10 and <= 120; } } }