我看了一下在SO上能找到的几个答案,但我还是没有任何运气。我正在使用Serverless,并试图使用api gateway lambda集成对一个lambda函数的请求体进行验证。在运行 sls offline
并使用postman进行POST请求,无论body是什么,请求都会成功。验证似乎根本没有发生。
我的情况是这样的...
serverless.yml。
service: onboard
# plugins
plugins:
- serverless-offline
provider:
name: aws
runtime: nodejs12.x
stage: dev
region: us-east-1
functions:
onboard:
handler: api/onboard.onboard
events:
- http:
path: onboard
method: post
integration: lambda
request:
passThrough: NEVER
schema:
application/json: ${file(models/onboard.schema.json)}
template:
application/json: '{ "body" : "$input.body" }'
apionboard.js
const onboard = async (event) => {
const response = {
message: event
};
return response;
};
exports.onboard = onboard;
modelsonboard.schema.json。
{
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "https://path.to/onboard",
"title": "title",
"description": "description",
"type": "object",
"properties": {
"environment": { "type": "string" },
"git": {
"type": "object",
"properties": {
"repo": {
"type": "string",
"format": "uri",
"pattern": ".git$"
},
"token": { "type": "string" }
},
"required": ["repo", "token"],
"maxProperties": 2
},
"name": { "type": "string" },
"team": {
"type": "string",
"pattern": "(?i)(^red$|^blue$|^green$|^yellow$|^black$|^white$)"
}
},
"additionalProperties": false,
"required": ["name", "team"]
}
这里有几个需要注意的地方。
Serverless-offline不支持请求验证。https:/github.comdheraultserverless-offlineissues369。
最好通过定义模拟响应在apigateway上单独测试。这将确保lambda响应(如果基于集成类型的不正确)不会造成任何误导。