Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom error keys returned from validate function #73

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

cichaczem
Copy link

Hi Sean,
I have a specific case: I'd like to user arc and arc_ecto to validate if extension and MIME type of uploaded file are allowed in the current schema and show the reason of invalidity to the user. I changes validate function. Now it enforces Tuple as the return type {:ok, :valid} or {:error, :reason}.

Unfortunately, it won't work for me because of Ecto cast function which return type is either a Tuple {:ok, casted_valie} or an Atom :error.

I'm creating pull request because the code works and some people may find it useful. Feel free to either accept or reject it.

For now only and probably the best way of solving my problem is to run the validations twice. Once in validate function and once after parameters have been cast to their corresponding Ecto types.

I'll gladly accept any advice regarding this case.

Best,
Dawid

@@ -7,7 +7,13 @@ defmodule ArcTest.Actions.Store do
use Arc.Actions.Store
use Arc.Definition.Storage

def validate({file, _}), do: String.ends_with?(file.file_name, ".png")
def validate({file, _}) do
case String.ends_with?(file.file_name, ".png") do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is considered as a bad practice. case with only true and false should be rewritten as if.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants