Custom attribute DataTypeAttribute does not start validation correctly

In connection with this question

I created my own DateValidationAttibute to make sure the string is in a valid date format (e.g. MM / DD / YYYY)

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class DateValidationAttribute : DataTypeAttribute
{
    public DateValidationAttribute() : base(DataType.Date){}

    //MM/DD/YYYY, MM-DD-YYYY
    public override bool IsValid(object value)
    {
        //validation logic
    }
}

I am trying to verify this attribute with this code

    [Test]
    public void Test()
    {
        var invalidObject = new TestValidation {DateField = "bah"};
        var validationContext = new ValidationContext(invalidObject, null, null);
        var validationResults = new System.Collections.Generic.List<ValidationResult>();

        bool result = Validator.TryValidateObject(invalidObject, validationContext, validationResults);

        Assert.IsFalse(result);
        Assert.AreEqual(1, validationResults.Count);
    }

    private class TestValidation
    {
        [DateValidation(ErrorMessage = "Invalid Date!")]
        public string DateField { get; set; }
    }

Unfortunately this does not work. I set a breakpoint in the DateValidationAttribute constructor and the IsValid method. It definitely hits the constructor, but never removes the IsValid method. Any ideas?

+3
source share
3 answers

TryValidateObject , validateAllProperties . , false, .

[Test]
public void Test()
{
    var invalidObject = new TestValidation {DateField = "bah"};
    var validationContext = new ValidationContext(invalidObject, null, null);
    var validationResults = new System.Collections.Generic.List<ValidationResult>();

    //Validate all attributes
    bool result = Validator.TryValidateObject(invalidObject, validationContext, validationResults, true);

    Assert.IsFalse(result);
    Assert.AreEqual(1, validationResults.Count);
}
+1

ValidationAttributes DataTypeAttribute, , , ValidationAttribute .

"DataTypeAttribute , , ".

:

[AttributeUsage(AttributeTargets.Field,  AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class MyCustomAttribute : ValidationAttribute
{
  public MyCustomAttribute()
    : base("Custom Error Message: {0}")
  {
  }

  public override bool IsValid(object value)
  {
    return true;
  }
}
0

MSDN , , .

This method evaluates each ValidationAttribute instance that is attached to the type of the object. It also checks to see if each property that is provided with a required attribute. It does not recursively validate object property values .

If you want to change your test to this, note that we set MemberName to a ValidationContext and tell Validator to check the invalidObject.DateField property.

[Test]
public void Test()
{
     var invalidObject = new TestValidation { DateField = "bah" };
     var validationContext = new ValidationContext(invalidObject,null , null){MemberName = "DateField"};
     var validationResults = new System.Collections.Generic.List<ValidationResult>();

     var result = Validator.TryValidateProperty(invalidObject.DateField, validationContext, validationResults);

    Assert.IsFalse(result);
    Assert.AreEqual(1, validationResults.Count);
}
0
source

Source: https://habr.com/ru/post/1772274/


All Articles