json-schema 简介

Json?

了解json schema首先要知道什么是json?
json 是 JavaScript Object Notation 的缩写,它是一种简化的数据交换格式,是目前互联网服务间进行数据交换最常见的一种交换格式,具有简洁、可读性好等特点。

在json中常见的数据类型主要包括

  • object

{ "key1": "value1", "key2": "value2" }

  • array

[ "first", "second", "third" ]

  • number

42
3.1415926

  • string

"This is a string"

  • boolean

true
false

  • null

null

一个示例json格式比如:

{
 "fruits": [ "apple", "orange", "pear" ],
 "vegetables": [
   {
     "veggieName": "potato",
     "veggieLike": true
   },
   {
     "veggieName": "broccoli",
     "veggieLike": false
   }
 ]
}

何为Json Schema

如前文所述,json是目前应用非常广泛的数据交换格式。既然是用于数据交换的格式,那么就存在数据交换的双方。如何约定或校验对方的数据格式是符合要求的,就成了服务交互需要解决的一个问题。所以Json Schema就是用来定义json数据约束的一个标准。根据这个约定模式,交换数据的双方可以理解json数据的要求和约束,也可以据此对数据进行验证,保证数据交换的正确性。

目前最新的Json-schema版本是draft 7,发布于2018-03-19。下面我们就以官网的一个实例来看看Json-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" ]
    }
  },
  "required": [ "productId", "productName", "price" ]
}

分析说明:

  "$schema": "http://json-schema.org/draft-07/schema#",

说明当前使用的schema版本,可以不包含

  "$id": "http://example.com/product.schema.json",

当前schema的唯一id标识,一般指向一个自主域名。方便后续引用,可以不包含

  "title": "Product",

当前schema的标题,简要描述信息,可不包含

"description": "A product from Acme's catalog",

详细描述信息,可不包含

"type": "object",

约束对象是object,也就是在 { } 中的数据

"properties": {
    "productId": {
      "description": "The unique identifier for a product",
      "type": "integer"
    },

object中具体属性的约束,description是描述信息,不产生具体约束。
type约束productid属性类型为整型

 "productName": {
      "description": "Name of the product",
      "type": "string"
    },

约束productName属性类型为字符型

    "price": {
      "description": "The price of the product",
      "type": "number",
      "exclusiveMinimum": 0
    },

约束price属性类型为数字型,可以是整型或浮点型。
exclusiveMinimum约束该数字>0(不包含0)

    "tags": {
      "description": "Tags for the product",
      "type": "array",
      "items": {
        "type": "string"
      },
      "minItems": 1,
      "uniqueItems": true
    },

约束tag属性是array数组。items是数组项约束,这里约束数组项均为字符型
minItems数组至少包含1项。
uniqueItems约束数组中每项不得重复

    "dimensions": {
      "type": "object",
      "properties": {
        "length": {
          "type": "number"
        },
        "width": {
          "type": "number"
        },
        "height": {
          "type": "number"
        }
      },
  "required": [ "length", "width", "height" ]
    }
  },

约束dimensions嵌套对象,其中length,width,height均为数字类型
且这三个字段在dimensions对象中必须包含

 "required": [ "productId", "productName", "price" ]
}

当前数据对象必须包含productId,productName,price三个字段


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值