unit-testing - 当我还没有发布它时,我如何为我的 JSON 模式提供它的 $id 的绝对 URL 因为它还没有经过测试?

标签 unit-testing go jsonschema

我正在整理 JSON 模式,我想使用 $ref 来 DRY 我的模式。我将有许多模式,每个模式都使用公共(public)子模式。我想在发布它们之前对我的模式进行单元测试,方法是编写单元测试,断言在给定特定输入的情况下,输入被视为有效或无效,使用我相信是正确的 JSON 模式库(这样我只是测试我的模式,而不是图书馆)。

让我感到困惑的是,为了在我 Release模式之前加载它们(我想在本地运行测试和 CI/CD 期间这样做),我需要使用这样的相对本地路径:

"pet": { "$ref": "file://./schemas/components/pet.schema.json" }

那是因为该 pet 模式尚未发布到 URL。尚未通过自动化测试验证它是否正确。这对于运行测试来说效果很好,而且它也适用于在 Docker 镜像中打包,以便可以在应用程序启动时从磁盘加载模式。

但是,如果我在将顶级模式之一发布到绝对 URL 后将其提供给某人(利用 $ref),它不会加载到他们的程序中,因为我使用的路径有效仅用于我的单元测试。

我发现我必须使用绝对 URL 发布我的模式,以便它们在消费程序中使用。我最终发布了模式 https://mattwelke.github.io/go-jsonschema-ref-docker-example/schemas/person.1-0-0.schema.jsonhttps://mattwelke.github.io/go-jsonschema-ref-docker-example/schemas/components/pet.1-0-0.schema.json那样。我通过编写程序测试了它们在消费程序中工作正常:

package main

import (
    "fmt"

    "github.com/xeipuuv/gojsonschema"
)

func main() {
    schemaLoader := gojsonschema.NewReferenceLoader("https://mattwelke.github.io/go-jsonschema-ref-docker-example/schemas/person.1-0-0.schema.json")

    jsonStr := `
    {
        "name": "Matt",
        "pet": {
            "name": "Shady"
        }
    }
    `

    documentLoader := gojsonschema.NewStringLoader(jsonStr)

    result, err := gojsonschema.Validate(schemaLoader, documentLoader)
    if err != nil {
        panic(fmt.Errorf("could not validate: %w", err))
    }

    if result.Valid() {
        fmt.Printf("The document is valid.\n")
    } else {
        fmt.Printf("The document is not valid. See errors:\n")
        for _, desc := range result.Errors() {
            fmt.Printf("- %s\n", desc)
        }
    }
}

这导致了以下预期输出:

The document is valid.

所以我对这种“先有鸡还是先有蛋”的情况感到困惑。

我能够发布可以使用的模式,只要我在发布它们之前没有对它们进行单元测试。

只要满足以下条件,我就能够对模式进行单元测试:

  • 我不想以单元测试验证正确的形式发布它们。
  • 我同意我的应用程序在启动时通过 HTTPS 加载它们,而不是从磁盘加载它们。我很担心这一点,因为我不希望 Web 服务器成为我的应用程序启动的故障点。

对于如何实现这两个目标,我将不胜感激。

最佳答案

Where I get confused is that in order to load my schemas before I've published them (which I want to do while running tests locally and during CI/CD), I need to use relative local paths

您最初的假设是错误的。 $id 关键字中使用的 URI 可以是任意标识符——它们不需要在指定位置通过网络或磁盘解析。事实上,JSON Schema 实现假设在指定位置找到模式文档是错误的:它们必须支持能够在本地加载文档并将它们与指定的标识符相关联:

The "$id" keyword identifies a schema resource with its canonical URI.

Note that this URI is an identifier and not necessarily a network locator. In the case of a network-addressable URL, a schema need not be downloadable from its canonical URI.

source

A schema need not be downloadable from the address if it is a network-addressable URL, and implementations SHOULD NOT assume they should perform a network operation when they encounter a network-addressable URI.

source

因此,您可以为架构文档提供您喜欢的任何标识符,例如您最终发布架构供公众使用时预期使用的 URI,并使用该标识符执行本地测试。

任何不支持这样做的实现都违反了规范,这应该作为错误报告给它的维护者。

关于unit-testing - 当我还没有发布它时,我如何为我的 JSON 模式提供它的 $id 的绝对 URL 因为它还没有经过测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71374864/

相关文章:

unit-testing - 编码前测试

go - 如何覆盖 Go 中的符号链接(symbolic link)?

go - 等价于 C++ reinterpret_cast a void* to a struct in Golang

javascript - 使用 Electron 商店时来自 ajv 的严格模式警告

c# - 跳过通用测试类中特定类型的测试方法

php - 给 PHP 对象类型参数一个默认值

unit-testing - Angular 2 在不使用 Testbed 的情况下测试注入(inject) Http 的服务

go - 如何在 gorilla/mux 包中初始化 HandleFunc 中的变量

JSON Schema - 基于所选值的下拉列表其他下拉列表

javascript - 使用 AJV 解析无效对象的自定义架构