/// <summary>
        /// Validates extracting a value from <paramref name="objectToValidate"/> and applying a validator
        /// to it.
        /// </summary>
        /// <param name="objectToValidate">The object to validate.</param>
        /// <param name="currentTarget">The object on the behalf of which the validation is performed.</param>
        /// <param name="key">The key that identifies the source of <paramref name="objectToValidate"/>.</param>
        /// <param name="validationResults">The validation results to which the outcome of the validation should be stored.</param>
        /// <remarks>
        /// <see langword="null"/> is considered a failed validation.
        /// </remarks>
        public override void DoValidate(object objectToValidate,
                                        object currentTarget,
                                        string key,
                                        ValidationResults validationResults)
        {
            object value;
            string valueAccessFailureMessage;
            bool   retrievalSucceeded = _valueAccess.GetValue(objectToValidate, out value, out valueAccessFailureMessage);

            if (retrievalSucceeded)
            {
                // override the key and the current target for validations down the chain
                _valueValidator.DoValidate(value, objectToValidate, _valueAccess.Key, validationResults);
            }
            else
            {
                LogValidationResult(validationResults, valueAccessFailureMessage, currentTarget, _valueAccess.Key);
            }
        }
Beispiel #2
0
        public override void DoValidate(object objectToValidate,
                                        object currentTarget,
                                        string key,
                                        ValidationResults validationResults)
        {
            object comparand;
            string valueAccessFailureMessage;
            bool   status = _valueAccess.GetValue(currentTarget, out comparand, out valueAccessFailureMessage);

            if (!status)
            {
                LogValidationResult(validationResults, string.Format(CultureInfo.CurrentCulture, Translations.ValueAccessComparisonValidatorFailureToRetrieveComparand, _valueAccess.Key, valueAccessFailureMessage),
                                    currentTarget, key);
                return;
            }

            bool valid = false;

            if (_comparisonOperator == ComparisonOperator.Equal || _comparisonOperator == ComparisonOperator.NotEqual)
            {
                valid = (objectToValidate != null ? objectToValidate.Equals(comparand) : comparand == null)
                        ^ (_comparisonOperator == ComparisonOperator.NotEqual)
                        ^ Negated;
            }
            else
            {
                var comparableObjectToValidate = objectToValidate as IComparable;
                if (comparableObjectToValidate != null &&
                    comparand != null &&
                    comparableObjectToValidate.GetType() == comparand.GetType())
                {
                    int comparison = comparableObjectToValidate.CompareTo(comparand);

                    switch (_comparisonOperator)
                    {
                    case ComparisonOperator.GreaterThan:
                        valid = comparison > 0;
                        break;

                    case ComparisonOperator.GreaterThanEqual:
                        valid = comparison >= 0;
                        break;

                    case ComparisonOperator.LessThan:
                        valid = comparison < 0;
                        break;

                    case ComparisonOperator.LessThanEqual:
                        valid = comparison <= 0;
                        break;
                    }

                    valid = valid ^ Negated;
                }
            }

            if (!valid)
            {
                LogValidationResult(validationResults, string.Format(CultureInfo.CurrentCulture, MessageTemplate, objectToValidate, key, Tag, comparand, _valueAccess.Key, _comparisonOperator),
                                    currentTarget, key);
            }
        }