/// <summary>
        /// Create faulted response from validation result
        /// </summary>
        /// <typeparam name="TResponse"></typeparam>
        /// <param name="validationResult"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        internal static TResponse CreateFaultedResponse <TResponse>(this IValidationResult validationResult, object input = null) where TResponse : VoidResult, new()
        {
            var _response = new TResponse();

            _response.IsFaulted = true;

            var _failedTypes = validationResult.GetTypesFailedRulesTypes();

            foreach (var _failedType in _failedTypes)
            {
                _response.ErrorMessages.Add(new RuleValidationError(_failedType, input));
            }

            return(_response);
        }
예제 #2
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());
        }