Git Product home page Git Product logo

event_schema_registry's Introduction

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:

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

result = SchemaRegistry.validate_event(event, 'billing.refund', version: 1)

if result.success?
  kafka.produce('topic', event.to_json)
end

Option two: with pure hash

result = SchemaRegistry.validate_event(event, 'billing.refund', version: 1)

if result.success?
  kafka.produce('topic', event.to_json)
end

event_schema_registry's People

Contributors

davydovanton avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.