mirror of
https://github.com/Readarr/Readarr
synced 2025-12-11 02:43:50 +01:00
Migrate to FluentValidation 9
(cherry picked from commit 40e54685b9e83ed24a3979bfe965c453339ad02e)
This commit is contained in:
parent
816969d0f5
commit
28f64d9a46
37 changed files with 94 additions and 194 deletions
|
|
@ -11,7 +11,7 @@
|
|||
<PackageVersion Include="Servarr.FluentMigrator.Runner" Version="3.3.2.9" />
|
||||
<PackageVersion Include="Servarr.FluentMigrator.Runner.SQLite" Version="3.3.2.9" />
|
||||
<PackageVersion Include="Servarr.FluentMigrator.Runner.Postgres" Version="3.3.2.9" />
|
||||
<PackageVersion Include="FluentValidation" Version="8.6.2" />
|
||||
<PackageVersion Include="FluentValidation" Version="9.5.4" />
|
||||
<PackageVersion Include="Ical.Net" Version="4.2.0" />
|
||||
<PackageVersion Include="ImpromptuInterface" Version="7.0.1" />
|
||||
<PackageVersion Include="LazyCache" Version="2.4.0" />
|
||||
|
|
@ -63,4 +63,4 @@
|
|||
<PackageVersion Include="System.ValueTuple" Version="4.5.0" />
|
||||
<PackageVersion Include="TagLibSharp-Lidarr" Version="2.2.0.19" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -16,18 +16,7 @@ public static bool IsValidUrl(this string path)
|
|||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,7 +61,6 @@ public void should_not_be_valid_if_from_is_empty()
|
|||
}
|
||||
|
||||
[TestCase("readarr")]
|
||||
[TestCase("readarr@readarr")]
|
||||
[TestCase("readarr.com")]
|
||||
public void should_not_be_valid_if_to_is_invalid(string email)
|
||||
{
|
||||
|
|
@ -71,7 +70,6 @@ public void should_not_be_valid_if_to_is_invalid(string email)
|
|||
}
|
||||
|
||||
[TestCase("readarr")]
|
||||
[TestCase("readarr@readarr")]
|
||||
[TestCase("readarr.com")]
|
||||
public void should_not_be_valid_if_cc_is_invalid(string email)
|
||||
{
|
||||
|
|
@ -81,7 +79,6 @@ public void should_not_be_valid_if_cc_is_invalid(string email)
|
|||
}
|
||||
|
||||
[TestCase("readarr")]
|
||||
[TestCase("readarr@readarr")]
|
||||
[TestCase("readarr.com")]
|
||||
public void should_not_be_valid_if_bcc_is_invalid(string email)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ public AddAuthorValidator(RootFolderValidator rootFolderValidator,
|
|||
QualityProfileExistsValidator qualityProfileExistsValidator,
|
||||
MetadataProfileExistsValidator metadataProfileExistsValidator)
|
||||
{
|
||||
RuleFor(c => c.Path).Cascade(CascadeMode.StopOnFirstFailure)
|
||||
RuleFor(c => c.Path).Cascade(CascadeMode.Stop)
|
||||
.IsValidPath()
|
||||
.SetValidator(rootFolderValidator)
|
||||
.SetValidator(recycleBinValidator)
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ public RTorrentDirectoryValidator(RootFolderValidator rootFolderValidator,
|
|||
PathExistsValidator pathExistsValidator,
|
||||
MappedNetworkDriveValidator mappedNetworkDriveValidator)
|
||||
{
|
||||
RuleFor(c => c.MusicDirectory).Cascade(CascadeMode.StopOnFirstFailure)
|
||||
RuleFor(c => c.MusicDirectory).Cascade(CascadeMode.Stop)
|
||||
.IsValidPath()
|
||||
.SetValidator(rootFolderValidator)
|
||||
.SetValidator(mappedNetworkDriveValidator)
|
||||
|
|
|
|||
|
|
@ -7,11 +7,12 @@ public class ImportListExclusionExistsValidator : PropertyValidator
|
|||
private readonly IImportListExclusionService _importListExclusionService;
|
||||
|
||||
public ImportListExclusionExistsValidator(IImportListExclusionService importListExclusionService)
|
||||
: base("This exclusion has already been added.")
|
||||
{
|
||||
_importListExclusionService = importListExclusionService;
|
||||
}
|
||||
|
||||
protected override string GetDefaultMessageTemplate() => "This exclusion has already been added.";
|
||||
|
||||
protected override bool IsValid(PropertyValidatorContext context)
|
||||
{
|
||||
if (context.PropertyValue == null)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
|
@ -32,22 +31,17 @@ public static IRuleBuilderOptions<T, string> ValidAuthorFolderFormat<T>(this IRu
|
|||
|
||||
public class ValidStandardTrackFormatValidator : PropertyValidator
|
||||
{
|
||||
public ValidStandardTrackFormatValidator()
|
||||
: base("Must contain Book Title AND PartNumber, OR Original Title")
|
||||
{
|
||||
}
|
||||
protected override string GetDefaultMessageTemplate() => "Must contain Book Title AND PartNumber, OR Original Title";
|
||||
|
||||
protected override bool IsValid(PropertyValidatorContext context)
|
||||
{
|
||||
var value = context.PropertyValue as string;
|
||||
|
||||
if (!(FileNameBuilder.BookTitleRegex.IsMatch(value) && FileNameBuilder.PartRegex.IsMatch(value)) &&
|
||||
!FileNameValidation.OriginalTokenRegex.IsMatch(value))
|
||||
if (context.PropertyValue is not string value)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return (FileNameBuilder.BookTitleRegex.IsMatch(value) && FileNameBuilder.PartRegex.IsMatch(value)) ||
|
||||
FileNameValidation.OriginalTokenRegex.IsMatch(value);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -55,29 +49,18 @@ public class IllegalCharactersValidator : PropertyValidator
|
|||
{
|
||||
private readonly char[] _invalidPathChars = Path.GetInvalidPathChars();
|
||||
|
||||
public IllegalCharactersValidator()
|
||||
: base("Contains illegal characters: {InvalidCharacters}")
|
||||
{
|
||||
}
|
||||
protected override string GetDefaultMessageTemplate() => "Contains illegal characters: {InvalidCharacters}";
|
||||
|
||||
protected override bool IsValid(PropertyValidatorContext context)
|
||||
{
|
||||
var value = context.PropertyValue as string;
|
||||
var invalidCharacters = new List<char>();
|
||||
|
||||
if (value.IsNullOrWhiteSpace())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach (var i in _invalidPathChars)
|
||||
{
|
||||
if (value.IndexOf(i) >= 0)
|
||||
{
|
||||
invalidCharacters.Add(i);
|
||||
}
|
||||
}
|
||||
|
||||
var invalidCharacters = _invalidPathChars.Where(i => value!.IndexOf(i) >= 0).ToList();
|
||||
if (invalidCharacters.Any())
|
||||
{
|
||||
context.MessageFormatter.AppendArgument("InvalidCharacters", string.Join("", invalidCharacters));
|
||||
|
|
|
|||
|
|
@ -10,11 +10,12 @@ public class DelayProfileTagInUseValidator : PropertyValidator
|
|||
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 @@ protected override bool IsValid(PropertyValidatorContext context)
|
|||
dynamic instance = context.ParentContext.InstanceToValidate;
|
||||
var instanceId = (int)instance.Id;
|
||||
|
||||
var collection = context.PropertyValue as HashSet<int>;
|
||||
|
||||
if (collection == null || collection.Empty())
|
||||
if (context.PropertyValue is not HashSet<int> collection || collection.Empty())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,11 +8,12 @@ public class FolderChmodValidator : PropertyValidator
|
|||
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)
|
||||
|
|
|
|||
|
|
@ -6,10 +6,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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -5,10 +5,7 @@ namespace NzbDrone.Core.Validation
|
|||
{
|
||||
public class GuidValidator : PropertyValidator
|
||||
{
|
||||
public GuidValidator()
|
||||
: base("String is not a valid Guid")
|
||||
{
|
||||
}
|
||||
protected override string GetDefaultMessageTemplate() => "String is not a valid Guid";
|
||||
|
||||
protected override bool IsValid(PropertyValidatorContext context)
|
||||
{
|
||||
|
|
@ -17,7 +14,7 @@ protected override bool IsValid(PropertyValidatorContext context)
|
|||
return false;
|
||||
}
|
||||
|
||||
return Guid.TryParse(context.PropertyValue.ToString(), out Guid guidOutput);
|
||||
return Guid.TryParse(context.PropertyValue.ToString(), out _);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,11 +8,12 @@ public class MetadataProfileExistsValidator : PropertyValidator
|
|||
private readonly IMetadataProfileService _profileService;
|
||||
|
||||
public MetadataProfileExistsValidator(IMetadataProfileService profileService)
|
||||
: base("Metadata profile does not exist")
|
||||
{
|
||||
_profileService = profileService;
|
||||
}
|
||||
|
||||
protected override string GetDefaultMessageTemplate() => "Metadata profile does not exist";
|
||||
|
||||
protected override bool IsValid(PropertyValidatorContext context)
|
||||
{
|
||||
if (context.PropertyValue == null)
|
||||
|
|
|
|||
|
|
@ -10,11 +10,12 @@ public class AuthorAncestorValidator : PropertyValidator
|
|||
private readonly IAuthorService _authorService;
|
||||
|
||||
public AuthorAncestorValidator(IAuthorService authorService)
|
||||
: base("Path is an ancestor of an existing author")
|
||||
{
|
||||
_authorService = authorService;
|
||||
}
|
||||
|
||||
protected override string GetDefaultMessageTemplate() => "Path is an ancestor of an existing author";
|
||||
|
||||
protected override bool IsValid(PropertyValidatorContext context)
|
||||
{
|
||||
if (context.PropertyValue == null)
|
||||
|
|
|
|||
|
|
@ -8,11 +8,12 @@ public class AuthorExistsValidator : PropertyValidator
|
|||
private readonly IAuthorService _authorService;
|
||||
|
||||
public AuthorExistsValidator(IAuthorService authorService)
|
||||
: base("This author has already been added.")
|
||||
{
|
||||
_authorService = authorService;
|
||||
}
|
||||
|
||||
protected override string GetDefaultMessageTemplate() => "This author has already been added";
|
||||
|
||||
protected override bool IsValid(PropertyValidatorContext context)
|
||||
{
|
||||
if (context.PropertyValue == null)
|
||||
|
|
|
|||
|
|
@ -10,11 +10,12 @@ public class AuthorPathValidator : PropertyValidator
|
|||
private readonly IAuthorService _authorService;
|
||||
|
||||
public AuthorPathValidator(IAuthorService authorService)
|
||||
: base("Path is already configured for another author")
|
||||
{
|
||||
_authorService = authorService;
|
||||
}
|
||||
|
||||
protected override string GetDefaultMessageTemplate() => "Path is already configured for another author";
|
||||
|
||||
protected override bool IsValid(PropertyValidatorContext context)
|
||||
{
|
||||
if (context.PropertyValue == null)
|
||||
|
|
|
|||
|
|
@ -8,11 +8,12 @@ public class FileExistsValidator : PropertyValidator
|
|||
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 @@ public class FolderWritableValidator : PropertyValidator
|
|||
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 @@ public class MappedNetworkDriveValidator : PropertyValidator
|
|||
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 @@ protected override bool IsValid(PropertyValidatorContext context)
|
|||
|
||||
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 @@ public class PathExistsValidator : PropertyValidator
|
|||
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)
|
||||
|
|
|
|||
|
|
@ -15,10 +15,7 @@ public static IRuleBuilderOptions<T, string> IsValidPath<T>(this IRuleBuilder<T,
|
|||
|
||||
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 @@ public class RecycleBinValidator : PropertyValidator
|
|||
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;
|
||||
|
|
|
|||
|
|
@ -10,11 +10,12 @@ public class RootFolderAncestorValidator : PropertyValidator
|
|||
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)
|
||||
|
|
|
|||
|
|
@ -9,11 +9,12 @@ public class RootFolderValidator : PropertyValidator
|
|||
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)
|
||||
|
|
|
|||
|
|
@ -9,11 +9,12 @@ public class StartupFolderValidator : PropertyValidator
|
|||
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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -8,11 +8,12 @@ public class QualityProfileExistsValidator : PropertyValidator
|
|||
private readonly IProfileService _profileService;
|
||||
|
||||
public QualityProfileExistsValidator(IProfileService profileService)
|
||||
: base("Quality Profile does not exist")
|
||||
{
|
||||
_profileService = profileService;
|
||||
}
|
||||
|
||||
protected override string GetDefaultMessageTemplate() => "Quality Profile does not exist";
|
||||
|
||||
protected override bool IsValid(PropertyValidatorContext context)
|
||||
{
|
||||
if (context.PropertyValue == null)
|
||||
|
|
|
|||
|
|
@ -14,10 +14,7 @@ public static IRuleBuilderOptions<T, string> IsValidUrl<T>(this IRuleBuilder<T,
|
|||
|
||||
public class UrlValidator : PropertyValidator
|
||||
{
|
||||
public UrlValidator()
|
||||
: base("Invalid Url")
|
||||
{
|
||||
}
|
||||
protected override string GetDefaultMessageTemplate() => "Invalid Url";
|
||||
|
||||
protected override bool IsValid(PropertyValidatorContext context)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -76,7 +76,7 @@ public AuthorController(IBroadcastSignalRMessage signalRBroadcaster,
|
|||
Http.Validation.RuleBuilderExtensions.ValidId(SharedValidator.RuleFor(s => s.MetadataProfileId));
|
||||
|
||||
SharedValidator.RuleFor(s => s.Path)
|
||||
.Cascade(CascadeMode.StopOnFirstFailure)
|
||||
.Cascade(CascadeMode.Stop)
|
||||
.IsValidPath()
|
||||
.SetValidator(rootFolderValidator)
|
||||
.SetValidator(mappedNetworkDriveValidator)
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ public HostConfigController(IConfigFileProvider configFileProvider,
|
|||
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)
|
||||
|
|
|
|||
|
|
@ -15,30 +15,17 @@ public static IRuleBuilderOptions<T, int> ValidCutoff<T>(this IRuleBuilder<T, in
|
|||
|
||||
public class ValidCutoffValidator<T> : 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)
|
||||
{
|
||||
int cutoff = (int)context.PropertyValue;
|
||||
var cutoff = (int)context.PropertyValue;
|
||||
dynamic instance = context.ParentContext.InstanceToValidate;
|
||||
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 @@ public static IRuleBuilderOptions<T, IList<QualityProfileQualityItemResource>> V
|
|||
|
||||
public class AllowedValidator<T> : 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<QualityProfileQualityItemResource>;
|
||||
|
||||
if (list == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!list.Any(c => c.Allowed))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return context.PropertyValue is IList<QualityProfileQualityItemResource> list &&
|
||||
list.Any(c => c.Allowed);
|
||||
}
|
||||
}
|
||||
|
||||
public class GroupItemValidator<T> : 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<QualityProfileQualityItemResource>;
|
||||
|
||||
if (items.Any(i => i.Name.IsNotNullOrWhiteSpace() && i.Items.Count <= 1))
|
||||
if (context.PropertyValue is not IList<QualityProfileQualityItemResource> items)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return !items.Any(i => i.Name.IsNotNullOrWhiteSpace() && i.Items.Count <= 1);
|
||||
}
|
||||
}
|
||||
|
||||
public class QualityNameValidator<T> : 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<QualityProfileQualityItemResource>;
|
||||
|
||||
if (items.Any(i => i.Name.IsNotNullOrWhiteSpace() && i.Quality != null))
|
||||
if (context.PropertyValue is not IList<QualityProfileQualityItemResource> items)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return !items.Any(i => i.Name.IsNotNullOrWhiteSpace() && i.Quality != null);
|
||||
}
|
||||
}
|
||||
|
||||
public class ItemGroupNameValidator<T> : 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<QualityProfileQualityItemResource>;
|
||||
|
||||
if (items.Any(i => i.Quality == null && i.Name.IsNullOrWhiteSpace()))
|
||||
if (context.PropertyValue is not IList<QualityProfileQualityItemResource> items)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return !items.Any(i => i.Quality == null && i.Name.IsNullOrWhiteSpace());
|
||||
}
|
||||
}
|
||||
|
||||
public class ItemGroupIdValidator<T> : 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<QualityProfileQualityItemResource>;
|
||||
|
||||
if (items.Any(i => i.Quality == null && i.Id == 0))
|
||||
if (context.PropertyValue is not IList<QualityProfileQualityItemResource> items)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return !items.Any(i => i.Quality == null && i.Id == 0);
|
||||
}
|
||||
}
|
||||
|
||||
public class UniqueIdValidator<T> : 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<QualityProfileQualityItemResource>;
|
||||
|
||||
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<QualityProfileQualityItemResource> 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<T> : 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<QualityProfileQualityItemResource>;
|
||||
if (context.PropertyValue is not IList<QualityProfileQualityItemResource> items)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var qualityIds = new HashSet<int>();
|
||||
|
||||
foreach (var item in items)
|
||||
|
|
|
|||
|
|
@ -28,8 +28,8 @@ public RemotePathMappingController(IRemotePathMappingService remotePathMappingSe
|
|||
.NotEmpty();
|
||||
|
||||
SharedValidator.RuleFor(c => c.LocalPath)
|
||||
.Cascade(CascadeMode.StopOnFirstFailure)
|
||||
.IsValidPath()
|
||||
.Cascade(CascadeMode.Stop)
|
||||
.IsValidPath()
|
||||
.SetValidator(mappedNetworkDriveValidator)
|
||||
.SetValidator(pathExistsValidator);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ public RootFolderController(IRootFolderService rootFolderService,
|
|||
_calibreProxy = calibreProxy;
|
||||
|
||||
SharedValidator.RuleFor(c => c.Path)
|
||||
.Cascade(CascadeMode.StopOnFirstFailure)
|
||||
.Cascade(CascadeMode.Stop)
|
||||
.IsValidPath()
|
||||
.SetValidator(mappedNetworkDriveValidator)
|
||||
.SetValidator(startupFolderValidator)
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
using System.Linq.Expressions;
|
||||
using FluentValidation;
|
||||
using FluentValidation.Internal;
|
||||
using FluentValidation.Resources;
|
||||
using Readarr.Http.ClientSchema;
|
||||
|
||||
namespace Readarr.Http.REST
|
||||
|
|
@ -15,7 +14,7 @@ public IRuleBuilderInitial<TResource, TProperty> RuleForField<TProperty>(Express
|
|||
{
|
||||
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 @@ private static object GetValue(object container, Func<TResource, IEnumerable<Fie
|
|||
{
|
||||
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 Readarr.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 Readarr.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 @@ protected override bool IsValid(PropertyValidatorContext context)
|
|||
return true;
|
||||
}
|
||||
|
||||
if (value >= 10 && value <= 120)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
return value is >= 10 and <= 120;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
2
src/Readarr.sln.DotSettings
Normal file
2
src/Readarr.sln.DotSettings
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Readarr/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
||||
Loading…
Reference in a new issue