/// <summary>
 /// Compose two distinct <see cref="ValidationResult"/> objects.
 /// </summary>
 /// <param name="res">The res.</param>
 public void Compose(ValidationResult res)
 {
     if (!res.Success )
     {
         Success = false;
         Errors.AddRange(res.Errors);
     }
 }
 public ValidationResult ValidateObject(
     ValidationResult validationResult,
     object objToValidate,
     ValidationFlags validationFlags)
 {
     ValidateObject(validationResult, objToValidate, validationFlags,
                    System.Threading.Thread.CurrentThread.CurrentUICulture);
     return validationResult;
 }
 /// <summary>
 /// Validate an object modifying an existing Validation Result.
 /// </summary>
 /// <param name="actualResult">The result object that contains validation result, it will
 /// be populated by this function</param>
 /// <param name="objToValidate">The object to be validated.</param>
 /// <param name="validationFlags">Options for comparing objects.</param>
 /// <param name="cultureInfo">The cultureinfo used to validate the object.</param>
 public void ValidateObject(
     ValidationResult actualResult,
     object objToValidate,
     ValidationFlags validationFlags,
     CultureInfo cultureInfo)
 {
     foreach (ValidationUnit vu in mList)
     {
         if (ValdationFlagsUtils.RecursiveValidation(validationFlags) || vu.IsFirstLevelValidationUnit)
         {
             Boolean validationResult = vu.Validate(actualResult, objToValidate, validationFlags, cultureInfo);
             if (!validationResult && ValdationFlagsUtils.StopOnFirstError(validationFlags)) break;
         }
     }
 }
        /// <summary>
        /// Validate an object
        /// </summary>
        /// <param name="objectToValidate"></param>
        /// <param name="validationFlags"></param>
        /// <returns></returns>
        public override ValidationResult Validate(object objectToValidate, ValidationFlags validationFlags)
        {
            //First of all retrieve the object, if it is null validate.
            ValidationResult results = new ValidationResult();
            object obj = Extract<Object>(objectToValidate);
            if (obj != null)
            {

                if (obj is IEnumerable)
                {
                    //the object is IEnumerable, we need to validate inner objects
                    Int32 index = 0;

                    foreach (var innerObj in obj as IEnumerable)
                    {
                        var errors = ValidateSingleObject(innerObj, validationFlags);
                        foreach (var validationError in errors)
                        {
                            results.Success = false;
                            results.AddErrorMessage(
                                validationError.Message,
                                string.Format("{0}[{1}].{2}",mValueExtractor.SourceName, index, validationError.SourceName));

                        }
                        index++;
                    }
                }
                else
                {
                    //Check if this object support a validation, if we do not have a rule the object should
                    //be considered valid.
                    var errors = ValidateSingleObject(obj, validationFlags);
                    foreach (var validationError in errors)
                    {
                        results.AddErrorMessage(
                            validationError.Message,
                            mValueExtractor.SourceName + "." + validationError.SourceName);

                    }
                }
                return results;

            }

            return results;
        }
        protected IEnumerable<ValidationError> ValidateSingleObject(object obj, ValidationFlags validationFlags)
        {
            Type type = obj.GetType();
            if (mRuleMap.ContainsKey(type))
            {
                ValidationUnitCollection vc = mRuleMap[type];
                if (vc.Count > 0)
                {
                    ValidationResult res = new ValidationResult();

                    vc.ValidateObject(res, obj, validationFlags);
                    if (!res)
                    {
                        return res.Errors;
                    }
                }
            }

            return new ValidationError[] { };
        }