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

Type excluded from validation triggers validation for Required members #2314

Closed
kichalla opened this issue Apr 1, 2015 · 4 comments
Closed

Comments

@kichalla
Copy link
Member

kichalla commented Apr 1, 2015

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?

@danroth27 danroth27 added this to the 6.0.0-beta5 milestone Apr 2, 2015
@Eilon Eilon removed this from the 6.0.0-beta5 milestone Apr 24, 2015
@Eilon Eilon added this to the 6.0.0-beta5 milestone May 8, 2015
@Eilon
Copy link
Member

Eilon commented May 8, 2015

Seemingly mildly approximately related to #2316

@Eilon
Copy link
Member

Eilon commented May 18, 2015

See if your Required change fixes this. If not, move to beta6.

@ajaybhargavb
Copy link
Contributor

I tried these scenarios.
Looks like case 2 is now working as expected.
Request: http://localhost:5001/Customers/Create?Id=10&Name=John&Address.City=Redmondddddddddddddddd
Result then: 400 Bad Request with error {"Address.State":["The State field is required."]}
Result now: 200 OK response with body {"Id":10,"Name":"John","Address":{"City":"Redmondddddddddddddddd","State":null}}

Whereas case 3 is still the same. (Added Customer type to exclude filter)
Request: http://localhost:5001/Customers/Create
Result: 400 Bad Request with error {"Name":["The Name field is required."]}

@danroth27 danroth27 modified the milestones: 6.0.0-beta6, 6.0.0-beta5 May 26, 2015
@ajaybhargavb
Copy link
Contributor

Fixed in d0927bd

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants