Json Schema簡(jiǎn)介

一、簡(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"
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末怜浅,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌恶座,老刑警劉巖搀暑,帶你破解...
    沈念sama閱讀 217,509評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異跨琳,居然都是意外死亡自点,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門脉让,熙熙樓的掌柜王于貴愁眉苦臉地迎上來桂敛,“玉大人,你說我怎么就攤上這事溅潜∈趸#” “怎么了?”我有些...
    開封第一講書人閱讀 163,875評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵滚澜,是天一觀的道長(zhǎng)碴开。 經(jīng)常有香客問我,道長(zhǎng)博秫,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,441評(píng)論 1 293
  • 正文 為了忘掉前任眶掌,我火速辦了婚禮挡育,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘朴爬。我一直安慰自己即寒,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評(píng)論 6 392
  • 文/花漫 我一把揭開白布召噩。 她就那樣靜靜地躺著母赵,像睡著了一般。 火紅的嫁衣襯著肌膚如雪具滴。 梳的紋絲不亂的頭發(fā)上凹嘲,一...
    開封第一講書人閱讀 51,365評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音构韵,去河邊找鬼周蹭。 笑死,一個(gè)胖子當(dāng)著我的面吹牛疲恢,可吹牛的內(nèi)容都是我干的凶朗。 我是一名探鬼主播,決...
    沈念sama閱讀 40,190評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼显拳,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼棚愤!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起杂数,我...
    開封第一講書人閱讀 39,062評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤宛畦,失蹤者是張志新(化名)和其女友劉穎瘸洛,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體刃永,經(jīng)...
    沈念sama閱讀 45,500評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡货矮,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評(píng)論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了斯够。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片囚玫。...
    茶點(diǎn)故事閱讀 39,834評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖读规,靈堂內(nèi)的尸體忽然破棺而出抓督,到底是詐尸還是另有隱情,我是刑警寧澤束亏,帶...
    沈念sama閱讀 35,559評(píng)論 5 345
  • 正文 年R本政府宣布铃在,位于F島的核電站,受9級(jí)特大地震影響碍遍,放射性物質(zhì)發(fā)生泄漏定铜。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評(píng)論 3 328
  • 文/蒙蒙 一怕敬、第九天 我趴在偏房一處隱蔽的房頂上張望揣炕。 院中可真熱鬧,春花似錦东跪、人聲如沸畸陡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽丁恭。三九已至,卻和暖如春斋日,著一層夾襖步出監(jiān)牢的瞬間牲览,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工桑驱, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留竭恬,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,958評(píng)論 2 370
  • 正文 我出身青樓熬的,卻偏偏與公主長(zhǎng)得像痊硕,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子押框,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容