-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathValidation.cs
471 lines (393 loc) · 17.7 KB
/
Validation.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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/***********************************************************************
Copyright 2024 CodeX Enterprises LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Major Changes:
12/2017 0.2 Initial release (Joel Champagne)
02/2018 0.2.4 Primary implementation (Joel Champagne)
***********************************************************************/
#nullable enable
using CodexMicroORM.Core.Helper;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace CodexMicroORM.Core.Services
{
public class ValidationService : ICEFValidationHost
{
private readonly static ConcurrentDictionary<Type, List<(string prop, object? defval)>> _typePropRequired = new(Globals.DefaultCollectionConcurrencyLevel, Globals.DefaultDictionaryCapacity);
private readonly static ConcurrentDictionary<Type, List<(string prop, int maxlength)>> _typePropMaxLength = new(Globals.DefaultCollectionConcurrencyLevel, Globals.DefaultDictionaryCapacity);
private readonly static ConcurrentDictionary<Type, List<(string prop, double minval, double maxval)>> _typePropRange = new(Globals.DefaultCollectionConcurrencyLevel, Globals.DefaultDictionaryCapacity);
private readonly static ConcurrentDictionary<Type, List<(string? prop, Func<object, string?> fn)>> _typeCustomValidator = new(Globals.DefaultCollectionConcurrencyLevel, Globals.DefaultDictionaryCapacity);
private readonly static ConcurrentDictionary<Type, List<string>> _typeIllegalUpdate = new(Globals.DefaultCollectionConcurrencyLevel, Globals.DefaultDictionaryCapacity);
/// <summary>
/// Registers a global validation for a specific type / specific property, indicating it cannot be updated after having been assigned a value.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="propName"></param>
public static void RegisterIllegalUpdate<T>(string propName)
{
CEF.RegisterForType<T>(new ValidationService());
_typeIllegalUpdate.TryGetValue(typeof(T), out var vl);
vl ??= [];
vl.Add(propName);
_typeIllegalUpdate[typeof(T)] = vl;
}
/// <summary>
/// For a given type and property name, return whether it was registered as a required property.
/// </summary>
/// <param name="t"></param>
/// <param name="propName"></param>
/// <returns></returns>
public static bool? GetRequiredFor(Type t, string propName)
{
if (_typePropRequired.TryGetValue(t, out var rvl))
{
var l = (from a in rvl where string.Compare(a.prop, propName) == 0 select a);
return l.Any();
}
return null;
}
/// <summary>
/// Registers a global validation for a specific type / specific property, indicating it is a required field.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="V"></typeparam>
/// <param name="propName"></param>
public static void RegisterRequired<T, V>(string propName) where T : class
{
CEF.RegisterForType<T>(new ValidationService());
_typePropRequired.TryGetValue(typeof(T), out var vl);
vl ??= [];
vl.Add((propName, default(V)));
_typePropRequired[typeof(T)] = vl;
}
/// <summary>
/// Registers a global validation for a specific type / specific property, indicating it is a required field.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="propType"></param>
/// <param name="propName"></param>
public static void RegisterRequired<T>(Type propType, string propName) where T : class
{
CEF.RegisterForType<T>(new ValidationService());
_typePropRequired.TryGetValue(typeof(T), out var vl);
vl ??= [];
if (propType.IsValueType)
{
vl.Add((propName, propType.FastCreateNoParm()));
}
else
{
vl.Add((propName, null));
}
_typePropRequired[typeof(T)] = vl;
}
/// <summary>
/// Registers a global validation for a specific type, allowing running a custom rule over each instance (optionally bound to a specific property). Returning a non-null string error message indicates an error state.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="validator"></param>
/// <param name="propName"></param>
public static void RegisterCustomValidation<T>(Func<T, string?> validator, string? propName = null) where T : class
{
CEF.RegisterForType<T>(new ValidationService());
_typeCustomValidator.TryGetValue(typeof(T), out var vl);
vl ??= [];
vl.Add((propName, (object p) => validator.Invoke((T)p)));
_typeCustomValidator[typeof(T)] = vl;
}
/// <summary>
/// Returns a registered maximum length for a type/property.
/// </summary>
/// <param name="t"></param>
/// <param name="propName"></param>
/// <returns></returns>
public static int? GetMaxLengthFor(Type t, string propName)
{
if (_typePropMaxLength.TryGetValue(t, out var mlv))
{
var l = (from a in mlv where string.Compare(a.prop, propName) == 0 select a);
if (l.Any())
{
return l.First().maxlength;
}
}
return null;
}
/// <summary>
/// Registers a global validation for a specific type / specific property, indicating its maximum length when cast to a string.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="propName"></param>
/// <param name="maxlength"></param>
public static void RegisterMaxLength<T>(string propName, int maxlength) where T : class
{
CEF.RegisterForType<T>(new ValidationService());
_typePropMaxLength.TryGetValue(typeof(T), out List<(string prop, int maxlength)>? vl);
vl ??= [];
vl.Add((propName, maxlength));
_typePropMaxLength[typeof(T)] = vl;
}
/// <summary>
/// Registers a global vlaidation for a specific type / specific property, indicating a minimum and/or maximum numeric value.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="propName"></param>
/// <param name="minval"></param>
/// <param name="maxval"></param>
public static void RegisterRangeValidation<T>(string propName, double? minval, double? maxval) where T : class
{
CEF.RegisterForType<T>(new ValidationService());
_typePropRange.TryGetValue(typeof(T), out List<(string prop, double minval, double maxval)>? vl);
vl ??= [];
vl.Add((propName, minval.GetValueOrDefault(double.MinValue), maxval.GetValueOrDefault(double.MaxValue)));
_typePropRange[typeof(T)] = vl;
}
public static string RequiredFieldMessage
{
get;
set;
} = "{0} is required.";
public static string TooLargeFieldMessage
{
get;
set;
} = "{0} is too long (maximum length is {1}).";
public static string RangeLessThanFieldMessage
{
get;
set;
} = "{0} must be less than or equal to {1}";
public static string RangeGreaterThanFieldMessage
{
get;
set;
} = "{0} must be greater than or equal to {1}.";
public static string RangeBetweenFieldMessage
{
get;
set;
} = "{0} must be between {1} and {2}.";
public static string IllegalUpdateMessage
{
get;
set;
} = "Cannot update {0}.";
public static bool SplitCamelCaseWords
{
get;
set;
} = true;
/// <summary>
/// Return zero, one or many validation messages that pertain to the input object, based on registered validations.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="o"></param>
/// <returns></returns>
public IEnumerable<(ValidationErrorCode error, string? message)> GetObjectMessage<T>(T? o) where T : class
{
List<(ValidationErrorCode error, string? message)> messages = [];
if (o == null)
{
return messages;
}
var uw = o.AsUnwrapped();
if (uw == null)
{
return messages;
}
var iw = uw.AsInfraWrapped();
if (iw == null)
{
return messages;
}
var bt = uw.GetBaseType();
if (_typePropRequired.TryGetValue(bt, out var vl))
{
foreach (var (prop, defval) in vl)
{
if (defval.IsSame(iw.GetValue(prop)))
{
messages.Add((ValidationErrorCode.MissingRequired, BuildMessageForProperty(RequiredFieldMessage, prop)));
}
}
}
if (_typePropMaxLength.TryGetValue(bt, out List<(string prop, int maxlength)>? vl2))
{
foreach (var (prop, maxlength) in vl2)
{
if (iw.GetValue(prop)?.ToString()?.Length > maxlength)
{
messages.Add((ValidationErrorCode.TooLarge, BuildMessageForProperty(TooLargeFieldMessage, prop, maxlength)));
}
}
}
if (_typeIllegalUpdate.TryGetValue(bt, out List<string>? vl2b))
{
iw.UpdateData();
foreach (var v in vl2b)
{
var rs = iw.GetRowState();
if (rs == ObjectState.Modified || rs == ObjectState.ModifiedPriority)
{
if (!iw.GetOriginalValue(v, false).IsSame(iw.GetValue(v)))
{
messages.Add((ValidationErrorCode.IllegalUpdate, BuildMessageForProperty(IllegalUpdateMessage, v)));
}
}
}
}
if (_typePropRange.TryGetValue(bt, out List<(string prop, double minval, double maxval)>? vl3))
{
foreach (var (prop, minval, maxval) in vl3)
{
if (double.TryParse(iw.GetValue(prop)?.ToString(), out double val))
{
if (val < minval || val > maxval)
{
if (minval != double.MinValue && maxval != double.MaxValue)
{
messages.Add((ValidationErrorCode.NumericRange, BuildMessageForProperty(RangeBetweenFieldMessage, prop, minval, maxval)));
}
else
{
if (minval == double.MinValue)
{
messages.Add((ValidationErrorCode.NumericRange, BuildMessageForProperty(RangeLessThanFieldMessage, prop, maxval)));
}
else
{
messages.Add((ValidationErrorCode.NumericRange, BuildMessageForProperty(RangeGreaterThanFieldMessage, prop, minval)));
}
}
}
}
}
}
if (_typeCustomValidator.TryGetValue(bt, out var vl4))
{
foreach (var (prop, fn) in vl4)
{
var msg = fn.Invoke(uw);
if (!string.IsNullOrEmpty(msg))
{
messages.Add((ValidationErrorCode.CustomError, msg));
}
}
}
return (from b in (from a in messages select new { a.error, a.message }).Distinct() select (b.error, b.message));
}
/// <summary>
/// Return zero, one or many validation messages that pertain to a specific property of the input object, based on registered validations.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="o"></param>
/// <param name="propName"></param>
/// <returns></returns>
public IEnumerable<(ValidationErrorCode error, string? message)> GetPropertyMessages<T>(T? o, string propName) where T : class
{
List<(ValidationErrorCode error, string? message)> messages = [];
if (o == null)
{
return messages;
}
var uw = o.AsUnwrapped();
if (uw == null)
{
return messages;
}
var iw = uw.AsInfraWrapped();
if (iw == null)
{
return messages;
}
var bt = uw.GetBaseType();
if (_typePropRequired.TryGetValue(bt, out var vl))
{
var pmatch = (from a in vl where string.Compare(propName, a.prop, !Globals.CaseSensitiveDictionaries) == 0 select a);
if (pmatch.Any() && pmatch.First().defval.IsSame(iw.GetValue(propName)))
{
messages.Add((ValidationErrorCode.MissingRequired, BuildMessageForProperty(RequiredFieldMessage, propName)));
}
}
if (_typePropMaxLength.TryGetValue(bt, out List<(string prop, int maxlength)>? vl2))
{
var pmatch = (from a in vl2 where string.Compare(propName, a.prop, !Globals.CaseSensitiveDictionaries) == 0 select a);
if (pmatch.Any() && iw.GetValue(propName)?.ToString()?.Length > pmatch.First().maxlength)
{
messages.Add((ValidationErrorCode.TooLarge, BuildMessageForProperty(TooLargeFieldMessage, propName, pmatch.First().maxlength)));
}
}
if (_typeIllegalUpdate.TryGetValue(bt, out List<string>? vl2b))
{
iw.UpdateData();
var pmatch = (from a in vl2b where string.Compare(propName, a, !Globals.CaseSensitiveDictionaries) == 0 select a).FirstOrDefault();
var rs = iw.GetRowState();
if (!string.IsNullOrEmpty(pmatch) && (rs == ObjectState.Modified || rs == ObjectState.ModifiedPriority) && !iw.GetOriginalValue(pmatch, false).IsSame(iw.GetValue(pmatch)))
{
messages.Add((ValidationErrorCode.IllegalUpdate, BuildMessageForProperty(IllegalUpdateMessage, pmatch)));
}
}
if (_typeCustomValidator.TryGetValue(bt, out var vl3))
{
var pmatch = (from a in vl3 where string.Compare(propName, a.prop, !Globals.CaseSensitiveDictionaries) == 0 select a);
if (pmatch.Any())
{
var msg = pmatch.First().fn.Invoke(uw);
if (!string.IsNullOrEmpty(msg))
{
messages.Add((ValidationErrorCode.CustomError, msg));
}
}
}
return (from b in (from a in messages select new { a.error, a.message }).Distinct() select (b.error, b.message));
}
private static string BuildMessageForProperty(string msg, string propName, object? opt1 = null, object? opt2 = null)
{
if (SplitCamelCaseWords)
{
foreach (Match m in Regex.Matches(propName, "[a-z][A-Z]").Cast<Match>().ToArray())
{
propName = propName.Replace(m.Value, $"{m.Value[0]} {m.Value[1]}");
}
}
return string.Format(msg, propName, opt1, opt2);
}
void ICEFService.Disposing(ServiceScope ss)
{
}
void ICEFService.FinishSetup(ServiceScope.TrackedObject to, ServiceScope ss, bool isNew, IDictionary<string, object?>? props, ICEFServiceObjState? state, bool initFromTemplate, RetrievalIdentityMode identityMode)
{
}
WrappingSupport ICEFService.IdentifyInfraNeeds(object o, object? replaced, ServiceScope ss, bool isNew)
{
var bt = o.GetBaseType();
// We only need to support data errors if we have a registered validation for the type in question!
if (_typeCustomValidator.ContainsKey(bt) || _typePropMaxLength.ContainsKey(bt) || _typePropRequired.ContainsKey(bt))
{
return WrappingSupport.DataErrors;
}
return WrappingSupport.None;
}
Type? ICEFService.IdentifyStateType(object o, ServiceScope ss, bool isNew)
{
return null;
}
IList<Type>? ICEFService.RequiredServices()
{
return null;
}
}
}