我使用 JSONschema 定义了一个模型并将其设置为 lambda。我可以看到模型被添加到请求正文中,如下图所示
但我还需要设置请求验证器来验证它。这是我下面的示例 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
Run Code Online (Sandbox Code Playgroud)
我可以看到,当您在 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>]
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
我多次阅读 Github 中的 SAM 规范文档并尝试设置请求验证器。但是我找不到任何方法来设置它与 SAM。有没有办法在方法上设置请求验证器,还是应该在 SAM 存储库中请求该功能?
感谢您阅读我的问题。
AWS::ApiGateway::Method
连接到您的Api
资源。RestApiId
ResourceId
下面是一个带有 RequestValidator 资源的示例。让我们首先添加一些新参数(contentHandling、validateRequestBody、validatorName)。然后是新资源。
Parameters:
...
Environment:
...
contentHandling:
Type: String
operationName:
Type: String
Default: testoperationName
validatorName:
Type: String
Default: testvalidatorName
validateRequestBody:
Type: String
Default: testvalidateRequestBody
Resources:
...
Method:
Type: AWS::ApiGateway::Method
Properties:
HttpMethod: POST
ResourceId: !GetAtt Api.RootResourceId
RestApiId: !Ref Api
...
RequestValidatorId: !Ref MyRequestValidator
OperationName: !Ref operationName
RequestValidator:
Type: AWS::ApiGateway::RequestValidator
Properties:
Name: !Ref validatorName
RestApiId: !Ref Api
ValidateRequestBody: !Ref validateRequestBody
ValidateRequestParameters: !Ref validateRequestParameters
Run Code Online (Sandbox Code Playgroud)
更多信息可以在这里找到:
AWS::ApiGateway::Method
关联的请求验证器示例(向下滚动以查看 json 后面的 yaml)AWS::ApiGateway::RequestValidator
文档 归档时间: |
|
查看次数: |
803 次 |
最近记录: |