json sechma data format validation

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

normal check
not null
after modification

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


insert image description here

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

insert image description here
insert image description here

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:

  1. For the elements of the map, we only need to compare a group
  2. 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

insert image description here

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"
        }
      }
    }
  }
}

insert image description here

Guess you like

Origin blog.csdn.net/Misszhoudandan/article/details/113729534