Skip to content

Commit

Permalink
Merge pull request #314 from enisn/4.1-validation-type-toleration
Browse files Browse the repository at this point in the history
4.1 validation type toleration
  • Loading branch information
enisn authored Sep 17, 2022
2 parents a20e28f + 71315de commit f8b73a3
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/InputKit.Maui/InputKit.Maui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<SingleProject>true</SingleProject>
<MauiVersion>6.0.486</MauiVersion>
<PackageId>InputKit.Maui</PackageId>
<Version>4.1.3</Version>
<Version>4.1.4</Version>
<DefineConstants Condition="$(TargetFramework.Contains('-windows'))">$(DefineConstants);UWP</DefineConstants>

<!-- NuGet Package Info -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
return number;
}

if (DateTime.TryParse(text, out var dateTime))
if (TimeSpan.TryParse(text, out var timeSpan))
{
return dateTime;
return timeSpan;
}

if (TimeSpan.TryParse(text, out var timeSpan))
if (DateTime.TryParse(text, out var dateTime))
{
return timeSpan;
return dateTime;
}

if (string.IsNullOrEmpty(text))
Expand Down
17 changes: 12 additions & 5 deletions src/InputKit.Maui/Shared/Validations/MaxValueValidation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System;
using System.ComponentModel;

namespace InputKit.Shared.Validations;
public class MaxValueValidation : IValidation
Expand All @@ -11,15 +12,21 @@ public class MaxValueValidation : IValidation

public bool Validate(object value)
{
if (value is null)
if (value is null || (value is string text && string.IsNullOrEmpty(text)))
{
return true;
}

var converted = ComparableTypeConverter.Instance.ConvertFrom(value);
if (converted is IComparable comparable && converted.GetType() == comparable.GetType())
var type = MaxValue.GetType();

if (value.GetType() != type)
{
value = Convert.ChangeType(value, type);
}

if (value is IComparable comparableValue)
{
return comparable.CompareTo(MaxValue) <= 0;
return comparableValue.CompareTo(MaxValue) <= 0;
}

return false;
Expand Down
16 changes: 10 additions & 6 deletions src/InputKit.Maui/Shared/Validations/MinValueValidation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@ public class MinValueValidation : IValidation

public bool Validate(object value)
{
if (value is null)
if (value is null || (value is string text && string.IsNullOrEmpty(text)))
{
return true;
}

var converted = ComparableTypeConverter.Instance.ConvertFrom(value);
var type = MinValue.GetType();

if (converted is IComparable comparable && converted.GetType() == comparable.GetType())
if (value.GetType() != type)
{
return comparable.CompareTo(MinValue) >= 0;
value = Convert.ChangeType(value, type);
}

if (value is IComparable comparableValue)
{
return comparableValue.CompareTo(MinValue) >= 0;
}

return false;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ public class MaxValueValidation : IValidation

public bool Validate(object value)
{
if (value is null)
if (value is null || (value is string text && string.IsNullOrEmpty(text)))
{
return true;
}

var converted = ComparableTypeConverter.Instance.ConvertFrom(value);
if (converted is IComparable comparable && converted.GetType() == comparable.GetType())
var type = MaxValue.GetType();

if (value.GetType() != type)
{
value = Convert.ChangeType(value, type);
}

if (value is IComparable comparableValue)
{
return comparable.CompareTo(MaxValue) <= 0;
return comparableValue.CompareTo(MaxValue) <= 0;
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ public class MinValueValidation : IValidation

public bool Validate(object value)
{
if (value is null)
if (value is null || (value is string text && string.IsNullOrEmpty(text)))
{
return true;
}

var converted = ComparableTypeConverter.Instance.ConvertFrom(value);
var type = MinValue.GetType();

if (converted is IComparable comparable && converted.GetType() == comparable.GetType())
if (value.GetType() != type)
{
return comparable.CompareTo(MinValue) >= 0;
value = Convert.ChangeType(value, type);
}

if (value is IComparable comparableValue)
{
return comparableValue.CompareTo(MinValue) >= 0;
}

return false;
Expand Down
2 changes: 1 addition & 1 deletion src/Xamarin.Forms.InputKit/Xamarin.Forms.InputKit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<RootNamespace>Plugin.InputKit</RootNamespace>
<PackageId>Xamarin.Forms.InputKit</PackageId>
<Product>$(AssemblyName) ($(TargetFramework))</Product>
<Version>4.1.3</Version>
<Version>4.1.4</Version>
<PackOnBuild>false</PackOnBuild>
<NeutralLanguage>en-US</NeutralLanguage>
<DefineConstants>$(DefineConstants);</DefineConstants>
Expand Down

0 comments on commit f8b73a3

Please sign in to comment.