Schema
JSON
In Tansu a JSON schema defines the key and/or value of each message on a particular topic. The schema is named after the topic with a json file extension.
{
"title": "Person",
"type": "object",
"properties": {
"key": {
"type": "string",
"pattern": "^\\d{3}-\\d{2}-\\d{4}$"
},
"value": {
"type": "object",
"properties": {
"firstName": {
"type": "string",
"description": "The person's first name."
},
"lastName": {
"type": "string",
"description": "The person's last name."
},
"age": {
"description": "Age in years which must be equal to or greater than zero.",
"type": "integer",
"minimum": 0
}
}
}
}
}
The above JSON schema defines the properties of both key and value. The broker will validate any message that is produced to the person topic, rejecting any that do not conform with an INVALID_RECORD error.
Example
Create the person topic if not already present:
tansu topic create person
The cat subcommand can be used to produce protocol buffer data to a schema backed topic:
echo '{"key": "345-67-6543", "value": {"firstName": "John", "lastName": "Doe", "age": 21}}' | tansu cat produce person
A zero return code indicates that the data was accepted as valid by the broker:
echo $?
0
While invalid data (age must be zero or more according to the schema) is rejected by the broker validation:
echo '{"key": "ABC-12-4242", "value": {"firstName": "John", "lastName": "Doe", "age": -1}}' | tansu cat produce person
The message was validated and rejected by the broker, resulting in a non-zero return code:
echo $?
1
The cat subcommand can also be used to produce validated bulk data from a file using data/etc/persons.json:
tansu cat produce etc/data/persons.json