JSON 模式 - 具有相同数据类型的对象

JSON schema - objects with same datatype

我生成了以下 JSON:

{
 "someString" : "example",
 "obj1" : {
     "opt1" : 1,
     "opt2" : 1,
     "opt3" : "aaa"
 },
 "obj2" : {
     "opt1" : 55,
     "opt2" : 55,
     "opt3" : "bbb"
 }
}

并且会有更多具有相同数据类型(opt1、opt2、opt3)的对象(obj1、obj2、obj3、obj4、...)

现在我想为此创建模式,但我不知道如何将所有这些对象组合到模式中。

编辑:

我创建了架构:

root: {
    "type" : "object",
    "oneOf" : [
        {
        "properties" : {
            "someString" : { "type" : "string" }
        },
        "patternProperties" : { "^.*$" : { "$ref" : "./schemas/myPatternProperties.json#" } },
        "additionalProperties" : false }
        }
    ]
}

和myPatternProperties.json看起来:

{
    "type" : "object",
    "properties" : {
        "opt1" : { "type" : "number" },
        "opt2" : { "type" : "number" },
        "opt3" : { "type" : "string" },
    }
    "required" : [ "opt1", "opt2", "opt3" ]
}

有什么问题吗,我生成的 JSON 仍然没有被识别为这个模式类型。

据我了解,您的问题是 describe object with a lot of properties with the same type and some naming rules。要解决这个问题,您必须指定 patternProperties 部分

{
    "patternProperties": {
        "^(/[^/]+)+$": { "$ref": "http://some.site.somewhere/entry-schema#" }
}

该构造为属性指定 pattern to match。示例 how to use patternProperties Read more at specification

更新

实际上完整的方案必须是这样的

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "type": "object",
    "properties": {
        "someString": {
            "type": "string"
        }
    },
    "patternProperties": {
        "^obj([0-9]+)$": {
            "$ref": "#/definitions/objEntity"
        }
    },
    "additionalProperties": false,
    "required": [ "someString" ],

    "definitions": {
        "objEntity": {
            "type": "object",
            "properties": {
                "opt1": { "type": "number" },
                "opt2": { "type": "number" },
                "opt3": { "type": "string" }
            },
            "required": ["opt1", "opt2", "opt3"]
        }
    }
}

当然,您可以将该方案拆分为多个文件,并将链接更改为类型定义。