设计规则-扩展

第三方扩展 HROpen JSON 标准的规则

第三方应扩展 HROpen 计划,以便更好地描述其服务返回的实例。为此,第三方必须向 HROpen 展示其更改或公开提供自己的 JSON 架构。

下面是一些如何更改架构的示例。

{
    "ActionCodeList": {
        "title": "List of action codes",
        "enum": [
            "create",
            "update",
            "delete"
        ]
    },
    "Base": {
        "title": "Base schema",
        "type": "object",
        "properties": {
            "a": { "type": "string" },
            "b": {
                "type": "integer",
                "maximum": 50
            },
            "c": {
                "type": "object",
                "properties": {
                    "x": { "type": "number" },
                    "y": { "type": "string" }
                }
            },
            "code": { "$ref": "#/ActionCodeList" }
        }
    }
}
  1. 禁止更改属性类型。

     "Ext1": {
         "title": "Change property type",
         "type": "object",
         "allOf": [
             { "$ref": "#/Base" }
         ],
         "properties": {
             "a": { "type": "number" }
         }
     }

    没有实例会针对此类架构进行验证,因为不能同时是一个数字和一个字符串。a

  2. 财产限制可以强制执行,但不得放宽。

     "Ext2": {
         "title": "Change property restriction",
         "type": "object",
         "allOf": [
             { "$ref": "#/Base" }
         ],
         "properties": {
             "b": {
                 "type": "integer",
                 "maximum": 20
             }
         }
     }

    您必须使用"allOf"关键字引用 HROpen 基本架构。例如,您可以将值限制为 20,但无法将最大值增加为 80。b

  3. 允许添加新属性

     "Ext3": {
         "title": "Add property",
         "type": "object",
         "allOf": [
             { "$ref": "#/Base" }
         ],
         "properties": {
             "d": { "type": "number" }
         }
     }

    您必须使用"allOf"关键字引用 HROpen 基本架构。您可以自由添加任何数量的自定义属性。

  4. 您可以删除 HROpen 可选属性,以便创建要使用的属性子集。这可以通过两种方式实现:

     "Ext4a": {
         "title": "Restrict properties to smaller set",
         "type": "object",
         "allOf": [
             { "$ref": "#/Base" }
         ],
         "properties": {
             "a": { "type": "string" },
             "b": { "type": "integer" }
         },
         "additionalProperties": false
     }

    您必须使用"allOf"关键字引用 HROpen 基本架构。列出要使用的原始架构的所有属性,并阻止其余属性。

    "Ext4b": {
        "title": "Restrict properties to smaller set",
        "type": "object",
        "allOf": [
            { "$ref": "#/Base" }
        ],
        "not": {
            "type": "object",
            "properties": {
                "c": {},
                "code": {}
            }
        }
    }

您必须使用"allOf"关键字引用 HROpen 基架构,并且不允许在新架构中不使用的所有属性。从这两个选项中,建议使用第一个选项作为更具可读性的选项。

  1. 不能重写 HROpen 对象类型。

     "Ext5": {
         "title": "Replace object property",
         "type": "object",
         "allOf": [
             { "$ref": "#/Base" }
         ],
         "properties": {
             "c": {
                 "type": "object",
                 "properties": {
                     "m": { "type": "bool" },
                     "n": { "type": "number" }
                 }
             }
         }
     }

    您必须使用"allOf"关键字引用 HROpen 基本架构。结果实例(具有 和 属性)将同时对 和 有效。这被认为是罚款, 只要这是预期的结果!xymncExt5Base

  2. 您无法扩展 HROpen 代码列表

     "Ext6": {
         "title": "Add new value to codelist",
         "type": "object",
         "allOf": [
             { "$ref": "#/Base" }
         ],
         "properties": {
             "code": {
                 "enum": [
                     "create",
                     "update",
                     "delete",
                     "void"
                 ]
             }
         }
     }

    尝试使用一个附加值扩展代码列表,但是这不起作用,因为具有 值"void"的实例对 无效。codeBase

  3. 扩展代码列表的唯一方法就是提供不显式引用 HROpen 架构的架构。HROpen 将承认这一事实,并为此案例提供验证和认证工具

     "Ext7": {
         "title": "Add new value to codelist",
         "type": "object",
         "properties": {
             "a": { "type": "string" },
             "b": {
                 "type": "integer",
                 "maximum": 50
             },
             "c": {
                 "type": "object",
                 "properties": {
                     "x": { "type": "number" },
                     "y": { "type": "string" }
                 }
             },
             "code": {
                 "enum": [
                     "create",
                     "update",
                     "delete",
                     "void"
                 ]
             }
         }
     }

    请注意,在这种情况下,您需要引用完整的 HROpen 架构,并完全替换代码列表。这是由 JSON 架构 v4 中的限制决定的,并且可能会在未来版本中解决。