增加 JSON 模式中的默认值

Incrementing default value in JSON schema

我正在构建要在 JSON 编辑器中使用的 JSON 模式。 在一个数组中将是一组对象。这些对象中的每一个都将有一个 属性 "id" 应该是唯一标识符。

{
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "id": {
                "type": "integer"
            },
            "name": {
                "type": "string",
                "title": "Name"
            },
            "brief": {
                "type": "string",
                "title": "Brief description"
            }
        },
        "required": ["id", "name"]
    }
}

是否可以在架构中指定 ID 应该:

  1. 从最后一个对象 ID 开始递增,或者
  2. 设置为当前时间戳

不,这不可能。 JSON 模式是一种简单的约定,用于定义和验证 JSON 文档的 结构 以及值的类型。值的验证是有限的,像递增 ID 这样复杂的事情超出了这个范围。

您必须在反序列化后或通过某种解析器为此进行编程。