Fixed: Migrate to FluentValidation 9

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

@ -16,18 +16,7 @@ namespace NzbDrone.Common.Extensions
return false; return false;
} }
Uri uri; return Uri.TryCreate(path, UriKind.Absolute, out var uri) && uri.IsWellFormedOriginalString();
if (!Uri.TryCreate(path, UriKind.Absolute, out uri))
{
return false;
}
if (!uri.IsWellFormedOriginalString())
{
return false;
}
return true;
} }
} }
} }

@ -16,12 +16,12 @@ namespace NzbDrone.Core.Download.Clients.rTorrent
public RTorrentDirectoryValidator(PathExistsValidator pathExistsValidator, public RTorrentDirectoryValidator(PathExistsValidator pathExistsValidator,
MappedNetworkDriveValidator mappedNetworkDriveValidator) MappedNetworkDriveValidator mappedNetworkDriveValidator)
{ {
RuleFor(c => c.Directory).Cascade(CascadeMode.StopOnFirstFailure) RuleFor(c => c.Directory).Cascade(CascadeMode.Stop)
.IsValidPath() .IsValidPath()
.SetValidator(mappedNetworkDriveValidator) .SetValidator(mappedNetworkDriveValidator)
.SetValidator(pathExistsValidator) .SetValidator(pathExistsValidator)
.When(c => c.Directory.IsNotNullOrWhiteSpace()) .When(c => c.Directory.IsNotNullOrWhiteSpace())
.When(c => c.Host == "localhost" || c.Host == "127.0.0.1"); .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="System.ServiceModel.Syndication" Version="6.0.0" />
<PackageReference Include="FluentMigrator.Runner.SQLite" Version="3.3.2" /> <PackageReference Include="FluentMigrator.Runner.SQLite" Version="3.3.2" />
<PackageReference Include="FluentMigrator.Runner.Postgres" 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="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.1.0" /> <PackageReference Include="NLog" Version="5.1.0" />
<PackageReference Include="System.Data.SQLite.Core.Servarr" Version="1.0.115.5-18" /> <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; private readonly IDiskProvider _diskProvider;
public FolderChmodValidator(IDiskProvider diskProvider) public FolderChmodValidator(IDiskProvider diskProvider)
: base("Must contain a valid Unix permissions octal")
{ {
_diskProvider = diskProvider; _diskProvider = diskProvider;
} }
protected override string GetDefaultMessageTemplate() => "Must contain a valid Unix permissions octal";
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
if (context.PropertyValue == null) if (context.PropertyValue == null)

@ -5,10 +5,7 @@ namespace NzbDrone.Core.Validation
{ {
public class FolderValidator : PropertyValidator public class FolderValidator : PropertyValidator
{ {
public FolderValidator() protected override string GetDefaultMessageTemplate() => "Invalid Path";
: base("Invalid Path")
{
}
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {

@ -8,11 +8,12 @@ namespace NzbDrone.Core.Validation.Paths
private readonly IDiskProvider _diskProvider; private readonly IDiskProvider _diskProvider;
public FileExistsValidator(IDiskProvider diskProvider) public FileExistsValidator(IDiskProvider diskProvider)
: base("File does not exist")
{ {
_diskProvider = diskProvider; _diskProvider = diskProvider;
} }
protected override string GetDefaultMessageTemplate() => "File does not exist";
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
if (context.PropertyValue == null) if (context.PropertyValue == null)

@ -9,11 +9,12 @@ namespace NzbDrone.Core.Validation.Paths
private readonly IDiskProvider _diskProvider; private readonly IDiskProvider _diskProvider;
public FolderWritableValidator(IDiskProvider diskProvider) public FolderWritableValidator(IDiskProvider diskProvider)
: base($"Folder is not writable by user {Environment.UserName}")
{ {
_diskProvider = diskProvider; _diskProvider = diskProvider;
} }
protected override string GetDefaultMessageTemplate() => $"Folder is not writable by user {Environment.UserName}";
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
if (context.PropertyValue == null) 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); private static readonly Regex DriveRegex = new Regex(@"[a-z]\:\\", RegexOptions.Compiled | RegexOptions.IgnoreCase);
public MappedNetworkDriveValidator(IRuntimeInfo runtimeInfo, IDiskProvider diskProvider) public MappedNetworkDriveValidator(IRuntimeInfo runtimeInfo, IDiskProvider diskProvider)
: base("Mapped Network Drive and Windows Service")
{ {
_runtimeInfo = runtimeInfo; _runtimeInfo = runtimeInfo;
_diskProvider = diskProvider; _diskProvider = diskProvider;
} }
protected override string GetDefaultMessageTemplate() => "Mapped Network Drive and Windows Service";
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
if (context.PropertyValue == null) if (context.PropertyValue == null)
@ -46,12 +47,7 @@ namespace NzbDrone.Core.Validation.Paths
var mount = _diskProvider.GetMount(path); var mount = _diskProvider.GetMount(path);
if (mount != null && mount.DriveType == DriveType.Network) return mount is not { DriveType: DriveType.Network };
{
return false;
}
return true;
} }
} }
} }

