Repository navigation
Expand file tree
/
Copy pathValidationService.cs
More file actions
248 lines (225 loc) · 7.93 KB
/
Copy pathValidationService.cs
File metadata and controls
248 lines (225 loc) · 7.93 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//------------------------------------------------------------------------------
// <copyright company="LeanKit Inc.">
// Copyright (c) LeanKit Inc. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using LeanKit.API.Client.Library.TransferObjects;
using RestSharp;
namespace LeanKit.API.Client.Library.Validation
{
public interface IValidationService
{
List<ValidationResult> ValidateRequest(RestRequest request);
}
public class ValidationService : IValidationService
{
private const string EntityIdQualifier = @".*id";
private const string EntityIdsQualifier = @".*ids";
private const string ApiTypesNamespaceQualifier = "LeanKit.API.Client.Library.TransferObjects";
private readonly Board _board;
private readonly List<ValidationResult> _validationResults;
public ValidationService(Board board)
{
_board = board;
_validationResults = new List<ValidationResult>();
}
public List<ValidationResult> ValidateRequest(RestRequest request)
{
foreach (Parameter requestParameter in request.Parameters)
{
string pName = requestParameter.Name;
object pValue = requestParameter.Value;
if (pValue == null)
{
continue;
}
Type pType = pValue.GetType();
if (pType.IsPrimitive)
{
TryValidateDomainIdentity(pName, EntityIdQualifier, pValue, pType);
}
else if (pType.IsClass)
{
if (typeof (IEnumerable).IsAssignableFrom(pType) && pType.IsGenericType)
{
Type genericType = pType.GetGenericArguments()[0];
if (genericType.IsPrimitive)
{
TryValidateDomainIdentity(pName, EntityIdsQualifier, pValue, pType);
}
else if (genericType.IsClass)
{
List<object> pValues = ((IEnumerable) pValue).Cast<object>().ToList();
foreach (object innerValue in pValues)
{
string newName = string.Format("{0}[{1}]", pName, pValues.IndexOf(innerValue));
ValidateDomainEntity(newName, innerValue, innerValue.GetType());
}
}
}
else
{
ValidateDomainEntity(pName, pValue, pType);
}
}
}
return _validationResults;
}
private void ValidateDomainEntity(string parameterName, object parameterValue, Type parameterType)
{
PropertyInfo[] allProperties = parameterType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
//perform DataAnnotations validation
if (allProperties.Any(x => x.GetCustomAttributes(typeof (ValidationAttribute), true).Length > 0))
{
var classValidatonResults = new List<System.ComponentModel.DataAnnotations.ValidationResult>();
bool isValid = Validator.TryValidateObject(parameterValue,
new ValidationContext(parameterValue, null, null),
classValidatonResults);
if (!isValid)
{
_validationResults.AddRange(
classValidatonResults.Select(x => new ValidationResult(parameterName, x)));
}
}
//perform custom validation
IEnumerable<PropertyInfo> customPropertiesInfo =
allProperties.Where(x => x.GetCustomAttributes(typeof (DomainIdentityValidationAttribute), false).Length > 0);
foreach (PropertyInfo customPropertyInfo in customPropertiesInfo)
{
Type customPropertyType = customPropertyInfo.PropertyType;
object customPropertyValue = customPropertyInfo.GetValue(parameterValue,
BindingFlags.Public | BindingFlags.Instance,
null, null, CultureInfo.InvariantCulture);
long[] domainIdentities = GetDomainIdentities(customPropertyValue, customPropertyType);
if (domainIdentities != null)
{
foreach (long domainIdentity in domainIdentities)
{
ValidateDomainIdentity(customPropertyInfo.Name, domainIdentity,
string.Format("{0}.{1}", parameterName, customPropertyInfo.Name));
}
}
}
//drill through nested properties and try to validate them
foreach (
PropertyInfo nestedProperty in allProperties.Where(x => x.PropertyType.IsClass || x.PropertyType.IsInterface))
{
string npName = nestedProperty.Name;
Type npType = nestedProperty.PropertyType;
object npValue = nestedProperty.GetValue(parameterValue, BindingFlags.Public | BindingFlags.Instance, null, null,
CultureInfo.InvariantCulture);
if (npValue == null)
{
continue;
}
if (typeof (IEnumerable).IsAssignableFrom(npType) && npType.IsGenericType)
{
Type genericType = npType.GetGenericArguments()[0];
if (ApiTypesNamespaceQualifier.Equals(genericType.Namespace, StringComparison.Ordinal))
{
List<object> pValues = ((IEnumerable) npValue).Cast<object>().ToList();
foreach (object innerValue in pValues)
{
string newName = string.Format("{0}.{1}[{2}]", parameterName, npName, pValues.IndexOf(innerValue));
ValidateDomainEntity(newName, innerValue, innerValue.GetType());
}
}
}
else
{
if (ApiTypesNamespaceQualifier.Equals(npType.Namespace, StringComparison.Ordinal))
{
string newName = string.Format("{0}.{1}", parameterName, npName);
ValidateDomainEntity(newName, npValue, npType);
}
}
}
}
private void TryValidateDomainIdentity(string parameterName, string matchPattern, object parameterValue,
Type parameterType)
{
bool nameMatched = Regex.IsMatch(parameterName, matchPattern, RegexOptions.Singleline | RegexOptions.IgnoreCase);
if (nameMatched)
{
long[] domainIdentities = GetDomainIdentities(parameterValue, parameterType);
if (domainIdentities != null)
{
Array.ForEach(domainIdentities, x =>
{
if (domainIdentities.Length > 1)
{
string correctedParameterName = string.Format("{0}[{1}]", parameterName, Array.IndexOf(domainIdentities, x));
ValidateDomainIdentity(parameterName.Substring(0, parameterName.Length - 1), x, correctedParameterName);
}
else
{
ValidateDomainIdentity(parameterName, x);
}
});
}
}
}
private void ValidateDomainIdentity(string parameterName, long parameterValue, string correctedParameterName = null)
{
string newParameterName = !string.IsNullOrEmpty(correctedParameterName)
? correctedParameterName
: parameterName;
if (parameterValue <= 0)
{
_validationResults.Add(GetValidationResult(newParameterName, parameterValue,
"Domain identity parameter must be greater than zero"));
}
if (_board != null)
{
object entity;
switch (parameterName.ToLowerInvariant())
{
case "cardid":
entity = _board.Lanes.SelectMany(x => x.Cards).FirstOrDefault(x => x.Id == parameterValue);
break;
case "laneid":
case "tolaneid":
entity = _board.Lanes.FirstOrDefault(x => x.Id == parameterValue);
break;
case "typeid":
entity = _board.CardTypes.FirstOrDefault(x => x.Id == parameterValue);
break;
case "classofserviceid":
entity = _board.ClassesOfService.FirstOrDefault(x => x.Id == parameterValue);
break;
case "assigneduserids":
entity = _board.BoardUsers.FirstOrDefault(x => x.Id == parameterValue);
break;
default:
return;
}
if (entity == null)
{
_validationResults.Add(GetValidationResult(newParameterName, parameterValue,
"Domain identity parameter doesn't have corresponding entity"));
}
}
}
private long[] GetDomainIdentities(object value, Type type)
{
if (typeof (IEnumerable).IsAssignableFrom(type))
{
if (value != null) return (value as IEnumerable).Cast<long>().ToArray();
}
var domainIdentities = new[] {Convert.ToInt64(value)};
return domainIdentities;
}
private ValidationResult GetValidationResult(string parameterName, object parameterValue, string message)
{
return new ValidationResult(string.Format("Name = {0}, Value = {1}. {2}", parameterName, parameterValue, message));
}
}
}