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

Strict mode only works for basic values #393

Open
arianf opened this issue Dec 25, 2018 · 4 comments
Open

Strict mode only works for basic values #393

arianf opened this issue Dec 25, 2018 · 4 comments

Comments

@arianf
Copy link

arianf commented Dec 25, 2018

require 'virtus'

class SomeRandomClass
end

class User
  include Virtus.model(strict: true)

  attribute :some_ran_class, SomeRandomClass
end

User.new(some_ran_class: 5) # no error

User.new() # no error

User.new(some_ran_class: SomeRandomClass.new) # should be the only one that doesn't cause an error
@arianf
Copy link
Author

arianf commented Dec 25, 2018

This is because if the type is not a recognized primitive, it defaults to Object, and coercing an object to an object will always work.

But I feel like if you have strict mode enabled, you should only be able to have the Class type you specified

@celesteking
Copy link

Just came across this - what's the sense of specifying attribute type if that type isn't honored? That's a total disaster. You can assign random things to attribute and there's no casting done when you supply virtus model #new with a hash of stuff.

@arianf
Copy link
Author

arianf commented May 20, 2019

@celesteking you should check out https://github.com/AaronLasseigne/active_interaction

It’s a much better implementation

@celesteking
Copy link

Me? Nope, because it's 3rd party (elasticsearch-persistence) that's using this stale software.
Anyways, I've managed to find a workaround:

require 'ipaddr'

class Mymodel
  include Virtus.model

  class TypeCoercer
    define_singleton_method(:[]) do |cast_type|
      Class.new(Virtus::Attribute).class_eval do
        define_method(:coerce) do |value|
          value && cast_type.new(value)
        end
        self
      end
    end
  end

  attribute :myattr, TypeCoercer[IPAddr], default: nil
end

puts Mymodel.new(myattr: '1.2.3.4').myattr.class

VIRTUS CUSTOM COERCION NOT WORKING attribute not coercing

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