mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-20 05:23:31 -07:00
Added support for schema field validation. no UI support.
This commit is contained in:
parent
8ad447a209
commit
576ea8560b
2 changed files with 34 additions and 1 deletions
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue