2023-04-01 13:03:53 +00:00
|
|
|
using System;
|
2013-08-21 06:27:15 +00:00
|
|
|
using System.Collections.Generic;
|
2018-11-23 07:03:32 +00:00
|
|
|
using System.Linq;
|
2013-08-21 06:27:15 +00:00
|
|
|
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 06:27:15 +00:00
|
|
|
{
|
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;
|
2023-04-01 13:03:53 +00:00
|
|
|
rule.SetDisplayName(fieldName);
|
2013-04-20 22:14:41 +00:00
|
|
|
|
2013-08-21 06:27:15 +00:00
|
|
|
AddRule(rule);
|
2019-09-02 20:27:49 +00:00
|
|
|
return new RuleBuilder<TResource, TProperty>(rule, this);
|
2013-08-21 06:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static object GetValue(object container, Func<TResource, IEnumerable<Field>> fieldListAccessor, string fieldName)
|
|
|
|
{
|
|
|
|
var resource = fieldListAccessor((TResource)container).SingleOrDefault(c => c.Name == fieldName);
|
|
|
|
|
2023-04-01 13:03:53 +00:00
|
|
|
return resource?.Value;
|
2013-08-21 06:27:15 +00:00
|
|
|
}
|
2013-04-20 22:14:41 +00:00
|
|
|
}
|
2019-12-22 21:24:10 +00:00
|
|
|
}
|