solnic / virtus

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects
MIT License
3.77k stars 228 forks source link

Strict mode cannot express nullable attributes #199

Closed MichaelXavier closed 10 years ago

MichaelXavier commented 10 years ago

Not sure what the solution is but this is not possible

class Foo
   include Virtus.model(:strict => true)
   attribute :bar, String, :default => nil # nullable somehow?
end

Foo.new # raises

Not sure the best way to express an attribute accepts nil but seems like a common use case. I don't think it fundamentally conflicts with strict.

dkubb commented 10 years ago

In DM1 what we did was add a :required option. We defaulted to false, but I think in this specific case we would want to default to true. To override it, you would then do:

class Foo
   include Virtus.model(strict: true)
   attribute :bar, String, required: false
end

I'm not sure what api @solnic has planned, but I always found this quite nice.

solnic commented 10 years ago

Yeah I guess :required would be OK. With axiom-types we can set lots of different constraints so that's cool. I just want to somehow make it clear that virtus doesn't have any validation system and it shouldn't be confused with it.

solnic commented 10 years ago

@MichaelXavier this is now available in beta7

MichaelXavier commented 10 years ago

Wow. I file this ticket and go to bed. Wake up and it is on rubygems. Thanks!

solnic commented 10 years ago

@MichaelXavier :smile: I hope it works for you