json sechma format comparison
Sechma usage introduction:
http://xaber.co/2015/10/20/JSON-schema-%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B%E6%96 %87%E6%A1%A3/https://json-schema.org/understanding-json-schema/index.html
online comparison of sechma format: http://json-schema-validator.herokuapp.com/index.jsp
online generation of sechma format: same as above
For the use and meaning of the entry, you can refer to the link above for learning.
The following is the accumulation of problems I encountered in my work.
1. When the format in sechma is null, only null in the data will match, and the others will not match. For fields that may return null, multiple modes are used
2. For undefined fields in sechma, fields with extra data pass the default verification, and if additionalProperties is specified as false, the verification will fail
sechma format
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"Array": {
"type": "array",
"items": {
"type": "number"
}
},
"Null": {
"type": "null"
},
"Object": {
"type": "object",
"properties": {
"12345": {
"type": "string"
},
"Number": {
"type": "number"
},
"String": {
"type": "string"
}
}
}
},
"additionalProperties": false
}
data
{
"Array": [
1,
2,
3
],
"Null": null,
"Object": {
"Number": 123,
"12345": "object",
"String": "Hello World"
},
"add":"nothing"
}
3. For the fields defined in sechma, the data missing fields pass the default verification, and required specifies which attributes are required, and the verification fails
sechma format
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"Array": {
"type": "array",
"items": {
"type": "number"
}
},
"Null": {
"type": "null"
},
"Object": {
"type": "object",
"properties": {
"12345": {
"type": "string"
},
"Number": {
"type": "number"
},
"String": {
"type": "string"
}
},
"required": [
"String"
]
}
}
}
data
{
"Array": [
1,
2,
3
],
"Null": null,
"Object": {
"Number": 123,
"12345": "object"
}
}
4. The meaning of object in sechma includes two meanings of map and the key in the map is dynamic. For example, the business id is often used as the key of the map in the project
At this time we need to pay attention to two points:
- For the elements of the map, we only need to compare a group
- For dynamically changing keys, we can use patternProperties to match keys
For example, user information includes, userId, name, age, where the key is userId
{
"666": {
"name": "易烊千玺",
"age": 22
},
"777": {
"name": "彭于晏",
"age": 37
}
}
Generated sechma format
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"666": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number"
}
}
},
"777": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number"
}
}
}
}
}
For example, the information of user Wu Yanzu we will verify next time is
{
"888": {
"name": "吴彦祖",
"age": 55
}
}
It is invalid to check directly according to the above sechma. The "888" attribute is not matched, same as 2 above, passed by default, as shown in the figure below
Modify, modify the sechma attribute to match the pattern patternProperties, and then perform regular matching on the attribute
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"patternProperties": {
"^[0-9]+$": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number"
}
}
}
}
}