web interface parameter calibration artifact -json schema Quick Start

Json Schema Getting Started

JSON JSON format is a specification model is defined based on the data structure of JSON. It was written in the draft IETF and expires in 2011. JSON mode:

  • Describe existing data formats.
  • Clean human and machine-readable documents.
  • The structural integrity verification is conducive to automated testing.
  • Structural integrity verification can be used to verify the data submitted by the client.

Json schema format

Json Json schema itself follows the specification itself is a Json string, look at an example

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Product",
    "description": "A product from Acme's catalog",
    "type": "object",
    "properties": {
        "id": {
            "description": "The unique identifier for a product",
            "type": "integer"
        },
        "name": {
            "description": "Name of the product",
            "type": "string"
        },
        "price": {
            "type": "number",
            "minimum": 0,
            "exclusiveMinimum": true
        }
    },
    "required": ["id", "name", "price"]
}

We look json schema outermost layer consists of the following fields

$schema description Examples
$schema $ Schema keyword status, indicating that the model is consistent with the draft v4 specification writing.  
title The title used to describe the structure  
description description  
type Types of .
properties Defined attributes  
required Required attributes

The above is just a simple example, it can be seen from the above Json schema itself is a JSON string, be indicated by the form of the key-value.
type and properties are used to define the type of property json. required period is necessary to constrain the field of Object. In fact, the Schema defines the type json json supported, and each type has 0-N kinds of constraints embodiment. The next section, the detailed introduction.


Json schema type

Object

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Product",
    "description": "A product from Acme's catalog",
    "type": "object",
    "properties": {
        "id": {
            "description": "The unique identifier for a product",
            "type": "integer"
        },
        "name": {
            "description": "Name of the product",
            "type": "string"
        },
        "price": {
            "type": "number",
            "minimum": 0,
            "exclusiveMinimum": true
        }
    },
    "required": ["id", "name", "price"]
}

There are three key object type: type (type Limited), Properties (the fields defined object), required (required field Limited), as follows:
| Keyword Description | exemplary |
|: --------- ---- |: --------------- | ----- |
. | of the type | type | |
| the properties | custom properties ||
| required | required attributes ||
| maxProperties | maximum number of attributes ||
| minProperties | minimum number of attributes ||
| additionalProperties | Object or to true or to false | reference |
properties define the name and type of each attribute, as in the example embodiment.

array

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Product",
    "description": "A product from Acme's catalog",
    "type": "array",
    "items": {
        "type": "string"
     },
     "minItems": 1,
     "uniqueItems": true
    }

 

There are three separate attribute array: items, minItems, uniqueItems:

Keyword description Examples
items Each array element type .
minItems Restriction attribute, a minimum number of elements in the array  
maxItems Attribute constraints, the maximum number of elements in an array  
uniqueItems Constraint properties, each element is different  
additionalProperties Type constraint items are not recommended Examples
Dependencies Attribute dependency usage
patternProperties   usage

string

{
   "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Product",
    "description": "A product from Acme's catalog",
    "type": "object",
    "properties": {
        "ip": {
            "mail": "string",
            "pattern":"w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*"
        },
        "host": {
            "type": "phoneNumber",
            "pattern":"((d{3,4})|d{3,4}-)?d{7,8}(-d{3})*"
        },
    },
    "required": ["ip", "host"]
}

  

Keyword description Examples
maxLength Define a maximum length of the string,> = 0 .
minLength Definition of a minimum length of the string,> = 0  
pattern Constraint string using regular expressions

integer

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Product",
    "description": "A product from Acme's catalog",
    "type": "object",
    "properties": {
        "name": {
            "description": "Name of the product",
            "type": "string"
        },
        "price": {
            "type": "integer",
            "minimum": 0,
            "exclusiveMinimum": true
        }
    },
    "required": ["id", "name", "price"]
}

 

number

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Product",
    "description": "A product from Acme's catalog",
    "type": "object",
    "properties": {
        "name": {
            "description": "Name of the product",
            "type": "string"
        },
        "price": {
            "type": "number",
            "minimum": 0,
            "exclusiveMinimum": true
        }
    },
    "required": ["id", "name", "price"]
}

| 关键字      | 描述  |    示例   |
|:------------- |:---------------| ----- |
| minimum |最小值|. |
| exclusiveMinimum |如果存在 "exclusiveMinimum" 并且具有布尔值 true,如果它严格意义上大于 "minimum" 的值则实例有效。||
| maximum |约束属性,最大值||
| exclusiveMaximum |如果存在 "exclusiveMinimum" 并且具有布尔值 true,如果它严格意义上小于 "maximum" 的值则实例有效。||
| multipleOf |是某数的倍数,必须大于0的整数||

Keywords can be described digital number of any length, any decimal point. number types of constraints are the following:

Keyword description Examples
minimum Minimum .
exclusiveMinimum If there is "exclusiveMinimum" and has the Boolean value true, if more than "minimum" in its strict sense of the value of the instance is valid.  
maximum Constraint Properties, the maximum value  
exclusiveMaximum If there is "exclusiveMinimum" and has the Boolean value true, if less than the value of "maximum" in its strict sense of the instance is valid.

boolean

{
"type": "object",
"properties": {
"number": { "type": "boolean" },
"street_name": { "type": "string" },
"street_type": { "type": "string",
"enum": ["Street", "Avenue", "Boulevard"]
}
}
}

true or false

enum

{
  "type": "object",
  "properties": {
    "number":      { "type": "number" },
    "street_name": { "type": "string" },
    "street_type": { "type": "string",
                     "enum": ["Street", "Avenue", "Boulevard"]
                   }
  }
}

You can do that

{
  "type": "object",
  "properties": {
    "number":      { "type": "number" },
    "street_name": { "type": "string" },
    "street_type": ["Street", "Avenue", "Boulevard"]                   
  }
}

null

Advanced

Understand the various types of the above definition and the agreed conditions, to meet the majority of cases. But in order to write better json schema, we'll learn a few keywords

$ref

$ REF is used to reference another schema,
the following example:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Product set",
    "type": "array",
    "items": {
        "title": "Product",
        "type": "object",
        "properties": {
            "id": {
                "description": "The unique identifier for a product",
                "type": "number"
            },
            "name": {
                "type": "string"
            },
            "price": {
                "type": "number",
                "minimum": 0,
                "exclusiveMinimum": true
            },
            "tags": {
                "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"]
            },
            "warehouseLocation": {
                "description": "Coordinates of the warehouse with the product",
                "$ref": "http://json-schema.org/geo"
            }
        },
        "required": ["id", "name", "price"]
    }
}

definitions

When a lot of time to write a schema, you may need to create an internal structure, and then referenced using $ ref, is shown below:

{
    "type": "array",
    "items": { "$ref": "#/definitions/positiveInteger" },
    "definitions": {
        "positiveInteger": {
            "type": "integer",
            "minimum": 0,
            "exclusiveMinimum": true
        }
    }
}

allOf

Meant to show all the properties, the proposed use requires replacement

Is not recommended, the following examples

{
  "definitions": {
    "address": {
      "type": "object",
      "properties": {
        "street_address": { "type": "string" },
        "city":           { "type": "string" },
        "state":          { "type": "string" }
      },
      "required": ["street_address", "city", "state"]
    }
  },

  "allOf": [
    { "$ref": "#/definitions/address" },
    { "properties": {
        "type": { "enum": [ "residential", "business" ] }
      }
    }
  ]
}

anyOf

Means any display attribute, and suggested alternatives requires minProperties Alternatively, for example:

{
  "anyOf": [
    { "type": "string" },
    { "type": "number" }
  ]
}

oneOf

one of them

{
  "oneOf": [
    { "type": "number", "multipleOf": 5 },
    { "type": "number", "multipleOf": 3 }
  ]
}

not

* Non-type
example

{ "not": { "type": "string" } }
 

Guess you like

Origin www.cnblogs.com/lianhaifeng/p/11886451.html