You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Readarr/NzbDrone.Api/REST/ResourceValidator.cs

37 lines
1.3 KiB

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using FluentValidation;
using FluentValidation.Internal;
using FluentValidation.Resources;
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 fieldName)
{
var rule = new PropertyRule(fieldListAccessor.GetMember(), c => GetValue(c, fieldListAccessor.Compile(), fieldName), null, () => CascadeMode.Continue, typeof(TProperty), typeof(TResource));
11 years ago
rule.PropertyName = fieldName;
rule.DisplayName = new StaticStringSource(fieldName);
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;
}
}
}