[top]

introduce

Json Schema defines a set of terms and rules for defining Json metadata, which is expressed as Json data. Json metadata defines specifications that Json data must meet, including membership, structure, type, and constraints

eg: { "city":"chicago", "number": 20, "user" : {"name":"Alex", "age":20}} In the above example, the Web API requires three members: city, number, and user, where city is a string, number is a number, and user is an object, {"type": "object", "properties": {"city": {"type": "string"}, "number": { "type": "number" }, "user": { "type": "object", "properties": { "name" : {"type": "string"}, "age" : {"type": "number"} } } } }Copy the code

The keyword

1.Type type

1.{“type”:”string”}

1. String length ** Keywords: minLength, maxLength** Specifies the minimum and maximum length of a string

eg:
{ "type" : "string", "minLength" : 2, "maxLength" : 3, }
Copy the code
  1. Regular expression** keyword: pattern**
eg: { "type" : "string", "pattern" : "^(\\([0-9]{3}\\))? [0-9] {3} - [0-9] {4} $"},Copy the code
  1. The Format string** Keyword: format**Used for email, dates, domain names, etc
{ "type" : "string", "format" : "date", }
Copy the code
2.{“type” : “object”}
  • Json objects are the most common Json data types, and valid data can be
{ 
    "name": "Froid", 
    "age" : 26, 
    "address" : { "city" : "New York", "country" : "USA" } 
}
Copy the code
  • Members of the Schema** properties**
{ "type": "object", "properties": { "name": {"type" : "string"}, "age" : {"type" : "integer" }, "address" : { "type" : "object", "properties" : { "city" : {"type" : "string"}, "country" : {"type" : "String "}}}}} For the Schema in the above example, the valid data is {"name": "Froid", "age" : 26, "address" : {"city" : "New York", "country" : "USA" } }Copy the code
  • Batch define member schemas** Keywords: patternProperties**
eg: {"S_1" : "abc"}
    {"S_1" : "abc", "I_3" : 1}
{ 
    "type": "object", 
    "patternProperties": { 
        "^S_": { "type": "string" }, 
        "^I_": { "type": "integer" } 
    } 
}
Copy the code
  • Members that must be present** keyword: Required **
{ "type": "object", "properties": { "name": {"type" : "string"}, "age" : {"type" : "integer"}, }, "required" : ["name"]} {"name" : "Mary ", "age" : 26} {"name" :" Mary "} {"name" : "Mary "} {"name" :" Mary "}Copy the code
  • Member dependencies** dependencies**
