public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            if (value == null)
            {
                return(null);
            }

            ChoNotNullableArrayList arrayList = new ChoNotNullableArrayList();
            object notNullItem = null;

            if (value.GetType().IsArray)
            {
                foreach (object item in (Array)value)
                {
                    if (item == null)
                    {
                        continue;
                    }
                    notNullItem = item;

                    ChoValidationResults validationResults = ChoValidation.Validate(item);
                    if (validationResults != null && validationResults.Count > 0)
                    {
                        continue;
                    }

                    arrayList.Add(item);
                }
                return(notNullItem == null ? null : arrayList.ToArray(notNullItem.GetType()));
            }
            else
            {
                return(value);
            }
        }
 public void Validate(MemberInfo memberInfo, object value, ChoValidationResults validationResults)
 {
     try
     {
         ValidationResult result = _validationAttribute.GetValidationResult(value, null);
         if (result != ValidationResult.Success)
         {
             if (validationResults != null)
             {
                 validationResults.AddResult(result);
             }
         }
     }
     catch (ChoFatalApplicationException)
     {
         throw;
     }
     catch (Exception ex)
     {
         if (validationResults != null)
         {
             validationResults.AddResult(new ChoValidationResult(ex.Message));
         }
     }
 }
Example #3
0
        public override void Validate(MemberInfo memberInfo, object value)
        {
            ChoValidationResults validationResults = new ChoValidationResults();

            foreach (IChoSurrogateValidator validator in Validators)
            {
                try
                {
                    validator.Validate(memberInfo, value);
                    return;
                }
                catch (ChoFatalApplicationException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    validationResults.AddResult(new ChoValidationResult(ex.Message));
                }
            }

            if (validationResults.Count > 0)
            {
                if (validationResults.Count == 1)
                {
                    throw new ChoValidationException("Failed to validate {0} member. {1}".FormatString(memberInfo.Name, validationResults.ToString()), validationResults);
                }
                else
                {
                    throw new ChoValidationException("Failed to validate {0} member. Must meet one of the following conditions. {2}{1}".FormatString(memberInfo.Name,
                                                                                                                                                     validationResults.ToString().Indent(), Environment.NewLine), validationResults);
                }
            }
        }
        public void Validate(MemberInfo memberInfo, object value, ChoValidationResults validationResults)
        {
            try
            {
                if (_configurationValidatorBase == null)
                {
                    return;
                }

                Type memberType = ChoType.GetMemberType(memberInfo);

                if (_configurationValidatorBase.CanValidate(memberType))
                {
                    _configurationValidatorBase.Validate(value);
                }
            }
            catch (ChoFatalApplicationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                validationResults.AddResult(new ChoValidationResult(ex.Message));
            }
        }
Example #5
0
 /// <summary>
 /// Initializes this object with a _message.
 /// </summary>
 public ChoValidationResult(string message, string tag, object target, IEnumerable <ChoValidationResult> nestedValidationResults)
 {
     _message = message;
     _tag     = tag;
     _target  = target;
     _nestedValidationResults = new ChoValidationResults();
     _nestedValidationResults.AddResults(nestedValidationResults);
 }
Example #6
0
        public static ChoValidationResults Validate <T>(T target)
        {
            ChoValidationResults validationResults = new ChoValidationResults();

            if (target != null)
            {
                DoValidate(target, validationResults);
            }

            return(validationResults);
        }
Example #7
0
        public override string ToString()
        {
            ChoValidationResults results = ChoValidation.Validate(this);

            if (results.Count == 0)
            {
                return(String.Format("SUCCESS - ShortName: {0}, Type: {1}, Override: {3} [TypeFound: {2}]", TypeShortName, TypeName, Type != null, Override));
            }
            else
            {
                return(String.Format("ERROR - ShortName: {0}, Type: {1}, Override: {3} [TypeFound: {2}], {4}[{4}{5}]",
                                     TypeShortName, TypeName, Type != null, Override, Environment.NewLine, results.ToString().Indent()));
            }
        }
Example #8
0
 public void Validate(MemberInfo memberInfo, object value, ChoValidationResults validationResults)
 {
     try
     {
         Validate(memberInfo, value);
     }
     catch (ChoFatalApplicationException)
     {
         throw;
     }
     catch (Exception ex)
     {
         validationResults.AddResult(new ChoValidationResult(ex.Message));
     }
 }
Example #9
0
        public virtual void Validate(object value, ChoValidationResults validationResults)
        {
            ChoGuard.ArgumentNotNull(validationResults, "ValidationResults");

            try
            {
                Validate(value);
            }
            catch (ChoFatalApplicationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                validationResults.AddResult(new ChoValidationResult(ex.Message));
            }
        }
Example #10
0
        private static void DoValidate <T>(T target, ChoValidationResults validationResults)
        {
            bool validationRoutineFound = false;
            bool canContinue            = true;

            foreach (MethodInfo methodInfo in GetValidationRoutines(target.GetType()))
            {
                canContinue            = false;
                validationRoutineFound = true;
                canContinue            = (bool)ChoType.InvokeMethod(target, methodInfo.Name, new object[] { validationResults });
                if (!canContinue)
                {
                    break;
                }
            }

            //Do built-in attribute validations
            if (!validationRoutineFound)
            {
                MemberInfo[] memberInfos = ChoType.GetMembers(target.GetType());
                foreach (MemberInfo memberInfo in memberInfos)
                {
                    IChoSurrogateValidator validator = ChoCompositeValidatorBuilder.GetValidator(memberInfo);
                    if (validator == null)
                    {
                        continue;
                    }

                    try
                    {
                        validator.Validate(memberInfo, ChoType.GetMemberValue(target, memberInfo.Name));
                    }
                    catch (ChoFatalApplicationException)
                    {
                        throw;
                    }
                    catch (Exception ex)
                    {
                        validationResults.AddResult(new ChoValidationResult(ex.Message));
                    }
                }
            }
        }
Example #11
0
        /// <summary>
        /// Returns a new instance of <see cref="ChoValidationResults"/> that includes the results from the receiver that
        /// match the provided tag names.
        /// </summary>
        /// <param name="tagFilter">The indication of whether to include or ignore the matching results.</param>
        /// <param name="tags">The list of tag names to match.</param>
        /// <returns>A <see cref="ChoValidationResults"/> containing the filtered results.</returns>
        public ChoValidationResults FindAll(TagFilter tagFilter, params string[] tags)
        {
            // workaround for params behavior - a single null parameter will be interpreted
            // as null array, not as an array with null as element
            if (tags == null)
            {
                tags = new string[] { null }
            }
            ;

            ChoValidationResults filteredValidationResults = new ChoValidationResults();

            foreach (ChoValidationResult validationResult in this)
            {
                bool matches = false;

                foreach (string tag in tags)
                {
                    if ((tag == null && validationResult.Tag == null) ||
                        (tag != null && tag.Equals(validationResult.Tag)))
                    {
                        matches = true;
                        break;
                    }
                }

                // if ignore, look for !match
                // if include, look for match
                if (matches ^ (tagFilter == TagFilter.Ignore))
                {
                    filteredValidationResults.AddResult(validationResult);
                }
            }

            return(filteredValidationResults);
        }
Example #12
0
 public bool Validate(ChoValidationResults validationResults)
 {
     //Dummy validation routine to instruct Framework, not to do auto validation on this object
     return(true);
 }
 public ChoValidationException(string message, ChoValidationResults validationResults, Exception e)
     : base(message, e)
 {
     _validationResults = validationResults;
 }
 public ChoValidationException(string message, ChoValidationResults validationResults)
     : this(message, validationResults, null)
 {
 }