JSON Schema:用于描述JSON文档的媒体类型
官方文档:http://json-schema.org/
关键字的文档:http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.2.5
首参考博客:https://blog.youkuaiyun.com/xiong9999/article/details/53688336
(说明了关键字的意义)
github文档:https://github.com/json-editor/json-editor
(待学习 $rel引用, format,checkbox 怎么变成选框,enum枚举 有时间需要学习)
参考以下博客:
博客:https://blog.youkuaiyun.com/taiyangdao/article/details/77865622
博客:https://www.cnblogs.com/fengxiongZz/p/6687058.html
博客:https://blog.youkuaiyun.com/bystarlight/article/details/72467822
博客:https://www.jianshu.com/p/8278eb2458c4?winzoom=1
关键字:
验证数组关键字:
定义了 “type”:“array”后,必须有
(1)items --》代表数组的每一项
(2)maxItems --》数组的最大长度
(3)minItems --》数组的最小长度
(4) uniqueItems --> 布尔值, 为 true时表示数组的每一项唯一性
编写的第一个Schema文件:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"defaultProperties":[
"hero",
"recommendSkin",
"skin"
],
"properties":{
"hero":{
"type":"array",
"id":"/properties/hero",
"title": "hero",
"description":"11",
"items":{
"type": "object",
"id": "/properties/hero/items",
"defaultProperties":["actUrl", "hotTag","id", "imgUrl","name","title"],
"properties":{
"actUrl":{
"type": "string",
"id": "/properties/hero/items/actUrl",
"title": "actUrl",
"default": "http://lol.qq.com/act/a20180514pyke/index.html",
"description": "acturl Notknow"
},
"hotTag":{
"type": "string",
"id": "/properties/hero/items/hotTag",
"title": "hotTag",
"default": "a20180514pyke",
"description": "acturl Notknow"
},
"id":{
"type": "string",
"id": "/properties/hero/items/id",
"title": "id",
"default": "Pyke",
"description": "acturl Notknow"
},
"imgUrl":{
"type": "string",
"id": "/properties/hero/items/imgUrl",
"title": "imgUrl",
"default": "//img.crawler.qq.com/lolwebschool/0/JAutoCMS_LOLWeb_cf79bd58f40f6031f1b1d4301b6b70a8/0",
"description": "acturl Notknow"
},
"name":{
"type": "string",
"id": "/properties/hero/items/name",
"title": "name",
"default": "\u8840\u6e2f\u9b3c\u5f71",
"description": "acturl Notknow"
},
"title":{
"type": "string",
"id": "/properties/hero/items/title",
"title": "title",
"default": "\u6d3e\u514b",
"description": "acturl Notknow"
}
}
}
},
"recommendSkin":{
"type":"object",
"id":"/properties/recommendSkin",
"defaultProperties":["actUrl", "hero","hotTag", "imgUrl","name"],
"properties":{
"actUrl":{
"type": "string",
"id": "/properties/recommendSkin/actUrl",
"title": "actUrl",
"default": "http://lol.qq.com/act/a20180622luckbox/",
"description": "acturl Notknow"
},
"hero":{
"type":"array",
"id":"/properties/recommendSkin/hero",
"title": "hero",
"default": "a20180622luckbox",
"description": "acturl Notknow"
},
"hotTag":{
"type": "string",
"id": "/properties/recommendSkin/hotTag",
"title": "hotTag",
"default": "a20180622luckbox",
"description": "acturl Notknow"
},
"imgUrl":{
"type": "string",
"id": "/properties/recommendSkin/imgUrl",
"title": "imgUrl",
"default": "//img.crawler.qq.com/lolwebschool/0/JAutoCMS_LOLWeb_55d5092c3d7d8ae3898e4ca6709c47cf/0",
"description": "acturl Notknow"
},
"name":{
"type": "string",
"id": "/properties/recommendSkin/name",
"title": "name",
"default": "\u795e\u738b\u5bf9\u51b3",
"description": "acturl Notknow"
}
}
},
"skin":{
"type":"array",
"id":"/properties/skin",
"title": "skin",
"description":"33",
"items":{
"type":"object",
"id":"/properties/skin/items",
"defaultProperties":["actUrl","hero", "hotTag", "imgUrl","name"],
"properties":{
"actUrl":{
"type": "string",
"id": "/properties/skin/items/actUrl",
"title": "actUrl",
"default": "",
"description": "acturl Notknow"
},
"hero":{
"type": "string",
"id": "/properties/skin/items/hero",
"title": "actUrl",
"default": "",
"description": "acturl Notknow"
},
"hotTag":{
"type": "string",
"id": "/properties/skin/items/hotTag",
"title": "actUrl",
"default": "",
"description": "acturl Notknow"
},
"imgUrl":{
"type": "string",
"id": "/properties/skin/items/imgUrl",
"title": "actUrl",
"default": "",
"description": "acturl Notknow"
},
"name":{
"type": "string",
"id": "/properties/skin/items/name",
"title": "actUrl",
"default": "",
"description": "acturl Notknow"
}
}
}
}
}
}
加了枚举的效果
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type":"array",
"title": "game",
"items":{
"type":"object",
"properties":{
"vid":{
"type":"string",
"title": "视频vid"
},
"name":{
"type":"string",
"title": "按钮名称"
},
"desc":{
"type":"string",
"title": "类型"
},
"types":{
"type":"integer",
"title":"物品类型",
"enum": [2,1,3,6,7,10,8,11,13,14,19,101,105,102,108,104,103],
"options": {
"enum_titles": [
"内容4",
"内容3",
"内容1",
"内容2",
"内容5",
"内容6",
"表情",
"经验值",
"内容8",
"内容7",
"内容9",
"内容10",
"内容11",
"内容12",
"内容13",
"内容14",
"实物"
]
}
}
}
}
}
效果会生成这样的select选择框。因为枚举的时候有配置options,生成后对应了内容,可以选择
参考:https://blog.youkuaiyun.com/u010080215/article/details/82349376
案例
string
{
"type": "string",
"pattern": "^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$",
"minLength": 2,
"maxLength": 3,
"format": "date-time|email|hostname|ipv4|ipv6|uri"
}
number
{
"type": "number",
"minimum": 0,
"maximum": 100
}
integer
{
"type": "integer"
}
object
{
"type": "object",
"properties":
{
"name": { "type": "string" },
"credit_card": { "type": "number" }
},
"required": ["name"]
}
array
{
"type": "array",
"minItems": 2,
"maxItems": 3,
"items": {
"type": "number"
}
}
boolean
{ "type": "boolean" }
null
{ "type": "null" }
组合类型
{ "type": ["number", "string"] }
not
{ "not": { "type": "string" } }
allOf
{
"allOf": [
{ "type": "string" },
{ "maxLength": 5 }
]
}
anyOf
{
"anyOf": [
{ "type": "string", "maxLength": 5 },
{ "type": "number", "minimum": 0 }
]
}
oneOf
{
"oneOf": [
{ "type": "number", "multipleOf": 5 },
{ "type": "number", "multipleOf": 3 }
]
}
通用属性
{
"title" : "Match anything",
"description" : "This is a schema that matches anything.",
"default" : "Default value"
}
综合案例
{
"$schema":"http://json-schema.org/draft-04/schema",
"type":"object",
"properties": {
"name": {
"type":"string"
},
"versions": {
"type":"array",
"items": {
"type":"object",
"properties": {
"id": {
"type":"string"
},
"version": {
"type":"integer"
},
"comment": {
"type":"string"
}
},
"required":["id", "version"],
"minItems":1
}
}
},
"required":["name", "versions"]
}
jsonschema类型
任意类型
type
6中基本类型:null,boolean,object,array,number,string
其他类型:integer
enum
const
数字类型
multipleOf
maximanum ( value <= maximanum)
exclusiveMaximum ( value < maximanum)
minimum ( value >= minimum)
exclusiveMinimum ( value > minimum)
字符型
maxLength
minLength
pattern
数组类型
items , 必须为数组类型
additionalItems
maxItems, 最大item数目,integer
minItems, 最小item数目,integer
uniquelItems, 所有item是否唯一
contains, 是否要包含某一jsonschema定义
Object类型
maxProperties, 最大属性个数
minProperties, 最小属性个数
required, 必须的属性名集和,是数组
properties, 属性
patternProperties, 属性名必须符合的正则表达式
additionalProperties
dependencies
propertiyNames
逻辑判断
allOf , array,必须符合所有的校验
anyOf, array,必须满足至少一项
oneOf, array
not, array
formats
dates and times
- date-time
- date
- time
email
- email
- idn-emal
Hostnames
- hostname
- idn-hostname
IP Address
- ipv4
- ipv6
Resource Identifiers
- uri
- uri-reference
- iri
- iri-reference
uri-template
JSON Pointers
- jsoin-pointer
- relative-json-pointer
regex
通用属性,不用于校验
title
description
default, 默认值
readOnly, boolean,是否只读
writeOnly, boolean,是否只写
examples, array,一组案例
---------------------