且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有没有办法只使用 Swagger 进行验证而不使用整个框架?

更新时间:2022-03-29 03:28:33

我认为没有什么可以单独完成,但您可以通过以下方式轻松完成:

I don't think there's anything ready to do this alone, but you can easily do this by the following:

  • Swagger Inflector 项目.您可以使用它来验证入站和出站负载
  • 为您的请求/响应定义分配架构部分.这意味着您需要将 JSON 架构的特定部分分配给您的操作
  • 为您的 API 创建一个过滤器以获取有效负载并使用架构
  • Grab the SchemaValidator from the Swagger Inflector project. You can use this to validate inbound and outbound payloads
  • Assign a schema portion to your request/response definitions. That means you'll need to assign a specific section of the JSON schema to your operations
  • Create a filter for your API to grab the payloads and use the schema

这将让您轻松查看负载是否与预期结构匹配.

That will let you easily see if the payloads match the expected structure.

当然,这一切都是使用 Inflector 自动为您完成的,但应该有足够的原始组件来帮助您在自己的实现中做到这一点

Of course, this is all done for you automatically with Inflector but there should be enough of the raw components to help you do this inside your own implementation