The requirement that dependent members of certain members cannot be present alone without the absence of dependent members is a data integrity constraint. { "type": "object", "dependencies": { "credit_card": ["billing_address"]}} Name = "key" and "value"; {} {"billing_address" : "ABC "} but if there is a "credit_card" attribute, the "billing_address" attribute cannot be absent. The following data is illegal {"credit_card": "7389301761239089"}Copy the code
  • Whether additional attributes are allowed** additionaProperties**
{"type" : "object", "properties": {" name": {"type" : {" name": {"type" : "string"}, "age" : {"type" : "integer"}, }, "required" : ["name"], "additionalProperties" : The above example states that the object cannot have members other than "name" and "age". Legitimate data {" name ":" Mary "} {" name ":" Mary ", "age" : 26} illegal data {" name ":" Mary ", "phone" : "84893948"}Copy the code
  • Limits on the number of attributes** Keywords: minProperties, maxProperties**Specify a minimum or maximum number of attribute members
{"name" : "Mary ", "age" : 26} {"name" :" Mary ", "age" : 26} {"name" : "mary", "age" : 26, "phone" : "37839233"}Copy the code
3.{“type” : “number”}

1. The valid value of number can be 2 or 0.1. The integer must be an integer

{"type" : "number", "multipleOf" : 10,}Copy the code

** Keywords: minimum, maximum, exclusiveMinimum, exclusiveMaximum** Can limit the position of the value, including the maximum value, the minimum value, the maximum value, the minimum value

{"type" : "number", "minimum": 0, "exclusiveMaximum": 100}Copy the code
4.{“type”: “integer”} specifies that the data must be an integer
5.{“type” : “array”}

1. Valid Json array data

[1, 2, 3]
[1, "abc", {"name" : "alex"}]
[]
Copy the code

2. The type specific parameters of an array can be used to restrict the type of the member, whether additional members are allowed, the minimum number of elements, the maximum number of elements, and whether duplicate elements are allowed

  • Array member type** keyword: items**
{"type": "array", "items": {"type": "number"}} 2 The keyword items can also correspond to an array. In this case, the elements in the Json array must match each Schema in the items array in the Json Schema. Eg: [1, "ABC "] {"type": "array", "items": [ {"type": "number" }, { "type": "string" } ] }Copy the code
  • Whether an array allows additional members** additionalItems**
This restriction only works when the Items keyword is used and the items keyword corresponds to a Schema array. The keyword additionalItems specifies whether additional tuples are allowed for elements in the Json array in addition to matching the Schema in the items array one by one. AdditionalItems allow additional elements when additionalItems is true eg:[1, "ABC ", "x"] {"type": "array", "items": [{"type": "number"}, {"type": "string" } ], "additionalItems" : true }Copy the code
  • Number of array elements** keywords: minItems, maxItems**You can limit the number of elements in an array
Eg: [6] {" type ":" array ", "items" : {" type ":" number "}, "minItems" : 5, "maxItems" : 10}Copy the code
  • Whether elements in an array must be unique** Keywords: uniqueItems**

Eg: [1, 2, 3, 4, 5] {" type ":" array ", "items" : {" type ":" number "}, "uniqueItems" : true}Copy the code
6.{“type” : “boolean”}
7.{“type” : “null”}
8. Logical combinations** keywords: allOf, anyOf, oneOf, not**

  1. The keyword name can be seen from its meaning, satisfy all, satisfy any, satisfy one. The first three keywords are used in the same form, with allOf as an example to illustrate their form
{ "allOf" : [ Schema1, Schema2, ... ] } where, the content of "allOf" is an array whose members are embedded Json Schema. The Schema1 and Schema2 examples above are embedded Json schemas. The entire Schema represents the current Json data, which must meet Schema1 and Schema2 requirements. Note that "additionalProperties" should not be set to false in either an embedded Schema or an external Schema. Otherwise, contradictory schemas may be generated that cannot be satisfied by any data. Can be used to implement "inheritance" like relationships, for example we define a Schema_base that we can implement if we want to modify it further. { "allOf" : [ Schema_base ], "properties" : { "other_pro1" : {"type" : "string"}, "other_pro2" : {"type" : "String "}}, "required" : [" other_PRO1 "," other_PRO2 "]} Json data must meet the Schema_base requirement and have the attributes "other_PRO1" and "other_PRO2".Copy the code
  1. The keywordnotSpecifies that Json does not satisfy the Schema for not
{" not" : {"type" : "string"}}Copy the code

9. Complex structure

  • ** Keyword: none **
To define a type, no special keywords are required. The common practice is to define schemas that need to be referenced multiple times under definations of the root node. Definations is a JSON object, key is the name of the "type" you want to define, and value is a JSON schema {" Definitions ": {"address": {"type": "object", "properties": { "street_address": { "type": "string" }, "city": { "type": "string" }, "state": { "type": "String"}}, "required": ["street_address", "city", "state"]}}, "type": "object", "properties": {"billing_address": { "$ref": "#/definitions/address" }, "shipping_address": { "$ref": The "#/definitions/address" example above defines an address schema and references it in two places, '#/definitions/address' representing the path from the rootCopy the code
  • ** keyword: $id**
You can add an ID attribute to the above definition so that the schema can be referenced by the ID attribute, so that the value of the ID attribute does not require a full path. "address": { "type": "object", "$id" : "address", "properties": { "street_address": { "type": "string" }, "city": { "type": "string" }, "state": { "type": "string" } }, "required": ["street_address", "city", "state"] }Copy the code
  • $ref** $ref**
The keyword '$ref' can be used anywhere you want to use JSON Schema. In the example above, the value of billing_address should be a JSON schema, replaced by a '$ref'. The value of '$ref' is the path of the schema defined in JSON, starting with # to represent the root node. { "properties": { "billing_address": { "$ref": "#/definitions/address" }, "shipping_address": { "$ref": "#/definitions/address"}} if the schema defines a $ID attribute, it can be referenced by the value of that attribute. { "properties": { "billing_address": { "$ref": "#address" }, "shipping_address": { "$ref": "#address" } } }Copy the code

10. Common keywords

  • enum** Keyword: enum**
Can appear in any JSON schema, and its value is a list, indicating that the value of the JSON data can only be a certain {"type": "string", "enum": ["red", "amber", "green"]} The schema above specifies that the data must be a string and must be one of "red", "amber", or "green".Copy the code
  • metadata** Keywords: title, description, default, example**
{ "title" : "Match anything", "description" : "This is a schema that matches anything.", "default" : "Default value", "examples" : ["Anything", 4035]} Are used only for description and do not affect data verificationCopy the code