/// <summary> /// This method validates model objects that use the Validation attribute class and provides formatting where a format string attribute was supplied. /// </summary> /// <returns>List of error strings</returns> private List <string> Validate() { List <string> _errors = new List <string>(); PropertyInfo[] properties = this.GetType().GetProperties(); foreach (PropertyInfo propertyInfo in properties) { string value = string.Empty; List <string> errors = null; IEnumerable <ValidationAttributes> attributes = (IEnumerable <ValidationAttributes>)propertyInfo.GetCustomAttributes(typeof(ValidationAttributes)); if (attributes.Count() > 0) { ValidationAttributes attribute = (ValidationAttributes)attributes.First(); if (!attribute.IsNull()) { //get the data type ValidationAttributes.DataType fieldFormat = attribute.FieldFormat; //call the appropriate validation method try { if (fieldFormat == ValidationAttributes.DataType.Numeric) { errors = NumericValidator(attribute, propertyInfo); } else if (fieldFormat == ValidationAttributes.DataType.Alphanumeric) { errors = AlphaNumericValidator(attribute, propertyInfo); } else if (fieldFormat == ValidationAttributes.DataType.DateTime) { errors = DateTimeValidator(attribute, propertyInfo); } else if (fieldFormat == ValidationAttributes.DataType.List) { errors = ListValidator(attribute, propertyInfo); } else if (fieldFormat == ValidationAttributes.DataType.Dictionary) { errors = DictionaryValidator(attribute, propertyInfo); } else if (fieldFormat == ValidationAttributes.DataType.Object) { errors = ObjectValidator(attribute, propertyInfo); } if (!errors.IsNull()) { foreach (string error in errors) { _errors.Add(error); } errors.Clear(); errors = null; } } catch (Exception ex) { throw new Exception($"{propertyInfo.Name }: { ex.Message}"); } } } } return(_errors.Count == 0 ? null : _errors); }