This repository is an example of how to make event schema registry for JSON schema events using only github. The general idea - how to share schemas across different services plus how to validate data for specific events.
Add this line into your Gemfile:
gem "schema_registry", git: "https://github.com/davydovanton/event_schema_registry.git"
For example, you want to create billing.refund
event. For make it happen you need:
- Create a new file
domain/event_name/version.json
inschemas/
folder. Forbilling.refund
it will beschemas/billing/refund/1.json
(because all new events should be first version; - Create a new json schema file like this:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Billing.Refund.v1",
"description": "json schema for billing refund event (version 1)",
"definitions": {
"event_data": {
"type": "object",
"properties": {
// event specific information here
},
"required": [
]
}
},
"type": "object",
"properties": {
"event_id": { "type": "string" },
"event_version": { "enum": [1] },
"event_name": { "type": "string" },
"event_time": { "type": "string" },
"producer": { "type": "string" },
"data": { "$ref": "#/definitions/event_data" }
},
"required": [
"event_id",
"event_version",
"event_name",
"event_time",
"producer",
"data"
]
}
For validating event data you need to use SchemaRegistry#validate_event
method with following options:
data
- event dataname
- name of event which you will use for getting schemaversion
- version of event data schema (default1
)
Example:
message = {
# ...
}
# will try to search `schemas/Billing/CompliteCycle/1.json` file
result = SchemaRegistry.validate_event(data, 'Billing.CompliteCycle', version: 1)
# will try to search `schemas/billing/complite_cycle/1.json` file
result = SchemaRegistry.validate_event(data, 'billing.complite_cycle', version: 1)
# After you can work with result object
result.success?
result.failure?
result.failure
result = SchemaRegistry.validate_event(event, 'billing.refund', version: 1)
if result.success?
kafka.produce('topic', event.to_json)
end
result = SchemaRegistry.validate_event(event, 'billing.refund', version: 1)
if result.success?
kafka.produce('topic', event.to_json)
end