public static bool IsStringIn(string text, bool allowNull, bool compareCase, string[] allowedValues, IErrors errors, string errorKey) { bool flag = string.IsNullOrEmpty(text); if (flag && allowNull) { return(true); } if (flag && !allowNull) { string str = allowedValues.JoinDelimited(",", (string val) => val); errors.Add(errorKey, "文本必须包含 : " + str); return(false); } bool flag2 = false; for (int i = 0; i < allowedValues.Length; i++) { string strB = allowedValues[i]; if (string.Compare(text, strB, compareCase) == 0) { flag2 = true; break; } } if (!flag2) { string str2 = allowedValues.JoinDelimited(",", (string val) => val); errors.Add(errorKey, "文本必须包含 : " + str2); return(false); } return(true); }
/// <summary> /// Determines whether the text [is string in] [the specified values]. /// </summary> /// <param name="text">The text.</param> /// <param name="allowNull">if set to <c>true</c> [allow null].</param> /// <param name="compareCase">if set to <c>true</c> [compare case].</param> /// <param name="allowedValues">The allowed values.</param> /// <param name="errors">The errors.</param> /// <param name="tag">The tag.</param> /// <returns> /// <c>true</c> if [is string in] [the specified text]; otherwise, <c>false</c>. /// </returns> public static bool IsStringIn(string text, bool allowNull, bool compareCase, string[] allowedValues, IErrors errors, string tag) { bool isEmpty = string.IsNullOrEmpty(text); if (isEmpty && allowNull) return true; if (isEmpty && !allowNull) { string vals = allowedValues.JoinDelimited(",", (val) => val); errors.Add(tag, "Text must be in : " + vals); return false; } bool isValid = false; foreach (string val in allowedValues) { if (string.Compare(text, val, compareCase) == 0) { isValid = true; break; } } if (!isValid) { string vals = allowedValues.JoinDelimited(",", (val) => val); errors.Add(tag, "Text must be in : " + vals); return false; } return true; }
/// <summary> /// Determines whether the text [is string in] [the specified values]. /// </summary> /// <param name="text">The text.</param> /// <param name="allowNull">if set to <c>true</c> [allow null].</param> /// <param name="compareCase">if set to <c>true</c> [compare case].</param> /// <param name="allowedValues">The allowed values.</param> /// <param name="errors">The error collection to populate with any errors.</param> /// <param name="tag">Tag used in an error message.</param> /// <returns> /// <c>true</c> if [is string in] [the specified text]; otherwise, <c>false</c>. /// </returns> public static bool IsStringIn(string text, bool allowNull, bool compareCase, string[] allowedValues, IErrors errors, string tag) { bool isEmpty = string.IsNullOrEmpty(text); if (isEmpty && allowNull) { return(true); } if (isEmpty && !allowNull) { string vals = allowedValues.JoinDelimited(",", (val) => val); errors.Add(tag, string.Format(_messages.TextMustBeIn, vals)); return(false); } bool isValid = false; foreach (string val in allowedValues) { if (string.Compare(text, val, compareCase) == 0) { isValid = true; break; } } if (!isValid) { string vals = allowedValues.JoinDelimited(",", (val) => val); errors.Add(tag, string.Format(_messages.TextMustBeIn, vals)); return(false); } return(true); }
public static bool IsTimeOfDayWithinRange(TimeSpan time, bool checkMinBound, bool checkMaxBound, TimeSpan min, TimeSpan max, IErrors errors, string errorKey) { if (checkMinBound && time < min) { errors.Add(errorKey, "时间不能小于: " + min.ToString()); return(false); } if (checkMaxBound && time > max) { errors.Add(errorKey, "时间不能大于: " + max.ToString()); return(false); } return(true); }
public static bool IsNumberWithinRange(int num, bool checkMinBound, bool checkMaxBound, int min, int max, IErrors errors, string errorKey) { if (checkMinBound && num < min) { errors.Add(errorKey, "整数小于 " + min + "."); return(false); } if (checkMaxBound && num > max) { errors.Add(errorKey, "整数大于 " + max + "."); return(false); } return(true); }
public static bool IsDateWithinRange(DateTime date, bool checkMinBound, bool checkMaxBound, DateTime minDate, DateTime maxDate, IErrors errors, string errorKey) { if (checkMinBound && date.Date < minDate.Date) { errors.Add(errorKey, "日期不能小于: " + minDate.ToString()); return(false); } if (checkMaxBound && date.Date > maxDate.Date) { errors.Add(errorKey, "日期不能大于: " + maxDate.ToString()); return(false); } return(true); }
public static bool IsNumericWithinRange(double num, bool checkMinBound, bool checkMaxBound, double min, double max, IErrors errors, string errorKey) { if (checkMinBound && num < min) { errors.Add(errorKey, "数值小于 " + min + "."); return(false); } if (checkMaxBound && num > max) { errors.Add(errorKey, "数值大于 " + max + "."); return(false); } return(true); }
/// <summary> /// Determines if the date supplied is a date within the specified bounds. /// </summary> /// <param name="date">Date to check if within bounds specified.</param> /// <param name="checkMinBound">Whether or not to check the min</param> /// <param name="checkMaxBound">Whether or not to check the max</param> /// <param name="minDate">Min date.</param> /// <param name="maxDate">Max date.</param> /// <param name="errors">The error collection to populate with any errors.</param> /// <param name="tag">Tag used in an error message.</param> /// <returns>True if the text is a valid date within the range.</returns> public static bool IsDateWithinRange(DateTime date, bool checkMinBound, bool checkMaxBound, DateTime minDate, DateTime maxDate, IErrors errors, string tag) { if (checkMinBound && date.Date < minDate.Date) { errors.Add(tag, string.Format(_messages.DateLessThanMinDate, minDate.ToShortDateString())); return(false); } if (checkMaxBound && date.Date > maxDate.Date) { errors.Add(tag, string.Format(_messages.DateMoreThanMaxDate, maxDate.ToShortDateString())); return(false); } return(true); }
/// <summary> /// Determines if the date supplied is a date within the specified bounds. /// </summary> /// <param name="date"></param> /// <param name="checkMinBound"></param> /// <param name="checkMaxBound"></param> /// <param name="minDate"></param> /// <param name="maxDate"></param> /// <returns></returns> public static bool IsDateWithinRange(DateTime date, bool checkMinBound, bool checkMaxBound, DateTime minDate, DateTime maxDate, IErrors errors, string tag) { if (checkMinBound && date.Date < minDate.Date) { errors.Add(tag, "Date supplied is less than minimum date " + minDate.ToShortDateString()); return false; } if (checkMaxBound && date.Date > maxDate.Date) { errors.Add(tag, "Date supplied is more than maximum date " + maxDate.ToShortDateString()); return false; } return true; }
/// <summary> /// Transfers all the messages from the source to the validation results. /// </summary> /// <param name="messages"></param> /// <param name="results"></param> public static void TransferMessages(IList<string> messages, IErrors errors) { foreach (string message in messages) { errors.Add(string.Empty, message); } }
/// <summary> /// Set the property using the value provided. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="prop"></param> /// <param name="item"></param> /// <param name="counterOrRefId"></param> /// <param name="errors"></param> /// <param name="val"></param> public static void SetProperty(PropertyInfo prop, object item, int counterOrRefId, IErrors errors, object val) { try { // Found prop. Can now map. if (prop != null) { object convertedVal = null; // Handle special conversions. e.g. $105 or 9am etc. if (_propertyTypeMappers.ContainsKey(prop.PropertyType) && val != null && val.GetType() == typeof(string)) { Func <string, object> converter = _propertyTypeMappers[prop.PropertyType]; convertedVal = converter((string)val); } else { convertedVal = ReflectionTypeChecker.ConvertToSameType(prop, val); } ReflectionUtils.SetProperty(item, prop, convertedVal, true); } } catch (Exception ex) { if (errors != null) { var err = string.Format("Unable to map property '{0}' for counter/refId '{1}'", prop.Name, counterOrRefId); err += Environment.NewLine + ex.Message; errors.Add(err); } } }
/// <summary> /// Determine if string is valid with regard to minimum / maximum length. /// </summary> /// <param name="text">Text to check length of.</param> /// <param name="allowNull">Indicate whether or not to allow null.</param> /// <param name="checkMinLength">Whether or not to check the min length</param> /// <param name="checkMaxLength">Whether or not to check the max length</param> /// <param name="minLength">-1 to not check min length, > 0 to represent min length.</param> /// <param name="maxLength">-1 to not check max length, > 0 to represent max length.</param> /// <param name="errors">The error collection to populate if any validation errors occur</param> /// <param name="tag">The tag to use when populating any errors into the error collection.</param> /// <returns>True if match based on parameter conditions, false otherwise.</returns> public static bool IsStringLengthMatch(string text, bool allowNull, bool checkMinLength, bool checkMaxLength, int minLength, int maxLength, IErrors errors, string tag) { if (string.IsNullOrEmpty(text)) { if (!allowNull) { errors.Add(tag + _messages.IsNotSupplied); } return(allowNull); } int textLength = text == null ? 0 : text.Length; // Check bounds . -1 value for min/max indicates not to check. if (checkMinLength && minLength > 0 && textLength < minLength) { return(CheckError(false, errors, tag, string.Format(_messages.TextLessThanMinLength, minLength))); } if (checkMaxLength && maxLength > 0 && textLength > maxLength) { return(CheckError(false, errors, tag, string.Format(_messages.TextMoreThanMaxLength, maxLength))); } return(true); }
/// <summary> /// Determines if text supplied is numeric and within the min/max bounds. /// </summary> /// <param name="text">Text to check if it's numeric and within bounds.</param> /// <param name="checkMinBound">Whether or not to check</param> /// <param name="checkMaxBound"></param> /// <param name="min"></param> /// <param name="max"></param> /// <returns></returns> public static bool IsNumericWithinRange(double num, bool checkMinBound, bool checkMaxBound, double min, double max, IErrors errors, string tag) { if (checkMinBound && num < min) { errors.Add(tag, "Number supplied is less than " + min + "."); return false; } if (checkMaxBound && num > max) { errors.Add(tag, "Number supplied is more than " + max + "."); return false; } return true; }
/// <summary> /// Handle the error by formatting the error message first and then adding it /// to the validation errors. Then add it to the log. /// </summary> /// <param name="errorDescriptor"></param> /// <param name="resources"></param> /// <param name="errors"></param> /// <param name="ex"></param> /// <param name="args"></param> public void Handle(string errorDescriptor, ILocalizationResourceProvider resources, IErrors errors, Exception ex, string[] args) { string error = resources.GetString(errorDescriptor); string errorDetails = error; if (args != null && args.Length > 0) { foreach (string arg in args) { errorDetails += arg + " "; } } // Add to validation results. errors.Add(errorDetails); // Add to log. if (ex == null) { Logger.Error(errorDetails); } else { Logger.Error(errorDetails, ex); } }
/// <summary> /// Determines if text supplied is numeric and within the min/max bounds. /// </summary> /// <param name="num">Number to check if it's numeric and within bounds.</param> /// <param name="checkMinBound">Whether or not to check the min</param> /// <param name="checkMaxBound">Whether or not to check the max</param> /// <param name="min">Min bound.</param> /// <param name="max">Max bound.</param> /// <param name="errors">The error collection to populate with any errors.</param> /// <param name="tag">Tag used in an error message.</param> /// <returns>True if the text is numeric and within range.</returns> public static bool IsNumericWithinRange(double num, bool checkMinBound, bool checkMaxBound, double min, double max, IErrors errors, string tag) { if (checkMinBound && num < min) { errors.Add(tag, string.Format(_messages.NumberLessThan, min)); return(false); } if (checkMaxBound && num > max) { errors.Add(tag, string.Format(_messages.NumberMoreThan, max)); return(false); } return(true); }
public static void TransferMessages(IList <string> messages, IErrors errors) { foreach (string current in messages) { errors.Add(string.Empty, current); } }
/// <summary> /// Validates the bool condition and adds the string error /// to the error list if the condition is invalid. /// </summary> /// <param name="isValid">Flag indicating if invalid.</param> /// <param name="error">Error message</param> /// <param name="errors"><see cref="ValidationResults"/></param> /// <returns>True if isError is false, indicating no error.</returns> public static bool Validate(bool isError, IErrors errors, string key, string message) { if (isError) { errors.Add(key, message); } return !isError; }
/// <summary> /// Check the condition and add the error. /// </summary> /// <param name="isValid"></param> /// <param name="errors"></param> /// <param name="tag"></param> /// <param name="error"></param> /// <param name="target"></param> /// <returns></returns> public static bool CheckError(bool isValid, IErrors errors, string tag, string error) { if (!isValid) { errors.Add(tag, error); } return isValid; }
public static bool CheckError(bool isValid, IErrors errors, string errorKey, string error) { if (!isValid) { errors.Add(errorKey, error); } return(isValid); }
/// <summary> /// Check the text for the regex pattern and adds errors in incorrect. /// </summary> /// <param name="inputText"></param> /// <param name="allowNull"></param> /// <param name="regExPattern"></param> /// <param name="errors"></param> /// <param name="tag"></param> /// <param name="error"></param> /// <param name="target"></param> /// <returns></returns> public static bool CheckErrorRegEx(string inputText, bool allowNull, string regExPattern, IErrors errors, string tag, string error) { bool isEmpty = string.IsNullOrEmpty(inputText); if (allowNull && isEmpty) return true; if (!allowNull && isEmpty) { errors.Add(tag, error); return false; } bool isValid = Regex.IsMatch(inputText, regExPattern); if (!isValid) errors.Add(tag, error); return isValid; }
public static bool Validate(bool isError, IErrors errors, string message) { if (isError) { errors.Add(string.Empty, message); } return(!isError); }
/// <summary> /// Determines if the date supplied is a date. /// </summary> /// <param name="text"></param> /// <param name="checkBounds"></param> /// <param name="minDate"></param> /// <param name="maxDate"></param> /// <returns></returns> public static bool IsDateWithinRange(string text, bool checkMinBound, bool checkMaxBound, DateTime minDate, DateTime maxDate, IErrors errors, string tag) { DateTime result = DateTime.MinValue; if (!DateTime.TryParse(text, out result)) { errors.Add(tag, "Text supplied is not a valid date"); return false; } return IsDateWithinRange(result, checkMinBound, checkMaxBound, minDate, maxDate, errors, tag); }
public static bool IsDateWithinRange(string text, bool checkMinBound, bool checkMaxBound, DateTime minDate, DateTime maxDate, IErrors errors, string errorKey) { DateTime minValue = DateTime.MinValue; if (!DateTime.TryParse(text, out minValue)) { errors.Add(errorKey, "不是一个有效的日期"); return(false); } return(Validation.IsDateWithinRange(minValue, checkMinBound, checkMaxBound, minDate, maxDate, errors, errorKey)); }
public static bool IsNumberSignWithinRange(string text, bool checkMinBound, bool checkMaxBound, int min, int max, IErrors errors, string tag) { if (!Regex.IsMatch(text, "^[+-]?[0-9]+$")) { errors.Add(tag, "不是有效的整数"); return(false); } int num = int.Parse(text); return(Validation.IsNumberWithinRange(num, checkMinBound, checkMaxBound, min, max, errors, tag)); }
public static bool IsNumericWithinRange(string text, bool checkMinBound, bool checkMaxBound, double min, double max, IErrors errors, string tag) { if (!Regex.IsMatch(text, "^\\-?[0-9]*\\.?[0-9]*$")) { errors.Add(tag, "不是有效的数值类型"); return(false); } double num = double.Parse(text); return(Validation.IsNumericWithinRange(num, checkMinBound, checkMaxBound, min, max, errors, tag)); }
/// <summary> /// Determines if the date supplied is a date. /// </summary> /// <param name="text">Text to check if it's date and within bounds specified.</param> /// <param name="checkMinBound">Whether or not to check the min</param> /// <param name="checkMaxBound">Whether or not to check the max</param> /// <param name="minDate">Min date.</param> /// <param name="maxDate">Max date.</param> /// <param name="errors">The error collection to populate with any errors.</param> /// <param name="tag">Tag used in an error message.</param> /// <returns>True if the text is a valid date within the range.</returns> public static bool IsDateWithinRange(string text, bool checkMinBound, bool checkMaxBound, DateTime minDate, DateTime maxDate, IErrors errors, string tag) { DateTime result = DateTime.MinValue; if (!DateTime.TryParse(text, out result)) { errors.Add(tag, _messages.TextInvalidDate); return(false); } return(IsDateWithinRange(result, checkMinBound, checkMaxBound, minDate, maxDate, errors, tag)); }
/// <summary> /// Determines if text supplied is numeric and within the min/max bounds. /// </summary> /// <param name="text">Text to check if it's numeric and within bounds.</param> /// <param name="checkMinBound">Whether or not to check</param> /// <param name="checkMaxBound"></param> /// <param name="min"></param> /// <param name="max"></param> /// <returns></returns> public static bool IsNumericWithinRange(string text, bool checkMinBound, bool checkMaxBound, double min, double max, IErrors errors, string tag) { bool isNumeric = Regex.IsMatch(text, RegexPatterns.Numeric); if (!isNumeric) { errors.Add(tag, "Text supplied is not numeric."); return false; } double num = Double.Parse(text); return IsNumericWithinRange(num, checkMinBound, checkMaxBound, min, max, errors, tag); }
public static bool CheckErrorRegEx(string inputText, bool allowNull, string regExPattern, IErrors errors, string errorKey, string error) { bool flag = string.IsNullOrEmpty(inputText); if (allowNull && flag) { return(true); } if (!allowNull && flag) { errors.Add(errorKey, error); return(false); } bool flag2 = Regex.IsMatch(inputText, regExPattern); if (!flag2) { errors.Add(errorKey, error); } return(flag2); }
/// <summary> /// Internal method for handling errors. /// </summary> /// <param name="error"></param> /// <param name="exception"></param> /// <param name="handler"></param> /// <param name="errorResults"></param> /// <param name="arguments"></param> protected virtual void InternalHandle(object error, Exception exception, IErrors errors, object[] arguments) { string fullError = error == null ? string.Empty : error.ToString(); // Add error to list and log. if (errors != null) { errors.Add(fullError); fullError = errors.Message(); } Logger.Error(fullError, exception, arguments); }
/// <summary> /// Validates the object using System.ComponentModel.DataAnnotations, and puts any errors into the errors object. /// </summary> /// <param name="objectToValidate">Object to validate</param> /// <param name="errors">The collection to put validation errors into.</param> /// <returns></returns> public static bool ValidateObject(object objectToValidate, IErrors errors = null) { var validationResults = new List<Val.ValidationResult>(); var ctx = new Val.ValidationContext(objectToValidate, null, null); var isValid = Val.Validator.TryValidateObject(objectToValidate, ctx, validationResults, true); if(!isValid && errors != null) foreach (var result in validationResults) errors.Add(result.ErrorMessage); return isValid; }
/// <summary> /// Copies all errors from this instance over to the supplied instance. /// </summary> /// <param name="errors">The errors.</param> public void CopyTo(IErrors errors) { if (errors == null) { return; } if (_errorList != null) { foreach (string error in _errorList) { errors.Add(error); } } if (_errorMap != null) { foreach (KeyValuePair <string, string> pair in _errorMap) { errors.Add(pair.Key, pair.Value); } } }
/// <summary> /// Determines if text supplied is numeric and within the min/max bounds. /// </summary> /// <param name="text">Text to check if it's numeric and within bounds.</param> /// <param name="checkMinBound">Whether or not to check the min</param> /// <param name="checkMaxBound">Whether or not to check the max</param> /// <param name="min">Min bound.</param> /// <param name="max">Max bound.</param> /// <param name="errors">The error collection to populate with any errors.</param> /// <param name="tag">Tag used in an error message.</param> /// <returns>True if the text is numeric and within range.</returns> public static bool IsNumericWithinRange(string text, bool checkMinBound, bool checkMaxBound, double min, double max, IErrors errors, string tag) { bool isNumeric = Regex.IsMatch(text, RegexPatterns.Numeric); if (!isNumeric) { errors.Add(tag, _messages.TextNotNumeric); return(false); } double num = Double.Parse(text); return(IsNumericWithinRange(num, checkMinBound, checkMaxBound, min, max, errors, tag)); }
/// <summary> /// Copies all errors from this instance over to the supplied instance. /// </summary> /// <param name="errors">The errors.</param> public void CopyTo(IErrors errors) { if (errors == null) return; if (_errorList != null) foreach (string error in _errorList) errors.Add(error); if (_errorMap != null) foreach (KeyValuePair<string, string> pair in _errorMap) errors.Add(pair.Key, pair.Value); }