Validator.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Abstractions;
  4. using System.Linq;
  5. namespace Quadarax.Foundation.Core.Value
  6. {
  7. public abstract class Validator<TValidatingObject>
  8. {
  9. public IList<ValidationResult> Results { get; private set; }
  10. public bool IsSuccess => GetIsSuccess();
  11. protected IFileSystem FileSystem { get; private set; }
  12. protected Validator() : this(new FileSystem())
  13. {
  14. }
  15. protected Validator(IFileSystem fileSystemAbstraction)
  16. {
  17. FileSystem = fileSystemAbstraction ?? throw new ArgumentNullException(nameof(fileSystemAbstraction));
  18. Results = new List<ValidationResult>();
  19. }
  20. public void Validate(TValidatingObject validatingObject, ValidatorContext context)
  21. {
  22. if (validatingObject == null) throw new ArgumentNullException(nameof(validatingObject));
  23. if (context == null) throw new ArgumentNullException(nameof(context));
  24. Results.Clear();
  25. OnValidate(validatingObject, context);
  26. }
  27. protected abstract void OnValidate(TValidatingObject validatingObject, ValidatorContext context);
  28. public void Reset()
  29. {
  30. Results.Clear();
  31. }
  32. public AggregateException ToAggregateException(bool includeWarning = false, bool inclueInfo = false)
  33. {
  34. return ToAggregateException(Results, includeWarning, inclueInfo);
  35. }
  36. public static AggregateException ToAggregateException(IEnumerable<ValidationResult> results, bool includeWarning = false, bool inclueInfo = false)
  37. {
  38. var errors = new List<Exception>();
  39. foreach (var result in results)
  40. {
  41. if (result.Severity == ValidationResult.ValidationSeverityEnum.Error ||
  42. result.Severity == ValidationResult.ValidationSeverityEnum.Critical ||
  43. (includeWarning && result.Severity == ValidationResult.ValidationSeverityEnum.Warning) ||
  44. (inclueInfo && result.Severity == ValidationResult.ValidationSeverityEnum.Info))
  45. {
  46. errors.Add(new Exception(result.Description));
  47. }
  48. }
  49. if (errors.Count>0)
  50. return new AggregateException(errors);
  51. return null;
  52. }
  53. protected void AddResults(IEnumerable<ValidationResult> resultsToAdd)
  54. {
  55. ((List<ValidationResult>)Results).AddRange(resultsToAdd);
  56. }
  57. protected bool CheckIfEmpty(string name,string value, ValidationResult.ValidationSeverityEnum severity, string description = "")
  58. {
  59. if (!string.IsNullOrEmpty(value))
  60. return false;
  61. if (string.IsNullOrEmpty(description))
  62. description = $"Property named '{name}' must be filled.";
  63. Results.Add(new ValidationResult(name, severity, description));
  64. return true;
  65. }
  66. protected bool CheckIfNull(string name,object value, ValidationResult.ValidationSeverityEnum severity, string description = "")
  67. {
  68. if (value!=null)
  69. return false;
  70. if (string.IsNullOrEmpty(description))
  71. description = $"Property named '{name}' must be filled.";
  72. Results.Add(new ValidationResult(name, severity, description));
  73. return true;
  74. }
  75. protected bool CheckIfStringMaxLengthOverflow(string name,string value, int maxLength, ValidationResult.ValidationSeverityEnum severity, string description = "")
  76. {
  77. if (string.IsNullOrEmpty(value))
  78. return false;
  79. if (string.IsNullOrEmpty(description))
  80. description = $"Property named '{name}' length ({maxLength}) overflow.";
  81. if (value.Length <= maxLength)
  82. return false;
  83. Results.Add(new ValidationResult(name, severity, description));
  84. return true;
  85. }
  86. protected bool CheckIfCollectionEmpty(string name,IEnumerable<object> value, ValidationResult.ValidationSeverityEnum severity, string description = "")
  87. {
  88. if (value!=null && value.Any())
  89. return false;
  90. if (string.IsNullOrEmpty(description))
  91. description = $"Collection named '{name}' must have at least one element.";
  92. Results.Add(new ValidationResult(name, severity, description));
  93. return true;
  94. }
  95. protected bool CheckIfDirectoryNotExists(string name,string value, ValidationResult.ValidationSeverityEnum severity, string description = "")
  96. {
  97. if (string.IsNullOrEmpty(description))
  98. description = $"Directory '{value}' in property '{name}' doesn't exists or not accessible.";
  99. try
  100. {
  101. if (FileSystem.Directory.Exists(value))
  102. return false;
  103. }
  104. catch
  105. {
  106. // ignored
  107. }
  108. Results.Add(new ValidationResult(name, severity, description));
  109. return true;
  110. }
  111. protected bool CheckIfNotNumeric(string name,string value, ValidationResult.ValidationSeverityEnum severity, string description = "")
  112. {
  113. if (string.IsNullOrEmpty(description))
  114. description = $"Property '{name}' must have numeric value.";
  115. if (decimal.TryParse(value, out decimal outval))
  116. return false;
  117. Results.Add(new ValidationResult(name, severity, description));
  118. return true;
  119. }
  120. protected bool CheckIfNotGreater(string name,string value, decimal compareValue, bool equalAllowed, ValidationResult.ValidationSeverityEnum severity, string description = "")
  121. {
  122. if (string.IsNullOrEmpty(description))
  123. description = $"Property '{name}' with value '{value}' must be greater than '{compareValue}'.";
  124. if (decimal.TryParse(value, out decimal outval))
  125. {
  126. var result = false;
  127. if (equalAllowed)
  128. result = (outval > compareValue);
  129. else
  130. result = (outval >= compareValue);
  131. if(result)
  132. return false;
  133. }
  134. Results.Add(new ValidationResult(name, severity, description));
  135. return true;
  136. }
  137. protected bool CheckIfNotGreater(string name, decimal value, decimal compareValue, bool equalAllowed,
  138. ValidationResult.ValidationSeverityEnum severity, string description = "")
  139. {
  140. if (string.IsNullOrEmpty(description))
  141. description = $"Property '{name}' with value '{value}' must be greater than '{compareValue}'.";
  142. var result = false;
  143. if (equalAllowed)
  144. result = (value > compareValue);
  145. else
  146. result = (value >= compareValue);
  147. if (result)
  148. return false;
  149. Results.Add(new ValidationResult(name, severity, description));
  150. return true;
  151. }
  152. protected bool CheckIfFileNotExists(string name,string value, ValidationResult.ValidationSeverityEnum severity, string description = "")
  153. {
  154. if (string.IsNullOrEmpty(description))
  155. description = $"File '{value}' in property '{name}' doesn't exists or not accessible.";
  156. try
  157. {
  158. if (FileSystem.File.Exists(value))
  159. return false;
  160. }
  161. catch
  162. {
  163. // ignored
  164. }
  165. Results.Add(new ValidationResult(name, severity, description));
  166. return true;
  167. }
  168. protected virtual bool GetIsSuccess()
  169. {
  170. return !Results.Any(x =>
  171. /*x.Severity == ValidationResult.ValidationSeverityEnum.Warning ||*/
  172. x.Severity == ValidationResult.ValidationSeverityEnum.Error ||
  173. x.Severity == ValidationResult.ValidationSeverityEnum.Critical);
  174. }
  175. }
  176. }