https://github.com/davydovanton/event_schema_registry
Simple implantation of schema registry for JSON schema events
https://github.com/davydovanton/event_schema_registry
events microservices schema-registry
Last synced: 8 months ago
JSON representation
Simple implantation of schema registry for JSON schema events
- Host: GitHub
- URL: https://github.com/davydovanton/event_schema_registry
- Owner: davydovanton
- Created: 2020-07-20T13:27:45.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-08-15T18:05:31.000Z (over 2 years ago)
- Last Synced: 2025-03-29T05:51:18.257Z (9 months ago)
- Topics: events, microservices, schema-registry
- Language: Ruby
- Homepage:
- Size: 11.7 KB
- Stars: 17
- Watchers: 2
- Forks: 13
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Event schema registry
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.
## Setup
### Ruby
Add this line into your Gemfile:
```
gem "schema_registry", git: "https://github.com/davydovanton/event_schema_registry.git"
```
## How to add a new event schema
For example, you want to create `billing.refund` event. For make it happen you need:
1. Create a new file `domain/event_name/version.json` in `schemas/` folder. For `billing.refund` it will be `schemas/billing/refund/1.json` (because all new events should be first version;
2. 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"
]
}
```
## How to validate an event data by specific schema
### Ruby
For validating event data you need to use `SchemaRegistry#validate_event` method with following options:
* `data` - event data
* `name` - name of event which you will use for getting schema
* `version` - version of event data schema (default `1`)
Example:
```ruby
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
```
## How to use this library with producer
### Option one: with event object
```ruby
result = SchemaRegistry.validate_event(event, 'billing.refund', version: 1)
if result.success?
kafka.produce('topic', event.to_json)
end
```
### Option two: with pure hash
```ruby
result = SchemaRegistry.validate_event(event, 'billing.refund', version: 1)
if result.success?
kafka.produce('topic', event.to_json)
end
```