简体   繁体   English

json模式用于类似对象的映射

[英]json schema for a map of similar objects

I wish to write a json schema to cover this (simplified) example 我希望编写一个json模式来涵盖这个(简化的)示例

{
    "errorMessage": "",
    "nbRunningQueries": 0,
    "isError": False,
    "result": {
        "foo": {"price":10.0, "country":"UK"},
        "bar": {"price":100.2, "country":"UK"}
    }
}

which can have this pretty trivial root schema 这可以有这个非常简单的根模式

schema = {
    "type":"object",
    "required":True,
    "properties":{
        "errorMessage": {"type":"string", "required":True},
        "isError": {"type":"boolean", "required":True},
        "nbRunningQueries": {"type":"number", "required":True},
        "result": {"type":"object","required":True}
    }
}

The complication is the results {} element. 复杂的是结果{}元素。 Unlike a standard pattern where results would be an array of same objects - each with an id field or similar this response models a python dictionary which looks like this: 与标准模式不同,其中结果将是相同对象的数组 - 每个都具有id字段或类似字符,此响应模拟python字典,如下所示:

{
    "foo": {},
    "bar": {},
    ...
}

So given that a will be getting a results object of flexible size with no set keys how can I write json schema for this? 因此,如果a将获得灵活大小的结果对象而没有设置键,我怎么能为此编写json模式?

I don't control the input sadly or I'd rewrite it to be something like 我不会悲伤地控制输入,或者我会把它改写成类似的东西

{
    "errorMessage": "",
    "nbRunningQueries": 0,
    "isError": False,
    "result": [
        {"id": "foo", "price":10.0, "country": "UK"},
        {"id": "bar", "price":100.2, "country":"UK"}
    ]
}

Any help or links to pertinent examples would be great. 任何帮助或相关示例的链接都会很棒。 Thanks. 谢谢。

With json-schema draft 4 , you can use additionalProperties keyword to specify the schema of any new properties that you could receive in your results object. 使用json-schema草案4 ,您可以使用additionalProperties关键字指定可在结果对象中接收的任何新属性的模式。

"result" : {
    "type" : "object"
    "additionalProperties" : {
        "type" : "number"
    }
}

If you can restrict the allowed key names, then you may use "patternProperties" keyword and a regular expression to limit the permited key names. 如果可以限制允许的键名,则可以使用“patternProperties”关键字和正则表达式来限制授予的键名。

Note that in json-schema draft 4 "required" must be an array which is bounded to the object, not to each property. 请注意,在json-schema草案4中,“required”必须是一个与对象绑定的数组,而不是每个属性。

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

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