Getting dry-types metadata into dry-schema's JSON Schema output

G’day everyone,

We are looking at having types something like this:

module Types
  include Dry.Types()

  Email = self::String.meta(
    json_schema: {
      description: "An email address",
      example: "person@example.com",
    }
  )
end

Which gets put into a schema like this:

schema = Dry::Schema.JSON do
  required(:email).filled(Types::Email)
end

And then converted into JSON schema including the metadata:

Dry::Schema.load_extensions(:json_schema)
schema.json_schema #=>
{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "type": "object",
  "properties": {
    "email": {
      "type": "string",
      "description": "An email address",
      "example": "person@example.com"
    }
  },
  "required": [
    "email"
  ]
}

To be included in an OpenAPI document.

Having a quick dig through dry-schema, it appears that the type metadata is not carried across to the schema, so there is no simple way to include it in the JSON Schema output. Does that sound correct?

Any ideas how we could achieve this?

@tomdalling if you extend the compiler so that it has access to type_schema, then you can look into meta-data too and use that info in the visit_key method. When I say type_schema I mean this:

dry-schema> schema.type_schema.to_ast
=> [:lax, [:schema, [[[:key, [:email, false, [:nominal, [String, {:json_schema=>{:description=>"An email address", :example=>"person@example.com"}, :required=>false, :maybe=>false}]]]]], {}, {}]]]

Ok cool, thanks for that. Would you be open to a PR that adds this functionality to the json_schema extension? /CC @ianks

1 Like

Yes, definitely :heart: