Added support for schema field validation. no UI support.

pull/4/head
kay.one 11 years ago
parent 8ad447a209
commit 576ea8560b

@ -1,9 +1,11 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using FluentValidation.Internal;
using NzbDrone.Api.ClientSchema; using NzbDrone.Api.ClientSchema;
using NzbDrone.Api.Mapping; using NzbDrone.Api.Mapping;
using NzbDrone.Api.REST; using NzbDrone.Api.REST;
using NzbDrone.Api.Validation;
using NzbDrone.Core.Indexers; using NzbDrone.Core.Indexers;
using Omu.ValueInjecter; using Omu.ValueInjecter;
using FluentValidation; using FluentValidation;
@ -26,6 +28,8 @@ namespace NzbDrone.Api.Indexers
SharedValidator.RuleFor(c => c.Name).NotEmpty(); SharedValidator.RuleFor(c => c.Name).NotEmpty();
SharedValidator.RuleFor(c => c.Implementation).NotEmpty(); SharedValidator.RuleFor(c => c.Implementation).NotEmpty();
SharedValidator.RuleForField<string>(c => c.Fields, "Url").NotEmpty();
PostValidator.RuleFor(c => c.Fields).NotEmpty(); PostValidator.RuleFor(c => c.Fields).NotEmpty();
} }

@ -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 namespace NzbDrone.Api.REST
{ {
public class ResourceValidator<TResource> : AbstractValidator<TResource> public class ResourceValidator<TResource> : AbstractValidator<TResource>
{ {
public IRuleBuilderInitial<TResource, TProperty> RuleForField<TProperty>(Expression<Func<TResource, IEnumerable<Field>>> 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<TResource, TProperty>(rule);
}
private static object GetValue(object container, Func<TResource, IEnumerable<Field>> fieldListAccessor, string fieldName)
{
var resource = fieldListAccessor((TResource)container).SingleOrDefault(c => c.Name == fieldName);
if (resource == null)
{
return null;
}
return resource.Value;
}
} }
} }
Loading…
Cancel
Save