From d6d6962f7469c81a06f228c54b2fc5b1d6589799 Mon Sep 17 00:00:00 2001 From: mark Date: Sat, 10 Apr 2021 21:17:49 +0800 Subject: [PATCH] Fix bugs --- .../Properties/AndroidManifest.xml | 12 ++--- .../PropertyValidator.Test.Android.csproj | 8 +++- .../PropertyValidator.Test/App.xaml.cs | 5 +-- .../Pages/AboutPage.xaml.cs | 11 +---- .../PropertyValidator.Test.csproj | 1 + .../ViewModels/ItemsPageViewModel.cs | 8 ++++ PropertyValidator/PropertyValidator.csproj | 6 +-- .../Services/ValidationService.cs | 44 +++++++++++++------ 8 files changed, 57 insertions(+), 38 deletions(-) diff --git a/PropertyValidator.Test/PropertyValidator.Test.Android/Properties/AndroidManifest.xml b/PropertyValidator.Test/PropertyValidator.Test.Android/Properties/AndroidManifest.xml index 27a1a29..958a344 100644 --- a/PropertyValidator.Test/PropertyValidator.Test.Android/Properties/AndroidManifest.xml +++ b/PropertyValidator.Test/PropertyValidator.Test.Android/Properties/AndroidManifest.xml @@ -1,6 +1,6 @@ - - - - - - + + + + + + \ No newline at end of file diff --git a/PropertyValidator.Test/PropertyValidator.Test.Android/PropertyValidator.Test.Android.csproj b/PropertyValidator.Test/PropertyValidator.Test.Android/PropertyValidator.Test.Android.csproj index e7e3311..146be18 100644 --- a/PropertyValidator.Test/PropertyValidator.Test.Android/PropertyValidator.Test.Android.csproj +++ b/PropertyValidator.Test/PropertyValidator.Test.Android/PropertyValidator.Test.Android.csproj @@ -17,7 +17,7 @@ Resources Assets false - v9.0 + v11.0 true true Xamarin.Android.Net.AndroidClientHandler @@ -33,6 +33,10 @@ prompt 4 None + false + false + false + false true @@ -96,7 +100,7 @@ - + \ No newline at end of file diff --git a/PropertyValidator.Test/PropertyValidator.Test/App.xaml.cs b/PropertyValidator.Test/PropertyValidator.Test/App.xaml.cs index 59b3c38..a1ad5ad 100644 --- a/PropertyValidator.Test/PropertyValidator.Test/App.xaml.cs +++ b/PropertyValidator.Test/PropertyValidator.Test/App.xaml.cs @@ -1,19 +1,16 @@ using Prism; using Prism.Ioc; using PropertyValidator.Test.Extensions; -using System; using System.Threading.Tasks; -using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace PropertyValidator.Test { - [XamlCompilation(XamlCompilationOptions.Compile)] public partial class App { public App() : this(null) { } - public App(IPlatformInitializer initializer) : base(initializer) { } + public App(IPlatformInitializer? initializer) : base(initializer) { } protected override void OnInitialized() { diff --git a/PropertyValidator.Test/PropertyValidator.Test/Pages/AboutPage.xaml.cs b/PropertyValidator.Test/PropertyValidator.Test/Pages/AboutPage.xaml.cs index 32b803c..8db98ee 100644 --- a/PropertyValidator.Test/PropertyValidator.Test/Pages/AboutPage.xaml.cs +++ b/PropertyValidator.Test/PropertyValidator.Test/Pages/AboutPage.xaml.cs @@ -1,13 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -using Xamarin.Forms; -using Xamarin.Forms.Xaml; - -namespace PropertyValidator.Test.Pages +namespace PropertyValidator.Test.Pages { public partial class AboutPage { diff --git a/PropertyValidator.Test/PropertyValidator.Test/PropertyValidator.Test.csproj b/PropertyValidator.Test/PropertyValidator.Test/PropertyValidator.Test.csproj index feb07d5..269577f 100644 --- a/PropertyValidator.Test/PropertyValidator.Test/PropertyValidator.Test.csproj +++ b/PropertyValidator.Test/PropertyValidator.Test/PropertyValidator.Test.csproj @@ -4,6 +4,7 @@ netstandard2.1 true latest + enable diff --git a/PropertyValidator.Test/PropertyValidator.Test/ViewModels/ItemsPageViewModel.cs b/PropertyValidator.Test/PropertyValidator.Test/ViewModels/ItemsPageViewModel.cs index 7554c9a..2010bb7 100644 --- a/PropertyValidator.Test/PropertyValidator.Test/ViewModels/ItemsPageViewModel.cs +++ b/PropertyValidator.Test/PropertyValidator.Test/ViewModels/ItemsPageViewModel.cs @@ -90,6 +90,14 @@ private void ShowValidationResult() private void Submit() { + try + { + validationService.EnsurePropertiesAreValid(); + } + catch (Exception ex) + { + var msg = ex.Message; + } if (!validationService.Validate()) { ShowValidationResult(); diff --git a/PropertyValidator/PropertyValidator.csproj b/PropertyValidator/PropertyValidator.csproj index f9cde43..c207eca 100644 --- a/PropertyValidator/PropertyValidator.csproj +++ b/PropertyValidator/PropertyValidator.csproj @@ -11,9 +11,9 @@ https://github.com/mr5z/PropertyValidator https://github.com/mr5z/PropertyValidator LICENSE - 1.0.6.2 - 1.0.6.2 - 1.0.6.2 + 1.0.6.3 + 1.0.6.3 + 1.0.6.3 en diff --git a/PropertyValidator/Services/ValidationService.cs b/PropertyValidator/Services/ValidationService.cs index 04db684..623ee92 100644 --- a/PropertyValidator/Services/ValidationService.cs +++ b/PropertyValidator/Services/ValidationService.cs @@ -144,14 +144,39 @@ public bool Validate([NotNull] string propertyName) return ValidateImpl(propertyName); } - private ValidationResultArgs GetValidationResultArgs(string propertyName) + public void EnsurePropertiesAreValid() { - var errorMessages = GetRules() - .Where(it => it.HasError && it.PropertyName == propertyName) + ValidateAllProperties(); ; + var resultArgs = GetValidationResultArgs(); + var firstError = resultArgs.FirstError; + if (!string.IsNullOrEmpty(firstError)) + throw new PropertyException(resultArgs); + } + + private void ValidateAllProperties() + { + var ruleCollection = GetRules(); + foreach (var rule in ruleCollection) + { + Validate(rule.PropertyName!); + } + } + + private ValidationResultArgs GetValidationResultArgs(string? propertyName = null) + { + var enumerable = GetRules().AsEnumerable(); + + if (!string.IsNullOrEmpty(propertyName)) + { + enumerable = enumerable.Where(it => it.PropertyName == propertyName); + } + + var errorMessages = enumerable.Where(it => it.HasError) .Select(it => new { it.PropertyName, ErrorMessage = it.Error }) .GroupBy(it => it.PropertyName) .ToDictionary(group => group.Key, g => g.Select(it => it.ErrorMessage)); - return new ValidationResultArgs(propertyName, errorMessages); + + return new ValidationResultArgs(propertyName!, errorMessages!); } private bool ValidateImpl(string? propertyName = null) @@ -177,20 +202,13 @@ public static bool ValidateRuleCollection( if (!string.IsNullOrEmpty(propertyName) && rule.PropertyName != propertyName) continue; - var property = target.GetType().GetProperty(rule.PropertyName); + var type = target.GetType(); + var property = type.GetProperty(rule.PropertyName); var value = property.GetValue(target, null); rule.Validate(value); noErrors = noErrors && !rule.HasError; } return noErrors; } - - public void EnsurePropertiesAreValid() - { - var resultArgs = GetValidationResultArgs(string.Empty); - var firstError = resultArgs.FirstError; - if (!string.IsNullOrEmpty(firstError)) - throw new PropertyException(resultArgs); - } } }