fceruti
Hey everyone!
Saša Jurić on his talk called Clarity @ ElixirConf EU 2021, mentions a way of architecting your code that uses a function that he names normalize/2, but sadly, he says he doesn’t have time to share it.
I want to implement this programming style, but I’m missing this key ingredient. Do you know any libraries or gist that accomplish this? Thanks ![]()
def register(conn, params) do
schema = [
email: {:string, required: true},
password: {:string, required: true}
date_of_birth: :datetime,
# ...
]
with {:ok, params} <- normalize(params, schema),
{:ok, user} <- MySystem.register(params) do
# respond success
else
{:error, reason} ->
# respond error
end
end
update: if it’s not clear, I’m looking for a way of checking the existence and type of all the fields specified in schema.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
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
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Marked As Solved- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
riebeekn
I’ve not used it myself, but the tarams library looks like it might handle your use case if you want to go with a library instead of a custom solution GitHub - bluzky/tarams: Cast and validate external data and request parameters for Elixir and Phoenix · GitHub
Also Liked
sasajuric
Yes, on both accounts. I can’t share the code (it’s owned by the clients), but the basic take is something like
This is probably not enough (e.g. you may want to handle nils and missing keys), but it’s a solid start.
tomekowal
I wanted to reply on Twitter but I saw there is already a solution in here. I’d like to share my anyway
Since we need to traverse the schema many times, I normalize it first and then use list comprehensions like this:
The line computing defaults might be tricky to understand because it is long and introduces a variable inside the comprehension filter.
Also, I’d vote for naming that function “parse” instead of normalize. Parsing is an action of taking a bunch of data and trying to transform it into a structure that is understandable. In the spirit of Parse, don’t validate
It would be possible to push the schema specification even more to introduce validations like:

email: {:string, format: ~r/@/}and then call
Changeset.validate_format(changeset, key, format)for each. But that would require another option for each Ecto.Changeset validatior. It might be an overkill but the schema format is very readable so it is temptingstefanchrobot
Just a few remarks if you don’t mind:
I’d go with something longer and less generic than
k(key?) andv(value?). Maybe{field, schema}? Plus in this case I think it makes sense to use{_k, v}to explain the meaning of the first element.Using more pattern matching would be more idiomatic:
And one more thing:
can be replaced with:
Last Post!
IvanR
If you are up to a declarative approach and dependency inversion towards your application core’s data types, you can use Domo library. That generates validators and constructor functions from a
t()type spec of the struct, and the type spec is validated for syntax correctness by elixir during the compilation.So the request can be accepted into the struct that looks like the following:
And the validation can be done like that:
The dependency inversion can be done by sharing
email()andpassword()in the shared module to have the same rules for emails and passwords across the whole app.Domo plays nicely with Ecto schemas because they are structs too. See Domo.Changeset for this kind of integration.