Pet*_*ter 7 ruby json-api openapi
我有根据jsonapi.org指南构建的 HTTP 请求负载:
{
"data": {
"type": "employee",
"attributes": {
"firstName": "John",
"lastName": "Doe",
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有这个有效负载的OpenApi规范 (yaml):
employee:
type: object
required: [data]
properties:
data:
type: object
properties:
type:
type: string
enum: [employee]
attributes:
type: object
required: [firstName, lastName]
properties:
firstName:
type: string
example: 'John'
lastName:
type: string
example: 'Doe'
Run Code Online (Sandbox Code Playgroud)
我想要的是根据规范(来自 Ruby/Rails)验证有效负载。
存在openapi3_parser gem,但它似乎只能验证规范,而不能验证实际的有效负载。
然后,还有jsonapi.org 有效负载反序列化器,但它们似乎根本不关心 OpenApi 正式规范。
我使用 OpenApi 来描述有效负载,因为我通过 Swagger 免费获得 http API 文档。
它可以通过使用json-schema来实现,例如:
errors = JSON::Validator.fully_validate(schema, payload)
Run Code Online (Sandbox Code Playgroud)
(它适用于我的 json-schema gem 2.8.1,模式本身是用 openapi v 2.0 编写的)