【发布时间】:2020-05-01 19:24:48
【问题描述】:
我使用 JSONschema 定义了一个模型并将其设置为 lambda。我可以看到模型已添加到请求正文中,如下图所示
但我还需要设置 Request Validator 来验证它。这是我下面的示例 AWS SAM 模板。
Resources:
Api:
Type: "AWS::Serverless::Api"
Properties:
StageName: !Ref Environment
TracingEnabled: false
EndpointConfiguration: REGIONAL
Auth:
Authorizers:
Auth:
FunctionPayloadType: TOKEN
FunctionArn: !GetAtt Auth.Arn
Identity:
Header: authorization
Models:
RegisterCat:
$schema: "http://json-schema.org/draft-04/hyper-schema#"
title: RegisterCat
type: object
properties:
name:
type: string
maxLength: 32
species:
type: string
maxLength: 32
age:
type: integer
minimum: 0
maximum: 100
required:
- name
- species
- age
RegisterCat:
Type: "AWS::Serverless::Function"
Properties:
FunctionName: !Join ["-", [example, !Ref Environment, register, cat]]
CodeUri: register_cat/
Environment:
Variables:
TABLE_NAME: !Join ["-", [!Ref Environment, cat, table]]
Policies:
- Statement:
- Sid: CatTable
Effect: Allow
Action:
- "dynamodb:PutItem"
Resource: !GetAtt CatTable.Arn
Events:
PublicApi:
Type: Api
Properties:
Path: /cat/
Method: POST
RestApiId: !Ref Api
RequestModel:
Model: RegisterCat
Required: true
我可以看到,当您在 aws cli 或 Cloudformation 中创建方法时,可以选择添加请求验证器
put-method
--rest-api-id <value>
--resource-id <value>
--http-method <value>
--authorization-type <value>
[--authorizer-id <value>]
[--api-key-required | --no-api-key-required]
[--operation-name <value>]
[--request-parameters <value>]
[--request-models <value>]
[--request-validator-id <value>]
[--authorization-scopes <value>]
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]
Type: AWS::ApiGateway::Method
Properties:
ApiKeyRequired: Boolean
AuthorizationScopes:
- String
AuthorizationType: String
AuthorizerId: String
HttpMethod: String
Integration:
Integration
MethodResponses:
- MethodResponse
OperationName: String
RequestModels:
Key : Value
RequestParameters:
Key : Value
RequestValidatorId: String
ResourceId: String
RestApiId: String
我在 Github 中多次阅读 SAM 规范文档并尝试设置请求验证器。但是我找不到任何方法来使用 SAM 进行设置。有没有办法在方法上设置请求验证器,或者我应该在 SAM repo 中请求该功能?
感谢您阅读我的问题。
【问题讨论】:
标签: aws-sam