Adzz
Ecto is great, we use it for validations. But validating different embeds can quickly get confusing, and there are some ways I found it a bit cumbersome.
I’ve created a lib to help add some sugar to what Ecto does, we’ve been using it in prod and it has been going well so far. Some examples, say you have a schema like this:
defmodule Embed do
use Ecto.Schema
embedded_schema do
field(:bar, :string)
end
end
defmodule Test do
use Ecto.Schema
embedded_schema do
field(:thing, :string)
embeds_one(:embed, Embed)
end
end
Usually you would have to do this:
Ecto.Changeset.cast(%Test{}, %{"thing" => "foo", "embed" => %{"bar"=> "baz"}}, [:thing])
|> Ecto.Changeset.cast_embed(:embed)
With EctoMorph you can do this:
EctoMorph.to_struct(%{"thing" => "foo", "embed" => %{"bar"=> "baz"}}, Test)
I would love feedback on whether people think this is useful, or feature requests!
Repo: GitHub - Adzz/ecto_morph: morph your Ecto capabilities into the s t r a t o s p h e r e ! · GitHub
I went into more detail here: https://medium.com/@ItizAdz/ecto-cast-ing-sugar-31bddbc62cd7
Trending in Announcing
Hey everyone!
Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application.
This library uses Erlang esaml to provide
plug enabl...
New
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries.
offset-based pagination with...
New
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub.
Docs are at OpenaiEx User Gu...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi all!
I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas.
You...
New
Hello
Published a new library - ProcessHub!
ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New
Hey folks,
I just published a post about Hologram’s funding and where the project goes next - the short version:
Curiosum as Main Spons...
New
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using.
We’re particularly inte...
New
It’s not that it’s vocabulary is too advanced. It’s something worse.
I get lost trying to follow even a paragraph written by Claude. It’...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #elixirconf-us
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
The Medium post mentions: “This is fine if you have defined those functions, but less good if you don’t want to put a changeset function on the schema.” as why you’d want this library.
But I’m not at all clear why defining
Test.changesetandEmbed.changesethere is undesirable. The code in those functions is tightly coupled to the schema’s field names, so it makes sense to keep it in the same module.Adzz
Yea fair point. I suppose a possible counter point is, imagine if you wanted multiple changeset functions for a schema, say to validate it differently depending on the action a user is taking. I think keeping the schema as a very thin definition is nice, similar to how in rails you might want to keep your models thin.
There is some coupling, I think a changeset function and the schema definition have different concerns. Also the coupling is reduced if the fields referenced in the schema are referenced dynamically, as they are in EctoMorph.
Adzz
Just by way of a feature update here are some of the things that are now available with ecto_morph:
cast_to_struct.You can now validate nested relations