【发布时间】:2020-10-09 05:28:32
【问题描述】:
我已经查看了我可以在 SO 上找到的几个答案,但我仍然没有任何运气。我正在使用无服务器并尝试使用 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" }'
api/onboard.js
const onboard = async (event) => {
const response = {
message: event
};
return response;
};
exports.onboard = onboard;
模型/onboard.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"]
}
【问题讨论】:
标签: aws-lambda aws-api-gateway jsonschema serverless-framework