1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

Added support for schema field validation. no UI support.

This commit is contained in:
kay.one 2013-08-20 23:27:15 -07:00
parent 8ad447a209
commit 576ea8560b
2 changed files with 34 additions and 1 deletions

View File

@ -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<string>(c => c.Fields, "Url").NotEmpty();
PostValidator.RuleFor(c => c.Fields).NotEmpty();
}

View File

@ -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<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;
}
}
}