From 576ea8560bdd97f48ad6262cf0da2c4d15e663f5 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Tue, 20 Aug 2013 23:27:15 -0700 Subject: [PATCH] Added support for schema field validation. no UI support. --- NzbDrone.Api/Indexers/IndexerModule.cs | 4 ++++ NzbDrone.Api/REST/ResourceValidator.cs | 31 +++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) 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 @@ namespace NzbDrone.Api.Indexers 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