Esempio n. 1
0
 /// <summary>
 /// 如果有异常则抛出
 /// </summary>
 /// <param name="dataValidationResult"></param>
 public static void AddError(this DataValidationResult dataValidationResult)
 {
     if (!dataValidationResult.IsValid)
     {
         throw Oops.Oh("[Validation]" + JsonSerializerUtility.Serialize(
                           dataValidationResult.ValidationResults
                           .Select(u => new
         {
             MemberNames = u.MemberNames.Any() ? u.MemberNames : new[] { $"{dataValidationResult.MemberOrValue}" },
             u.ErrorMessage
         })
                           .OrderBy(u => u.MemberNames.First())
                           .GroupBy(u => u.MemberNames.First())
                           .ToDictionary(u => u.Key, u => u.Select(c => c.ErrorMessage))));
     }
 }
        /// <summary>
        /// 直接抛出异常信息
        /// </summary>
        /// <param name="dataValidationResult"></param>
        public static void ThrowValidateFailedModel(this DataValidationResult dataValidationResult)
        {
            if (!dataValidationResult.IsValid)
            {
                // 解析验证失败消息,输出统一格式
                var validationFailMessage =
                    dataValidationResult.ValidationResults
                    .Select(u => new {
                    MemberNames = u.MemberNames.Any() ? u.MemberNames : new[] { $"{dataValidationResult.MemberOrValue}" },
                    u.ErrorMessage
                })
                    .OrderBy(u => u.MemberNames.First())
                    .GroupBy(u => u.MemberNames.First())
                    .ToDictionary(x => x.Key, u => u.Select(c => c.ErrorMessage).ToArray());

                // 抛出验证失败异常
                throw new AppFriendlyException(default, default, new ValidationException())
Esempio n. 3
0
 /// <summary>
 /// 如果有异常则抛出
 /// </summary>
 /// <param name="dataValidationResult"></param>
 public static void AddError(this DataValidationResult dataValidationResult)
 {
     if (!dataValidationResult.IsValid)
     {
         throw Oops.Oh("[Validation]" + JSON.Serialize(
                           dataValidationResult.ValidationResults
                           .Select(u => new {
             MemberNames = u.MemberNames.Any() ? u.MemberNames : new[] { $"{dataValidationResult.MemberOrValue}" },
             u.ErrorMessage
         })
                           .OrderBy(u => u.MemberNames.First())
                           .GroupBy(u => u.MemberNames.First())
                           .Select(u =>
                                   new ValidateFailedModel(u.Key,
                                                           u.Select(c => c.ErrorMessage).ToArray()))));
     }
 }
 /// <summary>
 /// 如果有异常则抛出
 /// </summary>
 /// <param name="dataValidationResult"></param>
 public static void AddError(this DataValidationResult dataValidationResult)
 {
     if (!dataValidationResult.IsValid)
     {
         throw Oops.Oh("[Validation]" + JsonSerializer.Serialize(
                           dataValidationResult.ValidationResults
                           .Select(u => new
         {
             MemberNames = u.MemberNames.Any() ? u.MemberNames : new[] { $"{dataValidationResult.MemberOrValue}" },
             u.ErrorMessage
         })
                           .OrderBy(u => u.MemberNames.First())
                           .GroupBy(u => u.MemberNames.First())
                           .ToDictionary(u => u.Key, u => u.Select(c => c.ErrorMessage)), new JsonSerializerOptions
         {
             PropertyNameCaseInsensitive = true,
             Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
         }));
     }
 }