-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathValidators.cs
More file actions
431 lines (370 loc) · 20.2 KB
/
Validators.cs
File metadata and controls
431 lines (370 loc) · 20.2 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Funq;
using ServiceStack.FluentValidation;
using ServiceStack.FluentValidation.Internal;
using ServiceStack.FluentValidation.Resources;
using ServiceStack.FluentValidation.Validators;
using ServiceStack.Script;
using ServiceStack.Text;
using ServiceStack.Validation;
using ServiceStack.Web;
namespace ServiceStack
{
public class ScriptConditionValidator : PropertyValidator, IPredicateValidator
{
public SharpPage Code { get; }
public ScriptConditionValidator(SharpPage code) : base(new LanguageStringSource(nameof(PredicateValidator)))
{
Code = code;
}
public override bool ShouldValidateAsynchronously(IValidationContext context) => true;
//public override bool ShouldValidateAsync(ValidationContext context) => true;
protected override async Task<bool> IsValidAsync(PropertyValidatorContext context, CancellationToken cancellation)
{
var ret = await HostContext.AppHost.EvalScriptAsync(context.ToPageResult(Code), context.ParentContext.Request);
return DefaultScripts.isTruthy(ret);
}
protected override bool IsValid(PropertyValidatorContext context)
{
var ret = HostContext.AppHost.EvalScript(context.ToPageResult(Code), context.ParentContext.Request);
return DefaultScripts.isTruthy(ret);
}
}
public static class Validators
{
public static Dictionary<Type, List<ITypeValidator>> TypeRulesMap { get; private set; } = new();
public static Dictionary<Type, List<IValidationRule>> TypePropertyRulesMap { get; private set; } = new();
public static Dictionary<string, string> ConditionErrorCodes { get; private set; } = new();
public static Dictionary<string, string> ErrorCodeMessages { get; private set; } = new();
internal static void Reset()
{
TypeRulesMap = new Dictionary<Type, List<ITypeValidator>>();
TypePropertyRulesMap = new Dictionary<Type, List<IValidationRule>>();
ConditionErrorCodes = new Dictionary<string, string>();
ErrorCodeMessages = new Dictionary<string, string>();
ValidationExtensions.Reset();
}
static readonly Func<CascadeMode> CascadeMode = () => ValidatorOptions.Global.CascadeMode;
public static bool HasValidateRequestAttributes(Type type) => type.HasAttributeOf<ValidateRequestAttribute>();
public static bool HasValidateAttributes(Type type) => type.GetPublicProperties().Any(x => x.HasAttributeOf<ValidateAttribute>());
public static async Task AssertTypeValidatorsAsync(IRequest req, object requestDto, Type requestType)
{
if (TypeRulesMap.TryGetValue(requestType, out var typeValidators))
{
foreach (var scriptValidator in typeValidators)
{
await scriptValidator.ThrowIfNotValidAsync(requestDto, req);
}
}
}
static void ThrowNoValidator(string validator) =>
throw new NotSupportedException(
$"Could not resolve matching '{validator}` Validator Script Method. " +
$"Ensure it's registered in AppHost.ScriptContext and called with correct number of arguments.");
static void ThrowInvalidValidator(string validator, string validatorType) =>
throw new NotSupportedException($"{validator} is not an `{validatorType}`");
static void ThrowInvalidValidate() =>
throw new NotSupportedException("[Validate] does not have a Validator or Condition");
static void ThrowInvalidValidateRequest() =>
throw new NotSupportedException("[ValidateRequest] does not have a Validator or Condition");
public static bool RegisterRequestRulesFor(Type type)
{
var requestAttrs = type.AllAttributes();
var to = new List<ITypeValidator>();
foreach (var attr in requestAttrs.OfType<ValidateRequestAttribute>())
{
AddTypeValidator(to, attr);
}
if (to.Count > 0)
{
TypeRulesMap[type] = to;
return true;
}
return false;
}
public static SharpPage ParseCondition(ScriptContext context, string condition)
{
var evalCode = ScriptCodeUtils.EnsureReturn(condition);
return context.CodeSharpPage(evalCode);
}
public static void AddTypeValidator(List<ITypeValidator> to, IValidateRule attr)
{
var appHost = HostContext.AppHost;
if (!string.IsNullOrEmpty(attr.Condition))
{
var code = ParseCondition(appHost.ScriptContext, attr.Condition);
to.Add(new ScriptValidator(code, attr.Condition).Init(attr));
}
else if (!string.IsNullOrEmpty(attr.Validator))
{
var ret = appHost.EvalExpression(attr
.Validator); //Validators can't be cached due to custom code/msgs
if (ret == null)
ThrowNoValidator(attr.Validator);
if (ret is ITypeValidator validator)
{
to.Add(validator.Init(attr));
}
else if (ret is List<object> objs)
{
foreach (var o in objs)
{
if (o is ITypeValidator itemValidator)
{
to.Add(itemValidator.Init(attr));
}
else ThrowInvalidValidator(attr.Validator, nameof(ITypeValidator));
}
}
else ThrowInvalidValidator(attr.Validator, nameof(ITypeValidator));
}
else ThrowInvalidValidateRequest();
}
/// <summary>
/// Register declarative property [Validate] attributes.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static bool RegisterPropertyRulesFor(Type type)
{
var container = HostContext.AppHost.Container;
var registerChildValidators = HostContext.GetPlugin<ValidationFeature>()?.ImplicitlyValidateChildProperties == true;
// Don't register child validators for explicit FluentValidation validators to avoid double registration
var typesValidatorIsDefault = ValidationExtensions.TypesValidatorsMap.TryGetValue(type, out var typeValidator)
&& typeValidator.HasInterface(typeof(IDefaultValidator));
var typeRules = new List<IValidationRule>();
foreach (var pi in type.GetPublicProperties())
{
var rule = CreateDeclarativePropertyRuleIfExists(type, pi);
if (rule != null)
{
typeRules.Add(rule);
}
if (rule == null && registerChildValidators && pi.PropertyType.IsClass && pi.PropertyType != typeof(string))
{
var collectionGenericType = pi.PropertyType.GetTypeWithGenericInterfaceOf(typeof(IEnumerable<>));
if (collectionGenericType != null)
{
var elementType = collectionGenericType.GetGenericArguments()[0];
if (!elementType.IsClass || elementType == typeof(string))
continue;
// Wire up child validators for auto generated validators
var customValidatorExist = ValidationExtensions.TypesValidatorsMap.ContainsKey(elementType) && typesValidatorIsDefault;
if (customValidatorExist ||
elementType.GetPublicProperties().Any(elProp => elProp.HasAttributeOf<ValidateAttribute>()))
{
// This code simulates setting a FluentValidation Collection validator:
// var RuleBuilder = RuleForEach(x => x.ChildCollection);
// RuleBuilder.SetValidator(new ChildValidator());
//RuleForEach() does:
// - expression: x => x.ChildCollection
// var rule = CollectionPropertyRule<T, TElement>.Create(expression, () => CascadeMode);
// AddRule(rule);
var genericTypeDef = typeof(CollectionPropertyRule<,>).MakeGenericType(type, elementType);
var member = pi;
var propAccessorExpr = TypeExtensions.CreatePropertyAccessorExpression(type, pi);
var propAccessorFn = (Func<object,object>)propAccessorExpr.Compile();
var ciCollectionPropRule = genericTypeDef.GetConstructor(CollectionCtorTypes)
?? throw new Exception("Could not find CollectionPropertyRule<T,TElement> Constructor");
var collectionRule = (PropertyRule)ciCollectionPropRule.Invoke(new object[]
{member, propAccessorFn, propAccessorExpr, CascadeMode, elementType, type});
// RuleBuilder.SetValidator(new ChildValidator()) does:
// var adaptor = new ChildValidatorAdaptor<T,TProperty>(validator, validator.GetType());
// Rule.AddValidator(validator);
//validator: Generate the declarative TypeValidator for this property
var propValidatorType = typeof(IValidator<>).MakeGenericType(elementType);
container.RegisterNewValidatorIfNotExists(elementType);
var validator = container.TryResolve(propValidatorType);
// var adaptor = new ChildValidatorAdaptor<T,TProperty>(validator, validator.GetType());
var childAdapterGenericTypeDef = typeof(ChildValidatorAdaptor<,>).MakeGenericType(type, elementType);
var ciChildAdaptor = childAdapterGenericTypeDef.GetConstructor(new[] { propValidatorType, typeof(Type) })
?? throw new Exception("Could not find ChildValidatorAdaptor<T,TElement> Constructor");
var childAdaptor = ciChildAdaptor.Invoke(new[] { validator, propValidatorType }) as IPropertyValidator;
// Rule.AddValidator(validator);
collectionRule.AddValidator(childAdaptor);
typeRules.Add(collectionRule);
}
}
else
{
// Wire up child validators for auto generated validators
var customValidatorExist = ValidationExtensions.TypesValidatorsMap.ContainsKey(pi.PropertyType) && typesValidatorIsDefault;
if (customValidatorExist ||
pi.PropertyType.GetPublicProperties().Any(elProp => elProp.HasAttributeOf<ValidateAttribute>()))
{
// var rule = PropertyRule.Create(expression, () => CascadeMode);
// var member = expression.GetMember();
// var compiled = AccessorCache<T>.GetCachedAccessor(member, expression, bypassCache);
// return new PropertyRule(member, compiled.CoerceToNonGeneric(), expression, cascadeModeThunk, typeof(TProperty), typeof(T));
var member = pi;
var propAccessorExpr = TypeExtensions.CreatePropertyAccessorExpression(type, pi);
var propAccessorFn = (Func<object,object>)propAccessorExpr.Compile();
var propertyRuleCtor = typeof(PropertyRule).GetConstructor(CollectionCtorTypes)
?? throw new Exception("Could not find PropertyRule Constructor");
var propRule = (PropertyRule)propertyRuleCtor.Invoke(new object[]
{member, propAccessorFn, propAccessorExpr, CascadeMode, pi.PropertyType, type});
// var adaptor = new ChildValidatorAdaptor<T,TProperty>(validator, validator.GetType());
// Rule.AddValidator(validator);
var propValidatorType = typeof(IValidator<>).MakeGenericType(pi.PropertyType);
container.RegisterNewValidatorIfNotExists(pi.PropertyType);
var validator = container.TryResolve(propValidatorType);
var childAdapterGenericTypeDef = typeof(ChildValidatorAdaptor<,>).MakeGenericType(type, pi.PropertyType);
var ciChildAdaptor = childAdapterGenericTypeDef.GetConstructor(new[] { propValidatorType, typeof(Type) })
?? throw new Exception("Could not find ChildValidatorAdaptor<T,TElement> Constructor");
var childAdaptor = ciChildAdaptor.Invoke(new[] { validator, propValidatorType }) as IPropertyValidator;
propRule.AddValidator(childAdaptor);
typeRules.Add(propRule);
}
}
}
}
if (typeRules.Count > 0)
{
TypePropertyRulesMap[type] = typeRules;
return true;
}
return false;
}
private static readonly Type[] CollectionCtorTypes = {
typeof(MemberInfo), typeof(Func<object, object>), typeof(LambdaExpression), typeof(Func<CascadeMode>), typeof(Type), typeof(Type)
};
private static IValidationRule CreateDeclarativePropertyRuleIfExists(Type type, PropertyInfo pi)
{
var allAttrs = pi.AllAttributes();
var validateAttrs = allAttrs.Where(x => x is ValidateAttribute).ToList();
if (validateAttrs.Count > 0)
{
var rule = CreatePropertyRule(type, pi);
var validators = (List<IPropertyValidator>) rule.Validators;
foreach (ValidateAttribute attr in validateAttrs)
{
validators.AddRule(pi, attr);
}
return rule;
}
return null;
}
public static List<ITypeValidator> GetTypeRules(Type type) => TypeRulesMap.TryGetValue(type, out var rules)
? rules
: TypeConstants<ITypeValidator>.EmptyList;
public static List<IValidationRule> GetPropertyRules(Type type) => TypePropertyRulesMap.TryGetValue(type, out var rules)
? rules
: TypeConstants<IValidationRule>.EmptyList;
public static void AddRule(Type type, string name, ValidateAttribute attr) =>
AddRule(type, type.GetProperty(name), attr);
public static void AddRule(Type type, PropertyInfo pi, ValidateAttribute attr)
{
var typeRules = TypePropertyRulesMap.TryGetValue(type, out var rules)
? rules
: TypePropertyRulesMap[type] = new List<IValidationRule>();
var rule = typeRules.FirstOrDefault(x => (x as PropertyRule)?.PropertyName == pi.Name);
if (rule == null)
typeRules.Add(rule = CreatePropertyRule(type, pi));
var validators = (List<IPropertyValidator>) rule.Validators;
validators.AddRule(pi, attr);
}
public static IValidationRule CreatePropertyRule(Type type, PropertyInfo pi)
{
var fn = pi.CreateGetter();
return new PropertyRule(pi, x => fn(x), null, CascadeMode, type, null);
}
public static IValidationRule CreateCollectionPropertyRule(Type type, PropertyInfo pi)
{
var fn = pi.CreateGetter();
return new PropertyRule(pi, x => fn(x), null, CascadeMode, type, null);
}
public static List<Action<PropertyInfo, IValidateRule>> RuleFilters { get; } = new() {
AppendDefaultValueOnEmptyValidators,
};
public static void AppendDefaultValueOnEmptyValidators(PropertyInfo pi, IValidateRule rule)
{
if (rule.Validator == "Empty" || rule.Validator == "NotEmpty")
{
// Not/EmptyValidator has a required default constructor required to accurately determine empty for value types
if (pi.PropertyType.IsValueType && !pi.PropertyType.IsNullableType())
{
rule.Validator += "(default('" + pi.PropertyType.Namespace + "." + pi.PropertyType.Name + "'))";
}
}
}
public static void AddRule(this List<IPropertyValidator> validators, PropertyInfo pi, IValidateRule propRule)
{
var appHost = HostContext.AppHost;
foreach (var ruleFilter in RuleFilters)
{
ruleFilter(pi, propRule);
}
IPropertyValidator apply(IPropertyValidator validator)
{
var errorCode = propRule.ErrorCode;
if (!string.IsNullOrEmpty(errorCode))
{
validator.Options.ErrorCodeSource = new StaticStringSource(errorCode);
}
else
{
if (propRule.Condition != null &&
ConditionErrorCodes.TryGetValue(propRule.Condition, out errorCode))
{
validator.Options.ErrorCodeSource = new StaticStringSource(errorCode);
}
}
if (!string.IsNullOrEmpty(propRule.Message))
{
validator.Options.ErrorMessageSource = new StaticStringSource(propRule.Message.Localize());
}
else if (errorCode != null && ErrorCodeMessages.TryGetValue(errorCode, out var errorMsg))
{
validator.Options.ErrorMessageSource = new StaticStringSource(errorMsg.Localize());
}
return validator;
}
if (propRule.Validator != null)
{
//Validators can't be cached due to custom code/msgs
var ret = appHost.EvalExpression(propRule.Validator);
if (ret == null)
ThrowNoValidator(propRule.Validator);
if (ret is IPropertyValidator validator)
{
validators.Add(apply(validator));
}
else if (ret is List<object> objs)
{
foreach (var o in objs)
{
if (o is IPropertyValidator itemValidator)
{
validators.Add(apply(itemValidator));
}
else ThrowInvalidValidator(propRule.Validator, nameof(IPropertyValidator));
}
}
else ThrowInvalidValidator(propRule.Validator, nameof(IPropertyValidator));
}
else if (!string.IsNullOrEmpty(propRule.Condition))
{
var evalCode = ScriptCodeUtils.EnsureReturn(propRule.Condition);
var page = appHost.ScriptContext.CodeSharpPage(evalCode);
validators.Add(apply(new ScriptConditionValidator(page)));
}
else ThrowInvalidValidate();
}
public static PageResult ToPageResult(this PropertyValidatorContext context, SharpPage page)
{
var to = new PageResult(page) {
Args = {
[ScriptConstants.It] = context.PropertyValue,
[ScriptConstants.Field] = context.PropertyName,
}
};
return to;
}
}
}