- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我想以与此处类似的方式验证 openapi 规范:http://bigstickcarpet.com/swagger-parser/www/index.html但不同之处在于,我将使用 GO 来编写该工具的代码,而且它只是一个 CLI。
我正在尝试使用它:
https://github.com/go-openapi/validate
但主要问题是文档几乎不存在。我来这里是为了寻求以前可能使用过这个库的人的帮助,并且可以给我一个最小的例子,发送一个包含这样的规范的文件,并让这个库以类似于在线 Swagger 验证器的方式抛出所有错误或警告.
我已经可以读取文件并对其中的字段进行一些手动验证,但这当然不是我需要做的,它只是一个示例。
此外,作为次要问题,我想在他们的 GitHub 存储库上发布同样的问题,但我得到了这个:
而且我不知道如何“查看”这些指南以便发布我的问题。
func validate_spec(spec string) []validator_error {
// RULES HERE. Now I am hardcoding since this is just a dummy app. On the real app we will need to use goapenapi plus a schema validator
var errors []validator_error
name_regex, _ := regexp.Compile("^[a-zA-Z]+[ ][a-zA-Z]+")
// Validate _.name field
if ( ! gjson.Get(spec, "name").Exists() ) {
n := validator_error{Path: "_.name", Message: "Does not exist!"}
errors = append(errors,n)
}
if gjson.Get(spec, "name").Exists() {
if _, ok := gjson.Get(spec, "name").Value().(string); !ok {
n := validator_error{Path: "_.name", Message: "should be a string"}
errors = append(errors,n)
}
if ( ! name_regex.MatchString(gjson.Get(spec, "name").String() ) ) {
n := validator_error{Path: "_.name", Message: "should match " + name_regex.String()}
errors = append(errors,n)
}
}
// ***************************
// Validate _.age field
if ( ! gjson.Get(spec, "age").Exists() ) {
n := validator_error{Path: "_.age", Message: "Does not exist!"}
errors = append(errors,n)
}
if gjson.Get(spec, "age").Exists() {
if _, ok := gjson.Get(spec, "age").Value().(float64); !ok {
n := validator_error{Path: "_.age", Message: "should be an int"}
errors = append(errors,n)
}
}
// ***************************
return errors
}
func validate_spec(spec string) []validator_error {
// Something like this is what I am looking for. On the above example I am just hard-coding some dummy rules. I need to use the library here to get the validity of the spec being passed.
return goopenapi.validate(spec )
}
最佳答案
我使用 https://github.com/go-openapi很多,发现这些包对于处理 OpenAPI 规范、验证和其他相关内容非常有用。
看看下面的代码:
document, err = loads.Spec(fpath)
if err != nil {
return nil, errors.Wrap(err, "Failed to load spec")
}
document, err = document.Expanded(&spec.ExpandOptions{RelativeBase: fpath})
if err != nil {
return nil, errors.Wrap(err, "Failed to expand spec")
}
if err := validate.Spec(document, strfmt.Default); err != nil {
return nil, errors.Wrap(err, "Spec is invalid")
}
首先,它加载规范。然后它扩展该规范中的所有引用 ($ref
-s)。之后,它会验证规范本身。
所以规范本身是正确的。例如,现在我们要根据该规范验证请求主体。
// sch here is the schema object that can be extracted from
// the spec that you created above.
// data is just an interface{} that represents your data
// structure that you need to validate. data is a struct
// you decoded a json body into like json.Unmarshal(b, &data)
err := validate.AgainstSchema(sch, data, strfmt.Default)
ve, ok := err.(*errors.CompositeError)
// now you can extract errors from ve.Errors
我构建了 some wrappers围绕它以便于请求验证,例如:
// op here is the OpenAPI operation object that can also be extracted
// from the spec loaded above.
if errs := validate.Body(op.Parameters, body); len(errs) > 0 {
// work with errs
}
免责声明:上面的一些链接指向存储库 oas2我是作者和维护者。该存储库建立在 go-openapi 之上我不是作者。
关于go - Golang 中的 OpenAPI 规范验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48752908/
我们即将为我的客户端实现一个服务 API,它由许多服务组成,比如说 ServiceA、ServiceB 和 ServiceC。每个服务都可以随着时间的推移(独立地)引入新版本,而旧版本仍然存在。所以我
我正在使用 OpenAPI 3 并有两个查询参数,其中至少一个是必需的,但哪个无关紧要。 即,作为sudocode: if parameter_1 OR parameter_2: do stu
为了生成客户端库,我会在构建时获取 Quarkus 生成的 openapi yml 文件。 目前我发现获取它的唯一方法是运行服务器并从/q/openapi 端点获取它,但在这个过程中必须运行服务器只是
假设我有一个描述 API Foo 的 OpenAPI 3 文档,如下所示: openapi: 3.0.0 info: version: '1' title: Foo paths: /foo:
我正在使用 FastAPI,它允许 pattern=re.compile("(?P[42a-z]+)...") . https://editor.swagger.io/显示此模式的错误。 我的猜测是
我有一个 Spring boot Gradle 项目,我想获取它的 OpenAPI 规范 YAML 文件。 据我了解官方swagger-core不支持 Spring boot 项目,所以我找到了 sp
我正在尝试记录包含各种身份验证是可选的端点的现有 API。也就是说,如果用户获得授权,则返回的数据比未授权时返回的数据多。 无法在 OAspec v3 中明确找到。是否有编码技巧来定义这种情况? 我目
Here它说我可以引用另一个文件中单个路径的定义,但该示例似乎引用了整个文件,而不是 paths 下的单个路径定义。目的。如何在另一个文件的 paths 中分配单个路径目的? 例如,我有 Anothe
我在 SwaggerHub 注册并使用 OpenAPI 3.0 创建了一个新 API。在我的 API 中,/tasks path 有 2 个非必需参数,但我无法将它们设置为不需要 - 编辑器显示“不允
在 OpenAPI 3.0 Specification ,根OpenAPI Object有 servers属性是 Server Objects 的数组.和 Path Item Object还允许可选的
我想要做的是指定有时对 API 调用的响应可能是 PDF 文档,有时是 JSON。我想以 OpenAPI 3.0 格式执行此操作。对于 PDF,响应将如下所示: responses:
我正在编写一个返回 MP3 文件的 API YAML。我不熟悉多媒体响应。在通过 Google 时,我发现我可以使用 audio/mp3 内容类型。但我找不到任何例子来说明如何去做。我应该如何处理这种
我正在寻找一个 JS 库(最好能在浏览器中使用)来: 根据 OpenAPI 3.0 架构(YAML 或 JSON)检查特定的 JSON 负载(通常是来自 API 的响应) 使用从 OpenAPI 3.
我正在构建一个简单的 OpenAPI 3 YAML 规范,如下所示: paths: /query: get: parameters: - $ref: '#/co
与 draft-07 相比,它定义了: { "type": ["object", "boolean"], "properties": { ... "$r
我正在尝试使用 OpenAPI 3、YAML 来构建 API 定义。 我一直在使用文档 https://swagger.io/docs/specification/adding-examples/关于
我有一个 Open API 3 规范的 yaml 文件,它有一些 x- 前缀的属性。我正在尝试使用 openapi-generator-cli 生成一个 Angular Typescript SDK。
我目前正在按照 Oracle 本身的指南和教程构建基于 Helidon Microprofile 的微服务,但在使用注释时遇到了与“自动 OpenAPI 规范生成器”相关的问题。 我的 POM 包含
我必须从头开始创建一个rest api。通过手动完成大部分工作,我已经对 Jersey 有了一些经验。 我想现在就做,因为这个项目是新的。因此,我目前正在尝试每次尝试 openapi 3.0 在线编辑
我想将以下 JSON 表示为 schema在 OpenAPI 3.0 API 定义中: { get-question: { question-id:string } } 到目前为止,我已经写了
我是一名优秀的程序员,十分优秀!