Amazon web services 带可选字段的无服务器aws文档模型定义?
我想定义请求和响应模型。我使用AWS的无服务器框架,我看到的所有东西都推荐使用 自述文件说我需要在Amazon web services 带可选字段的无服务器aws文档模型定义?,amazon-web-services,aws-api-gateway,serverless-framework,serverless-plugins,Amazon Web Services,Aws Api Gateway,Serverless Framework,Serverless Plugins,我想定义请求和响应模型。我使用AWS的无服务器框架,我看到的所有东西都推荐使用 自述文件说我需要在custom.documentation.models.MODELNAME schema: ${file(models/error.json)} 但是他们没有一个models/error.json的示例文件作为基线 在实际示例中,它们的定义如下: - name: DoSomethingRequest contentType: "application/json" schema:
custom.documentation.models.MODELNAME
schema: ${file(models/error.json)}
但是他们没有一个models/error.json
的示例文件作为基线
在实际示例中,它们的定义如下:
-
name: DoSomethingRequest
contentType: "application/json"
schema:
type: array
items:
type: string
schema:
type: object
properties:
items:
type: array
items:
type: string
message:
type: string
statusCode:
type: number
-
name: HttpStatusResponse
contentType: "application/json"
schema:
type: object
properties:
serverResponse:
${self:custom.commonModelSchemaFragments.HttpResponse}
messageArray:
${self:custom.commonModelSchemaFragments.StringArrayFragment}
{
"type": "object",
"required": ["message"],
"properties": {
"optionalMessage": {
"type": "string"
},
"message": {
"type": "string"
}
}
}
这并没有提供足够的细节,我正在尝试做什么
我的目标是为字符串对象数组、消息和状态代码定义一个模式。不过,消息和状态代码是可选的。这些也可能是其他模型的一部分,如果可能的话,我不想对每个模型重复它们的定义 我目前的尝试是:
-
name: ReturnArrayResponse
contentType: "application/json"
schema:
type: array
itemsArray:
type: string
message:
type: string
statusCode:
type: number
我想这是我想要的,但是我怎么能让message
和statusCode
成为可选的,并在我的其他型号中重复这两项呢
我很乐意将一个yml解决方案放在我的serverless.yml文件中,或者放在我可以引用的json文件中。只是一个猜测(将其作为保留格式的答案发布)-模式中的顶级实体应该是对象,而不是数组,类似这样:
-
name: DoSomethingRequest
contentType: "application/json"
schema:
type: array
items:
type: string
schema:
type: object
properties:
items:
type: array
items:
type: string
message:
type: string
statusCode:
type: number
-
name: HttpStatusResponse
contentType: "application/json"
schema:
type: object
properties:
serverResponse:
${self:custom.commonModelSchemaFragments.HttpResponse}
messageArray:
${self:custom.commonModelSchemaFragments.StringArrayFragment}
{
"type": "object",
"required": ["message"],
"properties": {
"optionalMessage": {
"type": "string"
},
"message": {
"type": "string"
}
}
}
包括一个文件
在给出的示例中,error.json
可以包含任何有效的模式。所以像这样简单的事情很好:
{“类型”:“对象”,“属性”:{“消息”:{“类型”:“字符串”}}
还可以包括$schema
和title
等属性:
{
"$schema" : "http://json-schema.org/draft-04/schema#",
"title" : "Error Schema",
"type" : "object",
"properties" : {
"message" : { "type" : "string" },
"statusCode": { "type": "number" },
"itemsArray": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
当您已经在AWS中定义了模型,但没有无服务器yaml来构建它们时,这尤其方便。您只需从AWS控制台复制模式,将json粘贴到文件中,然后使用问题中提到的schema:${file()}
语法。据我所知,你可以让AWS控制台接受的任何东西都可以工作
干燥
我不知道如何在无服务器文件中引用其他模型中的模型,但您可以使用与插件作者相同的方法,只需将需要重用的任何东西放在模型
之外,放在更容易重用的地方。插件作者使用commonModelSchemaFragments
如果你有这样的片段:
commonModelSchemaFragments:
# defining common fragments means you can reference them with a single line
StringArrayFragment:
type: array
items:
type: string
HttpResponse:
type: object
properties:
message:
type: string
statusCode:
type: number
您可以在如下模型中引用这些片段:
-
name: DoSomethingRequest
contentType: "application/json"
schema:
type: array
items:
type: string
schema:
type: object
properties:
items:
type: array
items:
type: string
message:
type: string
statusCode:
type: number
-
name: HttpStatusResponse
contentType: "application/json"
schema:
type: object
properties:
serverResponse:
${self:custom.commonModelSchemaFragments.HttpResponse}
messageArray:
${self:custom.commonModelSchemaFragments.StringArrayFragment}
{
"type": "object",
"required": ["message"],
"properties": {
"optionalMessage": {
"type": "string"
},
"message": {
"type": "string"
}
}
}
标记属性可选
您可以通过将属性标记为必需
来实现这一点。只需提供一个所有属性的列表,除了那些您希望是可选的属性。其json模式如下所示:
-
name: DoSomethingRequest
contentType: "application/json"
schema:
type: array
items:
type: string
schema:
type: object
properties:
items:
type: array
items:
type: string
message:
type: string
statusCode:
type: number
-
name: HttpStatusResponse
contentType: "application/json"
schema:
type: object
properties:
serverResponse:
${self:custom.commonModelSchemaFragments.HttpResponse}
messageArray:
${self:custom.commonModelSchemaFragments.StringArrayFragment}
{
"type": "object",
"required": ["message"],
"properties": {
"optionalMessage": {
"type": "string"
},
"message": {
"type": "string"
}
}
}
您可以在无服务器文件中使用yaml来构建它:
-
name: OptionalResponse
contentType: "application/json"
schema:
type: object
required:
- "message"
properties:
message:
type: string
optionalMessage:
type: string
请求验证注意事项
标记属性必需
或可选
仅在打开请求正文验证时起作用:
我不知道如何使用任何特殊的无服务器语法打开请求验证。看起来您可以在参考资料
部分执行此操作,但我还没有尝试过 您可以使用插件(我写的)创建和启用请求验证器,因为目前还没有现成的功能