JSON schemas

A JSON schema is a vocabulary that describes JSON data and enables the verification of data validity. It can be used for testing and it can be used as documentation. The schema specifies the type of an element and can add additional checks on its value. If the type is an array, it can also specify the type and details of each element. If the type is an object, it describes its fields. Let's see a JSON schema for the Character struct that we used in the examples:

{
"type": "object",
"properties": {
"name": { "type": "string" },
"surname": { "type": "string" },
"year_of_birth": { "type": "number"},
"job": { "type": "string" }
},
"required": ["name", "surname"]
}

We can see that it specifies an object with all of its fields and indicates which fields are mandatory. There are some third-party Go packages that permit us to verify JSON against schema very easily such as github.com/xeipuuv/gojsonschema.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.255.187