OpenAPI definitions of a sample API with basic request validation - Amazon API Gateway
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

OpenAPI definitions of a sample API with basic request validation

The following OpenAPI definition defines a sample API with request validation enabled. The API is a subset of the PetStore API. It exposes a POST method to add a pet to the pets collection and a GET method to query pets by a specified type.

There are two request validators declared in the x-amazon-apigateway-request-validators map at the API level. The params-only validator is enabled on the API and inherited by the GET method. This validator allows API Gateway to verify that the required query parameter (q1) is included and not blank in the incoming request. The all validator is enabled on the POST method. This validator verifies that the required header parameter (h1) is set and not blank. It also verifies that the payload format adheres to the specified RequestBodyModel If there is no matching content type is found, request validation is not performed. When using a model to validate the body, if no matching content type is found, request validation is not performed. To use the same model regardless of the content type, specify $default as the key.

This model requires that the input JSON object contains the name, type, and price properties. The name property can be any string, type must be one of the specified enumeration fields (["dog", "cat", "fish"]), and price must range between 25 and 500. The id parameter is not required.

OpenAPI 2.0
{ "swagger": "2.0", "info": { "title": "ReqValidators Sample", "version": "1.0.0" }, "schemes": [ "https" ], "basePath": "/v1", "produces": [ "application/json" ], "x-amazon-apigateway-request-validators" : { "all" : { "validateRequestBody" : true, "validateRequestParameters" : true }, "params-only" : { "validateRequestBody" : false, "validateRequestParameters" : true } }, "x-amazon-apigateway-request-validator" : "params-only", "paths": { "/validation": { "post": { "x-amazon-apigateway-request-validator" : "all", "parameters": [ { "in": "header", "name": "h1", "required": true }, { "in": "body", "name": "RequestBodyModel", "required": true, "schema": { "$ref": "#/definitions/RequestBodyModel" } } ], "responses": { "200": { "schema": { "type": "array", "items": { "$ref": "#/definitions/Error" } }, "headers" : { "test-method-response-header" : { "type" : "string" } } } }, "security" : [{ "api_key" : [] }], "x-amazon-apigateway-auth" : { "type" : "none" }, "x-amazon-apigateway-integration" : { "type" : "http", "uri" : "http://petstore-demo-endpoint.execute-api.com/petstore/pets", "httpMethod" : "POST", "requestParameters": { "integration.request.header.custom_h1": "method.request.header.h1" }, "responses" : { "2\\d{2}" : { "statusCode" : "200" }, "default" : { "statusCode" : "400", "responseParameters" : { "method.response.header.test-method-response-header" : "'static value'" }, "responseTemplates" : { "application/json" : "json 400 response template", "application/xml" : "xml 400 response template" } } } } }, "get": { "parameters": [ { "name": "q1", "in": "query", "required": true } ], "responses": { "200": { "schema": { "type": "array", "items": { "$ref": "#/definitions/Error" } }, "headers" : { "test-method-response-header" : { "type" : "string" } } } }, "security" : [{ "api_key" : [] }], "x-amazon-apigateway-auth" : { "type" : "none" }, "x-amazon-apigateway-integration" : { "type" : "http", "uri" : "http://petstore-demo-endpoint.execute-api.com/petstore/pets", "httpMethod" : "GET", "requestParameters": { "integration.request.querystring.type": "method.request.querystring.q1" }, "responses" : { "2\\d{2}" : { "statusCode" : "200" }, "default" : { "statusCode" : "400", "responseParameters" : { "method.response.header.test-method-response-header" : "'static value'" }, "responseTemplates" : { "application/json" : "json 400 response template", "application/xml" : "xml 400 response template" } } } } } } }, "definitions": { "RequestBodyModel": { "type": "object", "properties": { "id": { "type": "integer" }, "type": { "type": "string", "enum": ["dog", "cat", "fish"] }, "name": { "type": "string" }, "price": { "type": "number", "minimum": 25, "maximum": 500 } }, "required": ["type", "name", "price"] }, "Error": { "type": "object", "properties": { } } } }