{"$schema": "http://json-schema.org/draft-07/schema#","$id": "http://example.com/product.schema.json","title": "Product","description": "A product from Acme's catalog","type": "object","properties": {"productId": {"description":"The unique identifier for a product","type":"integer" },"productName": {"description":"Name of the product","type":"string"// 表明 productName 是个字符串 } },// 同时 productName 也是必填的"required": [ "productId","productName" ]}
4. 深入属性
对于商家而言,没有免费的产品
price 属性被添加近来,拥有通用的 description 注释和 type 检验关键字,这两个在之前都被提到过了。并且,也被添加到了 required 数组里了
通过 exclusiveMinimum 校验关键字,我们指定 price 的值必须是大于 0 的值
如果我们想要 price 大于等于 0, 那么我们可以使用 minimum 关键字
{"$schema": "http://json-schema.org/draft-07/schema#","$id": "http://example.com/product.schema.json","title": "Product","description": "A product from Acme's catalog","type": "object","properties": {"productId": {"description":"The unique identifier for a product","type":"integer" },"productName": {"description":"Name of the product","type":"string" },"price": {"description":"The price of the product","type":"number",// 表明 price 属性是个数字"exclusiveMinimum":0// 而且大于0 (不能等于) } },// price 也是必填项"required": [ "productId","productName","price" ]}
接下来,我们来到了 tags 属性
商家说了:
如果有 tags,那么至少要有一个 tag
所有的 tag 必须的唯一的,同属于一个产品的 tag 不能重复
所有的 tag 都必须的文本类型
tags 非常好,但是不一定是 必填项
因此:
添加 tags 属性,它拥有通用的注释和关键词
这一次,type 校验关键字是 array
我们介绍下 items 这个校验关键字,这样我们定义数组中的元素类型。在这个例子中,数组元素的 type 是 string
minItems 校验关键字用来确保数组中至少有一个元素
uniqueItem 校验关键字表明数组中所有的元素都是唯一的
因为 tags 是可选的,因此我们不把它加到 required 中
{"$schema": "http://json-schema.org/draft-07/schema#","$id": "http://example.com/product.schema.json","title": "Product","description": "A product from Acme's catalog","type": "object","properties": {"productId": {"description":"The unique identifier for a product","type":"integer" },"productName": {"description":"Name of the product","type":"string" },"price": {"description":"The price of the product","type":"number","exclusiveMinimum":0 },"tags": {"description":"Tags for the product","type":"array",// tags 是一个数组"items": {"type":"string"// 数组元素是字符串 },"minItems":1,// 至少有一个元素"uniqueItems":true// 每一个元素都是唯一的 } },"required": [ "productId","productName","price" ]}
5. 数据结构嵌套
到目前为止,我们都在处理一个比较扁平的 JSON 结构,只有一层。这一节示范一下嵌套的数据结构。
新增 dimensions 这个属性,因为它的 type 校验关键字是 Object, 我们可以使用 properties 校验关键字来定义一个嵌套的数据结构
{// 前面提到了,id 可以作为引用的 URI"$id": "https://example.com/geographical-location.schema.json","$schema": "http://json-schema.org/draft-07/schema#","title": "Longitude and Latitude","description": "A geographical coordinate on a planet (most commonly Earth).","required": [ "latitude","longitude" ],"type": "object","properties": {"latitude": { // 维度在 -90 到 90 之间"type":"number","minimum":-90,"maximum":90 },"longitude": {"type":"number",// 经度在 -180 到 180 之间"minimum":-180,"maximum":180 } }}// 即描述了一个对象,包含经度维度两个属性
接下来,我们引用这个 Schema , 这样就能组合成新的 Schema:
{"$schema": "http://json-schema.org/draft-07/schema#","$id": "http://example.com/product.schema.json","title": "Product","description": "A product from Acme's catalog","type": "object","properties": {"productId": {"description":"The unique identifier for a product","type":"integer" },"productName": {"description":"Name of the product","type":"string" },"price": {"description":"The price of the product","type":"number","exclusiveMinimum":0 },"tags": {"description":"Tags for the product","type":"array","items": {"type":"string" },"minItems":1,"uniqueItems":true },"dimensions": {"type":"object","properties": {"length": {"type":"number" },"width": {"type":"number" },"height": {"type":"number" } },"required": [ "length","width","height" ] },"warehouseLocation": {// 我们新增这个属性"description":"Coordinates of the warehouse where the product is located.",// 引用上面定义的 Schema"$ref":"https://example.com/geographical-location.schema.json" } },"required": [ "productId","productName","price" ]}