RangeAttribute.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. namespace Edge.Core.Parser.BinaryParser.Attributes
  7. {
  8. [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
  9. public class RangeAttribute : ValidationAttribute
  10. {
  11. /// <summary>
  12. /// default have 3 types of validation, the value based on 0, if need extend, then increase the value
  13. /// </summary>
  14. private int rangValidationType = -1;
  15. private string errorMsg = string.Empty;
  16. private string validatedValue = string.Empty;
  17. private int intMin = -1;
  18. private int intMax = -1;
  19. private decimal decimalMin = -1;
  20. private decimal decimalMax = -1;
  21. private IEnumerable<int> allowedIntValues = null;
  22. //private Type validatingType = null;
  23. private string regexString = string.Empty;
  24. private RegexOptions regexOptions;
  25. /// <summary>
  26. /// Range validation for int type values with specify the min and max value.
  27. /// </summary>
  28. /// <param name="min">min values</param>
  29. /// <param name="max">max values</param>
  30. /// <param name="errorMessage">error message to show, support String.Format, the {0}, {1} and {2} are auto set to actual value, min and max.</param>
  31. public RangeAttribute(int min, int max, string errorMessage)
  32. {
  33. this.intMin = min;
  34. this.intMax = max;
  35. this.errorMsg = errorMessage;
  36. this.rangValidationType = 0;
  37. }
  38. /// <summary>
  39. /// Range validation for decimal type values with specify the min and max value.
  40. /// </summary>
  41. /// <param name="min"></param>
  42. /// <param name="max"></param>
  43. /// <param name="errorMessage">error message to show, support String.Format, the {0}, {1} and {2} are auto set to concrete value, min and max.</param>
  44. public RangeAttribute(decimal min, decimal max, string errorMessage)
  45. {
  46. this.decimalMin = min;
  47. this.decimalMax = max;
  48. this.errorMsg = errorMessage;
  49. this.rangValidationType = 1;
  50. }
  51. /// <summary>
  52. /// Range validation for string type values with specify the regex constrait.
  53. /// </summary>
  54. /// <param name="regexString"></param>
  55. /// <param name="errorMessage">error message to show, support String.Format, the {0}, {1} are auto set to concrete value, regexString.</param>
  56. public RangeAttribute(string regexString, RegexOptions regexOptions, string errorMessage)
  57. {
  58. //this.validatingType = type;
  59. this.regexString = regexString;
  60. this.regexOptions = regexOptions;
  61. this.errorMsg = errorMessage;
  62. this.rangValidationType = 2;
  63. }
  64. /// <summary>
  65. /// Range validation for int type values with specify allowed values.
  66. /// </summary>
  67. /// <param name="allowedValues">the value must be the element of the array: allowedValues</param>
  68. /// <param name="errorMessage">error message to show, support String.Format, the {0}, {1} are auto set to concrete value, allowed ints string.</param>
  69. public RangeAttribute(int[] allowedValues, string errorMessage)
  70. {
  71. this.allowedIntValues = allowedValues;
  72. this.errorMsg = errorMessage;
  73. this.rangValidationType = 3;
  74. }
  75. public new string ErrorMessage
  76. {
  77. get
  78. {
  79. switch (this.rangValidationType)
  80. {
  81. case 0:
  82. return string.Format(this.errorMsg, this.validatedValue, this.intMin, this.intMax);
  83. case 1:
  84. return string.Format(this.errorMsg, this.validatedValue, this.decimalMin, this.decimalMax);
  85. case 2:
  86. return string.Format(this.errorMsg, this.validatedValue, this.regexString);
  87. case 3:
  88. return string.Format(this.errorMsg, this.validatedValue, this.allowedIntValues.Cast<string>().Aggregate((p, acc) => p + ", " + acc));
  89. }
  90. return this.errorMsg;
  91. }
  92. //private set { this.errorMsg = value; }
  93. }
  94. public override bool IsValid(object value)
  95. {
  96. if (value == null) return true;
  97. this.validatedValue = value?.ToString() ?? "";
  98. switch (this.rangValidationType)
  99. {
  100. case 0:
  101. int targetInt = -1;
  102. if (!int.TryParse(value.ToString(), out targetInt))
  103. {
  104. return false;
  105. }
  106. return targetInt >= this.intMin && targetInt <= this.intMax;
  107. case 1:
  108. decimal targetDecimal = -1;
  109. if (!decimal.TryParse(value.ToString(), out targetDecimal))
  110. {
  111. return false;
  112. }
  113. return targetDecimal >= this.decimalMin && targetDecimal <= this.decimalMax;
  114. case 2:
  115. return Regex.IsMatch(value.ToString(), this.regexString, this.regexOptions);
  116. case 3:
  117. int tempInt = -1;
  118. if (!int.TryParse(value.ToString(), out tempInt))
  119. {
  120. return false;
  121. }
  122. return this.allowedIntValues.Contains(tempInt);
  123. }
  124. return false;
  125. }
  126. }
  127. }