Validator.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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
  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(object validatingObject, ValidatorContext context)
  21. {
  22. Results.Clear();
  23. OnValidate(validatingObject, context);
  24. }
  25. protected abstract void OnValidate(object validatingObject, ValidatorContext context);
  26. public void Reset()
  27. {
  28. Results.Clear();
  29. }
  30. public AggregateException? ToAggregateException(bool includeWarning = false, bool inclueInfo = false)
  31. {
  32. return ToAggregateException(Results, includeWarning, inclueInfo);
  33. }
  34. public static AggregateException? ToAggregateException(IEnumerable<ValidationResult> results, bool includeWarning = false, bool inclueInfo = false)
  35. {
  36. var errors = new List<Exception>();
  37. foreach (var result in results)
  38. {
  39. if (result.Severity == ValidationResult.ValidationSeverityEnum.Error ||
  40. result.Severity == ValidationResult.ValidationSeverityEnum.Critical ||
  41. (includeWarning && result.Severity == ValidationResult.ValidationSeverityEnum.Warning) ||
  42. (inclueInfo && result.Severity == ValidationResult.ValidationSeverityEnum.Info))
  43. {
  44. errors.Add(new Exception(result.Description));
  45. }
  46. }
  47. if (errors.Count>0)
  48. return new AggregateException(errors);
  49. return null;
  50. }
  51. protected void AddResults(IEnumerable<ValidationResult> resultsToAdd)
  52. {
  53. ((List<ValidationResult>)Results).AddRange(resultsToAdd);
  54. }
  55. protected bool CheckIfEmpty(string name,string value, ValidationResult.ValidationSeverityEnum severity, string description = "")
  56. {
  57. if (!string.IsNullOrEmpty(value))
  58. return false;
  59. if (string.IsNullOrEmpty(description))
  60. description = $"Property named '{name}' must be filled.";
  61. Results.Add(new ValidationResult(name, severity, description));
  62. return true;
  63. }
  64. protected bool CheckIfNull(string name,object? value, ValidationResult.ValidationSeverityEnum severity, string description = "")
  65. {
  66. if (value!=null)
  67. return false;
  68. if (string.IsNullOrEmpty(description))
  69. description = $"Property named '{name}' must be filled.";
  70. Results.Add(new ValidationResult(name, severity, description));
  71. return true;
  72. }
  73. protected bool CheckIfCollectionEmpty(string name,IEnumerable<object>? value, ValidationResult.ValidationSeverityEnum severity, string description = "")
  74. {
  75. if (value!=null && value.Any())
  76. return false;
  77. if (string.IsNullOrEmpty(description))
  78. description = $"Collection named '{name}' must have at least one element.";
  79. Results.Add(new ValidationResult(name, severity, description));
  80. return true;
  81. }
  82. protected bool CheckIfDirectoryNotExists(string name,string value, ValidationResult.ValidationSeverityEnum severity, string description = "")
  83. {
  84. if (string.IsNullOrEmpty(description))
  85. description = $"Directory '{value}' in property '{name}' doesn't exists or not accessible.";
  86. try
  87. {
  88. if (FileSystem.Directory.Exists(value))
  89. return false;
  90. }
  91. catch
  92. {
  93. // ignored
  94. }
  95. Results.Add(new ValidationResult(name, severity, description));
  96. return true;
  97. }
  98. protected bool CheckIfNotNumeric(string name,string value, ValidationResult.ValidationSeverityEnum severity, string description = "")
  99. {
  100. if (string.IsNullOrEmpty(description))
  101. description = $"Property '{name}' must have numeric value.";
  102. if (decimal.TryParse(value, out decimal outval))
  103. return false;
  104. Results.Add(new ValidationResult(name, severity, description));
  105. return true;
  106. }
  107. protected bool CheckIfNotGreater(string name,string value, decimal compareValue, bool equalAllowed, ValidationResult.ValidationSeverityEnum severity, string description = "")
  108. {
  109. if (string.IsNullOrEmpty(description))
  110. description = $"Property '{name}' with value '{value}' must be greater than '{compareValue}'.";
  111. if (decimal.TryParse(value, out decimal outval))
  112. {
  113. var result = false;
  114. if (equalAllowed)
  115. result = (outval > compareValue);
  116. else
  117. result = (outval >= compareValue);
  118. if(result)
  119. return false;
  120. }
  121. Results.Add(new ValidationResult(name, severity, description));
  122. return true;
  123. }
  124. protected bool CheckIfNotGreater(string name, decimal value, decimal compareValue, bool equalAllowed,
  125. ValidationResult.ValidationSeverityEnum severity, string description = "")
  126. {
  127. if (string.IsNullOrEmpty(description))
  128. description = $"Property '{name}' with value '{value}' must be greater than '{compareValue}'.";
  129. var result = false;
  130. if (equalAllowed)
  131. result = (value > compareValue);
  132. else
  133. result = (value >= compareValue);
  134. if (result)
  135. return false;
  136. Results.Add(new ValidationResult(name, severity, description));
  137. return true;
  138. }
  139. protected bool CheckIfFileNotExists(string name,string value, ValidationResult.ValidationSeverityEnum severity, string description = "")
  140. {
  141. if (string.IsNullOrEmpty(description))
  142. description = $"File '{value}' in property '{name}' doesn't exists or not accessible.";
  143. try
  144. {
  145. if (FileSystem.File.Exists(value))
  146. return false;
  147. }
  148. catch
  149. {
  150. // ignored
  151. }
  152. Results.Add(new ValidationResult(name, severity, description));
  153. return true;
  154. }
  155. protected virtual bool GetIsSuccess()
  156. {
  157. return !Results.Any(x =>
  158. /*x.Severity == ValidationResult.ValidationSeverityEnum.Warning ||*/
  159. x.Severity == ValidationResult.ValidationSeverityEnum.Error ||
  160. x.Severity == ValidationResult.ValidationSeverityEnum.Critical);
  161. }
  162. }
  163. }