How to use JSON Schema to create Ecto schema and changesets?

I’m new to Elixir and Phoenix and I’ve been trying to prototype a difficult solution before committing to Phoenix for my new app, but I’m coming up empty handed and need some help!

I need to build dynamic forms using JSON Schema, and I’d prefer to do it in the backend to take advantage of LiveView and server side validation.

My idea was to convert between JSON Schema and Ecto schema/changeset so I could build and validate forms using the server. Every variation of the form has at least 3 unchanging fields and the rest can be saved as JSON in the database. Does this even make sense to do or should I just leverage some front end JS framework that already has support for JSON schema/forms and a rest api?

So far I’ve tried leveraging json_xema, xema, and also failed to build a macro to define a dynamic module with the fields from the JSON Schema (which might be the best solution?)… I’m totally stuck.

In PHP or Python I could probably use a form builder to get this done, but I’m out of my element with Elixir and any sample code, pointers, links to tutorials or docs are appreciated.

Pete

Are you aware of Schemaless Changesets? It allows the use of schemas from pure data.

1 Like

Yeah, schemaless changesets should serve you fine, you need to write functions to convert the JSON schema to their format but that’s IMO relatively easy.

Thank you, it seems schemaless changesets just might do the trick. :slight_smile:

Wondering how successful were you at your task? We now need to do the same at work.

Do you have any tips or code examples?