From d2787d818131119b513c10b71e235631ed6bc33a Mon Sep 17 00:00:00 2001 From: Stepan Goremykin Date: Sat, 1 Apr 2023 15:03:53 +0200 Subject: [PATCH] Migrate to FluentValidation 9 (cherry picked from commit 40e54685b9e83ed24a3979bfe965c453339ad02e) --- .../Extensions/UrlExtensions.cs | 13 +-- .../EmailSettingsValidatorFixture.cs | 3 - .../rTorrent/RTorrentDirectoryValidator.cs | 2 +- src/NzbDrone.Core/Movies/AddMovieValidator.cs | 2 +- .../Organizer/FileNameValidation.cs | 2 +- .../Delay/DelayProfileTagInUseValidator.cs | 7 +- src/NzbDrone.Core/Radarr.Core.csproj | 2 +- .../Validation/FolderChmodValidator.cs | 3 +- .../Validation/FolderValidator.cs | 5 +- .../Validation/Paths/FileExistsValidator.cs | 3 +- .../Paths/FolderWritableValidator.cs | 3 +- .../Paths/MappedNetworkDriveValidator.cs | 10 +-- .../Paths/MovieAncestorValidator.cs | 3 +- .../Validation/Paths/MovieExistsValidator.cs | 3 +- .../Validation/Paths/MoviePathValidation.cs | 3 +- .../Validation/Paths/PathExistsValidator.cs | 3 +- .../Validation/Paths/PathValidator.cs | 5 +- .../Validation/Paths/RecycleBinValidator.cs | 3 +- .../Paths/RootFolderAncestorValidator.cs | 3 +- .../Validation/Paths/RootFolderValidator.cs | 3 +- .../Paths/StartupFolderValidator.cs | 3 +- .../Validation/Paths/SystemFolderValidator.cs | 5 +- .../Validation/ProfileExistsValidator.cs | 3 +- src/NzbDrone.Core/Validation/UrlValidator.cs | 5 +- .../Radarr.Test.Common.csproj | 2 +- .../Config/HostConfigController.cs | 2 +- src/Radarr.Api.V3/Movies/MovieController.cs | 2 +- .../MovieFolderAsRootFolderValidator.cs | 7 +- .../Quality/QualityCutoffValidator.cs | 19 +--- .../Profiles/Quality/QualityItemsValidator.cs | 89 ++++++------------- src/Radarr.Api.V3/Radarr.Api.V3.csproj | 2 +- .../RemotePathMappingController.cs | 4 +- .../RootFolders/RootFolderController.cs | 4 +- src/Radarr.Http/REST/ResourceValidator.cs | 12 +-- src/Radarr.Http/Radarr.Http.csproj | 2 +- .../Validation/EmptyCollectionValidator.cs | 5 +- .../NetImportSyncIntervalValidator.cs | 12 +-- .../Validation/RssSyncIntervalValidator.cs | 12 +-- src/Radarr.sln.DotSettings | 3 + 39 files changed, 93 insertions(+), 181 deletions(-) create mode 100644 src/Radarr.sln.DotSettings 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.Test/NotificationTests/EmailTests/EmailSettingsValidatorFixture.cs b/src/NzbDrone.Core.Test/NotificationTests/EmailTests/EmailSettingsValidatorFixture.cs index 178ddf2a4..1b739d73e 100644 --- a/src/NzbDrone.Core.Test/NotificationTests/EmailTests/EmailSettingsValidatorFixture.cs +++ b/src/NzbDrone.Core.Test/NotificationTests/EmailTests/EmailSettingsValidatorFixture.cs @@ -61,7 +61,6 @@ namespace NzbDrone.Core.Test.NotificationTests.EmailTests } [TestCase("radarr")] - [TestCase("radarr@radarr")] [TestCase("radarr.video")] public void should_not_be_valid_if_to_is_invalid(string email) { @@ -71,7 +70,6 @@ namespace NzbDrone.Core.Test.NotificationTests.EmailTests } [TestCase("radarr")] - [TestCase("radarr@radarr")] [TestCase("radarr.video")] public void should_not_be_valid_if_cc_is_invalid(string email) { @@ -81,7 +79,6 @@ namespace NzbDrone.Core.Test.NotificationTests.EmailTests } [TestCase("radarr")] - [TestCase("radarr@radarr")] [TestCase("radarr.video")] public void should_not_be_valid_if_bcc_is_invalid(string email) { diff --git a/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentDirectoryValidator.cs b/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentDirectoryValidator.cs index 33bbf115a..a8c047268 100644 --- a/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentDirectoryValidator.cs +++ b/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentDirectoryValidator.cs @@ -17,7 +17,7 @@ namespace NzbDrone.Core.Download.Clients.rTorrent PathExistsValidator pathExistsValidator, MappedNetworkDriveValidator mappedNetworkDriveValidator) { - RuleFor(c => c.MovieDirectory).Cascade(CascadeMode.StopOnFirstFailure) + RuleFor(c => c.MovieDirectory).Cascade(CascadeMode.Stop) .IsValidPath() .SetValidator(rootFolderValidator) .SetValidator(mappedNetworkDriveValidator) diff --git a/src/NzbDrone.Core/Movies/AddMovieValidator.cs b/src/NzbDrone.Core/Movies/AddMovieValidator.cs index 00fdee689..0d52d8d2c 100644 --- a/src/NzbDrone.Core/Movies/AddMovieValidator.cs +++ b/src/NzbDrone.Core/Movies/AddMovieValidator.cs @@ -16,7 +16,7 @@ namespace NzbDrone.Core.Movies MoviePathValidator moviePathValidator, MovieAncestorValidator movieAncestorValidator) { - RuleFor(c => c.Path).Cascade(CascadeMode.StopOnFirstFailure) + RuleFor(c => c.Path).Cascade(CascadeMode.Stop) .IsValidPath() .SetValidator(rootFolderValidator) .SetValidator(recycleBinValidator) diff --git a/src/NzbDrone.Core/Organizer/FileNameValidation.cs b/src/NzbDrone.Core/Organizer/FileNameValidation.cs index 95e4cd3db..0352e7ba7 100644 --- a/src/NzbDrone.Core/Organizer/FileNameValidation.cs +++ b/src/NzbDrone.Core/Organizer/FileNameValidation.cs @@ -1,4 +1,4 @@ -using FluentValidation; +using FluentValidation; using FluentValidation.Validators; namespace NzbDrone.Core.Organizer diff --git a/src/NzbDrone.Core/Profiles/Delay/DelayProfileTagInUseValidator.cs b/src/NzbDrone.Core/Profiles/Delay/DelayProfileTagInUseValidator.cs index cfd08a2f4..124d75e54 100644 --- a/src/NzbDrone.Core/Profiles/Delay/DelayProfileTagInUseValidator.cs +++ b/src/NzbDrone.Core/Profiles/Delay/DelayProfileTagInUseValidator.cs @@ -10,11 +10,12 @@ namespace NzbDrone.Core.Profiles.Delay private readonly IDelayProfileService _delayProfileService; public DelayProfileTagInUseValidator(IDelayProfileService delayProfileService) - : base("One or more tags is used in another profile") { _delayProfileService = delayProfileService; } + protected override string GetDefaultMessageTemplate() => "One or more tags is used in another profile"; + protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) @@ -25,9 +26,7 @@ namespace NzbDrone.Core.Profiles.Delay dynamic instance = context.ParentContext.InstanceToValidate; var instanceId = (int)instance.Id; - var collection = context.PropertyValue as HashSet; - - if (collection == null || collection.Empty()) + if (context.PropertyValue is not HashSet collection || collection.Empty()) { return true; } diff --git a/src/NzbDrone.Core/Radarr.Core.csproj b/src/NzbDrone.Core/Radarr.Core.csproj index 992ba179a..c1de3e918 100644 --- a/src/NzbDrone.Core/Radarr.Core.csproj +++ b/src/NzbDrone.Core/Radarr.Core.csproj @@ -16,7 +16,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/MovieAncestorValidator.cs b/src/NzbDrone.Core/Validation/Paths/MovieAncestorValidator.cs index 13c673771..48fa1119f 100644 --- a/src/NzbDrone.Core/Validation/Paths/MovieAncestorValidator.cs +++ b/src/NzbDrone.Core/Validation/Paths/MovieAncestorValidator.cs @@ -10,11 +10,12 @@ namespace NzbDrone.Core.Validation.Paths private readonly IMovieService _movieService; public MovieAncestorValidator(IMovieService movieService) - : base("Path is an ancestor of an existing movie") { _movieService = movieService; } + protected override string GetDefaultMessageTemplate() => "Path is an ancestor of an existing movie"; + protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) diff --git a/src/NzbDrone.Core/Validation/Paths/MovieExistsValidator.cs b/src/NzbDrone.Core/Validation/Paths/MovieExistsValidator.cs index 656e10872..1e45a23a7 100644 --- a/src/NzbDrone.Core/Validation/Paths/MovieExistsValidator.cs +++ b/src/NzbDrone.Core/Validation/Paths/MovieExistsValidator.cs @@ -8,11 +8,12 @@ namespace NzbDrone.Core.Validation.Paths private readonly IMovieService _movieService; public MovieExistsValidator(IMovieService movieService) - : base("This movie has already been added") { _movieService = movieService; } + protected override string GetDefaultMessageTemplate() => "This movie has already been added"; + protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) diff --git a/src/NzbDrone.Core/Validation/Paths/MoviePathValidation.cs b/src/NzbDrone.Core/Validation/Paths/MoviePathValidation.cs index e613c8127..0010d4a7b 100644 --- a/src/NzbDrone.Core/Validation/Paths/MoviePathValidation.cs +++ b/src/NzbDrone.Core/Validation/Paths/MoviePathValidation.cs @@ -10,11 +10,12 @@ namespace NzbDrone.Core.Validation.Paths private readonly IMovieService _moviesService; public MoviePathValidator(IMovieService moviesService) - : base("Path is already configured for an existing movie: {moviePath}") { _moviesService = moviesService; } + protected override string GetDefaultMessageTemplate() => "Path is already configured for an existing movie: {moviePath}"; + protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) 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/RecycleBinValidator.cs b/src/NzbDrone.Core/Validation/Paths/RecycleBinValidator.cs index a8a714fe6..1299b72ac 100644 --- a/src/NzbDrone.Core/Validation/Paths/RecycleBinValidator.cs +++ b/src/NzbDrone.Core/Validation/Paths/RecycleBinValidator.cs @@ -9,11 +9,12 @@ namespace NzbDrone.Core.Validation.Paths private readonly IConfigService _configService; public RecycleBinValidator(IConfigService configService) - : base("Path is {relationship} configured recycle bin folder") { _configService = configService; } + protected override string GetDefaultMessageTemplate() => "Path is {relationship} configured recycle bin folder"; + protected override bool IsValid(PropertyValidatorContext context) { var recycleBin = _configService.RecycleBin; diff --git a/src/NzbDrone.Core/Validation/Paths/RootFolderAncestorValidator.cs b/src/NzbDrone.Core/Validation/Paths/RootFolderAncestorValidator.cs index 19f0800b5..1b7dd0bfa 100644 --- a/src/NzbDrone.Core/Validation/Paths/RootFolderAncestorValidator.cs +++ b/src/NzbDrone.Core/Validation/Paths/RootFolderAncestorValidator.cs @@ -10,11 +10,12 @@ namespace NzbDrone.Core.Validation.Paths private readonly IRootFolderService _rootFolderService; public RootFolderAncestorValidator(IRootFolderService rootFolderService) - : base("Path is an ancestor of an existing root folder") { _rootFolderService = rootFolderService; } + protected override string GetDefaultMessageTemplate() => "Path is an ancestor of an existing root folder"; + protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) diff --git a/src/NzbDrone.Core/Validation/Paths/RootFolderValidator.cs b/src/NzbDrone.Core/Validation/Paths/RootFolderValidator.cs index 8494ac28b..7ceffdd7a 100644 --- a/src/NzbDrone.Core/Validation/Paths/RootFolderValidator.cs +++ b/src/NzbDrone.Core/Validation/Paths/RootFolderValidator.cs @@ -9,11 +9,12 @@ namespace NzbDrone.Core.Validation.Paths private readonly IRootFolderService _rootFolderService; public RootFolderValidator(IRootFolderService rootFolderService) - : base("Path is already configured as a root folder") { _rootFolderService = rootFolderService; } + protected override string GetDefaultMessageTemplate() => "Path is already configured as a root folder"; + protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) diff --git a/src/NzbDrone.Core/Validation/Paths/StartupFolderValidator.cs b/src/NzbDrone.Core/Validation/Paths/StartupFolderValidator.cs index 9f097f8d2..0243923f1 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 {relationship} the start up folder") { _appFolderInfo = appFolderInfo; } + protected override string GetDefaultMessageTemplate() => "Path cannot be {relationship} 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/ProfileExistsValidator.cs b/src/NzbDrone.Core/Validation/ProfileExistsValidator.cs index d1875ecea..890f91c14 100644 --- a/src/NzbDrone.Core/Validation/ProfileExistsValidator.cs +++ b/src/NzbDrone.Core/Validation/ProfileExistsValidator.cs @@ -8,11 +8,12 @@ namespace NzbDrone.Core.Validation private readonly IProfileService _profileService; public ProfileExistsValidator(IProfileService profileService) - : base("QualityProfile does not exist") { _profileService = profileService; } + protected override string GetDefaultMessageTemplate() => "QualityProfile does not exist"; + protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) 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/Radarr.Test.Common.csproj b/src/NzbDrone.Test.Common/Radarr.Test.Common.csproj index 4a8f14b62..dc0a665ec 100644 --- a/src/NzbDrone.Test.Common/Radarr.Test.Common.csproj +++ b/src/NzbDrone.Test.Common/Radarr.Test.Common.csproj @@ -4,7 +4,7 @@ - + diff --git a/src/Radarr.Api.V3/Config/HostConfigController.cs b/src/Radarr.Api.V3/Config/HostConfigController.cs index 0e8b92869..23fb54b60 100644 --- a/src/Radarr.Api.V3/Config/HostConfigController.cs +++ b/src/Radarr.Api.V3/Config/HostConfigController.cs @@ -51,7 +51,7 @@ namespace Radarr.Api.V3.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/Radarr.Api.V3/Movies/MovieController.cs b/src/Radarr.Api.V3/Movies/MovieController.cs index 538311826..aca0b64e3 100644 --- a/src/Radarr.Api.V3/Movies/MovieController.cs +++ b/src/Radarr.Api.V3/Movies/MovieController.cs @@ -81,7 +81,7 @@ namespace Radarr.Api.V3.Movies SharedValidator.RuleFor(s => s.QualityProfileId).ValidId(); SharedValidator.RuleFor(s => s.Path) - .Cascade(CascadeMode.StopOnFirstFailure) + .Cascade(CascadeMode.Stop) .IsValidPath() .SetValidator(rootFolderValidator) .SetValidator(mappedNetworkDriveValidator) diff --git a/src/Radarr.Api.V3/Movies/MovieFolderAsRootFolderValidator.cs b/src/Radarr.Api.V3/Movies/MovieFolderAsRootFolderValidator.cs index efa97a408..2bb46a9d0 100644 --- a/src/Radarr.Api.V3/Movies/MovieFolderAsRootFolderValidator.cs +++ b/src/Radarr.Api.V3/Movies/MovieFolderAsRootFolderValidator.cs @@ -11,11 +11,12 @@ namespace Radarr.Api.V3.Movies private readonly IBuildFileNames _fileNameBuilder; public MovieFolderAsRootFolderValidator(IBuildFileNames fileNameBuilder) - : base("Root folder path contains movie folder") { _fileNameBuilder = fileNameBuilder; } + protected override string GetDefaultMessageTemplate() => "Root folder path contains movie folder"; + protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) @@ -23,7 +24,7 @@ namespace Radarr.Api.V3.Movies return true; } - var movieResource = context.Instance as MovieResource; + var movieResource = context.InstanceToValidate as MovieResource; if (movieResource == null) { @@ -37,7 +38,7 @@ namespace Radarr.Api.V3.Movies return true; } - var rootFolder = new DirectoryInfo(rootFolderPath).Name; + var rootFolder = new DirectoryInfo(rootFolderPath!).Name; var movie = movieResource.ToModel(); var movieFolder = _fileNameBuilder.GetMovieFolder(movie); diff --git a/src/Radarr.Api.V3/Profiles/Quality/QualityCutoffValidator.cs b/src/Radarr.Api.V3/Profiles/Quality/QualityCutoffValidator.cs index 8ef307784..f95718dff 100644 --- a/src/Radarr.Api.V3/Profiles/Quality/QualityCutoffValidator.cs +++ b/src/Radarr.Api.V3/Profiles/Quality/QualityCutoffValidator.cs @@ -15,10 +15,7 @@ namespace Radarr.Api.V3.Profiles.Quality public class ValidCutoffValidator : PropertyValidator { - public ValidCutoffValidator() - : base("Cutoff must be an allowed quality or group") - { - } + protected override string GetDefaultMessageTemplate() => "Cutoff must be an allowed quality or group"; protected override bool IsValid(PropertyValidatorContext context) { @@ -26,19 +23,9 @@ namespace Radarr.Api.V3.Profiles.Quality dynamic instance = context.ParentContext.InstanceToValidate; var items = instance.Items as IList; - var cutoffItem = items.SingleOrDefault(i => (i.Quality == null && i.Id == cutoff) || i.Quality?.Id == cutoff); - - if (cutoffItem == null) - { - return false; - } - - if (!cutoffItem.Allowed) - { - return false; - } + var cutoffItem = items?.SingleOrDefault(i => (i.Quality == null && i.Id == cutoff) || i.Quality?.Id == cutoff); - return true; + return cutoffItem is { Allowed: true }; } } } diff --git a/src/Radarr.Api.V3/Profiles/Quality/QualityItemsValidator.cs b/src/Radarr.Api.V3/Profiles/Quality/QualityItemsValidator.cs index ba74a24c8..1d0bc491c 100644 --- a/src/Radarr.Api.V3/Profiles/Quality/QualityItemsValidator.cs +++ b/src/Radarr.Api.V3/Profiles/Quality/QualityItemsValidator.cs @@ -23,139 +23,104 @@ namespace Radarr.Api.V3.Profiles.Quality public class AllowedValidator : PropertyValidator { - public AllowedValidator() - : base("Must contain at least one allowed quality") - { - } + protected override string GetDefaultMessageTemplate() => "Must contain at least one allowed quality"; protected override bool IsValid(PropertyValidatorContext context) { - var list = context.PropertyValue as IList; - - if (list == null) - { - return false; - } - - if (!list.Any(c => c.Allowed)) - { - return false; - } - - return true; + return context.PropertyValue is IList list && + list.Any(c => c.Allowed); } } public class GroupItemValidator : PropertyValidator { - public GroupItemValidator() - : base("Groups must contain multiple qualities") - { - } + protected override string GetDefaultMessageTemplate() => "Groups must contain multiple qualities"; protected override bool IsValid(PropertyValidatorContext context) { - var items = context.PropertyValue as IList; - - if (items.Any(i => i.Name.IsNotNullOrWhiteSpace() && i.Items.Count <= 1)) + if (context.PropertyValue is not IList items) { return false; } - return true; + return !items.Any(i => i.Name.IsNotNullOrWhiteSpace() && i.Items.Count <= 1); } } public class QualityNameValidator : PropertyValidator { - public QualityNameValidator() - : base("Individual qualities should not be named") - { - } + protected override string GetDefaultMessageTemplate() => "Individual qualities should not be named"; protected override bool IsValid(PropertyValidatorContext context) { - var items = context.PropertyValue as IList; - - if (items.Any(i => i.Name.IsNotNullOrWhiteSpace() && i.Quality != null)) + if (context.PropertyValue is not IList items) { return false; } - return true; + return !items.Any(i => i.Name.IsNotNullOrWhiteSpace() && i.Quality != null); } } public class ItemGroupNameValidator : PropertyValidator { - public ItemGroupNameValidator() - : base("Groups must have a name") - { - } + protected override string GetDefaultMessageTemplate() => "Groups must have a name"; protected override bool IsValid(PropertyValidatorContext context) { - var items = context.PropertyValue as IList; - - if (items.Any(i => i.Quality == null && i.Name.IsNullOrWhiteSpace())) + if (context.PropertyValue is not IList items) { return false; } - return true; + return !items.Any(i => i.Quality == null && i.Name.IsNullOrWhiteSpace()); } } public class ItemGroupIdValidator : PropertyValidator { - public ItemGroupIdValidator() - : base("Groups must have an ID") - { - } + protected override string GetDefaultMessageTemplate() => "Groups must have an ID"; protected override bool IsValid(PropertyValidatorContext context) { - var items = context.PropertyValue as IList; - - if (items.Any(i => i.Quality == null && i.Id == 0)) + if (context.PropertyValue is not IList items) { return false; } - return true; + return !items.Any(i => i.Quality == null && i.Id == 0); } } public class UniqueIdValidator : PropertyValidator { - public UniqueIdValidator() - : base("Groups must have a unique ID") - { - } + protected override string GetDefaultMessageTemplate() => "Groups must have a unique ID"; protected override bool IsValid(PropertyValidatorContext context) { - var items = context.PropertyValue as IList; - - if (items.Where(i => i.Id > 0).Select(i => i.Id).GroupBy(i => i).Any(g => g.Count() > 1)) + if (context.PropertyValue is not IList items) { return false; } - return true; + var ids = items.Where(i => i.Id > 0).Select(i => i.Id); + var groupedIds = ids.GroupBy(i => i); + + return groupedIds.All(g => g.Count() == 1); } } public class UniqueQualityIdValidator : PropertyValidator { - public UniqueQualityIdValidator() - : base("Qualities can only be used once") - { - } + protected override string GetDefaultMessageTemplate() => "Qualities can only be used once"; protected override bool IsValid(PropertyValidatorContext context) { - var items = context.PropertyValue as IList; + if (context.PropertyValue is not IList items) + { + return false; + } + var qualityIds = new HashSet(); foreach (var item in items) diff --git a/src/Radarr.Api.V3/Radarr.Api.V3.csproj b/src/Radarr.Api.V3/Radarr.Api.V3.csproj index 44e8380fb..7bbd91377 100644 --- a/src/Radarr.Api.V3/Radarr.Api.V3.csproj +++ b/src/Radarr.Api.V3/Radarr.Api.V3.csproj @@ -3,7 +3,7 @@ net6.0 - + diff --git a/src/Radarr.Api.V3/RemotePathMappings/RemotePathMappingController.cs b/src/Radarr.Api.V3/RemotePathMappings/RemotePathMappingController.cs index bc9e7f758..848138524 100644 --- a/src/Radarr.Api.V3/RemotePathMappings/RemotePathMappingController.cs +++ b/src/Radarr.Api.V3/RemotePathMappings/RemotePathMappingController.cs @@ -28,8 +28,8 @@ namespace Radarr.Api.V3.RemotePathMappings .NotEmpty(); SharedValidator.RuleFor(c => c.LocalPath) - .Cascade(CascadeMode.StopOnFirstFailure) - .IsValidPath() + .Cascade(CascadeMode.Stop) + .IsValidPath() .SetValidator(mappedNetworkDriveValidator) .SetValidator(pathExistsValidator); } diff --git a/src/Radarr.Api.V3/RootFolders/RootFolderController.cs b/src/Radarr.Api.V3/RootFolders/RootFolderController.cs index d3be17883..0912b6e0b 100644 --- a/src/Radarr.Api.V3/RootFolders/RootFolderController.cs +++ b/src/Radarr.Api.V3/RootFolders/RootFolderController.cs @@ -30,8 +30,8 @@ namespace Radarr.Api.V3.RootFolders _rootFolderService = rootFolderService; SharedValidator.RuleFor(c => c.Path) - .Cascade(CascadeMode.StopOnFirstFailure) - .IsValidPath() + .Cascade(CascadeMode.Stop) + .IsValidPath() .SetValidator(rootFolderValidator) .SetValidator(mappedNetworkDriveValidator) .SetValidator(startupFolderValidator) diff --git a/src/Radarr.Http/REST/ResourceValidator.cs b/src/Radarr.Http/REST/ResourceValidator.cs index 1c6ccd496..a1d2a8dbc 100644 --- a/src/Radarr.Http/REST/ResourceValidator.cs +++ b/src/Radarr.Http/REST/ResourceValidator.cs @@ -1,10 +1,9 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using FluentValidation; using FluentValidation.Internal; -using FluentValidation.Resources; using Radarr.Http.ClientSchema; namespace Radarr.Http.REST @@ -15,7 +14,7 @@ namespace Radarr.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 Radarr.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/Radarr.Http/Radarr.Http.csproj b/src/Radarr.Http/Radarr.Http.csproj index 098bf3a28..56ac3996d 100644 --- a/src/Radarr.Http/Radarr.Http.csproj +++ b/src/Radarr.Http/Radarr.Http.csproj @@ -3,7 +3,7 @@ net6.0 - + diff --git a/src/Radarr.Http/Validation/EmptyCollectionValidator.cs b/src/Radarr.Http/Validation/EmptyCollectionValidator.cs index 835832618..3fc46d12e 100644 --- a/src/Radarr.Http/Validation/EmptyCollectionValidator.cs +++ b/src/Radarr.Http/Validation/EmptyCollectionValidator.cs @@ -6,10 +6,7 @@ namespace Radarr.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/Radarr.Http/Validation/NetImportSyncIntervalValidator.cs b/src/Radarr.Http/Validation/NetImportSyncIntervalValidator.cs index c2719787d..fcbb691a9 100644 --- a/src/Radarr.Http/Validation/NetImportSyncIntervalValidator.cs +++ b/src/Radarr.Http/Validation/NetImportSyncIntervalValidator.cs @@ -4,10 +4,7 @@ namespace Radarr.Http.Validation { public class ImportListSyncIntervalValidator : PropertyValidator { - public ImportListSyncIntervalValidator() - : base("Must be greater than 6 hours") - { - } + protected override string GetDefaultMessageTemplate() => "Must be greater than 6 hours"; protected override bool IsValid(PropertyValidatorContext context) { @@ -18,12 +15,7 @@ namespace Radarr.Http.Validation var value = (int)context.PropertyValue; - if (value >= 6) - { - return true; - } - - return false; + return value >= 6; } } } diff --git a/src/Radarr.Http/Validation/RssSyncIntervalValidator.cs b/src/Radarr.Http/Validation/RssSyncIntervalValidator.cs index 76063ce28..0e47a4b74 100644 --- a/src/Radarr.Http/Validation/RssSyncIntervalValidator.cs +++ b/src/Radarr.Http/Validation/RssSyncIntervalValidator.cs @@ -4,10 +4,7 @@ namespace Radarr.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 Radarr.Http.Validation return true; } - if (value >= 10 && value <= 120) - { - return true; - } - - return false; + return value is >= 10 and <= 120; } } } diff --git a/src/Radarr.sln.DotSettings b/src/Radarr.sln.DotSettings new file mode 100644 index 000000000..c09f7c54f --- /dev/null +++ b/src/Radarr.sln.DotSettings @@ -0,0 +1,3 @@ + + True + True \ No newline at end of file