Validating request path parameter of AWS API gateway?

限于喜欢 提交于 2019-11-30 15:47:30

问题


Let's say I have an api with paths / and /{pets} and /{pets}/pet. Now I'm trying to validate the path {pets} parameter so that only path having alphanumerical characters of length 6 will be validated and processed to the backend lambda all others will be rejected. I tried the following swagger schema specifying format and type for the parameter. I even tried using pattern in the schema but it seems to be not working. May I know how can we limit only path parameters of certain IDs.

{
  ......
  "/{pets}/pet": {
      "get": {
          "consumes": [
              "application/json"
          ],
          "produces": [
              "application/json"
          ],
          "parameters": [{
              "name": "pets",
              "in": "path",
              "required": true,
              "schema": {
                  "type": "string",
                  "pattern": "[A-Za-z0-9]{6}"
              }
          }],
          "responses": {
              "200": {
                  "description": "200 response",
                  "schema": {
                      "$ref": "#/definitions/Empty"
                  }
              }
          },
          "x-amazon-apigateway-request-validator": "Validate query string parameters and headers",
          "x-amazon-apigateway-integration": {
              "responses": {
                  "default": {
                      "statusCode": "200"
                  }
              },
              "requestTemplates": {
                  "application/json": "##  See ...."
              },
              "uri": "........",
              "passthroughBehavior": "when_no_templates",
              "httpMethod": "POST",
              "contentHandling": "CONVERT_TO_TEXT",
              "type": "aws"
          }
      }
  }
  .....
}

What I'm trying to achieve:

https://api.example.com/aacc77/pet -- Accept
https://api.example.com/aacc77s/pet -- Reject
https://api.example.com/aacc7/pet -- Reject
https://api.example.com/aacc_7/pet -- Reject

Basically, I want to use this regex pattern for the path [A-Za-z0-9]{6}.

"pets": {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Pets Schema",
  "type": "string",
  "pattern": "[A-Za-z0-9]{6}"
}

I see that there is a related question here, I wonder whether there is a solution available for this.

来源:https://stackoverflow.com/questions/47194894/validating-request-path-parameter-of-aws-api-gateway

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!