JSON Schema is a declarative language that allows you to annotate. It provides a format for what JSON data is required for a given application and how to interact with it and validate JSON documents to ensure it meets the requirements.
Applying JSON Schemas validation in your solutions will let you enforce consistency and data validity across similar JSON data.
If you are not familiar with JSON Schema, you will then notice that the JSON Schema itself is written in JSON-based format. It’s just a declarative format for “describing the structure of other data”. This is both its strength and its weakness (which it shares with other similar schema languages). It is easy to concisely describe the surface structure of data, and automate validating data against it. However, since a JSON Schema can’t contain arbitrary code, certain constraints exist on the relationships between data elements that can’t be expressed. JSON Schema is a proposed IETF standard.
JSON Schema Validation Function
The JSON Schema Validation is a simple Azure Function that allows you to validate your JSON message against a JSON Schema, enabling you to specify constraints on the structure of instance data to ensure it meets the requirements.
The function receives a JSON payload with two properties:
- The JSON message in the json property.
- And the JSON Schema in the jsonSchema property.
Example:
{
"json": {
"name": "",
"extension": "xml"
},
"jsonSchema": {
"type": "object",
"properties": {
"name": {
"type": "string",
"pattern": "^.*[a-zA-Z0-9]+.*$"
},
"extension": {
"type": "string"
}
}
}
The function’s output will be:
- A 200 OK if the JSON message is valid.
- Or a 400 Bad Request if there are validation errors/issues.
Where can I download it?
You can download the complete Azure Functions source code here:
Hope you find this helpful! So, if you liked the content or found it helpful and want to help me write more content, you can buy (or help buy) my son a Star Wars Lego!
Big thanks to my team member Diogo Formosinho for testing and helping me develop this function with me!