How can I share schemes between microservices?

On one of my microservices A, there is a conditionally such scheme:

schema = Dry::Validation.Schema do
  required(:email).filled
  required(:age).filled
end

How can I transfer the rules of this scheme to microservice B and initialize the same scheme there?

You can create a private gem and share any code like that between your projects.

This is a good solution, but I was hoping it could be done out of the box :slight_smile:

Gemfiles are basically your only code-sharing option in Ruby. One good thing about Bundler is that it’s smart enough to allow reading gems from subfolders of a larger project, so you wouldn’t necessarily need to split it out of the project it belongs to.

# Gemfile
path "schema" do
  gem "my_service-schema"
end

You could then define the schema subfolder with its own gemspec, and then any project that needs it would do:

gem "my_service-schema", git: "https://your-host.com/path/to/my_service.git", glob: "schema/*.gemspec"

I did this to share generated Ruby code from a protobufs definition, and it works well.

Yes, you right.

I was interested in the ability to initialize schemas, for example, from ast. It would be very convenient.

Another disadvantage of the approach through the gem is that let’s say I have a schema that I put in the gem. I can use this scheme on my other service. But I got a case when from this scheme I want to make my own custom and not one. Every time to go to the gem and add the scheme by hand is a bad option for me.

Therefore, I wanted to know if you can have some solution for this so that you can somehow dump the circuit, and then make a load.

I agree, the AST-Visitor approach the dry-rb libraries use is a powerful pattern, and there is a lot of untapped potential for code generation.

It would be really interesting to generate dry-types from RBI definitions, for instance. Or schemas from API definitions like Swagger or JSON Schema.

I think the primary reason why this hasn’t been done yet is it’s a lot of work. If I ever get around to building something generally useful like this, I’ll post about it.

Yes, it’s my dream to have this work out of the box :slight_smile:

In the vastness of github, I found a gem that can get the metadata from the schema. (This is interesting, by the way) But the problem is again that having a schema in json format, conditionally, I cannot initialize it without some magic.

P.S. my fork dry-metadata rep https://github.com/gogochikago/dry-metadata btw