龙空技术网

深入浅出Json-Schema-Validation

深度学习社区 313

前言:

现在看官们对“javahttpjson”可能比较注重,姐妹们都需要学习一些“javahttpjson”的相关资讯。那么小编同时在网摘上汇集了一些有关“javahttpjson””的相关文章,希望大家能喜欢,我们快快来学习一下吧!

一、目的

在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": ""

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": "",

"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:

[2] The JSON Schema github:

[3] JSON instance to JSON schema:

[4] JSON Schema instance validation:

标签: #javahttpjson #python jsonschema