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

Allow_nil: true not working #9

Closed
majksner opened this issue Jun 13, 2019 · 6 comments
Closed

Allow_nil: true not working #9

majksner opened this issue Jun 13, 2019 · 6 comments

Comments

@majksner
Copy link

Hi @DmitryTsepelev,

Thanks for the gem! It seems allow_nil: true not working, when I try to save model with json attributes set to nil it raises

raise StoreModel::Types::CastError,
                "failed casting #{value.inspect}, only String or Array instances are allowed"

or

raise StoreModel::Types::CastError,
                "failed casting #{value.inspect}, only String, " \
                "Hash or #{@model_klass.name} instances are allowed"

If I add

when nil then value

in both array_type.rb and json_type.rb everything works fine.

@DmitryTsepelev
Copy link
Owner

🤦‍♂ I guess I know where it comes from - investigating

@majksner
Copy link
Author

majksner commented Jun 13, 2019

[27] pry(main)> machine = Machine.new(project_id: 1, configuration: nil)
=> #<Machine:0x00007fdeae95dfc8
 id: nil,
 project_id: 1,
 configuration: nil>
[28] pry(main)> machine.save!
   (0.3ms)  BEGIN
   (0.3ms)  ROLLBACK
StoreModel::Types::CastError: failed casting nil, only String, Hash or MachineConfig instances are allowed
from /Users/test/.rvm/gems/ruby-2.6.3/gems/store_model-0.3.0/lib/store_model/types/json_type.rb:22:in `cast_value'

@DmitryTsepelev
Copy link
Owner

@majksner Thank you for catching this! Just released 0.3.1 with the fix according to your suggestion

@majksner
Copy link
Author

You're welcome, I could still see one problem and that is we don't check if allow_nil: true is set anywhere in the code.

@DmitryTsepelev
Copy link
Owner

In this case we use a regular ActiveRecord validation, and allow_nil is shipped out of the box

@majksner
Copy link
Author

👍🏻 Thanks for the such a quick fix!

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

No branches or pull requests

2 participants