-
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
Elements with specific type #351
Comments
This is correct behaviour, how would you coerce "baz" to a Integer? You can filter it yourself
|
@zenry no it's not and it never will be. If a user asks for an integer array and a non-integer is given it should throw at minimum or reject at the extreme... that is correct behavior. |
Well I have strict mode on so in my case it will fail loudly. That said I would not like Virtus or any library for that matter to just discard elements in my array. Coerce in non strict mode means to me that it will try to coerce it into the proper type and leave the rest as is. What you guys want sounds more like a filter. |
I don't think it should coerce that's for sure, I think it should throw though. |
Agreed, but I also like that fact that you can turn that option on and off (strict mode) |
Hi! Second, default coersions are unexpectable. But coercing arrays is problem for me. class MyInteger < Virtus::Attribute
primitive Integer
def coerce(input)
res = super
res.is_a?(Integer) ? res : nil
end
end
class MyArray < Virtus::Attribute::Array
def coerce
super.uniq.compact
end
end
class Foo
include Virtus.model(nulify_blank: true)
attribute :int_array, MyArray[MyInteger]
end So, behaviour I look for, could be like this: Foo.new(int_array: ["", "1", 2, "1bar", "baz"]).int_array #=> [1, 2] In sources I found Also I could override getter like this. class Foo::ArrayCoercer
def initialize(array)
@array = array
end
def to_a
@array.uniq.compact
end
end
class Foo
include Virtus.model(nulify_blank: true)
attribute :int_array, Array[Integer]
def int_array
ArrayCoercer.new(super)
end
end |
I'm facing a similar issue. I want a custom Array attribute, that ignores class CompactedArray < Virtus::Attribute::Array
def coerce(value)
super&.compact
end
end
class Foo
include Virtus.mode
attribute :bars, CompactedArray[Bar]
end But |
Hi!
It doesn't seems like bug or something. But it's look pretty weird for me.
Here is my code:
Is there ways to drop attributes that wasn't converted to integer?
The text was updated successfully, but these errors were encountered: