Skip to content

Commit

Permalink
[#251][edit] change type of exception when argument error
Browse files Browse the repository at this point in the history
  • Loading branch information
yollosun committed Jan 7, 2024
1 parent 9954111 commit d09194e
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,14 @@ public void Validate_NullValue_NoExceptions()
[Test]
public void Validate_DifferentTypes_ExceptionThrown()
{
// Assign

var value = 15.2;
var defaultMessage = "Type mismatch. The maximum value and property value should be of the same type.";

// Act & Assert
TestAttribute(value, defaultMessage);
Assert.Throws<ArgumentException>(() => TestAttributeForValidValue(15.2));
}

[Test]
public void Validate_ValueTypeNotInheritIComparable_ExceptionThrown()
{
// Assign

var value = new object();
var defaultMessage = $"The type of specified property value must be inherited from {typeof(IComparable)}";

// Act & Assert
TestAttribute(value, defaultMessage);
Assert.Throws<ArgumentException>(() => TestAttributeForValidValue(new object()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,14 @@ public void Validate_NullValue_NoExceptions()
[Test]
public void Validate_DifferentTypes_ExceptionThrown()
{
// Assign

var value = 12.5;
var defaultMessage = "Type mismatch. The minimum value and property value should be of the same type.";

// Act & Assert
TestAttribute(value, defaultMessage);
Assert.Throws<ArgumentException>(() => TestAttributeForValidValue(12.5));
}

[Test]
public void Validate_ValueTypeNotInheritIComparable_ExceptionThrown()
{
// Assign

var value = new object();
var defaultMessage = $"The type of specified property value must be inherited from {typeof(IComparable)}";

// Act & Assert
TestAttribute(value, defaultMessage);
Assert.Throws<ArgumentException>(() => TestAttributeForValidValue(new object()));
}
}
4 changes: 2 additions & 2 deletions src/Simplify.Web/Model/Validation/Attributes/MaxAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override void Validate(object? value, PropertyInfo propertyInfo, IDIResol
return;

if (value is not IComparable comparableValue)
throw new ModelValidationException($"The type of specified property value must be inherited from {typeof(IComparable)}");
throw new ArgumentException($"The type of specified property value must be inherited from {typeof(IComparable)}");

ValidateTypesMatching(comparableValue);

Expand All @@ -52,6 +52,6 @@ public override void Validate(object? value, PropertyInfo propertyInfo, IDIResol
private void ValidateTypesMatching(IComparable comparableValue)
{
if (comparableValue.GetType() != MaxValue.GetType())
throw new ModelValidationException("Type mismatch. The maximum value and property value should be of the same type.");
throw new ArgumentException("Type mismatch. The maximum value and property value should be of the same type.");
}
}
4 changes: 2 additions & 2 deletions src/Simplify.Web/Model/Validation/Attributes/MinAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override void Validate(object? value, PropertyInfo propertyInfo, IDIResol
return;

if (value is not IComparable comparableValue)
throw new ModelValidationException($"The type of specified property value must be inherited from {typeof(IComparable)}");
throw new ArgumentException($"The type of specified property value must be inherited from {typeof(IComparable)}");

ValidateTypesMatching(comparableValue);

Expand All @@ -52,6 +52,6 @@ public override void Validate(object? value, PropertyInfo propertyInfo, IDIResol
private void ValidateTypesMatching(IComparable comparableValue)
{
if (comparableValue.GetType() != MinValue.GetType())
throw new ModelValidationException("Type mismatch. The minimum value and property value should be of the same type.");
throw new ArgumentException("Type mismatch. The minimum value and property value should be of the same type.");
}
}

0 comments on commit d09194e

Please sign in to comment.