简体   繁体   English

Json 模式对象属性必须是对象的键

[英]Json schema object property must be the key of an object

I'm currently writing a json schema and I was wondering if someone knows an answer to my problem.我目前正在编写一个 json 模式,我想知道是否有人知道我的问题的答案。 Can I ensure that an object's value equals the key of an object somewhere in the JSON.我可以确保对象的值等于 JSON 中某个对象的键吗? Given the following JSON:给定以下 JSON:

{
    "defaultConfig" : "config1",
    "configs" : {
        "config0" : {...},
        "config1" : {...},
        "config2" : {...}
    }
}

Can I validate that the content of "defaultConfig" must be one of the keys of the properties of "configs" (eg "config0", "config1", "config2")?我可以验证“defaultConfig”的内容必须是“configs”属性的键之一(例如“config0”、“config1”、“config2”)吗? I can't use enums in this case, as the config names are not known beforehand?在这种情况下我不能使用枚举,因为事先不知道配置名称?

Edit: Here's the schema I have so far:编辑:这是我到目前为止的架构:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties" : {
        "defaultConfig" : {
            "type" : "string"
        },
        "configs" : {
            "type" : "object", 
            "patternProperties": {
                "." : {"type" : "object"}
            }
        }
    }
 }

No, there is nothing in JSON Schema that allows a keyword to reference a different part of the data instance in that way.不,JSON Schema 中没有任何内容允许关键字以这种方式引用数据实例的不同部分。

However, the latest version of the specification allows extensions via the $vocabulary keyword, so if you are so inclined, you could write your own keyword that did what you needed.但是,最新版本的规范允许通过 $vocabulary 关键字进行扩展,因此如果您愿意,可以编写自己的关键字来满足您的需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM