type关键字是JSON模式的基础。它指定模式的数据类型。
object
{
"key": "value"
}
{
1: "123"
}
"123"
"date-time"
:2018-11-13T20:20:39+00:00。"time"
:20:20:39+00:00"date"
:2018-11-13。"email"
: 互联网电子邮箱"idn-email"
:Internet电子邮箱地址的国际化形式"hostname"
:互联网主机名"idn-hostname"
:国际化internet主机名"uri"
"uri-reference"
"iri"
"iri-reference"
"uri-template"
"json-pointer"
"relative-json-pointer"
{ "type": "object" }
{ "type": "object", "properties": { "name": "string", "age": "number" } }
{ "type": "object", "properties": { "name": "string", "age": "number" }, "required": ["name"] }
{ "type": "object", "minProperties": 2, "maxProperties": 3 }
{ "type": "object", "properties": { "name": "string", "age": "number" }, "additionalProperties": false }
{ "type": "object", "properties": { "name": "string", "age": "number" }, "additionalProperties": { "type": "string" } }
{ "propertyNames": { "type": "string" } }
{ "type": "object", "propertyNames": { "pattern": "^[A-Za-z_][A-Za-z0-9_]*$" } }
{ "type": "object", "properties": { "age": { "type": "number" }, "name": { "type": "string" }, "gender": "string" }, "dependencies": { "name": ["gender"] } } // success { "age": 123, "name": "123", "gender": "women" } // error 缺少gender { "age": 123, "name": "123" }
{ "type": "object", "properties": { "age": { "type": "number" }, "name": { "type": "string" } }, "dependencies": { "name": { "properties": { "gender": { "type": "string" } }, "required": ["gender"] } } } // success { "age": 123, "name": "123", "gender": "women" }
"patternProperties": { "^a": { "type": "number" }, "^b": { "type": "string" } }
{ "abc": 123, "bc": "123" }
{ "type": "array", "items": { "type": "number" } }
{ "type": "array", "items": [ { "type": "number" }, { "type": "string" }, { "type": "string", "enum": ["Street", "Avenue", "Boulevard"] }, { "type": "string", "enum": ["NW", "NE", "SW", "SE"] } ], "additionalItems": false } --- // success [1600, "Pennsylvania", "Avenue", "NW"] // error [1600, "Pennsylvania", "Avenue", "NW", "Washington"]
{ "type": "array", "items": [ { "type": "number" }, { "type": "string" }, { "type": "string", "enum": ["Street", "Avenue", "Boulevard"] }, { "type": "string", "enum": ["NW", "NE", "SW", "SE"] } ], "additionalItems": { "type": "string" } } --- // success [1600, "Pennsylvania", "Avenue", "NW", "Washington"] // error [1600, "Pennsylvania", "Avenue", "NW", 20500]
{ "type": "array", "contains": { "type": "number" } } --- // success [1, 2, 3] [1, "2", 3] // error ["1", "2", "3"]
{ "type": "array", "uniqueItems": true }
{ "type": "string", "pattern": "^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$" }
true false
{ "type": "integer" }
{ "type": "number", "multipleOf": 1.0 }
{ "type": "number" }
{ "type" : "number", "multipleOf" : 10 }
x >= `minimum` x > `exclusiveMinimum` x <= `maximum` x < `exclusiveMaximum`
null