locked
How to validate FromBody request data? RRS feed

  • Question

  • User-1104215994 posted

    Hello,

    I wonder if there is a way to validate FromBody request data like model ModelState.IsValid?

    Here is my sample request:

    {
        "customerID":"1",
        "password":"1234",
        "referenceId":["46300000460", "46300000461","46300000463", "46600000466"],
        "productCode":["000000006446", "000000006446","000000006446"]
    }

    I would like to validate if there are <g class="gr_ gr_10 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="10" data-gr-id="10">customerID</g>, password, <g class="gr_ gr_11 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="11" data-gr-id="11">referenceId</g> and <g class="gr_ gr_12 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="12" data-gr-id="12">productCode</g> in the request.

    Best Regards.

    Wednesday, April 24, 2019 7:31 AM

Answers

  • User753101303 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 7, 2019 2:50 PM

All replies

  • User753101303 posted

    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.

    Wednesday, April 24, 2019 8:10 AM
  • User-1104215994 posted

    <g class="gr_ gr_5 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="5" data-gr-id="5">In  short</g> you won't check the payload (seems it was your intent ?) but you'll let ASP.NET to deserialize this to your model and <g class="gr_ gr_6 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="6" data-gr-id="6">then  you</g> are just testing your model.

    I think I should send x-www-form-<g class="gr_ gr_61 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="61" data-gr-id="61">urlencoded</g> in the body of Postman in order to validate <g class="gr_ gr_158 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="158" data-gr-id="158">model</g> in the code.

    Tuesday, May 7, 2019 2:01 PM
  • User753101303 posted

    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.

    Tuesday, May 7, 2019 2:18 PM
  • User-1104215994 posted

    I am validating as follows:

    [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-<g class="gr_ gr_128 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="128" data-gr-id="128">urlencoded</g> with Postman. But I will they sending raw JSON in postman and let you know.

    Tuesday, May 7, 2019 2:30 PM
  • User753101303 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 7, 2019 2:50 PM
  • User-1104215994 posted

    thank you they all worked.

    Tuesday, May 7, 2019 6:26 PM