Fixed: Migrate to FluentValidation 9

pull/3582/head
Bogdan 1 year ago committed by Qstick
parent 9259ad7d49
commit ea0a78b620

@ -78,7 +78,7 @@ namespace Lidarr.Api.V1.Artist
Http.Validation.RuleBuilderExtensions.ValidId(SharedValidator.RuleFor(s => s.MetadataProfileId)); Http.Validation.RuleBuilderExtensions.ValidId(SharedValidator.RuleFor(s => s.MetadataProfileId));
SharedValidator.RuleFor(s => s.Path) SharedValidator.RuleFor(s => s.Path)
.Cascade(CascadeMode.StopOnFirstFailure) .Cascade(CascadeMode.Stop)
.IsValidPath() .IsValidPath()
.SetValidator(rootFolderValidator) .SetValidator(rootFolderValidator)
.SetValidator(mappedNetworkDriveValidator) .SetValidator(mappedNetworkDriveValidator)

@ -51,7 +51,7 @@ namespace Lidarr.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)

@ -10,7 +10,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.7.0" /> <PackageReference Include="System.Reflection.TypeExtensions" Version="4.7.0" />
<PackageReference Include="FluentValidation" Version="8.6.2" /> <PackageReference Include="FluentValidation" Version="9.5.4" />
<PackageReference Include="Ical.Net" Version="4.2.0" /> <PackageReference Include="Ical.Net" Version="4.2.0" />
<PackageReference Include="NLog" Version="5.0.4" /> <PackageReference Include="NLog" Version="5.0.4" />
<PackageReference Include="System.IO.Abstractions" Version="17.0.24" /> <PackageReference Include="System.IO.Abstractions" Version="17.0.24" />

@ -31,76 +31,34 @@ namespace Lidarr.Api.V1.Profiles.Metadata
public class PrimaryTypeValidator<T> : PropertyValidator public class PrimaryTypeValidator<T> : PropertyValidator
{ {
public PrimaryTypeValidator() protected override string GetDefaultMessageTemplate() => "Must have at least one allowed primary type";
: base("Must have at least one allowed primary type")
{
}
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
var list = context.PropertyValue as IList<ProfilePrimaryAlbumTypeItemResource>; return context.PropertyValue is IList<ProfilePrimaryAlbumTypeItemResource> list &&
list.Any(c => c.Allowed);
if (list == null)
{
return false;
}
if (!list.Any(c => c.Allowed))
{
return false;
}
return true;
} }
} }
public class SecondaryTypeValidator<T> : PropertyValidator public class SecondaryTypeValidator<T> : PropertyValidator
{ {
public SecondaryTypeValidator() protected override string GetDefaultMessageTemplate() => "Must have at least one allowed secondary type";
: base("Must have at least one allowed secondary type")
{
}
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
var list = context.PropertyValue as IList<ProfileSecondaryAlbumTypeItemResource>; return context.PropertyValue is IList<ProfileSecondaryAlbumTypeItemResource> list &&
list.Any(c => c.Allowed);
if (list == null)
{
return false;
}
if (!list.Any(c => c.Allowed))
{
return false;
}
return true;
} }
} }
public class ReleaseStatusValidator<T> : PropertyValidator public class ReleaseStatusValidator<T> : PropertyValidator
{ {
public ReleaseStatusValidator() protected override string GetDefaultMessageTemplate() => "Must have at least one allowed release status";
: base("Must have at least one allowed release status")
{
}
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
var list = context.PropertyValue as IList<ProfileReleaseStatusItemResource>; return context.PropertyValue is IList<ProfileReleaseStatusItemResource> list &&
list.Any(c => c.Allowed);
if (list == null)
{
return false;
}
if (!list.Any(c => c.Allowed))
{
return false;
}
return true;
} }
} }
} }

@ -15,30 +15,17 @@ namespace Lidarr.Api.V1.Profiles.Quality
public class ValidCutoffValidator<T> : PropertyValidator public class ValidCutoffValidator<T> : PropertyValidator
{ {
public ValidCutoffValidator() protected override string GetDefaultMessageTemplate() => "Cutoff must be an allowed quality or group";
: base("Cutoff must be an allowed quality or group")
{
}
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
int cutoff = (int)context.PropertyValue; var cutoff = (int)context.PropertyValue;
dynamic instance = context.ParentContext.InstanceToValidate; dynamic instance = context.ParentContext.InstanceToValidate;
var items = instance.Items as IList<QualityProfileQualityItemResource>; var items = instance.Items as IList<QualityProfileQualityItemResource>;
QualityProfileQualityItemResource cutoffItem = items.SingleOrDefault(i => (i.Quality == null && i.Id == cutoff) || i.Quality?.Id == cutoff); 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;
}
return true; return cutoffItem is { Allowed: true };
} }
} }
} }

