Adzz
Casting Sugar - Announcing EctoMorph, a lib to help casting between structs for Ecto
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: https://github.com/Adzz/ecto_morph
I went into more detail here: https://medium.com/@ItizAdz/ecto-cast-ing-sugar-31bddbc62cd7
Most Liked
Adzz
Just by way of a feature update here are some of the things that are now available with ecto_morph:
- Bug fix: We now ignore through associations when casting or generating changesets. Those kinds of associations are readonly anyway, so we just ensure we don’t error.
- to_struct has been renamed to
cast_to_struct. - You can specify a list of fields to filter the changes by, like in ecto, but this is recursive like so:
%{comments: [%{like: %{count: 10, ignored: "okay"}}}]}
|> EctoMorph.cast_to_struct(Post, [comments: [like: :count]])
You can now validate nested relations
json = %{
"has_many" => [
%{"steamed_hams" => [%{"pickles" => 1}, %{"pickles" => 2}]},
%{"steamed_hams" => [%{"pickles" => 1}]},
%{"steamed_hams" => [%{"pickles" => 4}, %{"pickles" => 5}]}
]
}
# Here each of the steamed_hams above will have their pickle count validated:
EctoMorph.generate_changeset(json, MySchema)
|> EctoMorph.validate_nested_changeset([:has_many, :steamed_hams], fn changeset ->
changeset
|> Ecto.Changeset.validate_number(:pickles, greater_than: 3)
end)
2
Popular in Libraries
I just released the first version of Temple: an HTML DSL for Elixir and Phoenix!
You can read this blog post or the docs for more info...
New
TypeCheck: Fast and flexible runtime type-checking for your Elixir projects.
Core ideas
Type- and function specifications are const...
New
Image is an image processing library for Elixir. It is based upon the fabulous vix library that provides a libvips wrapper for Elixir.
I...
New
Hey everyone :wave:
Excited to join this forum - I am one of the founders and current project maintainers of a popular and open-source U...
New
Dear Elixir community,
After a year of development, bug fixes, and improvements, we are proudly ready to share the release of Crawly 0.1...
New
Hi everyone,
I’m coming from the Symfony (PHP) framework. I like Phoenix, but it has a one thing that was build much better in the Symfo...
New
If you would like to migrate away from node/npm/webpack while still using sass, the dart_sass package provides a installer and runner for...
New
I’ve just released version 3 of Comeonin, a password hashing library.
The following small changes have been made:
changes to the NIF c...
New
Hello everybody :wave:
Recently, some of my colleagues talked about database ids and uuids and their problems, and I remembered the pain...
New
While not as prevalent as in imperative languages, arrays (collections with efficient random element access) are still very useful in Eli...
New
Other popular topics
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
I would like to know what is the best IDE for elixir development?
New
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
Hi everyone,
One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New







