小编rDe*_*Dev的帖子

AWS API Gateway Serverless 验证请求正文并返回错误消息作为响应

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

我想让它验证传入该项目中设置的各种 Lambda 函数的请求主体。应通过将收到的正文与关联的 JSON 模式文件进行比较来完成此验证。在请求格式不正确的情况下,我希望响应指出哪些属性丢失或类型错误。

我找到了serverless-reqvalidator-plugin来解决针对模式进行验证的问题。验证项在资源中进行描述,架构使用serverless-aws-documentation 插件与自定义部分中的验证项相关联,最后使用 reqValidatorName 属性将函数与函数部分中的验证对象相关联。 http 事件。理想情况下,在这里的某个地方,我想设置在发生错误时应在响应中返回什么消息。

// 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 …
Run Code Online (Sandbox Code Playgroud)

aws-lambda aws-api-gateway aws-serverless

4
推荐指数
1
解决办法
4779
查看次数