JSON Schema 產生與驗證
從 JSON 範例反推 JSON Schema(draft-07):推斷型別、必填欄位、date-time / email / uri / ipv4 等 format、同欄位的 enum、陣列 items 與巢狀物件,可選 additionalProperties: false、$defs 重複使用與 YAML 輸出。
瀏覽器本機執行所有計算都在你的瀏覽器裡完成,資料不會離開本機。
範例 JSON 與推斷選項
產生的 Schema
輸出的是 draft-07;$defs 是 2019-09 起的寫法,draft-07 裡的同義關鍵字是 definitions。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"email": {
"type": "string",
"format": "email"
},
"homepage": {
"type": "string",
"format": "uri"
},
"createdAt": {
"type": "string",
"format": "date-time"
},
"ipv4": {
"type": "string",
"format": "ipv4"
},
"active": {
"type": "boolean"
},
"score": {
"type": "number"
},
"tags": {
"type": "array",
"items": {
"type": "string",
"enum": [
"math",
"engine"
]
}
},
"address": {
"type": "object",
"properties": {
"city": {
"type": "string"
},
"zip": {
"type": "string"
}
},
"required": [
"city",
"zip"
]
},
"orders": {
"type": "array",
"items": {
"type": "object",
"properties": {
"sku": {
"type": "string",
"enum": [
"A-1",
"B-2"
]
},
"qty": {
"type": "integer",
"enum": [
2,
1
]
},
"address": {
"type": "object",
"properties": {
"city": {
"type": "string",
"enum": [
"London",
"Paris"
]
},
"zip": {
"type": "string",
"enum": [
"SW1A",
"75001"
]
}
},
"required": [
"city",
"zip"
]
}
},
"required": [
"sku",
"qty",
"address"
]
}
}
},
"required": [
"id",
"name",
"email",
"homepage",
"createdAt",
"ipv4",
"active",
"score",
"tags",
"address",
"orders"
]
}統計
18425403這個工具能做什麼
- 介面聯調前先定契約:把後端返回的真實 JSON 粘進來,直接得到帶 type、required、format 的 draft-07 Schema,比手寫快也更不容易漏欄位。
- 給配置檔案寫校驗規則:把示例配置轉成 Schema,再用任意 JSON Schema 校驗器檢查使用者填的配置,欄位寫錯、型別不對立刻報錯。
- 對接低程式碼或表單引擎:同欄位取值有限時自動生成 enum,前端下拉框可以直接複用這份取值列表。
- OpenAPI 裡的 components.schemas 不知道怎麼寫時,先用它把示例物件推成 Schema,再粘進 OpenAPI 文件的 schema 段。
範例
輸入
{
"id": 1024,
"name": "Ada Lovelace",
"email": "[email protected]",
"createdAt": "2024-05-06T07:08:09Z",
"tags": ["math", "engine"],
"address": { "city": "London", "zip": "SW1A" },
"billing": { "city": "London", "zip": "SW1A" }
}輸出
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"email": {
"type": "string",
"format": "email"
},
"createdAt": {
"type": "string",
"format": "date-time"
},
"tags": {
"type": "array",
"items": {
"type": "string",
"enum": [
"math",
"engine"
]
}
},
"address": {
"$ref": "#/$defs/Address"
},
"billing": {
"$ref": "#/$defs/Address"
}
},
"required": [
"id",
"name",
"email",
"createdAt",
"tags",
"address",
"billing"
],
"$defs": {
"Address": {
"type": "object",
"properties": {
"city": {
"type": "string"
},
"zip": {
"type": "string"
}
},
"required": [
"city",
"zip"
]
}
}
}address 與 billing 結構完全相同,被抽成 $defs.Address 並用 $ref 引用;tags 只有兩個取值,所以生成了 enum。
常見問題
為什麼所有欄位都被標成必填?
單個示例物件裡出現的欄位都算必填,因為推斷只看這一份資料。想讓必填判斷更準,就把物件放進數組裡放多個樣本:只有每個樣本都出現的欄位才會進 required。
enum 是什麼時候生成的?
同一個位置有多個樣本、且不同取值不超過閾值(預設 6 個,可以調成 0 關閉)時才會生成。單個示例物件沒有對照樣本,所以不會出現 enum。
format 會不會誤判?
只有該位置所有字串都匹配同一個格式時才會寫入 format,例如全部是郵箱才標 email。如果只是長得像(比如版本號 1.2.3 不會命中 date-time),會退回普通 string。識別不了時可以在介面上關掉 format 推斷。
$defs 和 definitions 有什麼區別?
結構完全相同、出現兩次以上的物件會被抽到 $defs,原位置換成 $ref。$defs 是 JSON Schema 2019-09 起的名字,draft-07 裡對應的關鍵字叫 definitions;兩者內容一樣,按你的校驗器支援哪個改一下鍵名即可。
生成的 Schema 能直接用在 OpenAPI 裡嗎?
可以,OpenAPI 3.1 直接使用 JSON Schema,把 properties 段粘進 components.schemas 即可;OpenAPI 3.0 需要把 $schema 去掉,並把 nullable 之類的寫法單獨調整。
關鍵字:json 校验json schema 生成类型推断数据校验json schemaschema generatordraft-07infer schemaapi contractopenapi schema