using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using FluentValidation; using FluentValidation.Internal; using Readarr.Http.ClientSchema; namespace Readarr.Http.REST { public class ResourceValidator : AbstractValidator { public IRuleBuilderInitial RuleForField(Expression>> fieldListAccessor, string fieldName) { var rule = new PropertyRule(fieldListAccessor.GetMember(), c => GetValue(c, fieldListAccessor.Compile(), fieldName), null, () => CascadeMode.Continue, typeof(TProperty), typeof(TResource)); rule.PropertyName = fieldName; rule.SetDisplayName(fieldName); AddRule(rule); return new RuleBuilder(rule, this); } private static object GetValue(object container, Func> fieldListAccessor, string fieldName) { var resource = fieldListAccessor((TResource)container).SingleOrDefault(c => c.Name == fieldName); return resource?.Value; } } }