Fixed: Migrate to FluentValidation 9

pull/1616/head
Bogdan 2 years ago
parent 1c173fc984
commit 88e3f86262

@ -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();
}
}
}

@ -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");
}
}
}

@ -15,7 +15,7 @@
<PackageReference Include="System.ServiceModel.Syndication" Version="6.0.0" />
<PackageReference Include="FluentMigrator.Runner.SQLite" Version="3.3.2" />
<PackageReference Include="FluentMigrator.Runner.Postgres" Version="3.3.2" />
<PackageReference Include="FluentValidation" Version="8.6.2" />
<PackageReference Include="FluentValidation" Version="9.5.4" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.1.0" />
<PackageReference Include="System.Data.SQLite.Core.Servarr" Version="1.0.115.5-18" />

@ -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)

@ -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)
{

@ -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)

@ -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)

@ -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 };
}
}
}

@ -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)

@ -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)
{

@ -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)

@ -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)
{

@ -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)
{

@ -4,7 +4,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="FluentValidation" Version="8.6.2" />
<PackageReference Include="FluentValidation" Version="9.5.4" />
<PackageReference Include="Moq" Version="4.17.2" />
<PackageReference Include="NLog" Version="5.1.0" />
<PackageReference Include="NUnit" Version="3.13.3" />

@ -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)

@ -3,7 +3,7 @@
<TargetFrameworks>net6.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentValidation" Version="8.6.2" />
<PackageReference Include="FluentValidation" Version="9.5.4" />
<PackageReference Include="NLog" Version="5.1.0" />
</ItemGroup>
<ItemGroup>

@ -3,7 +3,7 @@
<TargetFrameworks>net6.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentValidation" Version="8.6.2" />
<PackageReference Include="FluentValidation" Version="9.5.4" />
<PackageReference Include="ImpromptuInterface" Version="7.0.1" />
<PackageReference Include="NLog" Version="5.1.0" />
</ItemGroup>

@ -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<TResource, TProperty>(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;
}
}
}

@ -6,10 +6,7 @@ namespace Prowlarr.Http.Validation
{
public class EmptyCollectionValidator<T> : PropertyValidator
{
public EmptyCollectionValidator()
: base("Collection Must Be Empty")
{
}
protected override string GetDefaultMessageTemplate() => "Collection Must Be Empty";
protected override bool IsValid(PropertyValidatorContext context)
{

@ -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;
}
}
}

@ -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;
}
}
}

Loading…
Cancel
Save