nmcalabroso
I’m new to elixir and functional programming, in general. I want to ask what’s the idiomatic way to validate our input parameters? Say I want to do this in my UserController.create.
Check if password is valid by
PasswordManager.valid?/1
If not, render error
else, continue
This is what I’ve come up with:
defmodule MyApp.UserController do
def create(conn, %{"data" => data}) do
case PasswordManager.valid? user_attrs["password"] do
{:ok, raw_password} ->
# make changeset with hashed password
case Repo.insert(changeset) do
{:ok, user} ->
# success
{:error, invalid_changeset} ->
# render failure
{:error, reason} ->
render(conn, :errors, data: reason)
end
end
end
The issue here is the “ugly” nesting seems unnecessary. I’m looking for a “return early” statements but I guess elixir wants us to write pure functions as much as possible and utilize pattern matching accordingly. How do I refactor this?
Coming from python, I would do this:
def create(user_attrs):
if not PasswordManager.is_valid(user_attrs['password']):
# display error
return
# continue
But what’s the best practice in elixir?
Feel free to do some nitpicks to my code so I can improve writing in this language.
Also, please do note that I’m asking this as a generic question. I hope you don’t dwell too much with the password validation as an example. Thank you.
Trending in Questions
Other Trending Topics
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
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
wmnnd
Hello @nmcalabroso,
in Elixir and other functional languages, you cannot »return early« as you would do in Python or Ruby.
In your particular case, you could use the
withkeyword in order to avoid nesting and chain pattern-matched conditions together:You can read more about the
withkeyword in the Elixir Getting Started Guide: http://elixir-lang.org/getting-started/mix-otp/docs-tests-and-with.html#withAnd here is a nice blog article about the
withkeyword and what’s called »Railway-oriented programming« in the F# world: http://www.scottmessinger.com/2016/03/25/railway-development-in-elixir-using-with/cpgo
Maybe extract the call to
PasswordManager.valid?to your schema?This post explains how to implement a custom validation.
Edit: I think this stackoverflow answer demonstrates better how to use
add_error.nmcalabroso
Agreed. Maybe I just chose a poor example. But what I’m trying to get at is how do we validate and “return early” in elixir? Of course, we can’t. But what’s the idiomatic way for this scenario?
michalmuskala
I would say the most natural way of emulating “early return” would be to split the logic into multiple functions each one returning or continuing to the next one. The
withconstruct is kind of a “get out of jail free card” - it allows you to encode those patterns less verbosely in a single expression. I’d say that’s the most “common” way to do this.In case of ecto, the most idiomatic way of doing validations is to use, as mentioned, changesets.
nmcalabroso
Hi @michalmuskala,
Can you give an example for this? Apologies if this is considered spoon-feeding but I genuinely interested to know.
How would you emulate this the elixir-way?
nmcalabroso
Definitely reading this. Thanks a lot.
NobbZ
Nope, I think its meant like this: