Ecto Schema to JSON Schema

I’m trying to figure out the best way to convert a bunch of Ecto schemata into respective JSON schemata for use with react-jsonschema-form.

First, I tried using SchemaDefiningModule.__schema__(:types) and mapping the types. This worked well until I realized that there is not an easy way to check if something is an enum. I have a lot of enums defined in the Postgres side.

I decided that a better solution would be to make my own DSL that basically copies Ecto.Schema with a few additional flags to delineate enum types and a few others. But it seems like a lot of work for only a little return on investment.

Instead I decided to make a rudimentary mix task that does a rough estimate of what the schema should be and then edit that output by hand. It still isn’t really the solution I was wishing for.

I was wondering if you could steer me in the right direction or tell me how you’d tackle this problem.

1 Like

How about creating your own schema DSL that expands to an Ecto schema definition and a JSON schema definition?

I’ve wanted something similar for declaring embedded schemas for casting and validating request params with Ecto, and including the JSON schema in a swagger spec.

Please share whatever you come up with

2 Likes