In short you won't check the payload (seems it was your intent ?) but you'll let ASP.NET to deserialize this to your model and then you are just testing your model.
In short you won't check the payload (seems it was your intent ?) but you'll let ASP.NET to deserialize this to your model and then you are just testing your model.
I think I should send x-www-form-urlencoded in the body of Postman in order to validate model in the code.
Keep your friends close and your enemies even closer
No, it works as well with Json or XML. Extracting incoming data to populate the model and then validating the model are two connected but distinct and independant steps.
Or this you that posted as well about checking for example that the json payload doesn't contain additional properties that would be just ignored ??? (do you really have to validate the json payload rather than the model populated from this payload ?)
For now it seems the usual validation approach so IMO start with that and see what happens. Then if you have an issue explain the problem you have with the usual approach. I'll give this a try but with that you should be able to validate quite easily that
both CustomerID and password are provided and that you have at least one element in the referenceId and productCode arrays.
I'll see later today if I can create a quick test for that in case you really can't write the needed code from the documentation.
[Authorize]
[HttpPatch, Route("reconConfirmation")]
public async Task<IHttpActionResult> PatchReconciliationConfirmation(Recon recon)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
...
Here is my model entity:
public class Recon
{
public int id { get; set; }
[Required(ErrorMessage = "Please add CustomerID to the request.")]
public int customerID { get; set; }
[Required(ErrorMessage = "Please add Password to the request.")]
public string password { get; set; }
public string[] referenceId { get; set; }
public string[] productCode { get; set; }
public string companyToken { get; set; }
public DateTime? reconDateTime { get; set; } = DateTime.Now;
}
As I said I am sending x-www-form-urlencoded with Postman. But I will they sending raw JSON in postman and let you know.
Keep your friends close and your enemies even closer
Member
527 Points
2729 Posts
How to validate FromBody request data?
Apr 24, 2019 07:31 AM|cenk1536|LINK
Hello,
I wonder if there is a way to validate FromBody request data like model ModelState.IsValid?
Here is my sample request:
I would like to validate if there are customerID, password, referenceId and productCode in the request.
Best Regards.
All-Star
48570 Points
18079 Posts
Re: How to validate FromBody request data?
Apr 24, 2019 08:10 AM|PatriceSc|LINK
Hi,
You can use model validation as well in ASP.NET Web API : https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api plus maybe testing if referenceId or productCode have the expected content (needs at least one array element ?)
In short you won't check the payload (seems it was your intent ?) but you'll let ASP.NET to deserialize this to your model and then you are just testing your model.
Edit: wondered about writing a custom attribute for the array length but from a search it seems that https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.minlengthattribute?view=netframework-4.7.2 works for arrays as well in addition to strings.
Participant
850 Points
492 Posts
Re: How to validate FromBody request data?
Apr 24, 2019 12:49 PM|AddWeb Solution|LINK
Hi, cenk1536
Please refer below link hope you will solve your problem
https://stackoverflow.com/questions/47599983/validating-httprequestmessage-body
https://stackoverflow.com/questions/45495432/asp-net-core-mvc-mixed-route-frombody-model-binding-validation?rq=1
Member
527 Points
2729 Posts
Re: How to validate FromBody request data?
May 07, 2019 02:01 PM|cenk1536|LINK
I think I should send x-www-form-urlencoded in the body of Postman in order to validate model in the code.
All-Star
48570 Points
18079 Posts
Re: How to validate FromBody request data?
May 07, 2019 02:18 PM|PatriceSc|LINK
No, it works as well with Json or XML. Extracting incoming data to populate the model and then validating the model are two connected but distinct and independant steps.
Or this you that posted as well about checking for example that the json payload doesn't contain additional properties that would be just ignored ??? (do you really have to validate the json payload rather than the model populated from this payload ?)
For now it seems the usual validation approach so IMO start with that and see what happens. Then if you have an issue explain the problem you have with the usual approach. I'll give this a try but with that you should be able to validate quite easily that both CustomerID and password are provided and that you have at least one element in the referenceId and productCode arrays.
I'll see later today if I can create a quick test for that in case you really can't write the needed code from the documentation.
Member
527 Points
2729 Posts
Re: How to validate FromBody request data?
May 07, 2019 02:30 PM|cenk1536|LINK
I am validating as follows:
Here is my model entity:
As I said I am sending x-www-form-urlencoded with Postman. But I will they sending raw JSON in postman and let you know.
All-Star
48570 Points
18079 Posts
Re: How to validate FromBody request data?
May 07, 2019 02:50 PM|PatriceSc|LINK
And so try as well something such as :
[MinLength(Length=1)]
public string[] referenceId { get; set; }
if you want to validate that the length of your array is at least 1. IMHO it should just work.
Member
527 Points
2729 Posts
Re: How to validate FromBody request data?
May 07, 2019 06:26 PM|cenk1536|LINK
thank you they all worked.