JSON.CN让代码更轻
JSON 整理与类型生成可运行案例

预约记录:从样本起草 Draft-07 Schema

从 预约记录 的样本推断字段与类型。样本无法证明全部必填项、合法范围和业务状态,发布前应补正常与异常样本,再用 Schema 校验工具核对。

浏览器本地处理合成数据同场景 32 个案例 ↗

示例工作台

在完整工具中打开 ↗
JSON 整理与类型生成修改下方数据后重新运行
运行结果
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "ok": {
      "type": "boolean"
    },
    "data": {
      "type": "object",
      "properties": {
        "bookings": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "bookingId": {
                "type": "string"
              },
              "title": {
                "type": "string"
              },
              "guests": {
                "type": "integer"
              },
              "confirmed": {
                "type": "boolean"
              },
              "roomType": {
                "type": "string"
              },
              "schedule": {
                "type": "object",
                "properties": {
                  "date": {
                    "type": "string"
                  },
                  "slots": {
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  }
                },
                "required": [
                  "date",
                  "slots"
                ]
              }
            },
            "required": [
              "bookingId",
              "title",
              "guests",
              "confirmed",
              "roomType",
              "schedule"
            ]
          }
        },
        "summary": {
          "type": "object",
          "properties": {
            "total": {
              "type": "integer"
            },
            "note": {
              "type": "null"
            }
          },
          "required": [
            "total",
            "note"
          ]
        }
      },
      "required": [
        "bookings",
        "summary"
      ]
    }
  },
  "required": [
    "ok",
    "data"
  ]
}

原例已核验,可修改输入后重新运行。

⌘ / Ctrl + Enter 运行
01

场景与操作说明

预约日期字符串与时区需要额外约定。样本确认标记不代表真实资源可用,此页面不提交预约。

guests 是演示参与人数;房间容量不由本例自动校验。

  1. 核对 预约记录 的字段层级,保留 bookingId 的字符串类型。
  2. 修改一项输入并运行,观察结果中的值或路径如何变化。
  3. 载入对照样本,复现下面的变化;恢复原例会替换当前输入。
02

对照样本

对照样本没有 bookings 元素,无法推断元素结构。比较两份 Schema,确认哪些约束来自样本证据,哪些仍需人工补充。

查看对照输入
{
  "ok": true,
  "data": {
    "bookings": [],
    "summary": {
      "total": 2,
      "note": null
    }
  }
}

对照样本的实际结果

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "ok": {
      "type": "boolean"
    },
    "data": {
      "type": "object",
      "properties": {
        "bookings": {
          "type": "array",
          "items": {}
        },
        "summary": {
          "type": "object",
          "properties": {
            "total": {
              "type": "integer"
            },
            "note": {
              "type": "null"
            }
          },
          "required": [
            "total",
            "note"
          ]
        }
      },
      "required": [
        "bookings",
        "summary"
      ]
    }
  },
  "required": [
    "ok",
    "data"
  ]
}