I have made a controller for a REST POST api endpoint, is as following
public IActionResult POST([FromBody]Person person)
{
....
}
and where Person is defined as
public class Person
{
public string Name {get; set;}
public int Age {get; set;}
}
meaning that the post request is able deserialise an input like this
{
"Name": "Peter",
"Age": 2
}
without any problems...
Problems occurs though when I pass something like
{
"Name": "Peter",
"Age": 2,
"Error": 123123123
}
It still creates an Person which have the first two filled out..
I would like to trigger an error here stating that the input format is wrong.
I am storing the first input - and then use it to compare the next input based on the first one. If they are the same - nothing should happen.
But since the last input contains a invalid field but the actual instance the input created is completely similar to the first one - I get an error on they are not equal?
I assume the error statement in the JSON is somehow stored in the new instance?
but how do i make sure that the input is being validated before the controller created an instance given the JSON input?