Aws lambda AWS API网关无服务器验证请求正文并在响应中返回错误消息

Aws lambda AWS API网关无服务器验证请求正文并在响应中返回错误消息,aws-lambda,aws-api-gateway,aws-serverless,Aws Lambda,Aws Api Gateway,Aws Serverless,我正在使用设置AWS API网关。理想情况下,不应使用AWS控制台配置任何内容,只应使用无服务器进行部署 我想让它验证进入各种Lambda函数的请求体,这些函数也在这个项目中设置。此验证应通过将收到的正文与关联的JSON模式文件进行比较来完成。对于格式不正确的请求,我希望响应指示缺少哪些属性或键入的属性不正确 我找到了解决根据模式进行验证问题的方法。验证项在资源中描述,模式使用与自定义节中的验证项相关联,然后在http事件中使用reqValidatorName属性将函数与功能节中的验证对象相关联

我正在使用设置AWS API网关。理想情况下,不应使用AWS控制台配置任何内容,只应使用无服务器进行部署

我想让它验证进入各种Lambda函数的请求体,这些函数也在这个项目中设置。此验证应通过将收到的正文与关联的JSON模式文件进行比较来完成。对于格式不正确的请求,我希望响应指示缺少哪些属性或键入的属性不正确

我找到了解决根据模式进行验证问题的方法。验证项在资源中描述,模式使用与自定义节中的验证项相关联,然后在http事件中使用reqValidatorName属性将函数与功能节中的验证对象相关联。理想情况下,在这里的某个地方,我想设置在出现错误时响应中应该返回什么消息

// serverless.yml

resources:
   Resources:
      BodyValidation:  
         Type: "AWS::ApiGateway::RequestValidator"
         Properties:
           Name: 'BodyValidation'
           RestApiId: 
             Ref: ApiGatewayRestApi
           ValidateRequestBody: true
           ValidateRequestParameters: false
           /// Ideally, some property about how to dynamically return an error message for invalid requests would be here, or bellow

custom:
  documentation:
// --- cut out api info
    models:
      - name: BodyValidationRequest
        contentType: "application/json"
        schema: ${file(./SampleValidationRequest.json)}

functions:
   SampleValidationFunction:
      //--- cut handler info
       events: 
         - http: 
              //--- cut path and method info
              reqValidatorName: BodyValidation
              documentation: // --- cut documentation info


我这样做是为了回答一个类似的问题。我希望在不依赖reqvalidator插件的情况下实现这一点,但在AWS API网关文档中,它没有显示如何使用无服务器实现这一点

看起来是这样,但似乎是这样。但是,我找不到任何严格使用serverless.yml的示例

我正在AWS控制台和Postman中测试请求。举个例子,下面是我正在测试的一个模式:

// SampleValidationRequest.json

{
  "type": "object",
  "properties" :{
    "name": {
      "type": "string"
    },
    "id": {
      "type": "number"
    }
  },
  "required": ["name", "id"]
}
当发送缺少参数的正文时,例如id为:

{
    "name": "test"
}
我收到了回复

{
  "message": "Invalid request body"
}
我希望它说的是我在AWS中查看日志时看到的内容

{
  "message": "Request body does not match model schema for content type application/json: [object has missing required properties (["id"])]
}
因此,总结一下:

1。是否可以添加一些属性来动态解释响应消息中请求正文的错误?

2。是否可以仅使用AWS API的Serverless设置,而不是使用reqvalidator,根据模式验证请求正文?

  • 是否可以添加一些属性来动态地解释响应消息中请求主体的错误
  • 在某种程度上。请参阅此线程:

    您需要向
    serverless.yml
    添加
    resource
    属性,如下所示:

    resources:
      Resources:
        ApiGatewayRestApi:
          Type: AWS::ApiGateway::RestApi
          Properties:
            Name: ${self:provider.stage}-${self:service}
        GatewayResponseResourceNotFound:
          Type: 'AWS::ApiGateway::GatewayResponse'
          Properties:
            RestApiId:
              Ref: 'ApiGatewayRestApi'
            ResponseType: BAD_REQUEST_BODY
            "StatusCode" : "422"
            ResponseTemplates:
              application/json: "{\"message\": \"$context.error.message\", \"error\": \"$context.error.validationErrorString\"}"
    
  • 是否可以仅使用AWS API的Serverless设置,而不是使用reqvalidator,根据模式验证请求主体
  • 是的,这是可能的。请确保更新到Serverless的最新版本,否则它将自动无法生成API网关模型

    以下是一个适合我的设置:

        events:
          - http:
              path: my_method/
              method: post
              request:
                schema:
                  application/json: ${file(validate_my_method.json)}
    

    你明白了吗?不,很不幸。