コード例 #1
0
        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);
                }
            }
        }
コード例 #2
0
 public static void CheckRequired(this PropertyInfo pi, object propValue)
 {
     if (propValue is string && ((string)propValue).IsNullOrEmpty() && ChoTypeDescriptor.GetPropetyAttributes <RequiredAttribute>(ChoTypeDescriptor.GetProperty <RequiredAttribute>(pi.DeclaringType, pi.Name)).Any())
     {
         throw new ChoHL7Exception("'{0}' member is required.".FormatString(pi.FullName()));
     }
     else if (propValue == null && ChoTypeDescriptor.GetPropetyAttributes <RequiredAttribute>(ChoTypeDescriptor.GetProperty <RequiredAttribute>(pi.DeclaringType, pi.Name)).Any())
     {
         throw new ChoHL7Exception("'{0}' member is required.".FormatString(pi.FullName()));
     }
 }
コード例 #3
0
        public static void CheckForValidEnumValue(this PropertyInfo pi, string propValueText)
        {
            if (propValueText.IsNullOrEmpty())
            {
                return;
            }

            ChoHL7EnumTypeAttribute enumAttribute = ChoTypeDescriptor.GetPropetyAttributes <ChoHL7EnumTypeAttribute>(ChoTypeDescriptor.GetProperty(pi.DeclaringType, pi.Name)).FirstOrDefault();

            if (enumAttribute != null)
            {
                Type enumType = enumAttribute.EnumType;
                if (!enumType.IsEnum)
                {
                    throw new ChoHL7Exception("'{0}' is not an enum type defined in '{1}' member.".FormatString(enumType.FullName, pi.FullName()));
                }

                if (!propValueText.IsNullOrWhiteSpace())
                {
                    string[] enumTxts    = GetEnumValues(enumType);
                    string[] splEnumTxts = GetSplEnumValues(enumType);
                    if (enumTxts.Length > 0 && !enumTxts.Contains(propValueText, StringComparer.CurrentCultureIgnoreCase))
                    {
                        if (splEnumTxts.Length > 0)
                        {
                            bool found = false;
                            foreach (var splEnumTxt in splEnumTxts)
                            {
                                if (propValueText.StartsWith(splEnumTxt))
                                {
                                    found = true;
                                    break;
                                }
                            }
                            if (!found)
                            {
                                throw new ChoHL7Exception("Incorrect '{0}' value found for '{1}' member.".FormatString(propValueText, pi.FullName()));
                            }
                        }
                        else
                        {
                            //char enumChar = propValueText[0];
                            //if (!Enum.IsDefined(enumType, (int)enumChar))
                            throw new ChoHL7Exception("Incorrect '{0}' value found for '{1}' member.".FormatString(propValueText, pi.FullName()));
                        }
                    }
                }
            }
        }
コード例 #4
0
ファイル: ChoHL7Segment.cs プロジェクト: lanicon/ChoETL.HL7
        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);
                    }
                }
            }
        }