JSON 模式模板和 JSON 编辑器

标签 json editor jsonschema swig-template

我已经挣扎了几天,试图使用 JSON Editor Javascript 插件。我正在使用 SWIG JS engine ,但我愿意接受能够解决我的问题的建议。

我创建了一个到目前为止运行良好的 JSON 模板,但这并没有完全实现我需要做的事情,我尝试理解和使用 JSON 编辑器 github 页面提供的语法,但到目前为止运气不好。

如何使模板的行为如下:

如果 features.type 的值 == "Point",那么我希望 features.display 具有类型为“#/definitions/marker”的属性。否则 (features.type == "Polygon") 我希望 features.display 具有类型为“#/definitions/area”的属性。 filter.display 也是如此。

到目前为止,我使用“oneOf”属性,因为它最接近我想要的,但绝对不完全是。

感谢您的帮助!

亲切的问候。

到目前为止,这是我的 JSON 模式:

{
    "type": "array",
    "title": "Layers",
    "items": {
        "title": "Layer",
        "type": "object",
        "headerTemplate": "{{table.id.public_name}}",
        "properties": {
            "table":{"$ref": "#/definitions/table"},
            "features":{"$ref": "#/definitions/features"}
        }
    },
    "definitions": {
        "table": {
            "title": "Table Information",
            "type": "object",
            "properties": {
                "id":{"$ref": "#/definitions/id"},
                "primary_key":{"$ref": "#/definitions/primary_key"},
                "read":{"$ref": "#/definitions/read"}
            }
        },
        "features": {
            "title": "Features Settings",
            "type": "object",
            "properties": {
                "type":{"$ref": "#/definitions/type"},
                "id":{"$ref": "#/definitions/id"},
                "cols":{"$ref": "#/definitions/cols"},
                "display": {
                    "type": "object",
                    "title": "Display",
                    "format": "grid",
                    "oneOf": [
                        {"$ref": "#/definitions/marker"},
                        {"$ref": "#/definitions/area"}
                    ]
                }
            }
        },
        "cols": {
            "title": "Table Columns",
            "type": "array",
            "items": {
                "type": "object",
                "title": "Column",
                "properties": {
                    "id":{"$ref": "#/definitions/id"},
                    "read":{"$ref": "#/definitions/read"},
                    "write":{"$ref": "#/definitions/write"},
                    "filter":{"$ref": "#/definitions/filter"}
                }
            }
        },
        "id": {
            "title": "Identifier",
            "type": "object",
            "format": "grid",
            "properties": {
                "name":{"$ref": "#/definitions/name"},
                "public_name":{"$ref": "#/definitions/public_name"}
            }
        },
        "name": {
            "title": "Name",
            "type": "string"
        },
        "public_name": {
            "title": "Public Name",
            "type": "string"
        },
        "primary_key": {
            "title": "Primary key",
            "type": "string",
            "format": "grid"
        },
        "write": {
            "title": "Editing",
            "type": "object",
            "format": "grid",
            "properties": {
                "read":{"$ref": "#/definitions/read"},
                "method":{"$ref": "#/definitions/method"}
            }
        },
        "read": {
            "title": "Reading",
            "type": "array",
            "items": {
                "type": "integer",
                "title": "Access Level",
                "format": "grid",
                "properties":{"$ref": "#/definitions/access_level"}
            }
        },
        "type": {
            "title": "Type",
            "type": "string",
            "enum": [ "Point", "Polygon" ]
        },
        "access_level": {
            "title": "Access Level",
            "type": "integer",
            "format": "number"
        },
        "method": {
            "title": "Method",
            "type": "object",
            "format": "grid",
            "properties": {
                "type":{"$ref": "#/definitions/method_type"},
                "data":{"$ref": "#/definitions/data"}
            }
        },
        "data": {
            "title": "Data",
            "type": "array",
            "items": {
                "type": "string",
                "title": "Data",
                "format": "grid",
                "properties":{"$ref": "#/definitions/value"}
            }
        },
        "value": {
            "title": "Value",
            "type": "string"
        },
        "filter": {
            "title": "Filter",
            "type": "array",
            "items": {
                "type": "object",
                "title": "Filter",
                "properties": {
                    "value":{"$ref": "#/definitions/value"},
                    "display": {
                        "type": "object",
                        "oneOf": [
                            {"$ref": "#/definitions/marker"},
                            {"$ref": "#/definitions/area"}
                        ]
                    }
                }
            }
        },
        "marker": {
            "title": "Marker",
            "type": "object",
            "format": "grid",
            "properties": {
                "color":{"$ref": "#/definitions/color"},
                "icon":{"$ref": "#/definitions/icon"}
            }
        },
        "color": {
            "title": "Color",
            "type": "string",
            "enum": ["red", "darkred", "orange", "green", "darkgreen", "blue", "purple", "darkpuple", "cadetblue"]
        },
        "css_color": {
            "title": "CSS Color",
            "type": "string",
            "format": "color"
        },
        "icon": {
            "title": "Icon",
            "type": "object",
            "format": "grid",
            "properties": {
                "name":{"$ref": "#/definitions/name"},
                "color":{"$ref": "#/definitions/css_color"}
            }
        },
        "area": {
            "title": "Area",
            "type": "object",
            "properties": {
                "color":{"$ref": "#/definitions/color"},
                "border":{"$ref": "#/definitions/border"}
            }
        },
        "border": {
            "title": "Border",
            "type": "object",
            "properties": {
                "border_color":{"$ref": "#/definitions/border_color"},
                "width":{"$ref": "#/definitions/width"}
            }
        },
        "border_color": {
            "title": "Color",
            "type": "object",
            "format": "grid",
            "properties": {
                "normal":{"$ref": "#/definitions/normal"},
                "hover":{"$ref": "#/definitions/hover"}
            }
        },
        "width": {
            "title": "Width",
            "type": "string"
        },
        "normal": {
            "title": "Normal",
            "type": "string",
            "format": "color"
        },
        "hover": {
            "title": "Hover",
            "type": "string",
            "format": "color"
        },
        "method_type": {
            "title": "Type",
            "type": "string",
            "enum": [ "text", "select" ]
        }
    }
}

最佳答案

强制执行此操作的唯一方法(直到 json 模式草案 5 出现)是使用枚举作为鉴别器。您可以在 How to use dependencies in JSON schema (draft-04) 中找到示例

关于JSON 模式模板和 JSON 编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24842256/

相关文章:

javascript - Windows 上的 Javascript 编辑器推荐?

用于数据描述与数据验证与输入验证的 JSON 模式

arrays - 对象数组的 Json 架构未验证

javascript - JSON 模式 : represent constraint to encode object type in string type

javascript - 如何从所有 JSON 值中删除引号并且不删除 Node JS 中 JSON 键中的引号

php - 关于跟踪 div 比 notepad++ 更好的软件的问题

java - Jackson ObjectMapper 无法反序列化 POJO,抛出异常 : No suitable constructor found for type [. ..]:无法从 JSON 对象实例化

asp.net - HTML 编辑器 asp.net

javascript - 从服务器请求 JSON 对象?

android - 从android中的共享首选项中删除数据