Radarr/src/Radarr.Http/REST/ResourceValidator.cs

31 lines
1.2 KiB
C#
Raw Permalink Normal View History

using System;
using System.Collections.Generic;
2018-11-23 07:03:32 +00:00
using System.Linq;
using System.Linq.Expressions;
using FluentValidation;
using FluentValidation.Internal;
2018-11-23 07:03:32 +00:00
using Radarr.Http.ClientSchema;
2013-04-20 22:14:41 +00:00
2018-11-23 07:03:32 +00:00
namespace Radarr.Http.REST
2013-04-20 22:14:41 +00:00
{
public class ResourceValidator<TResource> : AbstractValidator<TResource>
{
2013-08-21 19:08:03 +00:00
public IRuleBuilderInitial<TResource, TProperty> RuleForField<TProperty>(Expression<Func<TResource, IEnumerable<Field>>> fieldListAccessor, string fieldName)
{
2013-08-21 19:08:03 +00:00
var rule = new PropertyRule(fieldListAccessor.GetMember(), c => GetValue(c, fieldListAccessor.Compile(), fieldName), null, () => CascadeMode.Continue, typeof(TProperty), typeof(TResource));
2013-08-22 00:36:35 +00:00
rule.PropertyName = fieldName;
rule.SetDisplayName(fieldName);
2013-04-20 22:14:41 +00:00
AddRule(rule);
return new RuleBuilder<TResource, TProperty>(rule, this);
}
private static object GetValue(object container, Func<TResource, IEnumerable<Field>> fieldListAccessor, string fieldName)
{
var resource = fieldListAccessor((TResource)container).SingleOrDefault(c => c.Name == fieldName);
return resource?.Value;
}
2013-04-20 22:14:41 +00:00
}
2019-12-22 21:24:10 +00:00
}