为 AWS::Serverless::Api GET 查询参数添加高级验证

问题描述 投票:0回答:1

我正在尝试向

AWS::Serverless::Api
GET
请求、查询参数添加“高级”验证。

minimal
Int
参数的值:
{page}
应为 1。或枚举检查值

但是 Api 网关不处理此类验证,仅检查参数是否设置。

如何做到这一点?

我在 aws 文档中仅找到有关

body
验证(例如创建模型)的信息。但是,它不适用于查询参数

云信息配置

  SearchApi:
    Type: AWS::Serverless::Api
    Properties:
      Name: !Sub ${AWS::StackName}
      StageName: !Ref StageName
      DefinitionBody:
        openapi: "3.0.1"
        info: {}
        paths:
          /api/search:
            get:
              parameters:
                - name: text
                  in: "query"
                  required: true
                  schema:
                    type: "string"

                - name: page
                  in: "query"
                  required: true
                  schema:
                    type: integer
                    minimum: 1

                - name: hitsPerPage
                  in: query
                  required: true
                  schema:
                    type: integer
                    minimum: 1

                - name: subscription
                  in: query
                  required: true
                  schema:
                    type: string
                    enum: [PLUS, FREE, HD]
                    
                - name: distributionTenant
                  in: query
                  required: true
                  schema:
                    type: "string"
                    minLength: 4
  
              responses:
                "200":
                  description: "200 response"
                  headers:
                    Access-Control-Allow-Origin:
                      schema:
                        type: "string"
                    Access-Control-Allow-Methods:
                      schema:
                        type: "string"
                    Access-Control-Allow-Credentials:
                      schema:
                        type: "string"
                    Access-Control-Allow-Headers:
                      schema:
                        type: "string"
                  content: {}
              x-amazon-apigateway-request-validator: "params-only"
              x-amazon-apigateway-integration:
                credentials: !Ref APIGatewayRole
                httpMethod: "POST"
                uri:
                  Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${SearchFunction.Arn}/invocations"
                passthroughBehavior: "when_no_match"
                type: "aws_proxy"

        x-amazon-apigateway-request-validators:
          params-only:
            validateRequestParameters: true
            validateRequestBody: false
amazon-web-services aws-api-gateway aws-serverless aws-sam
1个回答
0
投票

来自官方 aws qa:

不幸的是,我们没有选项对传入请求的标头和查询字符串参数进行高级验证。但是,在请求正文的情况下,我们可以利用 JSON 模式模型来验证有效负载结构。

链接:https://repost.aws/questions/QUITruD8XeQN-dsmMU-6NqYQ#ANqyHGhMsvSH2Ib_fUvQd-Xw

© www.soinside.com 2019 - 2024. All rights reserved.