首页 > 解决方案 > 如何仅使用请求在没有插件的情况下验证 AWS 无服务器架构

问题描述

我正在尝试使用正文/有效负载和参数验证发布请求:

curl -X POST \
  [url]/api/{param1}/comment/{param2} \
  -H 'Content-Type: application/json' \
  -H 'cache-control: no-cache' \
  -d '{
        "bodyParam1": "45",
        "bodyParam2": 123123,
        "bodyParam3": 123
}
'

使用 node js 的无服务器框架。

这是我用 lambda 和 lambda-proxy 测试过的 serverless.yml,对于这两个我都无法验证参数:

 functions:
  someName:
    handler: src/comment.post
    events:
      - http:
          integration: lambda-proxy
          path: /api/{param1}/comment/{param2}
          method: post
          request:
            schema:
              application/json: ${file(./src/models/schema1.json)}
          cors: true

这是我的 schema1.json:

{
  "definitions" : {},
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "title" :"Get object",
  "required": [
    "bodyParam1",
    "bodyParam2",
    "bodyParam3"
  ],
  "properties": {
    "bodyParam1":
    {
      "type":
      "string"
    },
    "bodyParam2":
    {
      "type":
      "integer"
    },
    "bodyParam3":
    {
      "type":
      "integer"
    }
  }
}

请注意,这是对无服务器请求的本机支持,而不是插件版本。

标签: validationserverless-frameworkaws-serverless

解决方案


使用aws cli

aws cloudformation validate-template --template-body file://schema1.json

推荐阅读