Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from dbuenor/dbueno/add-MinElementsIfAttribute
Added new Conditional Validation attributes for MinElements (@2.5h)
- Loading branch information
Showing
9 changed files
with
204 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
HermaFx.DataAnnotations.Tests/MinElementsIfAttributeTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
| ||
using NUnit.Framework; | ||
using System; | ||
|
||
namespace HermaFx.DataAnnotations | ||
{ | ||
[TestFixture] | ||
public class MinElementsIfAttributeTest | ||
{ | ||
private class Model : ModelBase<MinElementsIfAttribute> | ||
{ | ||
public string Value1 { get; set; } | ||
|
||
[MinElementsIf(3, "Value1", "hello")] | ||
public string[] Value2 { get; set; } | ||
} | ||
|
||
private class ComplexModel : ModelBase<MinElementsIfAttribute> | ||
{ | ||
public class SubModel | ||
{ | ||
public string InnerValue { get; set; } | ||
} | ||
|
||
public SubModel Value1 { get; set; } | ||
|
||
[MinElementsIf(3, "Value1.InnerValue", "hello")] | ||
public string[] Value2 { get; set; } | ||
} | ||
|
||
[Test] | ||
public void IsValidTest() | ||
{ | ||
var model = new Model() { Value1 = "hello", Value2 = new string[] { "hello", "hello2", "hello3" } }; | ||
Assert.IsTrue(model.IsValid("Value2")); | ||
} | ||
|
||
[Test] | ||
public void IsValidTestComplex() | ||
{ | ||
var model = new ComplexModel() {Value1 = new ComplexModel.SubModel() {InnerValue = "hello"}, Value2 = new string[] { "bla", "bla2", "bla3" } }; | ||
Assert.IsTrue(model.IsValid("Value2")); | ||
} | ||
|
||
[Test] | ||
public void IsNotValidTest() | ||
{ | ||
var model = new Model() { Value1 = "hello", Value2 = new string[] { "hello", "hello2" } }; | ||
Assert.IsFalse(model.IsValid("Value2")); | ||
} | ||
|
||
[Test] | ||
public void IsNotValidTestComplex() | ||
{ | ||
var model = new ComplexModel() { Value1 = new ComplexModel.SubModel() { InnerValue = "hello" }, Value2 = new string[] { "bla", "bla2" } }; | ||
Assert.IsFalse(model.IsValid("Value2")); | ||
} | ||
|
||
[Test] | ||
public void IsValidWithValue2NullTest() | ||
{ | ||
var model = new Model() { Value1 = "hello", Value2 = null }; | ||
Assert.IsTrue(model.IsValid("Value2")); | ||
} | ||
|
||
[Test] | ||
public void IsNotRequiredToHaveMinElementsTest() | ||
{ | ||
var model = new Model() { Value1 = "goodbye" }; | ||
Assert.IsTrue(model.IsValid("Value2")); | ||
} | ||
|
||
[Test] | ||
public void IsNotRequiredToHaveMinElementsWithValue1NullTest() | ||
{ | ||
var model = new Model() { Value1 = null }; | ||
Assert.IsTrue(model.IsValid("Value2")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace HermaFx.DataAnnotations | ||
{ | ||
public class MinElementsIfAttribute : ContingentValidationAttribute | ||
{ | ||
public Operator Operator { get; private set; } | ||
public object DependentValue { get; private set; } | ||
private MinElementsAttribute MinElements{ get; set; } | ||
/// <summary> | ||
/// Gets or sets a flag indicating whether the attribute should allow empty strings. | ||
/// </summary> | ||
public bool AllowEmptyStrings { get; set; } | ||
protected OperatorMetadata Metadata { get; private set; } | ||
|
||
public MinElementsIfAttribute(uint elements, string dependentProperty, Operator @operator, object dependentValue) | ||
: base(dependentProperty) | ||
{ | ||
Operator = @operator; | ||
DependentValue = dependentValue; | ||
Metadata = OperatorMetadata.Get(Operator); | ||
MinElements = new MinElementsAttribute(elements); | ||
} | ||
|
||
public MinElementsIfAttribute(uint elements, string dependentProperty, object dependentValue) | ||
: this(elements, dependentProperty, Operator.EqualTo, dependentValue) { } | ||
|
||
public override string FormatErrorMessage(string name) | ||
{ | ||
if (string.IsNullOrEmpty(ErrorMessageResourceName) && string.IsNullOrEmpty(ErrorMessage)) | ||
ErrorMessage = DefaultErrorMessage; | ||
|
||
return MinElements.ErrorMessage; | ||
} | ||
|
||
public override string ClientTypeName | ||
{ | ||
get { return "MinElementsIf"; } | ||
} | ||
|
||
protected override IEnumerable<KeyValuePair<string, object>> GetClientValidationParameters() | ||
{ | ||
return base.GetClientValidationParameters() | ||
.Union(new[] { | ||
new KeyValuePair<string, object>("Operator", Operator.ToString()), | ||
new KeyValuePair<string, object>("DependentValue", DependentValue), | ||
new KeyValuePair<string, object>("Elements", MinElements.Elements), | ||
}); | ||
} | ||
|
||
public override bool IsValid(object value, object dependentValue, object container) | ||
{ | ||
if (Metadata.IsValid(dependentValue, DependentValue)) | ||
return MinElements.IsValid(value); | ||
|
||
return true; | ||
} | ||
|
||
public override string DefaultErrorMessage | ||
{ | ||
get { return MinElements.ErrorMessage; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace HermaFx.DataAnnotations | ||
{ | ||
public class MinElementsIfHasFlagAttribute : MinElementsIfAttribute | ||
{ | ||
public MinElementsIfHasFlagAttribute(uint elements, string dependentProperty, object flags) : base(elements, dependentProperty, Operator.HasFlag, flags) { } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#region LICENSE | ||
// Source Code licensed under MS-PL. | ||
// Derived from: MVC Foolproof Validation (http://foolproof.codeplex.com/) | ||
#endregion | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace HermaFx.DataAnnotations | ||
{ | ||
public class MinElementsIfNotAttribute : MinElementsIfAttribute | ||
{ | ||
public MinElementsIfNotAttribute(uint elements, string dependentProperty, object dependentValue) : base(elements, dependentProperty, Operator.NotEqualTo, dependentValue) { } | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
HermaFx.DataAnnotations/MinElementsIfNotHasFlagAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace HermaFx.DataAnnotations | ||
{ | ||
public class MinElementsIfNotHasFlagAttribute : MinElementsIfAttribute | ||
{ | ||
public MinElementsIfNotHasFlagAttribute(uint elements, string dependentProperty, object flags) : base(elements, dependentProperty, Operator.NotHasFlag, flags) { } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters