jsonschema校验规范解读


参考

http://json-schema.org/latest/json-schema-validation.html#rfc.section.7.1
https://blog.csdn.net/taiyangdao/article/details/77865622

案例

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,一组案例
     

你可能感兴趣的:(java)