@ -23,139 +23,104 @@ namespace Lidarr.Api.V1.Profiles.Quality
public class AllowedValidator<T> : PropertyValidator public class AllowedValidator<T> : PropertyValidator
{ {
public AllowedValidator() protected override string GetDefaultMessageTemplate() => "Must contain at least one allowed quality";
: base("Must contain at least one allowed quality")
{
}
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
var list = context.PropertyValue as IList<QualityProfileQualityItemResource>; return context.PropertyValue is IList<QualityProfileQualityItemResource> list &&
list.Any(c => c.Allowed);
if (list == null)
{
return false;
}
if (!list.Any(c => c.Allowed))
{
return false;
}
return true;
} }
} }
public class GroupItemValidator<T> : PropertyValidator public class GroupItemValidator<T> : PropertyValidator
{ {
public GroupItemValidator() protected override string GetDefaultMessageTemplate() => "Groups must contain multiple qualities";
: base("Groups must contain multiple qualities")
{
}
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
var items = context.PropertyValue as IList<QualityProfileQualityItemResource>; if (context.PropertyValue is not IList<QualityProfileQualityItemResource> items)
if (items.Any(i => i.Name.IsNotNullOrWhiteSpace() && i.Items.Count <= 1))
{ {
return false; return false;
} }
return true; return !items.Any(i => i.Name.IsNotNullOrWhiteSpace() && i.Items.Count <= 1);
} }
} }
public class QualityNameValidator<T> : PropertyValidator public class QualityNameValidator<T> : PropertyValidator
{ {
public QualityNameValidator() protected override string GetDefaultMessageTemplate() => "Individual qualities should not be named";
: base("Individual qualities should not be named")
{
}
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
var items = context.PropertyValue as IList<QualityProfileQualityItemResource>; if (context.PropertyValue is not IList<QualityProfileQualityItemResource> items)
if (items.Any(i => i.Name.IsNotNullOrWhiteSpace() && i.Quality != null))
{ {
return false; return false;
} }
return true; return !items.Any(i => i.Name.IsNotNullOrWhiteSpace() && i.Quality != null);
} }
} }
public class ItemGroupNameValidator<T> : PropertyValidator public class ItemGroupNameValidator<T> : PropertyValidator
{ {
public ItemGroupNameValidator() protected override string GetDefaultMessageTemplate() => "Groups must have a name";
: base("Groups must have a name")
{
}
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
var items = context.PropertyValue as IList<QualityProfileQualityItemResource>; if (context.PropertyValue is not IList<QualityProfileQualityItemResource> items)
if (items.Any(i => i.Quality == null && i.Name.IsNullOrWhiteSpace()))
{ {
return false; return false;
} }
return true; return !items.Any(i => i.Quality == null && i.Name.IsNullOrWhiteSpace());
} }
} }
public class ItemGroupIdValidator<T> : PropertyValidator public class ItemGroupIdValidator<T> : PropertyValidator
{ {
public ItemGroupIdValidator() protected override string GetDefaultMessageTemplate() => "Groups must have an ID";
: base("Groups must have an ID")
{
}
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
var items = context.PropertyValue as IList<QualityProfileQualityItemResource>; if (context.PropertyValue is not IList<QualityProfileQualityItemResource> items)
if (items.Any(i => i.Quality == null && i.Id == 0))
{ {
return false; return false;
} }
return true; return !items.Any(i => i.Quality == null && i.Id == 0);
} }
} }
public class UniqueIdValidator<T> : PropertyValidator public class UniqueIdValidator<T> : PropertyValidator
{ {
public UniqueIdValidator() protected override string GetDefaultMessageTemplate() => "Groups must have a unique ID";
: base("Groups must have a unique ID")
{
}
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
var items = context.PropertyValue as IList<QualityProfileQualityItemResource>; if (context.PropertyValue is not IList<QualityProfileQualityItemResource> items)
if (items.Where(i => i.Id > 0).Select(i => i.Id).GroupBy(i => i).Any(g => g.Count() > 1))
{ {
return false; 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<T> : PropertyValidator public class UniqueQualityIdValidator<T> : PropertyValidator
{ {
public UniqueQualityIdValidator() protected override string GetDefaultMessageTemplate() => "Qualities can only be used once";
: base("Qualities can only be used once")
{
}
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
var items = context.PropertyValue as IList<QualityProfileQualityItemResource>; if (context.PropertyValue is not IList<QualityProfileQualityItemResource> items)
{
return false;
}
var qualityIds = new HashSet<int>(); var qualityIds = new HashSet<int>();
foreach (var item in items) foreach (var item in items)

@ -28,7 +28,7 @@ namespace Lidarr.Api.V1.RemotePathMappings
.NotEmpty(); .NotEmpty();
SharedValidator.RuleFor(c => c.LocalPath) SharedValidator.RuleFor(c => c.LocalPath)
.Cascade(CascadeMode.StopOnFirstFailure) .Cascade(CascadeMode.Stop)
.IsValidPath() .IsValidPath()
.SetValidator(mappedNetworkDriveValidator) .SetValidator(mappedNetworkDriveValidator)
.SetValidator(pathExistsValidator); .SetValidator(pathExistsValidator);

@ -32,7 +32,7 @@ namespace Lidarr.Api.V1.RootFolders
_rootFolderService = rootFolderService; _rootFolderService = rootFolderService;
SharedValidator.RuleFor(c => c.Path) SharedValidator.RuleFor(c => c.Path)
.Cascade(CascadeMode.StopOnFirstFailure) .Cascade(CascadeMode.Stop)
.IsValidPath() .IsValidPath()
.SetValidator(mappedNetworkDriveValidator) .SetValidator(mappedNetworkDriveValidator)
.SetValidator(startupFolderValidator) .SetValidator(startupFolderValidator)

@ -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.0.4" /> <PackageReference Include="NLog" Version="5.0.4" />
</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 Lidarr.Http.ClientSchema; using Lidarr.Http.ClientSchema;
namespace Lidarr.Http.REST namespace Lidarr.Http.REST
@ -15,7 +14,7 @@ namespace Lidarr.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 Lidarr.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 Lidarr.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 Lidarr.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 Lidarr.Http.Validation
return true; return true;
} }
if (value >= 10 && value <= 120) return value is >= 10 and <= 120;
{
return true;
}
return false;
} }
} }
} }

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

