Layer7 API Management

  • 1.  Json request validation using multiple schemas

    Posted Jul 06, 2017 05:42 AM

    Hi,

    Validate JSON request using Swagger document using multiple schema resources.

    Using assertion- "Validate Against Swagger Document", how can we validate body of the request using the same.

    For Example...

    Validation Structure would be like as follows

    { “validations”:

       [

         {“uripath”:  “/xy/ysas”,

          “methods”: [”POST”,”PUT”]

          “schema” : ”{ my schema1 }”

         },

        {“uripath”:  “/xy/{1}”,                               // dynamic part e.g. ID

           “methods”: [”POST”,”PUT”]

           “schema” : ”{ my schema2 }”

         },

             {“uripath”:  “/xy/{1}/abc”,                 // dynamic part e.g. ID + resource

           “methods”: [”POST”,”PUT”]

           “schema” : ”{ my schema3 }”

         },

           {“uripath”:  “/xy/{1}/abc”,                              

           “methods”: [”PATCH”]

           “schema” : ”{ my schema4 }”

       }

       ]

    }

     

    so for above structure, I have created a sample swagger json doc for POST Request only as

    {
    "swagger": "2.0",
    "info": {
    "title": "Player API",
    "description": "A simple API for Player resources",
    "version": "1.0.1",
    "contact": {
    "name": "test",
    "email": "apiteam@swagger.io"
    },
    "license": {
    "name": "Apache 2.0",
    "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
    }
    },
    "paths": {
    "/player/{id}": {
    "post": {
    "description": "Add a new player",
    "produces": [
    "application/json"
    ],
    "consumes": [
    "application/json"
    ],
    "parameters": [
    {
    "in": "body",
    "name": "Player",
    "required": true,
    "description": "The user to create.",
    "schema": {
    "$ref": "#/definitions/Player"
    }
    }
    ],
    "responses": {
    "200": {
    "description": "Succesfull!!!"
    },
    "500": {
    "description": "unexpected error"
    }
    }
    }
    }
    },
    "definitions": {
    "Player": {
    "type": "object",
    "properties": {
    "first_name": {
    "type": "string"
    },
    "last_name": {
    "type": "string"
    }
    },
    "required": [
    "first_name"
    ]
    }
    }
    }

     

    Result :  Validations against Path, method are being done successfully but request body is not being parsed. Here, as I marked "first_name" as mandatory but policy is parsed successfully without first_name in the request.

     

     

    Please advise how I can validate body of the request.

     

    Thanks!

    Vidhi



  • 2.  Re: Json request validation using multiple schemas
    Best Answer

    Posted Jul 07, 2017 09:48 AM

    I think the issue here is that the Gateway only supports JSON Schema v2 and the use of "required" was introduced in JSON Schema v4. See the following community post for information on using "optional" instead of "required" in your JSON schema...

     

    How to define schema using Validate JSON Schema Assertion?
     



  • 3.  Re: Json request validation using multiple schemas

    Posted Jul 10, 2017 12:33 AM

    Thanks Peterson for your response.

    But still Validation is successfully parsed for invalid request.

    Please find below SCHEMA Defintion and Sample Request ::::

    Here first_name : supposed to be mandatory but "swagger validatiuon assertion" is passed successfully for the request without first_name in the request.

     

    SCHEMA DEFINTION:

    {
     "swagger": "2.0",
     "info": {
      "title": "Player API",
      "description": "A simple API for Player resources",
      "version": "1.0.1",
      "contact": {
       "name": "test",
       "email": "apiteam@swagger.io"
      },
      "license": {
       "name": "Apache 2.0",
       "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
      }
     },
     "paths": {
      "/player/{id}": {
       "post": {
        "description": "Add a new player",
        "produces": [
         "application/json"
        ],
        "consumes": [
         "application/json"
        ],
        "Player": {
         "type": "object",
         "properties": {
          "first_name": {
           "type": "string"
          },
          "last_name": {
           "type": "string",
           "optional": true
          },
          "dateOfBirth": {
           "description": "Date of birth",
           "type": "string",
           "format": "date",
           "example": "1978-06-21T00:00:00.000Z",
           "optional": true
          }
         }
        },
        "responses": {
         "200": {
          "description": "Succesfull!!!"
         },
         "500": {
          "description": "unexpected error"
         }
        }
       }
      }
     }
    }

     

     

     

    Sample Request:

    {
       
        "last_name": "Doe",
         "date_of_birth": "1981-12-07T00:00:00.000Z"
    }

     

    Please provide your inputs.