AnsweredAssumed Answered

Json request validation using multiple schemas

Question asked by vidhi.2112 on Jul 6, 2017
Latest reply on Jul 10, 2017 by vidhi.2112

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

Outcomes