@ -40,7 +40,6 @@ namespace NzbDrone.Core.Test.NotificationTests.EmailTests
public void should_not_be_valid_if_port_is_out_of_range() public void should_not_be_valid_if_port_is_out_of_range()
{ {
_emailSettings.Port = 900000; _emailSettings.Port = 900000;
_validator.Validate(_emailSettings).IsValid.Should().BeFalse(); _validator.Validate(_emailSettings).IsValid.Should().BeFalse();
} }
@ -48,7 +47,6 @@ namespace NzbDrone.Core.Test.NotificationTests.EmailTests
public void should_not_be_valid_if_server_is_empty() public void should_not_be_valid_if_server_is_empty()
{ {
_emailSettings.Server = ""; _emailSettings.Server = "";
_validator.Validate(_emailSettings).IsValid.Should().BeFalse(); _validator.Validate(_emailSettings).IsValid.Should().BeFalse();
} }
@ -61,7 +59,6 @@ namespace NzbDrone.Core.Test.NotificationTests.EmailTests
} }
[TestCase("lidarr")] [TestCase("lidarr")]
[TestCase("lidarr@lidarr")]
[TestCase("lidarr.audio")] [TestCase("lidarr.audio")]
public void should_not_be_valid_if_to_is_invalid(string email) public void should_not_be_valid_if_to_is_invalid(string email)
{ {
@ -71,7 +68,6 @@ namespace NzbDrone.Core.Test.NotificationTests.EmailTests
} }
[TestCase("lidarr")] [TestCase("lidarr")]
[TestCase("lidarr@lidarr")]
[TestCase("lidarr.audio")] [TestCase("lidarr.audio")]
public void should_not_be_valid_if_cc_is_invalid(string email) public void should_not_be_valid_if_cc_is_invalid(string email)
{ {
@ -81,7 +77,6 @@ namespace NzbDrone.Core.Test.NotificationTests.EmailTests
} }
[TestCase("lidarr")] [TestCase("lidarr")]
[TestCase("lidarr@lidarr")]
[TestCase("lidarr.audio")] [TestCase("lidarr.audio")]
public void should_not_be_valid_if_bcc_is_invalid(string email) public void should_not_be_valid_if_bcc_is_invalid(string email)
{ {

@ -17,13 +17,13 @@ namespace NzbDrone.Core.Download.Clients.rTorrent
PathExistsValidator pathExistsValidator, PathExistsValidator pathExistsValidator,
MappedNetworkDriveValidator mappedNetworkDriveValidator) MappedNetworkDriveValidator mappedNetworkDriveValidator)
{ {
RuleFor(c => c.MusicDirectory).Cascade(CascadeMode.StopOnFirstFailure) RuleFor(c => c.MusicDirectory).Cascade(CascadeMode.Stop)
.IsValidPath() .IsValidPath()
.SetValidator(rootFolderValidator) .SetValidator(rootFolderValidator)
.SetValidator(mappedNetworkDriveValidator) .SetValidator(mappedNetworkDriveValidator)
.SetValidator(pathExistsValidator) .SetValidator(pathExistsValidator)
.When(c => c.MusicDirectory.IsNotNullOrWhiteSpace()) .When(c => c.MusicDirectory.IsNotNullOrWhiteSpace())
.When(c => c.Host == "localhost" || c.Host == "127.0.0.1"); .When(c => c.Host == "localhost" || c.Host == "127.0.0.1");
} }
} }
} }

