New: Root folder exists validation for import lists

This commit is contained in:
Bogdan 2024-05-09 20:09:45 +03:00 committed by Mark McDowall
parent 084fcc2295
commit 4440aa3cac
2 changed files with 33 additions and 4 deletions

View File

@ -0,0 +1,25 @@
using FluentValidation.Validators;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.RootFolders;
namespace NzbDrone.Core.Validation.Paths
{
public class RootFolderExistsValidator : PropertyValidator
{
private readonly IRootFolderService _rootFolderService;
public RootFolderExistsValidator(IRootFolderService rootFolderService)
{
_rootFolderService = rootFolderService;
}
protected override string GetDefaultMessageTemplate() => "Root folder '{path}' does not exist";
protected override bool IsValid(PropertyValidatorContext context)
{
context.MessageFormatter.AppendArgument("path", context.PropertyValue?.ToString());
return context.PropertyValue == null || _rootFolderService.All().Exists(r => r.Path.PathEquals(context.PropertyValue.ToString()));
}
}
}

View File

@ -1,3 +1,4 @@
using FluentValidation;
using NzbDrone.Core.ImportLists;
using NzbDrone.Core.Validation;
using NzbDrone.Core.Validation.Paths;
@ -11,13 +12,16 @@ namespace Sonarr.Api.V3.ImportLists
public static readonly ImportListResourceMapper ResourceMapper = new ();
public static readonly ImportListBulkResourceMapper BulkResourceMapper = new ();
public ImportListController(IImportListFactory importListFactory, QualityProfileExistsValidator qualityProfileExistsValidator)
public ImportListController(IImportListFactory importListFactory, RootFolderExistsValidator rootFolderExistsValidator, QualityProfileExistsValidator qualityProfileExistsValidator)
: base(importListFactory, "importlist", ResourceMapper, BulkResourceMapper)
{
Http.Validation.RuleBuilderExtensions.ValidId(SharedValidator.RuleFor(s => s.QualityProfileId));
SharedValidator.RuleFor(c => c.RootFolderPath).Cascade(CascadeMode.Stop)
.IsValidPath()
.SetValidator(rootFolderExistsValidator);
SharedValidator.RuleFor(c => c.RootFolderPath).IsValidPath();
SharedValidator.RuleFor(c => c.QualityProfileId).SetValidator(qualityProfileExistsValidator);
SharedValidator.RuleFor(c => c.QualityProfileId).Cascade(CascadeMode.Stop)
.ValidId()
.SetValidator(qualityProfileExistsValidator);
}
}
}