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));
         }
     }
 }
Exemple #2
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));
            }
        }
Exemple #4
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));
     }
 }
Exemple #5
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));
            }
        }
Exemple #6
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));
                    }
                }
            }
        }
        /// <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);
        }