@ -7,11 +7,12 @@ namespace NzbDrone.Core.ImportLists.Exclusions
private readonly IImportListExclusionService _importListExclusionService; private readonly IImportListExclusionService _importListExclusionService;
public ImportListExclusionExistsValidator(IImportListExclusionService importListExclusionService) public ImportListExclusionExistsValidator(IImportListExclusionService importListExclusionService)
: base("This exclusion has already been added.")
{ {
_importListExclusionService = importListExclusionService; _importListExclusionService = importListExclusionService;
} }
protected override string GetDefaultMessageTemplate() => "This exclusion has already been added.";
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
if (context.PropertyValue == null) if (context.PropertyValue == null)

@ -12,7 +12,7 @@
<PackageReference Include="Servarr.FluentMigrator.Runner" Version="3.3.2.9" /> <PackageReference Include="Servarr.FluentMigrator.Runner" Version="3.3.2.9" />
<PackageReference Include="Servarr.FluentMigrator.Runner.SQLite" Version="3.3.2.9" /> <PackageReference Include="Servarr.FluentMigrator.Runner.SQLite" Version="3.3.2.9" />
<PackageReference Include="Servarr.FluentMigrator.Runner.Postgres" Version="3.3.2.9" /> <PackageReference Include="Servarr.FluentMigrator.Runner.Postgres" Version="3.3.2.9" />
<PackageReference Include="FluentValidation" Version="8.6.2" /> <PackageReference Include="FluentValidation" Version="9.5.4" />
<PackageReference Include="MailKit" Version="2.15.0" /> <PackageReference Include="MailKit" Version="2.15.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="NLog" Version="5.0.4" /> <PackageReference Include="NLog" Version="5.0.4" />

