一、簡(jiǎn)介
我們先看下面的json數(shù)據(jù)
{
"id": 1,
"name": "g2",
"desc":"b2"
}
假設(shè), 我們要求id為long型, id寝优、name非空条舔。desc可空。如何衡量json數(shù)據(jù)是有效的呢乏矾?現(xiàn)在流行的json schema 是用來校驗(yàn)json數(shù)據(jù)是否合法孟抗。
詳情請(qǐng)移至 https://spacetelescope.github.io/understanding-json-schema/
二、keyWords介紹
1钻心、keyWords速記表
關(guān)鍵詞 | 注釋 |
---|---|
$schema | 聲明當(dāng)前文檔是標(biāo)準(zhǔn)的 JSON Schema 文檔凄硼,當(dāng)然,這個(gè)關(guān)鍵詞并不是必需的捷沸。 |
title | 題目, 不是必須的 |
description | 描述,不是必須的 |
type | 屬性類型, eg : 字符摊沉、數(shù)字 |
properties | json串出現(xiàn)的屬性 |
patternProperties | 正則表達(dá)匹配 json串出現(xiàn)的屬性 |
additionalProperties | ①、true : json串可以出現(xiàn)除schema定義之外屬性 ②痒给、false :json串不可以出現(xiàn)除schema定義之外屬性additionalProperties": { "type": "string" }json串可以出現(xiàn)除schema定義之外"字符"屬性 |
required | 存放必要屬性列表说墨。json串必須出現(xiàn)required要求的屬性 |
dependencies | 依賴出現(xiàn) |
size | 屬性個(gè)數(shù) |
minimum | 給值設(shè)置的約束條件,表示可以接受的最小值苍柏。 |
exclusiveMinimum | true : 不包括最小值 |
maximum | 給值設(shè)置的約束條件尼斧,表示可以接受的最大值。 |
exclusiveMaximum | true : 不包括最大值 |
multipleOf | 如果通過這個(gè)關(guān)鍵字的值分割實(shí)例的結(jié)果是一個(gè)數(shù)字則表示緊靠 "multipleOf" 的數(shù)字實(shí)例是有效的试吁。 |
2棺棵、具體使用如下
type關(guān)鍵字
①、type = object, properties關(guān)鍵字是必需的潘悼。
②律秃、type = array, items關(guān)鍵字是必需的。
porperties關(guān)鍵字
{
type: "object";
properties: {
number: { type: "number" };
name: { type: "string" };
};
}
additionalProperties關(guān)鍵字
"additionalProperties": false :json串只能出現(xiàn)schema定義的屬性治唤。
"additionalProperties": { "type": "string" } :json串能出現(xiàn)不在schema定義范圍內(nèi)的屬性, 但屬性的類型必須為string
{
type: "object";
properties: { number: { type: "number" }; name: { type: "string" } };
additionalProperties: false;
}
required關(guān)鍵字
"required": ["name"] : json串必須出現(xiàn)name屬性
{
type: "object";
properties: { number: { type: "number" }; name: { type: "string" } };
additionalProperties: false;
required: ["name"];
}
dependencies關(guān)鍵字
dependencies": {
"number": ["name"]
}
number的出現(xiàn)必須依賴在name的基礎(chǔ)上, 換而言之 , number出現(xiàn),則name必須出現(xiàn);name的出現(xiàn)與number并沒有毛關(guān)系
{
type: "object";
properties: { number: { type: "number" }; name: { type: "string" } };
additionalProperties: false;
required: ["name"];
dependencies: { number: ["name"] };
}
patternProperties關(guān)鍵字
"patternProperties": {
"^S_": { "type": "string" },
"^I_": { "type": "integer" }
}
以s棒动、I開頭的屬性必須是String類型
{
type: "object";
properties: { number: { type: "number" }; name: { type: "string" } };
additionalProperties: false;
required: ["name"];
dependencies: { number: ["name"] };
patternProperties: {
"^S_": { type: "string" };
"^I_": { type: "integer" };
};
}
enum關(guān)鍵字
{
type: "string";
enum: ["red", "amber", "green"];
}
anyOf關(guān)鍵字
anyOf: 對(duì)所有屬性范疇生效。所有屬性字符串長(zhǎng)度最大不能》5
{
"anyOf": [
{ "type": "string", "maxLength": 5 },
{ "type": "number", "minimum": 0 }
]
}
allOf關(guān)鍵字
allOf : type必須相同宾添。不能即String船惨、又number
{
allOf: [{ type: "string" }, { maxLength: 5 }];
}
oneOf關(guān)鍵字
{
"type": "number",
"oneOf": [
{ "multipleOf": 5 },
{ "multipleOf": 3 }
]
}
**not關(guān)鍵字****
{
"not": {
"type": "string"
}
}
$schema關(guān)鍵字
schema : 聲明json片段是json schema模式, 并不是 普通json串, 同時(shí)也聲明了json schema版本
三、拓展
1缕陕、復(fù)用
{
"type": "object",
"properties": {
"street_address": { "type": "string" },
"city": { "type": "string" },
"state": { "type": "string" }
},
"required": ["street_address", "city", "state"]
}
如果我們想復(fù)用上述的json串, 可以使用definitions關(guān)鍵字粱锐。json結(jié)構(gòu)定義如下 :
{
"definitions": {
"address": {
"type": "object",
"properties": {
"street_address": { "type": "string" },
"city": { "type": "string" },
"state": { "type": "string" }
},
"required": ["street_address", "city", "state"]
}
}
}
如何引用 ? 可以使用$ref關(guān)鍵字
{ "$ref": "#/definitions/address" }
或者
{ "$ref": "definitions.json#/address" }
例子如下 :
schema定義
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"address": {
"type": "object",
"properties": {
"street_address": { "type": "string" },
"city": { "type": "string" },
"state": { "type": "string" }
},
"required": ["street_address", "city", "state"]
}
},
"type": "object",
"properties": {
"billing_address": { "$ref": "#/definitions/address" },
"shipping_address": { "$ref": "#/definitions/address" }
}
}
目標(biāo)匹配json數(shù)據(jù)
{
"shipping_address": {
"street_address": "1600 Pennsylvania Avenue NW",
"city": "Washington",
"state": "DC"
},
"billing_address": {
"street_address": "1st Street SE",
"city": "Washington",
"state": "DC"
}
}
2扛邑、延伸屬性
json schema
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"address": {
"type": "object",
"properties": {
"street_address": { "type": "string" },
"city": { "type": "string" },
"state": { "type": "string" }
},
"required": ["street_address", "city", "state"]
}
},
"type": "object",
"properties": {
"billing_address": { "$ref": "#/definitions/address" },
"shipping_address": {
"allOf": [
{ "$ref": "#/definitions/address" },
{ "properties":
{ "type": { "enum": [ "residential", "business" ] } },
"required": ["type"]
}
]
}
}
}
可匹配json
{
"shipping_address": {
"street_address": "1600 Pennsylvania Avenue NW",
"city": "Washington",
"state": "DC",
"type": "business"
}
}