using JSON Schema for validating correctness of json
JSONSchema is way of defining what shape your desired JSON should have and what certain fields type and content should be.
JSON Schema is a vocabulary that allows you to annotate and validate JSON documents.
project’s webiste: http://json-schema.org/
GREAT DOCS ❤️https://spacetelescope.github.io/understanding-json-schema/index.html
in python project you can use this pip installed package https://pypi.python.org/pypi/jsonschema
example validation:
Defining list of basic types vs list of objects
You define list of basic types, like e.g "hobbys"
in employee just by defining "type"
inside "items"
dict:
while if you need to define list of certain shape objects you have to define "type": "object"
and define "type"
of each "properties"
inside "items"
:
More options to validate:
- each of field can be in
required
list to make it obligatory - for lists you can define
maxItems
,minItems
to control allowed number of objects. - for ensuring list object uniqness just provide
"uniqueItems": true
this works only on basic data types (not on objects) - you can validate strings with regex by using
pattern
like in above example for valid date inYYYY-MM-DD
- tuple validation may be something nice for structured data types https://spacetelescope.github.io/understanding-json-schema/reference/array.html#tuple-validation