trenpixster / addict

User management lib for Phoenix Framework
MIT License
645 stars 99 forks source link

extra_validation function reference #70

Closed AvaelKross closed 8 years ago

AvaelKross commented 8 years ago

I'm very new to elixir, phoenix and addict, so maybe it's a dumb question. I created new app, added 'addict', configured it all properly - everything works well locally. Now I want to deploy my app, so I'm running mix release. First time I did it, I faced this error:

Cannot add file sys.config to tar file - [{error,{83,erl_parse,["syntax error before: ",["Fun"]]}}]
===> Provider (tar) failed with: {error,
                                         {rlx_prv_archive,
                                          {tar_unknown_generation_error,
                                           "blog_phoenix","0.0.1"}}}

I checked sys.config file and found that it fails on this line of config.exs:

extra_validation: fn ({valid, errors}, user_params) -> {valid, errors} end,

It's being converted to this content in sys.config: {extra_validation,#Fun<erl_eval.12.50752066>}

Then I found configs.md here and tried to replace this: extra_validation: fn ({valid, errors}, user_params) -> {valid, errors} end with this: extra_validation: BlogPhoenix.User.validation

Of course I added this to my User model:

def validate({valid, errors}, user_params), do: {valid, errors}

Now 'mix release' fails with undefined function BlogPhoenix.User.validate/0 (module BlogPhoenix.User is not available).

Do you have any idea how to fix it and why initial code didn't worked at all?

trenpixster commented 8 years ago

Hey @AvaelKross! No idea why it didn't work, will have to try to make a release and get back to you, thanks for reporting!

Regarding your new function, thanks for the heads up, that example is wrong. The way to capture a function, instead of invoking it, is via &your_func/n, where n is the number of arguments to the function. In your case, that would be

# config.exs
(...)
    extra_validation: &BlogPhoenix.User.validate/2
(...)

Check the official docs here for more information into this :)

AvaelKross commented 8 years ago

@trenpixster It still does not work during mix release.

AvaelKross commented 8 years ago

As a temporary solution I made a fork where I call configs.user_schema.addict_validate function explicitly in register.ex. It's like a workaround, but may be useful for someone else

0b01 commented 7 years ago

Any update on this?

0b01 commented 7 years ago

I'm a newbie but according to this guy the config has to be an MFA tuple(don't know what that is)

Relevant links: https://github.com/bitwalker/distillery/issues/185#issuecomment-285924281 https://github.com/mrluc/deferred_config/blob/master/README.md

AvaelKross commented 7 years ago

@rickyhan with latest versions of elixir/phoenix/this module, the following code works to me:

extra_validation: {AppName.ModelName, :addict_validate_method_name}

0b01 commented 7 years ago

I dug into the source code and found the Addict.Helper.exec({mod, func}, args) function. So I logged the module name and function name. The module name has Elixir appended in the front: Elixir.ModuleName.ModelName

Is it the expected scoping behavior?

mikaoelitiana commented 7 years ago

@AvaelKross's solution solved this issue for me.