编写依赖于其他键的更复杂的 json 模式

发布于 2024-10-17 17:14:26 字数 531 浏览 5 评论 0原文

我一直在编写简单的 JSON 模式,但遇到了一个稍微复杂一点的 API 输入调用。我有一条安静的最终路线,可以采用 3 种不同类型的 JSON:

localhost/foo

可以采用:

{ "type" : "ice_cream", "cone" : "waffle" ...}

{"type" : "hot_dog ", "bun" : "wheat" ...}

如果“type”键包含“ice_cream”,我只想看到键“cone”而不是键“bun”。同样,如果“type”包含“hot_dog”,我只想看到“bun”而不是“cone”。我知道我可以进行模式匹配以确保我只看到类型“ice_cream”或类型“hot_dog”,但我不知道如果该键设置为该值,如何强制要求某些其他字段。我看到有一个名为“dependency”的 json 模式字段,但我还没有找到任何关于如何使用它的好示例。

顺便说一句,我不确定这个输入 JSON 是否是好的形式(有效地重载了它所采用的 JSON 结构类型),但我没有更改 api 的选项。

I've been writing simple JSON schemas but I ran into an API input call that is a bit more complex. I have one restful end route that can take 3 very different types of JSON:

localhost/foo

can take:

{ "type" : "ice_cream", "cone" : "waffle" ...}

or

{"type" : "hot_dog", "bun" : "wheat" ...}

If the "type" key contains "ice_cream", I only ever want to see the key "cone" and not the key "bun". Similiarly if "type" contains "hot_dog" I only want to see "bun" and not "cone". I know I can pattern match to make sure I only ever see type "ice_cream" or type "hot_dog", but I don't know how to force the requirement of certain other fields if that key is set to that value. I see that there is a json schema field called "dependency" but I haven't found any good examples on how to use it.

BTW, I'm not sure if this input JSON is good form (overloading the type of JSON structure it takes, effectively), but I don't have the option of changing the api.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹倦 2024-10-24 17:14:26

我终于得到了一些关于此的信息 - 事实证明,您可以将几个有效的不同对象联合起来,如下所示:

{
    "description" : "Food",
    "type" : [
        {
            "type" : "object",
            "additionalProperties" : false,
            "properties" : {
                "type" : {
                    "type" : "string",
                    "required" : true,
                    "enum": [
                        "hot_dog"
                    ]
                },
                "bun" : {
                    "type" : "string",
                    "required" : true 
                },
                "ketchup" : {
                    "type" : "string",
                    "required" : true 
                } 
            } 
        },
        {
            "type" : "object",
            "additionalProperties" : false,
            "properties" : {
                "type" : {
                    "type" : "string",
                    "required" : true,
                    "enum": [
                        "ice_cream"
                    ]
                },
                "cone" : {
                    "type" : "string",
                    "required" : true 
                },
                "chocolate_sauce" : {
                    "type" : "string",
                    "required" : true 
                } 
            } 
        }
    ]
}

我仍然不确定这是否是有效的 JSON,因为我的 Schemavalidator 因某些无效输入而死亡,但它接受预期的有效输入。

I finally got some information about this - it turns out you can make a union of several different objects that are valid like so:

{
    "description" : "Food",
    "type" : [
        {
            "type" : "object",
            "additionalProperties" : false,
            "properties" : {
                "type" : {
                    "type" : "string",
                    "required" : true,
                    "enum": [
                        "hot_dog"
                    ]
                },
                "bun" : {
                    "type" : "string",
                    "required" : true 
                },
                "ketchup" : {
                    "type" : "string",
                    "required" : true 
                } 
            } 
        },
        {
            "type" : "object",
            "additionalProperties" : false,
            "properties" : {
                "type" : {
                    "type" : "string",
                    "required" : true,
                    "enum": [
                        "ice_cream"
                    ]
                },
                "cone" : {
                    "type" : "string",
                    "required" : true 
                },
                "chocolate_sauce" : {
                    "type" : "string",
                    "required" : true 
                } 
            } 
        }
    ]
}

I'm still not sure if this is valid JSON, since my Schemavalidator dies on some invalid input, but it accepts the valid input as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文