achedeuzot
Ecto_commons – Ecto common helper functions such as Changeset validators
This library contains common helpers used with Ecto.Changeset.
I noticed myself copying over validators from various projects multiple times. So I figured I’d create a common library. I know everyone likes to have their own validation logic but I tried keeping it as generic as possible so it’ll be useful for many situations.
https://github.com/achedeuzot/ecto_commons
For now, it contains validators for the following cases:
Date,DateTimeandTimeEmailValidator: validate emails and exclude temporary email providersURLValidator: validate URLs with various levels of strictnessStringValidator: validate a string has a given prefixPostalCodeValidator: validate postal codes for multiple countries (to be improved)SocialSecurityValidator: validate social security number (SSN)LuhnValidator: validate Luhn-type numbers such as credit card numbers and other administrative codes.
Happy to receive feedback, pull requests from motivated folks and ideas for improvement
I hope this lib will grow into a set of good common ecto helpers and tools we can all benefit from.
Cheers !
Most Liked
achedeuzot
Hi folks !
I’ve published v0.2.0 with your feedbacks.
- I’ve improved the URLValidator by better documenting what is and what isn’t supported.
- I’ve added all countries for the PostalCodeValidator (still no full database but a first sanity check of the format). The formats come from http://i18napis.appspot.com/, recommended by the (now deprecated) unicode postal code database.
- I’ve added better documentation to EmailValidator and an
opt-inapproach to which checks you want to apply. I also addedpow’s package email validator and fixed some of the shortcomings found by @hauleth. - I’ve added the
validate_manyhelper as it’s indeed common to validate multiple fields with the same options.
As usual, happy to have your feedbacks, issues and pull requests ![]()
Cheers ![]()
Kurisu
Another helper I used not exactly for changeset but when using Ecto.Multi:
@doc """
Normalize Ecto.Multi transaction result so it returns
{:ok, value} or {:error, failed_value}
"""
def normalize(result, key \\ nil)
def normalize({:error, _, failed_value, _}, _key), do: {:error, failed_value}
def normalize({:ok, result}, key), do: {:ok, Map.get(result, key, result)}
def normalize(result, _key), do: result
Example of usage:
def create_slider(attrs \\ %{}) do
changeset = Slider.changeset(%Slider{}, attrs)
Multi.new()
|> Multi.insert(:slider, changeset)
|> Multi.update(:slider_with_image, &Slider.image_changeset(&1.slider, attrs))
|> Repo.transaction()
|> MyLib.Multi.normalize(:slider_with_image)
end
This way in the Phoenix controller we don’t have to change the classic way to handle CRUD actions.
hauleth
Seems that this is still not fully valid, as this do not allow [::1] as a domain part. Additionally it will allow .foo. as a domain, which is incorrect.
Popular in Announcing
Other popular topics
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
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








