Exemple #1
0
        /// <summary>
        /// Give the validation error message (used as system error message)
        /// </summary>
        /// <param name="validationResult">Result of the validation</param>
        /// <param name="input">Data that failed on validation specifications</param>
        public RuleValidationError(IValidationResult validationResult, object input = null)
        {
            // get the calling method
            CallingMethod = @"N/A";

            if (input != null)
            {
                // get the json from the validated input
                ValidatedDataJson = input.SerializeToJSon(false);
            }
            else
            {
                ValidatedDataJson = @"N/A";
            }

            // log only in case when we have a valid result
            if (validationResult.IsNull())
            {
                return;
            }
            var _failedTypes = validationResult.GetTypesFailedRulesTypes();

            if (_failedTypes.IsNullOrHasZeroElements())
            {
                return;
            }
            // preserve the failed validations
            FalseReturnValidationRules = new ReadOnlyCollection <Type>(_failedTypes);
            // build the error info
            var _rulesBuilder = new StringBuilder();

            _rulesBuilder.AppendFormat(@"{0}: ", GetType().Name);
            for (var _i = 0; _i < FalseReturnValidationRules.Count; _i++)
            {
                var _validationRule = FalseReturnValidationRules[_i];
                if (_i + 1 == FalseReturnValidationRules.Count || FalseReturnValidationRules.Count == 1)
                {
                    _rulesBuilder.Append(_validationRule.Name);
                }
                else
                {
                    _rulesBuilder.AppendFormat(@"{0}, ", _validationRule.Name);
                }
            }

            FaultMessage = string.Concat(@"Failed rules: ", _rulesBuilder.ToString(), Environment.NewLine, GetCombinedExceptionMessage());
        }