public virtual void Validate() { string propName = null; object propValue = null; string propValueText = null; var results = new List <ValidationResult>(); foreach (PropertyInfo pi in ChoType.GetProperties(GetType())) { propName = pi.Name; results.Clear(); try { propValue = ChoType.GetPropertyValue(this, pi); } catch (Exception ex) { throw new ChoHL7Exception("Failed to validate '{0}' member.".FormatString(pi.FullName()), ex); } try { propValueText = propValue.ToNString(); } catch (Exception ex) { throw new ChoHL7Exception("Failed to validate '{0}' member.".FormatString(pi.FullName()), ex); } pi.CheckRequired(propValue); if (Field != null && Field.Configuration != null && !Field.Configuration.DisableEnumChecks) { pi.CheckForValidEnumValue(propValueText); } var context = new ValidationContext(propValueText, null, null); context.MemberName = pi.Name; Validator.TryValidateValue(propValueText, context, results, ChoTypeDescriptor.GetPropetyAttributes <ValidationAttribute>(ChoTypeDescriptor.GetProperty <ValidationAttribute>(GetType(), pi.Name))); if (results.Count > 0) { throw new ChoHL7Exception("Failed to validate '{0}' member. {2}{1}".FormatString(pi.FullName(), ChoHL7Helper.ToString(results), Environment.NewLine)); } try { if (propValue is ChoHL7AbstractField) { ((ChoHL7AbstractField)propValue).Validate(); } } catch (Exception ex) { throw new ChoHL7Exception("Failed to validate '{0}' member.".FormatString(pi.FullName()), ex); } } }
public virtual void Validate() { string propName = null; object propValue = null; string propValueText = null; var results = new List <ValidationResult>(); foreach (PropertyInfo pi in ChoType.GetProperties(GetType())) { propName = pi.Name; results.Clear(); try { propValue = ChoType.GetPropertyValue(this, pi); } catch (Exception ex) { throw new ChoHL7Exception("Failed to validate '{0}' member.".FormatString(pi.FullName()), ex); } if (propValue is Array) { Array arr = (Array)propValue; if (arr.Length == 0 && ChoTypeDescriptor.GetPropetyAttributes <RequiredAttribute>(ChoTypeDescriptor.GetProperty <RequiredAttribute>(GetType(), pi.Name)).Any()) { throw new ChoHL7Exception("'{0}' member is required.".FormatString(pi.FullName())); } var maxCountAttr = ChoTypeDescriptor.GetPropetyAttributes <ChoHL7MaxCountAttribute>(ChoTypeDescriptor.GetProperty <ChoHL7MaxCountAttribute>(GetType(), pi.Name)).FirstOrDefault(); if (maxCountAttr != null) { if (arr.Length > maxCountAttr.Count) { throw new ChoHL7Exception("Incorrect number of elements found in '{0}' member. [Expected: {1}, Found: {2}]".FormatString(pi.FullName(), maxCountAttr.Count, arr.Length)); } } foreach (ChoHL7AbstractField arrValue in arr) { try { propValueText = arrValue.ToNString(); } catch (Exception ex) { throw new ChoHL7Exception("Failed to validate '{0}' member.".FormatString(pi.FullName()), ex); } pi.CheckRequired(propValueText); if (Configuration != null && !Configuration.DisableEnumChecks) { pi.CheckForValidEnumValue(propValueText); } var context = new ValidationContext(propValueText, null, null); context.MemberName = pi.Name; Validator.TryValidateValue(propValueText, context, results, ChoTypeDescriptor.GetPropetyAttributes <ValidationAttribute>(ChoTypeDescriptor.GetProperty <ValidationAttribute>(GetType(), pi.Name))); if (results.Count > 0) { throw new ChoHL7Exception("Failed to validate '{0}' member. {2}{1}".FormatString(pi.FullName(), ChoHL7Helper.ToString(results), Environment.NewLine)); } try { if (arrValue is ChoHL7AbstractField) { ((ChoHL7AbstractField)arrValue).Validate(); } } catch (Exception ex) { throw new ChoHL7Exception("Failed to validate '{0}' member.".FormatString(pi.FullName()), ex); } } } else { try { propValueText = propValue.ToNString(); } catch (Exception ex) { throw new ChoHL7Exception("Failed to validate '{0}' member.".FormatString(pi.FullName()), ex); } pi.CheckRequired(propValueText); if (Configuration != null && !Configuration.DisableEnumChecks) { pi.CheckForValidEnumValue(propValueText); } var context = new ValidationContext(propValueText, null, null); context.MemberName = pi.Name; Validator.TryValidateValue(propValueText, context, results, ChoTypeDescriptor.GetPropetyAttributes <ValidationAttribute>(ChoTypeDescriptor.GetProperty <ValidationAttribute>(GetType(), pi.Name))); if (results.Count > 0) { if (Configuration.DisableValueInErrorMessage) { throw new ChoHL7Exception("Failed to validate '{0}' member. {2}{1}".FormatString(pi.FullName(), ChoHL7Helper.ToString(results), Environment.NewLine)); } else { throw new ChoHL7Exception("Failed to validate '{0}' member [Value: {3}]. {2}{1}".FormatString(pi.FullName(), ChoHL7Helper.ToString(results), Environment.NewLine, propValueText)); } } try { if (propValue is ChoHL7AbstractField) { ((ChoHL7AbstractField)propValue).Validate(); } } catch (Exception ex) { throw new ChoHL7Exception("Failed to validate '{0}' member.".FormatString(pi.FullName()), ex); } } } }