Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Type excluded from validation triggers validation for Required members #2314

@kichalla

Description

@kichalla

For the following setup:

public class Customer
{
    public int Id { get; set; }

    [Required]
    [MaxLength(5)]
    public string Name { get; set; }

    public Address Address { get; set; }
}

public class Address
{
    [Required]
    [MaxLength(5)]
    public string City { get; set; }

    [Required]
    [MaxLength(5)]
    public string State { get; set; }
}

//-------------------------

services.ConfigureMvc(options =>
{
    options.ValidationExcludeFilters.Add(typeof(Address));
});

//-------------------------

public class CustomersController : Controller
{
    public IActionResult Create(Customer customer)
    {
        if(!ModelState.IsValid)
        {
            return HttpBadRequest(ModelState);
        }

        return Json(customer);
    }
}

Following are the behaviors:

  • Request: http://localhost:5001/Customers/Create?Id=10&Name=John
    Result: 200 OK response with body having {"Id":10,"Name":"John","Address":null}
  • Request: http://localhost:5001/Customers/Create?Id=10&Name=John&Address.City=Redmondddddddddddddddd
    Result: 400 Bad Request with error {"Address.State":["The State field is required."]}
  • Request: http://localhost:5001/Customers/Create
    Result: 400 Bad Request with error {"Name":["The Name field is required."]}
    NOTE: For this case I excluded the type Customer itself

Expected: For the 2nd case, the data should be bound successfully without any validation errors and for the 3rd case the model should be null without any validation errors?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions