diff --git a/NzbDrone.Api/Indexers/IndexerModule.cs b/NzbDrone.Api/Indexers/IndexerModule.cs index 9a110a073..0b5ffc052 100644 --- a/NzbDrone.Api/Indexers/IndexerModule.cs +++ b/NzbDrone.Api/Indexers/IndexerModule.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; using System.Linq; +using FluentValidation.Internal; using NzbDrone.Api.ClientSchema; using NzbDrone.Api.Mapping; using NzbDrone.Api.REST; +using NzbDrone.Api.Validation; using NzbDrone.Core.Indexers; using Omu.ValueInjecter; using FluentValidation; @@ -26,6 +28,8 @@ public IndexerModule(IIndexerService indexerService) SharedValidator.RuleFor(c => c.Name).NotEmpty(); SharedValidator.RuleFor(c => c.Implementation).NotEmpty(); + SharedValidator.RuleForField(c => c.Fields, "Url").NotEmpty(); + PostValidator.RuleFor(c => c.Fields).NotEmpty(); } diff --git a/NzbDrone.Api/REST/ResourceValidator.cs b/NzbDrone.Api/REST/ResourceValidator.cs index d9850375b..556a6034b 100644 --- a/NzbDrone.Api/REST/ResourceValidator.cs +++ b/NzbDrone.Api/REST/ResourceValidator.cs @@ -1,9 +1,38 @@ -using FluentValidation; +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using FluentValidation; +using FluentValidation.Internal; +using NzbDrone.Api.ClientSchema; +using System.Linq; namespace NzbDrone.Api.REST { public class ResourceValidator : AbstractValidator { + public IRuleBuilderInitial RuleForField(Expression>> fieldListAccessor, string filedName) + { + var rule = new PropertyRule(fieldListAccessor.GetMember(), c => GetValue(c, fieldListAccessor.Compile(), filedName), null, () => { return CascadeMode.Continue; }, typeof(TProperty), typeof(TResource)); + rule.PropertyName += "." + filedName; + AddRule(rule); + return new RuleBuilder(rule); + } + + private static object GetValue(object container, Func> fieldListAccessor, string fieldName) + { + + var resource = fieldListAccessor((TResource)container).SingleOrDefault(c => c.Name == fieldName); + + if (resource == null) + { + return null; + } + + return resource.Value; + } } + + + } \ No newline at end of file