一、簡介
我們先看下面的json數(shù)據(jù)
{
"id": 1,
"name": "g2",
"desc":"b2"
}
假設, 我們要求id為long型, id、name非空。desc可空。如何衡量json數(shù)據(jù)是有效的呢?現(xiàn)在流行的json schema 是用來校驗json數(shù)據(jù)是否合法。
詳情請移至 https://spacetelescope.github.io/understanding-json-schema/
二、keyWords介紹
1、keyWords速記表
| 關鍵詞 | 注釋 |
|---|---|
| $schema | 聲明當前文檔是標準的 JSON Schema 文檔,當然,這個關鍵詞并不是必需的。 |
| title | 題目, 不是必須的 |
| description | 描述,不是必須的 |
| type | 屬性類型, eg : 字符、數(shù)字 |
| properties | json串出現(xiàn)的屬性 |
| patternProperties | 正則表達匹配 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 | 屬性個數(shù) |
| minimum | 給值設置的約束條件,表示可以接受的最小值。 |
| exclusiveMinimum | true : 不包括最小值 |
| maximum | 給值設置的約束條件,表示可以接受的最大值。 |
| exclusiveMaximum | true : 不包括最大值 |
| multipleOf | 如果通過這個關鍵字的值分割實例的結果是一個數(shù)字則表示緊靠 "multipleOf" 的數(shù)字實例是有效的。 |
2、具體使用如下
type關鍵字
①、type = object, properties關鍵字是必需的。
②、type = array, items關鍵字是必需的。
porperties關鍵字
{
type: "object";
properties: {
number: { type: "number" };
name: { type: "string" };
};
}
additionalProperties關鍵字
"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關鍵字
"required": ["name"] : json串必須出現(xiàn)name屬性
{
type: "object";
properties: { number: { type: "number" }; name: { type: "string" } };
additionalProperties: false;
required: ["name"];
}
dependencies關鍵字
dependencies": {
"number": ["name"]
}
number的出現(xiàn)必須依賴在name的基礎上, 換而言之 , number出現(xiàn),則name必須出現(xiàn);name的出現(xiàn)與number并沒有毛關系
{
type: "object";
properties: { number: { type: "number" }; name: { type: "string" } };
additionalProperties: false;
required: ["name"];
dependencies: { number: ["name"] };
}
patternProperties關鍵字
"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關鍵字
{
type: "string";
enum: ["red", "amber", "green"];
}
anyOf關鍵字
anyOf: 對所有屬性范疇生效。所有屬性字符串長度最大不能》5
{
"anyOf": [
{ "type": "string", "maxLength": 5 },
{ "type": "number", "minimum": 0 }
]
}
allOf關鍵字
allOf : type必須相同。不能即String、又number
{
allOf: [{ type: "string" }, { maxLength: 5 }];
}
oneOf關鍵字
{
"type": "number",
"oneOf": [
{ "multipleOf": 5 },
{ "multipleOf": 3 }
]
}
**not關鍵字****
{
"not": {
"type": "string"
}
}
$schema關鍵字
schema : 聲明json片段是json schema模式, 并不是 普通json串, 同時也聲明了json schema版本
三、拓展
1、復用
{
"type": "object",
"properties": {
"street_address": { "type": "string" },
"city": { "type": "string" },
"state": { "type": "string" }
},
"required": ["street_address", "city", "state"]
}
如果我們想復用上述的json串, 可以使用definitions關鍵字。json結構定義如下 :
{
"definitions": {
"address": {
"type": "object",
"properties": {
"street_address": { "type": "string" },
"city": { "type": "string" },
"state": { "type": "string" }
},
"required": ["street_address", "city", "state"]
}
}
}
如何引用 ? 可以使用$ref關鍵字
{ "$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" }
}
}
目標匹配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"
}
}