网站首页 > 开源技术 正文
一、目的
在Web Service对外交互方式的演变中,RESTful API已经成为主流的设计风格,JSON的轻量且简洁优点,使得JSON成为RESTful API主要的数据交换格式。RESTful请求过程是,客户端发送RESTful请求到Web Service节点,请求header中设置Content-Type=application/json,请求body中是JSON数据,Web Service根据Content-Type校验JSON数据。JSON数据校验有两种策略:一种是在进行实际业务处理前校验,该方式将数据校验和业务处理耦合在一起,可能存在前面数据校验和业务处理成功,后面数据校验失败,导致前面业务回退,效率低下;另一种是将数据校验和业务处理分开,提前设定JSON数据的Schema,所有业务处理前,JSON数据进行Schema校验,该方式简洁且效率高,已经成为主流方式。
本文介绍JSON Schema语法、样例、以及Python和Java中Validation方法,为大家提供参考。
二、语法
2.1 Json Schema Keywords
序号 | keyword | 是否必须 | 说明 |
1 | $schema | 否 | 当前文档是否为标准JSON Schema文档,例如: "$schema": "http://json-schema.org/draft-04/schema#" |
2 | $ref | 否 | 引用其他的JSON Schema |
3 | id | 否 | id |
4 | title | 否 | 标题 |
5 | description | 否 | 描述 |
6 | type | 是 | 类型,例如:object、array、string、number、integer、boolean、null、any |
7 | properties | - | type=object时,properties必须存在 |
8 | patternProperties | 否 | 正则属性,例如: "patternProperties": { "^test_": { "type": "string" }, } test开头且类型为string的属性 |
9 | additionalProperties | 否 | false - JSON串中只能出现Schema中定义的属性 true - JSON串中可以出现不在Schema中定义的属性 "additionalProperties": { "type": "string" } - JSON串中可以出现不在Schema中定义的属性,但类型必须string |
10 | required | 否 | 必须出现的properties |
11 | dependencies | 否 | |
12 | size | 否 | |
13 | minimum | 否 | type=number,最小值 |
14 | exclusiveMinimum | 否 | type=number,最小值,不包含minimum |
15 | maximum | 否 | type=number,最大值 |
16 | exclusiveMaximum | 否 | type=number,最大值,不包含maximum |
17 | multipleOf | 否 | |
18 | anyOf | 否 | 满足任意一个即可,例如: { "anyOf": [ { "type": "string"}, { "type": "number"} ] } |
19 | allOf | 否 | 满足所有,例如: { "allOf": [ { "type": "string" }, { "maxLength": 5 } ] } |
20 | oneOf | 否 | 满足唯一一个,例如: { "type": "string", "oneOf": [ { "format": "host-name" }, { "format": "ipv4" }, { "format": "ipv6" } ] } |
21 | not | 否 | 例如: { "not": { "type": "string" } } |
22 | items | - | type=array时,items必须存在 |
23 | enum | 否 | 枚举类型,例如: { "type": "string", "enum": ["test1", "test2", "test3"] } 属性类型为string,且只能是test1、test2、test3其中一个。 |
24 | minItems | 否 | type=array,最小条目个数 |
25 | maxItems | 否 | type=array,最大条目个数 |
26 | uniqueItems | 否 | type=array,true - 条目不重复,反之可重复 |
27 | minLength | 否 | type=string,最小字符个数 |
28 | maxLength | 否 | type=string,最大字符个数 |
29 | format | 否 | format规则,优先级大于type,包括:date-time、date、time、utc-millisec、regex、color、style、phone、uri、email、ip-address、ipv6、host-name、uuid、anything else(type unchanged) |
30 | divisibleBy | 否 | |
31 | disallow | 否 | 不包含,例如: { "type": "object", "properties": { "id": { "disallow": "any" }, "name": { "type": "string", "required": true } } } |
32 | extends | 否 | 继承,例如: { "type" : "object", "extends" : { "$ref" : "xxx.json" } } |
33 | default | 否 | 默认值 |
34 | definitions | 否 | 复用Json Schema,例如: { "$ref": "#/definitions/xxx" } { "$ref": "definitions.json#/xxx" } |
35 | pattern | 否 | 正则值,例如: { "type": "string", "pattern": "^test" } test开头的值 |
2.2 Json Schema Example
Json Schema |
{ "$schema": "http://json-schema.org/draft-04/schema#", "title": "Test", "type": "array", "items": { "type": "object", "properties": { "prop1": { "description": "prop1", "type": "integer" }, "prop2": { "description": "prop2", "type": "string", "enum": ["test1", "test2", "test3"] }, "prop3": { "type": "number", "minimum": 60, "exclusiveMinimum": true, "maximum": 100, "exclusiveMaximum": true } }, "required": ["prop1", "prop2", "prop3"] }, minItems: 1, maxItems: 5, uniqueItems: true } |
Json Instance |
[{ 'prop1': 1, 'prop2': 'test1', 'prop3': 91 }, { 'prop1': 2, 'prop2': 'test2', 'prop3': 92 } ] |
三、应用
3.1 Python Json Schema Validation
# JSON_SCHEMA:json schema string
# JSON_INSTANCE: json instance string
from jsonschema.validators import Draft4Validator
validator = Draft4Validator(schema=JSON_SCHEMA)
validator.validate(JSON_INSTANCE)
3.2 Java Json Schema Validation
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.JsonLoader;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
JsonNode schemaNode = JsonLoader.fromString(JSON_SCHEMA);
JsonNode instanceNode = JsonLoader.fromString(JSON_INSTANCE);
JsonSchema jsonSchema = factory.getJsonSchema(schemaNode);
ProcessingReport report = jsonSchema.validate(instanceNode);
四、参考
[1] The JSON Schema web site:
http://www.json-schema.org
[2] The JSON Schema github:
https://github.com/json-schema-org
[3] JSON instance to JSON schema:
https://jsonschema.net/#/
[4] JSON Schema instance validation:
https://www.jsonschemavalidator.net/
猜你喜欢
- 2024-09-16 比较一下XML, JSON和YAML(xml与json区别)
- 2024-09-16 JSON的概念及应用场景举例(json的概念及应用场景举例分析)
- 2024-09-16 Java实现在线SQL编程(完整版)(java代码中怎样写sql语句)
- 2024-09-16 RESTful API (Application Programming Interface)
- 2024-09-16 API低代码开发平台实践(低代码开发工具)
- 2024-09-16 JSON 格式的接口测试流程【Eolink Apikit】
- 2024-09-16 推荐腾讯开源的零代码、全功能、强安全API架构
- 2024-09-16 下个十年高性能 JSON 库来了:fastjson2!
- 2024-09-16 高并发之API接口,分布式,防刷限流,如何做?
- 2024-09-16 可以让你零代码快速开发REST API的几个开源项目
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)