@ -8,11 +8,12 @@ namespace NzbDrone.Core.Validation.Paths
private readonly IDiskProvider _diskProvider; private readonly IDiskProvider _diskProvider;
public PathExistsValidator(IDiskProvider diskProvider) public PathExistsValidator(IDiskProvider diskProvider)
: base("Path does not exist")
{ {
_diskProvider = diskProvider; _diskProvider = diskProvider;
} }
protected override string GetDefaultMessageTemplate() => "Path does not exist";
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
if (context.PropertyValue == null) if (context.PropertyValue == null)

@ -14,10 +14,7 @@ namespace NzbDrone.Core.Validation.Paths
public class PathValidator : PropertyValidator public class PathValidator : PropertyValidator
{ {
public PathValidator() protected override string GetDefaultMessageTemplate() => "Invalid Path";
: base("Invalid Path")
{
}
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {

@ -9,11 +9,12 @@ namespace NzbDrone.Core.Validation.Paths
private readonly IAppFolderInfo _appFolderInfo; private readonly IAppFolderInfo _appFolderInfo;
public StartupFolderValidator(IAppFolderInfo appFolderInfo) public StartupFolderValidator(IAppFolderInfo appFolderInfo)
: base("Path cannot be an ancestor of the start up folder")
{ {
_appFolderInfo = appFolderInfo; _appFolderInfo = appFolderInfo;
} }
protected override string GetDefaultMessageTemplate() => "Path cannot be an ancestor of the start up folder";
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
if (context.PropertyValue == null) if (context.PropertyValue == null)

@ -6,10 +6,7 @@ namespace NzbDrone.Core.Validation.Paths
{ {
public class SystemFolderValidator : PropertyValidator public class SystemFolderValidator : PropertyValidator
{ {
public SystemFolderValidator() protected override string GetDefaultMessageTemplate() => "Is {relationship} system folder {systemFolder}";
: base("Is {relationship} system folder {systemFolder}")
{
}
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {

@ -14,10 +14,7 @@ namespace NzbDrone.Core.Validation
public class UrlValidator : PropertyValidator public class UrlValidator : PropertyValidator
{ {
public UrlValidator() protected override string GetDefaultMessageTemplate() => "Invalid Url";
: base("Invalid Url")
{
}
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {

@ -4,7 +4,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" /> <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="Moq" Version="4.17.2" />
<PackageReference Include="NLog" Version="5.1.0" /> <PackageReference Include="NLog" Version="5.1.0" />
<PackageReference Include="NUnit" Version="3.13.3" /> <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.SslPort).NotEqual(c => c.Port).When(c => c.EnableSsl);
SharedValidator.RuleFor(c => c.SslCertPath) SharedValidator.RuleFor(c => c.SslCertPath)
.Cascade(CascadeMode.StopOnFirstFailure) .Cascade(CascadeMode.Stop)
.NotEmpty() .NotEmpty()
.IsValidPath() .IsValidPath()
.SetValidator(fileExistsValidator) .SetValidator(fileExistsValidator)

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

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

@ -4,7 +4,6 @@ using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using FluentValidation; using FluentValidation;
using FluentValidation.Internal; using FluentValidation.Internal;
using FluentValidation.Resources;
using Prowlarr.Http.ClientSchema; using Prowlarr.Http.ClientSchema;
namespace Prowlarr.Http.REST 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)); var rule = new PropertyRule(fieldListAccessor.GetMember(), c => GetValue(c, fieldListAccessor.Compile(), fieldName), null, () => CascadeMode.Continue, typeof(TProperty), typeof(TResource));
rule.PropertyName = fieldName; rule.PropertyName = fieldName;
rule.DisplayName = new StaticStringSource(fieldName); rule.SetDisplayName(fieldName);
AddRule(rule); AddRule(rule);
return new RuleBuilder<TResource, TProperty>(rule, this); 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); var resource = fieldListAccessor((TResource)container).SingleOrDefault(c => c.Name == fieldName);
if (resource == null) return resource?.Value;
{
return null;
}
return resource.Value;
} }
} }
} }

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

@ -4,10 +4,7 @@ namespace Prowlarr.Http.Validation
{ {
public class ImportListSyncIntervalValidator : PropertyValidator public class ImportListSyncIntervalValidator : PropertyValidator
{ {
public ImportListSyncIntervalValidator() protected override string GetDefaultMessageTemplate() => "Must be between 10 and 1440 or 0 to disable";
: base("Must be between 10 and 1440 or 0 to disable")
{
}
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
@ -23,12 +20,7 @@ namespace Prowlarr.Http.Validation
return true; return true;
} }
if (value >= 10 && value <= 1440) return value is >= 10 and <= 1440;
{
return true;
}
return false;
} }
} }
} }

@ -4,10 +4,7 @@ namespace Prowlarr.Http.Validation
{ {
public class RssSyncIntervalValidator : PropertyValidator public class RssSyncIntervalValidator : PropertyValidator
{ {
public RssSyncIntervalValidator() protected override string GetDefaultMessageTemplate() => "Must be between 10 and 120 or 0 to disable";
: base("Must be between 10 and 120 or 0 to disable")
{
}
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
@ -23,12 +20,7 @@ namespace Prowlarr.Http.Validation
return true; return true;
} }
if (value >= 10 && value <= 120) return value is >= 10 and <= 120;
{
return true;
}
return false;
} }
} }
} }

Loading…
Cancel
Save