-
Notifications
You must be signed in to change notification settings - Fork 230
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
Comments
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 |
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. |
@celesteking you should check out https://github.com/AaronLasseigne/active_interaction It’s a much better implementation |
Me? Nope, because it's 3rd party (elasticsearch-persistence) that's using this stale software. 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 |
The text was updated successfully, but these errors were encountered: