如何第一時間看到新文章?
收藏本部落格列表頁,並在首頁與工具聚合頁留意指南入口。閱讀文章無需註冊或訂閱電子報。
可複製 JSON Schema 範例:物件、字串、陣列、enum、anyOf、$ref 等,並整理常見驗證誤區,方便搭配 JSON Work 本機驗證與轉 Schema 工具使用。
JSON Schema 用來描述 JSON 資料的允許結構與規則。語法檢查回答「是否為合法 JSON」;Schema 驗證回答「是否符合我們的資料契約」。
以下範例對齊 Draft 2020-12(見 $schema)。請與你所用驗證器支援的草案一致。
若需複習 JSON 基礎,可先讀 什麼是 JSON?。
name(字串)與 age(整數),tags 選填:{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/schemas/person.min.json",
"title": "Person",
"type": "object",
"additionalProperties": false,
"properties": {
"name": { "type": "string", "minLength": 1 },
"age": { "type": "integer", "minimum": 0 },
"tags": {
"type": "array",
"items": { "type": "string" }
}
},
"required": ["name", "age"]
}合法實例{ "name": "Asha", "age": 30, "tags": ["beta"] }不合法實例(缺少 age,且 additionalProperties: false 禁止未知欄位 extra){ "name": "Asha", "extra": true }{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"properties": {
"handle": { "type": "string", "pattern": "^[a-z0-9_]{3,20}$" },
"contact": { "type": "string", "format": "email" },
"createdAt": { "type": "string", "format": "date-time" }
},
"required": ["handle"]
}提示: 在核心規格中 format 常作為註解;Ajv 等需外掛才會依格式驗證。未啟用時請把 format 當文件約定。{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"properties": {
"qty": { "type": "integer", "minimum": 1, "maximum": 99 },
"price": { "type": "number", "minimum": 0, "multipleOf": 0.01 }
},
"required": ["qty", "price"]
}整數用 integer,需要小數時用 number。
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"properties": {
"role": { "type": "string", "enum": ["admin", "editor", "viewer"] },
"version": { "const": 1 }
},
"required": ["role", "version"]
}{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"properties": {
"ids": {
"type": "array",
"items": { "type": "string", "pattern": "^[A-Z]{3}-[0-9]{4}$" },
"minItems": 1,
"maxItems": 10,
"uniqueItems": true
}
},
"required": ["ids"]
}{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"additionalProperties": false,
"properties": {
"value": {
"anyOf": [
{ "type": "string", "minLength": 1 },
{ "type": "number" }
]
}
},
"required": ["value"]
}oneOf — 恰好一個分支(注意分支條件勿重疊)。allOf — 合併所有子 schema 的約束。{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$defs": {
"Email": { "type": "string", "format": "email" }
},
"type": "object",
"additionalProperties": false,
"properties": {
"owner": { "$ref": "#/$defs/Email" },
"billing": { "$ref": "#/$defs/Email" }
},
"required": ["owner"]
}在根物件使用 additionalProperties: false 有助於抓欄位拼字錯誤。對外 API 通常與明確的 properties、required 一併使用。
{ "type": ["string", "null"] } 等寫法。生產環境常使用 Ajv(JavaScript)或 jsonschema(Python)等;務必核對函式庫對所宣告 $schema 草案的支援。
致力於為開發者提供最佳的 JSON 處理工具
更多文章即將發布...
返回部落格關於跟進更新、選題與互動方式。
收藏本部落格列表頁,並在首頁與工具聚合頁留意指南入口。閱讀文章無需註冊或訂閱電子報。
圍繞 JSON 驗證、格式化、轉換與除錯流程,以及 JSON Work 工具更新,與站內工具的本地能力互相呼應。
可以。請透過關於頁的聯絡方式或 GitHub 回饋;我們會優先安排貼近真實開發情境的教學。