How can I make JSON.NET / MVC 4 WebAPI reject integer values for which the enum has no member? Eg:
If I have this model:
public enum Colour { Red = 1 };
public class Model
{
public Colour Colour { get; set; }
}
Model Post(Model model)
{
// model.Colour could be 99, 34234234, 0 etc, etc
}
If I post { Color: 9999 }
, I end up with a model where model.Color = 999 and I want to return a Bad Request status code instead.
One option is to write a validator:
Use as:
In the controller,
ModelState.IsValid
would befalse
.You can also throw a
ValidationException
, if you really want to fail the request, but I'm not quite sure that is how they should be used.It turns out the EnumDataTypeAttribute, which comes with the out-of-the-box ValidationAttributes in the
System.ComponentModel.DataAnnotations
namespace, does an Enum.Defined check.Once I applied this attribute to my view model, out-of-range integer values failed validation:
Note: values that can be parsed into integers that are defined on the enum will still pass validation due to the default behavior of enum model binding. This means, for example,
true
will be parsed as1
, which would be valid for this enum. I assume characters that can be mapped to integers will also work.If you only want one flavor of enum parsing to work, be it string or integer, consider using that specific type on your view model, and then write a custom
ValidationAttribute
that takes in the enum type, validating that the string or integer on your view model matches a value in the enum.