Complex Custom Validation Attribute Specific to Entity

System.Component model.DataAnnotation is a namespace in the .Net Framework that provides a set of attribute classes. These attribute classes decorate a property of the user-defined entity and validate the entity.

To see more about each class go to the MSDN at https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations%28v=vs.95%29.aspx.

ValidationAttribute

Most of the required attributes for validation are provided in the DataAnnotation namespace but there are some complex validations required to validate an entity that must be defined by the developer themself.

Validation Attribute is a class in the DataAnnotation namespace that allows the creation of a custom attribute depending on the needs of the developer to validate the entity.

The following is an implementation of the custom validation attribute.

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public sealed class AccountBalaceCheckAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        ValidationResult result = ValidationResult.Success;
        string[] memberNames = new string[] { validationContext.MemberName };

        decimal val = Convert.ToDecimal(value);

        BankAccount account = (BankAccount)validationContext.ObjectInstance;

        if (account.AcType == BankAccount.AccountType.Saving && val < 500)
            result = new ValidationResult(string.Format(this.ErrorMessage, 500), memberNames);
        else if (account.AcType == BankAccount.AccountType.Current && val < 1000)
            result = new ValidationResult(string.Format(this.ErrorMessage, 1000), memberNames);

        return result;
    }
}

The following shows how to use it.

public class BankAccount
{
    public enum AccountType
    {
        Saving,
        Current
    }
    public AccountType AcType { get; set; }
    [AccountBalaceCheckAttribute(ErrorMessage = "Account Balance should have value more than {0}")]
    public double AccountBalance { get; set; }
}

In this example, the attribute is defined for the specific entity BankAccount entity. The requirement for defining this attribute is “Based on the value of AccountType, the account should hold the minimum balance.

So for that here, the Bank Account object is accessed in the IsValid method, and then by checking the value of the Account type attribute it validates the AccountBalance value.

The following are other things to note here.

  1. The Attribute class is Sealed and Derived from the validation attribute.
  2. The custom attribute class overrides the IsValid method of the base ValidationAttribute class.
  3. The attribute can be used for a property and is not allowed to define an attribute multiple times on the property.

To see how to define an attribute not specific to an entity go to the MSDN at https://msdn.microsoft.com/en-us/library/cc668224%28v=vs.140%29.aspx.


Recommended Free Ebook
Similar Articles