@ -19,7 +19,7 @@ namespace NzbDrone.Core.Music
QualityProfileExistsValidator qualityProfileExistsValidator, QualityProfileExistsValidator qualityProfileExistsValidator,
MetadataProfileExistsValidator metadataProfileExistsValidator) MetadataProfileExistsValidator metadataProfileExistsValidator)
{ {
RuleFor(c => c.Path).Cascade(CascadeMode.StopOnFirstFailure) RuleFor(c => c.Path).Cascade(CascadeMode.Stop)
.IsValidPath() .IsValidPath()
.SetValidator(rootFolderValidator) .SetValidator(rootFolderValidator)
.SetValidator(recycleBinValidator) .SetValidator(recycleBinValidator)

@ -41,53 +41,36 @@ namespace NzbDrone.Core.Organizer
public class ValidStandardTrackFormatValidator : PropertyValidator public class ValidStandardTrackFormatValidator : PropertyValidator
{ {
public ValidStandardTrackFormatValidator() protected override string GetDefaultMessageTemplate() => "Must contain Track Title and Track numbers OR Original Title";
: base("Must contain Track Title and Track numbers OR Original Title")
{
}
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
var value = context.PropertyValue as string; if (context.PropertyValue is not string value)
if (!(FileNameBuilder.TrackTitleRegex.IsMatch(value) &&
FileNameBuilder.TrackRegex.IsMatch(value)) &&
!FileNameValidation.OriginalTokenRegex.IsMatch(value))
{ {
return false; return false;
} }
return true; return (FileNameBuilder.TrackTitleRegex.IsMatch(value) && FileNameBuilder.TrackRegex.IsMatch(value)) ||
FileNameValidation.OriginalTokenRegex.IsMatch(value);
} }
} }
public class IllegalCharactersValidator : PropertyValidator public class IllegalCharactersValidator : PropertyValidator
{ {
private readonly char[] _invalidPathChars = Path.GetInvalidPathChars(); private static readonly char[] InvalidPathChars = Path.GetInvalidPathChars();
public IllegalCharactersValidator() protected override string GetDefaultMessageTemplate() => "Contains illegal characters: {InvalidCharacters}";
: base("Contains illegal characters: {InvalidCharacters}")
{
}
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
var value = context.PropertyValue as string; var value = context.PropertyValue as string;
var invalidCharacters = new List<char>();
if (value.IsNullOrWhiteSpace()) if (value.IsNullOrWhiteSpace())
{ {
return true; return true;
} }
foreach (var i in _invalidPathChars) var invalidCharacters = InvalidPathChars.Where(i => value!.IndexOf(i) >= 0).ToList();
{
if (value.IndexOf(i) >= 0)
{
invalidCharacters.Add(i);
}
}
if (invalidCharacters.Any()) if (invalidCharacters.Any())
{ {
context.MessageFormatter.AppendArgument("InvalidCharacters", string.Join("", invalidCharacters)); context.MessageFormatter.AppendArgument("InvalidCharacters", string.Join("", invalidCharacters));

@ -10,11 +10,12 @@ namespace NzbDrone.Core.Profiles.Delay
private readonly IDelayProfileService _delayProfileService; private readonly IDelayProfileService _delayProfileService;
public DelayProfileTagInUseValidator(IDelayProfileService delayProfileService) public DelayProfileTagInUseValidator(IDelayProfileService delayProfileService)
: base("One or more tags is used in another profile")
{ {
_delayProfileService = delayProfileService; _delayProfileService = delayProfileService;
} }
protected override string GetDefaultMessageTemplate() => "One or more tags is used in another profile";
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
if (context.PropertyValue == null) if (context.PropertyValue == null)
@ -25,9 +26,7 @@ namespace NzbDrone.Core.Profiles.Delay
dynamic instance = context.ParentContext.InstanceToValidate; dynamic instance = context.ParentContext.InstanceToValidate;
var instanceId = (int)instance.Id; var instanceId = (int)instance.Id;
var collection = context.PropertyValue as HashSet<int>; if (context.PropertyValue is not HashSet<int> collection || collection.Empty())
if (collection == null || collection.Empty())
{ {
return true; return true;
} }

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

@ -5,10 +5,7 @@ namespace NzbDrone.Core.Validation
{ {
public class GuidValidator : PropertyValidator public class GuidValidator : PropertyValidator
{ {
public GuidValidator() protected override string GetDefaultMessageTemplate() => "String is not a valid Guid";
: base("String is not a valid Guid")
{
}
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {

@ -8,11 +8,12 @@ namespace NzbDrone.Core.Validation
private readonly IMetadataProfileService _profileService; private readonly IMetadataProfileService _profileService;
public MetadataProfileExistsValidator(IMetadataProfileService profileService) public MetadataProfileExistsValidator(IMetadataProfileService profileService)
: base("Metadata profile does not exist")
{ {
_profileService = profileService; _profileService = profileService;
} }
protected override string GetDefaultMessageTemplate() => "Metadata profile does not exist";
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
if (context.PropertyValue == null) if (context.PropertyValue == null)

@ -10,11 +10,12 @@ namespace NzbDrone.Core.Validation.Paths
private readonly IArtistService _artistService; private readonly IArtistService _artistService;
public ArtistAncestorValidator(IArtistService artistService) public ArtistAncestorValidator(IArtistService artistService)
: base("Path is an ancestor of an existing artist")
{ {
_artistService = artistService; _artistService = artistService;
} }
protected override string GetDefaultMessageTemplate() => "Path is an ancestor of an existing artist";
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
if (context.PropertyValue == null) if (context.PropertyValue == null)

@ -8,11 +8,12 @@ namespace NzbDrone.Core.Validation.Paths
private readonly IArtistService _artistService; private readonly IArtistService _artistService;
public ArtistExistsValidator(IArtistService artistService) public ArtistExistsValidator(IArtistService artistService)
: base("This artist has already been added.")
{ {
_artistService = artistService; _artistService = artistService;
} }
protected override string GetDefaultMessageTemplate() => "This artist has already been added.";
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
if (context.PropertyValue == null) if (context.PropertyValue == null)

@ -10,11 +10,12 @@ namespace NzbDrone.Core.Validation.Paths
private readonly IArtistService _artistService; private readonly IArtistService _artistService;
public ArtistPathValidator(IArtistService artistService) public ArtistPathValidator(IArtistService artistService)
: base("Path is already configured for an existing artist")
{ {
_artistService = artistService; _artistService = artistService;
} }
protected override string GetDefaultMessageTemplate() => "Path is already configured for an existing artist";
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
if (context.PropertyValue == null) if (context.PropertyValue == null)

@ -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 IConfigService _configService; private readonly IConfigService _configService;
public RecycleBinValidator(IConfigService configService) public RecycleBinValidator(IConfigService configService)
: base("Path is {relationship} configured recycle bin folder")
{ {
_configService = configService; _configService = configService;
} }
protected override string GetDefaultMessageTemplate() => "Path is {relationship} configured recycle bin folder";
protected override bool IsValid(PropertyValidatorContext context) protected override bool IsValid(PropertyValidatorContext context)
{ {
var recycleBin = _configService.RecycleBin; var recycleBin = _configService.RecycleBin;

@ -10,11 +10,12 @@ namespace NzbDrone.Core.Validation.Paths
private readonly IRootFolderService _rootFolderService; private readonly IRootFolderService _rootFolderService;
public RootFolderAncestorValidator(IRootFolderService rootFolderService) public RootFolderAncestorValidator(IRootFolderService rootFolderService)
: base("Path is an ancestor of an existing root folder")
{ {
_rootFolderService = rootFolderService; _rootFolderService = rootFolderService;
} }
protected override string GetDefaultMessageTemplate() => "Path is an ancestor of an existing root folder";
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 IRootFolderService _rootFolderService; private readonly IRootFolderService _rootFolderService;
public RootFolderValidator(IRootFolderService rootFolderService) public RootFolderValidator(IRootFolderService rootFolderService)
: base("Path is already configured as a root folder")
{ {
_rootFolderService = rootFolderService; _rootFolderService = rootFolderService;
} }
protected override string GetDefaultMessageTemplate() => "Path is already configured as a root folder";
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 IAppFolderInfo _appFolderInfo; private readonly IAppFolderInfo _appFolderInfo;
public StartupFolderValidator(IAppFolderInfo appFolderInfo) public StartupFolderValidator(IAppFolderInfo appFolderInfo)
: base("Path cannot be {relationship} the start up folder")
{ {
_appFolderInfo = appFolderInfo; _appFolderInfo = appFolderInfo;
} }
protected override string GetDefaultMessageTemplate() => "Path cannot be {relationship} 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)
{ {

@ -8,11 +8,12 @@ namespace NzbDrone.Core.Validation
private readonly IQualityProfileService _profileService; private readonly IQualityProfileService _profileService;
public QualityProfileExistsValidator(IQualityProfileService profileService) public QualityProfileExistsValidator(IQualityProfileService profileService)
: base("Quality Profile does not exist")
{ {
_profileService = profileService; _profileService = profileService;
} }
protected override string GetDefaultMessageTemplate() => "Quality Profile 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
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.16.1" /> <PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="NLog" Version="5.0.4" /> <PackageReference Include="NLog" Version="5.0.4" />
<PackageReference Include="NUnit" Version="3.13.3" /> <PackageReference Include="NUnit" Version="3.13.3" />

Loading…
Cancel
Save