Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 3 commits
  • 14 files changed
  • 0 commit comments
  • 2 contributors
Commits on Aug 30, 2012
@mattleibow mattleibow A slightly better value type comparer
This is used when comparing values of different types that should be
valid, such as Int32 and Double. This first does the default and if this
fails, the value types comparison. If both fail, it just throws the
normal exception.
fa1b686
@mattleibow mattleibow Testcase for original failure without new Comparer 5438898
Commits on Aug 31, 2012
@JeremySkinner Merge pull request #12 from mattleibow/Comparer
A slightly better value type comparer
78ffadc
View
5 src/FluentValidation.Tests/LessThanOrEqualToValidatorTester.cs
@@ -49,6 +49,11 @@ public class LessThanOrEqualToValidatorTester {
}
[Test]
+ public void Should_succeed_when_less_than_input_using_different_types() {
+ new LessThanOrEqualValidator(100M).IsValid(20D, 100M).ShouldBeTrue();
+ }
+
+ [Test]
public void Should_succeed_when_equal_to_input() {
var result = validator.Validate(new Person{Id=value});
result.IsValid.ShouldBeTrue();
View
5 src/FluentValidation.Tests/LessThanValidatorTester.cs
@@ -79,6 +79,11 @@ public class LessThanValidatorTester {
}
[Test]
+ public void Should_not_throw_when_value_to_compare_is_of_different_type() {
+ new LessThanValidator(10M).IsValid(5M, 10).ShouldBeTrue();
+ }
+
+ [Test]
public void Extracts_property_from_expression() {
var validator = new TestValidator(v => v.RuleFor(x => x.Id).LessThan(x => x.AnotherInt));
var propertyValidator = validator.CreateDescriptor().GetValidatorsForMember("Id").OfType<LessThanValidator>().Single();
View
12 src/FluentValidation.Tests/StandalonePropertyValidationTester.cs
@@ -19,6 +19,18 @@ public class StandalonePropertyValidationTester {
result.Single().ShouldNotBeNull();
}
+ [Test]
+ public void Should_validate_property_value_without_instance_different_types() {
+ var validator = new EqualValidator(100M); // decimal
+ var parentContext = new ValidationContext(null);
+ var rule = new PropertyRule(null, x => 100D /* double */, null, null, typeof(string), null) {
+ PropertyName = "Surname"
+ };
+ var context = new PropertyValidatorContext(parentContext, rule, null);
+ var result = validator.Validate(context); // would fail saying that decimal is not double
+ result.Count().ShouldEqual(0);
+ }
+
//TODO: Test the other standalone validators.
}
}
View
1 src/FluentValidation/FluentValidation.csproj
@@ -82,6 +82,7 @@
<Compile Include="DefaultValidatorExtensions.cs" />
<Compile Include="DefaultValidatorOptions.cs" />
<Compile Include="InlineValidator.cs" />
+ <Compile Include="Internal\Comparer.cs" />
<Compile Include="Internal\DefaultValidatorSelector.cs" />
<Compile Include="Internal\DelegateValidator.cs" />
<Compile Include="Internal\Extensions.cs" />
View
61 src/FluentValidation/Internal/Comparer.cs
@@ -0,0 +1,61 @@
+using System;
+
+namespace FluentValidation.Internal
+{
+ public static class Comparer {
+ public static bool TryCompare(IComparable value, IComparable valueToCompare, out int result) {
+ try {
+ Compare(value, valueToCompare, out result);
+ return true;
+ }
+ catch {
+ }
+
+ result = 0;
+ return false;
+ }
+
+ /// <summary>
+ /// Tries to do a proper comparison but may fail.
+ /// First it tries the default comparison, if this fails, it will see
+ /// if the values are fractions. If they are, then it does a double
+ /// comparison, otherwise it does a long comparison.
+ /// </summary>
+ private static void Compare(IComparable value, IComparable valueToCompare, out int result) {
+ try {
+ // try default (will work on same types)
+ result = value.CompareTo(valueToCompare);
+ } catch {
+ // attempt to to value type comparison
+ if (value is decimal || valueToCompare is decimal ||
+ value is double || valueToCompare is double ||
+ value is float || valueToCompare is float) {
+ // we are comparing a decimal/double/float, then compare using doubles
+ result = Convert.ToDouble(value).CompareTo(Convert.ToDouble(valueToCompare));
+ }
+ else {
+ // use long integer
+ result = ((long)value).CompareTo((long)valueToCompare);
+ }
+ }
+ }
+
+ public static int GetComparisonResult(IComparable value, IComparable valueToCompare) {
+ int result;
+ if (TryCompare(value, valueToCompare, out result)) {
+ return result;
+ }
+
+ return value.CompareTo(valueToCompare);
+ }
+
+ public static bool GetEqualsResult(IComparable value, IComparable valueToCompare) {
+ int result;
+ if (TryCompare(value, valueToCompare, out result)) {
+ return result == 0;
+ }
+
+ return value.Equals(valueToCompare);
+ }
+ }
+}
View
2 src/FluentValidation/Validators/EqualValidator.cs
@@ -83,7 +83,7 @@ public EqualValidator(Func<object, object> comparisonProperty, MemberInfo member
return comparer.Equals(comparisonValue, propertyValue);
}
- return Equals(comparisonValue, propertyValue);
+ return Internal.Comparer.GetEqualsResult((IComparable)comparisonValue, (IComparable)propertyValue);
}
}
}
View
5 src/FluentValidation/Validators/ExclusiveBetweenValidator.cs
@@ -19,14 +19,15 @@
namespace FluentValidation.Validators {
using System;
using Attributes;
+ using Internal;
using Resources;
public class ExclusiveBetweenValidator : PropertyValidator, IBetweenValidator {
public ExclusiveBetweenValidator(IComparable from, IComparable to) : base(() => Messages.exclusivebetween_error) {
To = to;
From = from;
- if (to.CompareTo(from) == -1) {
+ if (Comparer.GetComparisonResult(to, from) == -1) {
throw new ArgumentOutOfRangeException("to", "To should be larger than from.");
}
}
@@ -42,7 +43,7 @@ public class ExclusiveBetweenValidator : PropertyValidator, IBetweenValidator {
// This should not be a failure condition - only a NotNull/NotEmpty should cause a null to fail.
if (propertyValue == null) return true;
- if (propertyValue.CompareTo(From) <= 0 || propertyValue.CompareTo(To) >= 0) {
+ if (Comparer.GetComparisonResult(propertyValue, From) <= 0 || Comparer.GetComparisonResult(propertyValue, To) >= 0) {
context.MessageFormatter
.AppendArgument("From", From)
View
2 src/FluentValidation/Validators/GreaterThanOrEqualValidator.cs
@@ -31,7 +31,7 @@ public GreaterThanOrEqualValidator(Func<object, object> valueToCompareFunc, Memb
}
public override bool IsValid(IComparable value, IComparable valueToCompare) {
- return value.CompareTo(valueToCompare) >= 0;
+ return Comparer.GetComparisonResult(value, valueToCompare) >= 0;
}
public override Comparison Comparison {
View
2 src/FluentValidation/Validators/GreaterThanValidator.cs
@@ -32,7 +32,7 @@ public GreaterThanValidator(Func<object, object> valueToCompareFunc, MemberInfo
}
public override bool IsValid(IComparable value, IComparable valueToCompare) {
- return value.CompareTo(valueToCompare) > 0;
+ return Comparer.GetComparisonResult(value, valueToCompare) > 0;
}
public override Comparison Comparison {
View
5 src/FluentValidation/Validators/InclusiveBetweenValidator.cs
@@ -19,14 +19,15 @@
namespace FluentValidation.Validators {
using System;
using Attributes;
+ using Internal;
using Resources;
public class InclusiveBetweenValidator : PropertyValidator, IBetweenValidator {
public InclusiveBetweenValidator(IComparable from, IComparable to) : base(() => Messages.inclusivebetween_error) {
To = to;
From = from;
- if (to.CompareTo(from) == -1) {
+ if (Comparer.GetComparisonResult(to, from) == -1) {
throw new ArgumentOutOfRangeException("to", "To should be larger than from.");
}
@@ -42,7 +43,7 @@ public class InclusiveBetweenValidator : PropertyValidator, IBetweenValidator {
// This should not be a failure condition - only a NotNull/NotEmpty should cause a null to fail.
if (propertyValue == null) return true;
- if (propertyValue.CompareTo(From) < 0 || propertyValue.CompareTo(To) > 0) {
+ if (Comparer.GetComparisonResult(propertyValue, From) < 0 || Comparer.GetComparisonResult(propertyValue, To) > 0) {
context.MessageFormatter
.AppendArgument("From", From)
View
27 src/FluentValidation/Validators/LengthValidator.cs
@@ -33,16 +33,15 @@ public class LengthValidator : PropertyValidator, ILengthValidator {
Max = max;
Min = min;
- if (max < min) {
+ if (max != -1 && max < min) {
throw new ArgumentOutOfRangeException("max", "Max should be larger than min.");
}
-
}
protected override bool IsValid(PropertyValidatorContext context) {
int length = context.PropertyValue == null ? 0 : context.PropertyValue.ToString().Length;
- if (length < Min || length > Max) {
+ if (length < Min || (length > Max && Max != -1)) {
context.MessageFormatter
.AppendArgument("MinLength", Min)
.AppendArgument("MaxLength", Max)
@@ -61,6 +60,28 @@ public class ExactLengthValidator : LengthValidator {
}
}
+ public class MaximumLengthValidator : LengthValidator {
+ public MaximumLengthValidator(int max) : this(max, () => Messages.length_error) {
+
+ }
+
+ public MaximumLengthValidator(int max, Expression<Func<string>> errorMessageResourceSelector)
+ : base(0, max, errorMessageResourceSelector) {
+
+ }
+ }
+
+ public class MinimumLengthValidator : LengthValidator {
+ public MinimumLengthValidator(int min) : this(min, () => Messages.length_error) {
+
+ }
+
+ public MinimumLengthValidator(int min, Expression<Func<string>> errorMessageResourceSelector)
+ : base(min, -1, errorMessageResourceSelector) {
+
+ }
+ }
+
public interface ILengthValidator : IPropertyValidator {
int Min { get; }
int Max { get; }
View
4 src/FluentValidation/Validators/LessThanOrEqualValidator.cs
@@ -32,8 +32,8 @@ public LessThanOrEqualValidator(Func<object, object> valueToCompareFunc, MemberI
: base(valueToCompareFunc, member, () => Messages.lessthanorequal_error) {
}
- public override bool IsValid(IComparable value, IComparable valueToCompare) {
- return value.CompareTo(valueToCompare) <= 0;
+ public override bool IsValid(IComparable value, IComparable valueToCompare) {
+ return Comparer.GetComparisonResult(value, valueToCompare) <= 0;
}
public override Comparison Comparison {
View
2 src/FluentValidation/Validators/LessThanValidator.cs
@@ -33,7 +33,7 @@ public LessThanValidator(Func<object, object> valueToCompareFunc, MemberInfo mem
}
public override bool IsValid(IComparable value, IComparable valueToCompare) {
- return value.CompareTo(valueToCompare) < 0;
+ return Comparer.GetComparisonResult(value, valueToCompare) < 0;
}
public override Comparison Comparison {
View
3 src/FluentValidation/Validators/NotEqualValidator.cs
@@ -83,7 +83,8 @@ public NotEqualValidator(object comparisonValue, IEqualityComparer equalityCompa
if(comparer != null) {
return comparer.Equals(comparisonValue, propertyValue);
}
- return Equals(comparisonValue, propertyValue);
+
+ return Internal.Comparer.GetEqualsResult((IComparable)comparisonValue, (IComparable)propertyValue);
}
}
}

No commit comments for this range

Something went wrong with that request. Please try again.