I'm working on a Web API 2 project. besides the requirement that some properties are required, some only can have specific values. One option is that I could try to save the model to the database (EF6) and create some logic while saving, but I think it is better to validate if the correct value is set before I make a call to the database. Does data annotations provide an attribute like Range but then for specific string values like in the example below? Or do I have to write my own validator attribute?
public class Person {
public int PersonID { get; set; }
[Required]
public string FirstName { get; set; }
public string LastName { get; set; }
[StringRange("M","F")]
public string Gender { get; set; }
}
In the above example, when a post is done to the controller, the only values to accept are "M" or "F".