Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mr5z committed Apr 10, 2021
1 parent 4363528 commit d6d6962
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.propertyvalidator.test">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
<application android:label="PropertyValidator.Test.Android"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.propertyvalidator.test" android:installLocation="auto">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />
<application android:label="PropertyValidator.Test.Android"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
<AndroidUseLatestPlatformSdk>false</AndroidUseLatestPlatformSdk>
<TargetFrameworkVersion>v9.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v11.0</TargetFrameworkVersion>
<AndroidEnableSGenConcurrent>true</AndroidEnableSGenConcurrent>
<AndroidUseAapt2>true</AndroidUseAapt2>
<AndroidHttpClientHandlerType>Xamarin.Android.Net.AndroidClientHandler</AndroidHttpClientHandlerType>
Expand All @@ -33,6 +33,10 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidLinkMode>None</AndroidLinkMode>
<AotAssemblies>false</AotAssemblies>
<EnableLLVM>false</EnableLLVM>
<AndroidEnableProfiledAot>false</AndroidEnableProfiledAot>
<BundleAssemblies>false</BundleAssemblies>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -96,7 +100,7 @@
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
<ProjectExtensions>
<VisualStudio>
<UserProperties XamarinHotReloadDebuggerTimeoutExceptionPropertyValidatorTestAndroidHideInfoBar="True" TriggeredFromHotReload="False" />
<UserProperties TriggeredFromHotReload="False" XamarinHotReloadDebuggerTimeoutExceptionPropertyValidatorTestAndroidHideInfoBar="True" />
</VisualStudio>
</ProjectExtensions>
</Project>
5 changes: 1 addition & 4 deletions PropertyValidator.Test/PropertyValidator.Test/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>netstandard2.1</TargetFramework>
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ private void ShowValidationResult()

private void Submit()
{
try
{
validationService.EnsurePropertiesAreValid();
}
catch (Exception ex)
{
var msg = ex.Message;
}
if (!validationService.Validate())
{
ShowValidationResult();
Expand Down
6 changes: 3 additions & 3 deletions PropertyValidator/PropertyValidator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<PackageProjectUrl>https://github.com/mr5z/PropertyValidator</PackageProjectUrl>
<RepositoryUrl>https://github.com/mr5z/PropertyValidator</RepositoryUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<FileVersion>1.0.6.2</FileVersion>
<AssemblyVersion>1.0.6.2</AssemblyVersion>
<Version>1.0.6.2</Version>
<FileVersion>1.0.6.3</FileVersion>
<AssemblyVersion>1.0.6.3</AssemblyVersion>
<Version>1.0.6.3</Version>
<NeutralLanguage>en</NeutralLanguage>
</PropertyGroup>

Expand Down
44 changes: 31 additions & 13 deletions PropertyValidator/Services/ValidationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
}
}

0 comments on commit d6d6962

Please sign in to comment.