Using Data Annotations to validate models

The .NET Framework provides us a set of attributes that we can use to validate objects. By using the namespace System.ComponentModel.DataAnnotations we can annotate our model’s properties with validation attributes.

There are attributes to mark a property as required, set a maximum length and so on. For example:

public class Game
{
    [Required]
    [StringLength(20)]
    public string Name { get; set; }

    [Range(0, 100)]
    public decimal Price { get; set; }
}

To check if an instance is valid we can use the following code:

Validator.TryValidateObject(obj
    , new ValidationContext(obj)
    , results, true);

The return is true if the object does not have any errors or false if it does have errors. And the parameter results is populated with errors, if any. The full definition of this method can be found at MSDN documentation.

To test our Game class we can use the following code:

static void Main(string[] args)
{
    ICollection<ValidationResult> results = null;

    var invalidGame = new Game
    {
        Name = "My name is way over 20 characters",
        Price = 300,
    };

    if (!Validate(invalidGame, out results))
    {
        Console.WriteLine(String.Join("\n", results.Select(o => o.ErrorMessage)));
    }
    else
    {
        Console.WriteLine("I'm a valid object!");
    }

    Console.ReadKey(true);
}

static bool Validate<T>(T obj, out ICollection<ValidationResult> results)
{
    results = new List<ValidationResult>();

    return Validator.TryValidateObject(obj, new ValidationContext(obj), results, true);
}

Running it results in:
2015-11-04 12-11-43-490

If we then change the properties to valid values:

var validGame = new Game
{
    Name = "Magicka",
    Price = 5,
};

And test again:
2015-14-04 12-14-55-202

It is also possible to create your own attributes. All you have to do is inherit from ValidationAttribute. In the following example the attribute is going to check if the value is divisible by 7. If not it will return an error message.

public class DivisibleBy7Attribute : ValidationAttribute
{
    public DivisibleBy7Attribute()
        : base("{0} value is not divisible by 7")
    {
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        decimal val = (decimal)value;

        bool valid = val % 7 == 0;

        if (valid)
            return null;

        return new ValidationResult(base.FormatErrorMessage(validationContext.MemberName)
            , new string[] { validationContext.MemberName });
    }
}

And in the object to be validated:

[DivisibleBy7]
public decimal Price { get; set; }

If the validation fails it is going to return the following error message:
2015-31-04 12-31-04-005

All built-in validation attributes

A full list of validation attributes can be found at MSDN documentation.

Advertisements

3 thoughts on “Using Data Annotations to validate models

  1. Pingback: Screen validation with Data Annotations in WPF | Coding Wise

  2. Pingback: View validation with Data Annotations and custom client validations in MVC | Coding Wise

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s