123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace Edge.Core.Parser.BinaryParser.Attributes
- {
- [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
- public class RangeAttribute : ValidationAttribute
- {
-
-
-
- private int rangValidationType = -1;
- private string errorMsg = string.Empty;
- private string validatedValue = string.Empty;
- private int intMin = -1;
- private int intMax = -1;
- private decimal decimalMin = -1;
- private decimal decimalMax = -1;
- private IEnumerable<int> allowedIntValues = null;
-
- private string regexString = string.Empty;
- private RegexOptions regexOptions;
-
-
-
-
-
-
- public RangeAttribute(int min, int max, string errorMessage)
- {
- this.intMin = min;
- this.intMax = max;
- this.errorMsg = errorMessage;
- this.rangValidationType = 0;
- }
-
-
-
-
-
-
- public RangeAttribute(decimal min, decimal max, string errorMessage)
- {
- this.decimalMin = min;
- this.decimalMax = max;
- this.errorMsg = errorMessage;
- this.rangValidationType = 1;
- }
-
-
-
-
-
- public RangeAttribute(string regexString, RegexOptions regexOptions, string errorMessage)
- {
-
- this.regexString = regexString;
- this.regexOptions = regexOptions;
- this.errorMsg = errorMessage;
- this.rangValidationType = 2;
- }
-
-
-
-
-
- public RangeAttribute(int[] allowedValues, string errorMessage)
- {
- this.allowedIntValues = allowedValues;
- this.errorMsg = errorMessage;
- this.rangValidationType = 3;
- }
- public new string ErrorMessage
- {
- get
- {
- switch (this.rangValidationType)
- {
- case 0:
- return string.Format(this.errorMsg, this.validatedValue, this.intMin, this.intMax);
- case 1:
- return string.Format(this.errorMsg, this.validatedValue, this.decimalMin, this.decimalMax);
- case 2:
- return string.Format(this.errorMsg, this.validatedValue, this.regexString);
- case 3:
- return string.Format(this.errorMsg, this.validatedValue, this.allowedIntValues.Cast<string>().Aggregate((p, acc) => p + ", " + acc));
- }
- return this.errorMsg;
- }
-
- }
- public override bool IsValid(object value)
- {
- if (value == null) return true;
- this.validatedValue = value?.ToString() ?? "";
- switch (this.rangValidationType)
- {
- case 0:
- int targetInt = -1;
- if (!int.TryParse(value.ToString(), out targetInt))
- {
- return false;
- }
- return targetInt >= this.intMin && targetInt <= this.intMax;
- case 1:
- decimal targetDecimal = -1;
- if (!decimal.TryParse(value.ToString(), out targetDecimal))
- {
- return false;
- }
- return targetDecimal >= this.decimalMin && targetDecimal <= this.decimalMax;
- case 2:
- return Regex.IsMatch(value.ToString(), this.regexString, this.regexOptions);
- case 3:
- int tempInt = -1;
- if (!int.TryParse(value.ToString(), out tempInt))
- {
- return false;
- }
- return this.allowedIntValues.Contains(tempInt);
- }
- return false;
- }
- }
- }
|