-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropertyValidations.cs
92 lines (83 loc) · 3.64 KB
/
PropertyValidations.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace NHail.ComponentModel.DataAnnotations.Fluent
{
public class PropertyValidations<TSource, TProperty> : IPropertyValidations<TSource, TProperty>
{
private readonly Expression<Func<TSource, TProperty>> _property;
private readonly IAttributeConfiguration<TSource> _provider;
public PropertyValidations(IAttributeConfiguration<TSource> provider,
Expression<Func<TSource, TProperty>> property)
{
_provider = provider;
_property = property;
}
/// <summary>
/// Adds the validation attribute to the property
/// </summary>
/// <typeparam name="TValidationAttribute"></typeparam>
/// <param name="validationAttribute"></param>
/// <returns></returns>
public IPropertyValidations<TSource, TProperty> Add<TValidationAttribute>(
TValidationAttribute validationAttribute)
where TValidationAttribute : ValidationAttribute
{
_provider.AddPropertyAttributes(_property, validationAttribute);
return this;
}
/// <summary>
/// Helper method to chain into Add(TValidationAttribute validationAttribute) but don't need to create attribute
/// </summary>
/// <typeparam name="TValidationAttribute"></typeparam>
/// <param name="setter"></param>
/// <returns></returns>
public IPropertyValidations<TSource, TProperty> Add<TValidationAttribute>(
Action<TValidationAttribute> setter = null)
where TValidationAttribute : ValidationAttribute, new()
{
var attribute = new TValidationAttribute();
setter?.Invoke(attribute);
return Add(attribute);
}
/// <summary>
/// Helper method to chain into Add(TValidationAttribute validationAttribute) but don't need to create attribute
/// </summary>
/// <typeparam name="TValidationAttribute"></typeparam>
/// <param name="properties"></param>
/// <returns></returns>
public IPropertyValidations<TSource, TProperty> AddIfValid<TValidationAttribute>(
params Expression<Func<TSource, object>>[] properties)
where TValidationAttribute : ValidationAttribute, new()
{
return AddIfValid<TValidationAttribute>(null, properties);
}
/// <summary>
/// Helper method to chain into Add(TValidationAttribute validationAttribute) but don't need to create attribute
/// </summary>
/// <typeparam name="TValidationAttribute"></typeparam>
/// <param name="setter"></param>
/// <param name="properties"></param>
/// <returns></returns>
public IPropertyValidations<TSource, TProperty> AddIfValid<TValidationAttribute>(
Action<TValidationAttribute> setter,
params Expression<Func<TSource, object>>[] properties)
where TValidationAttribute : ValidationAttribute, new()
{
var attribute = new TValidationAttribute();
setter?.Invoke(attribute);
var propsToValidate = properties.ToDictionary(p => p.NameOf(), p =>
{
var func = p.Compile();
Func<object, object> convert = o => func((TSource) o);
return convert;
});
var validationAttribute = new ValidateIfAttribute(propsToValidate, attribute);
return Add(validationAttribute);
}
}
}