Announcement

Collapse
No announcement yet.

How to make a field required for a post request in an API?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to make a field required for a post request in an API?

    Hello I have an create function in an entity and it have 2 field name, age
    So if i want to create a new record i have to fill both field, and it working correctly.
    But if i use postman and send the POST api with only name field and the result is still successful (Status 200)
    I'm curious to see if there is a solution to send Field required together in the API.

  • #2
    You would need to add a API schema validator to handle this.

    Comment


    • #3
      Originally posted by AgentT View Post
      You would need to add a API schema validator to handle this.
      Do you have document or example about API schema validator?

      Comment


      • #4
        We use the justinrainbow validator in our application.
        PHP implementation of JSON schema. Fork of the http://jsonschemaphpv.sourceforge.net/ project - GitHub - jsonrainbow/json-schema: PHP implementation of JSON schema. Fork of the http://jsonschemaph...


        Swaggest also does something similar is what I've heard.

        Comment


        • #5
          Originally posted by AgentT View Post
          We use the justinrainbow validator in our application.
          PHP implementation of JSON schema. Fork of the http://jsonschemaphpv.sourceforge.net/ project - GitHub - jsonrainbow/json-schema: PHP implementation of JSON schema. Fork of the http://jsonschemaph...


          Swaggest also does something similar is what I've heard.
          https://github.com/swaggest/php-json-schema
          Thanks for your reply. But i have a question how to integrate the justinrainbow validator into an api because i dont see any default api use it

          Comment


          • #6
            You will need to have API definitions in a json file and compare any API requests with that. Checkout the documentation that they have. It can get complicated. But it is an scalable solution where you can add any number of definitions and have it validated instantly.

            If your application is small with just a few APIs, then you can just manually compare the fields and send out errors if it doesn't fit your need. But this is not scalable.
            Like:
            Code:
            if(!$data->age){
                throw new BadRequest("Age not found in payload", 400);
            }

            Comment


            • #7
              Originally posted by AgentT View Post
              You will need to have API definitions in a json file and compare any API requests with that. Checkout the documentation that they have. It can get complicated. But it is an scalable solution where you can add any number of definitions and have it validated instantly.

              If your application is small with just a few APIs, then you can just manually compare the fields and send out errors if it doesn't fit your need. But this is not scalable.
              Like:
              Code:
              if(!$data->age){
              throw new BadRequest("Age not found in payload", 400);
              }

              Thank you very much. It solves my problem.

              Comment


              • #8
                Originally posted by AgentT View Post
                You will need to have API definitions in a json file and compare any API requests with that. Checkout the documentation that they have. It can get complicated. But it is an scalable solution where you can add any number of definitions and have it validated instantly.

                If your application is small with just a few APIs, then you can just manually compare the fields and send out errors if it doesn't fit your need. But this is not scalable.
                Like:
                Code:
                if(!$data->age){
                throw new BadRequest("Age not found in payload", 400);
                }
                Thanks it was helpful.
                Last edited by JohnnyBravo; 07-29-2022, 10:23 AM.

                Comment

                Working...
                X