为了账号安全,请及时绑定邮箱和手机立即绑定

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三个字段

以上是对Json-Schema的简要说明,详细介绍大家可以关注后续更新和相关实战课。

点击查看更多内容
9人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
软件测试工程师
手记
粉丝
1.5万
获赞与收藏
497

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消