4

I have a .Net Core 2.0 Web Api. I have various models with validation attributes on properties as such:

[Required]
public short? Quantity { get; set; }

I have an ActionFilter that checks model state:

if (!context.ModelState.IsValid)
     context.Result = new BadRequestObjectResult(context.ModelState);

No matter what I do the ModelState is always coming back as valid when I purposely omit the required properties. My controllers are marked as:

[Produces("application/json")]

The models are getting deserialized correctly and I have the models parameters in my action methods marked with [FromBody]. It just doesn't seem to be running any validation (standard or custom). I've looked at this answer and this one and several others but I just can't figure out what I'm missing. My APIs are protected with IdenityServer 4 so not sure if that plays into it but at this point I have to validate every action method myself which is not what I want to be doing. Anyone have suggestions?

user1015196
  • 617
  • 1
  • 7
  • 24
  • You have defined your Quantity as nullable short and also marked it as required so that is causing a conflict. Change Quantity to `short` datatype and then try. – Mohsin Mehmood Aug 04 '18 at 20:12
  • @MohsinMehmood appreciate your response but If you look at the links I provided and also look at the code when Json deserializes an integer it will deserialize it to a default value (0). Since that is a valid int value it would pass validation. Hence, why I make my required variables nullable. Regardless, if I change it to int still validates as valid. – user1015196 Aug 04 '18 at 23:01

2 Answers2

16

So my issue appears to be because I'm using services.AddMvcCore() and not services.AddMvc() I have to explicitly set .AddDataAnnotations() where this is baked into AddMvc. AddMvcCore() gives you bare bones and you add what you need where AddMvc gives you everything whether you need it or not. Hope this helps someone else.

services.AddMvcCore()
                .AddAuthorization()
                .AddJsonFormatters()
                .AddApiExplorer()
                .AddDataAnnotations()
                .AddMvcOptions(opt =>
                    opt.Filters.Add<RequestFilterAttribute>());
user1015196
  • 617
  • 1
  • 7
  • 24
0

For required field validation decorate your function with [BindRequired, Range(1, 10, ErrorMessage = "your error message")] instead of using [Required]

Samim Hussain
  • 396
  • 5
  • 14