smart_properties icon indicating copy to clipboard operation
smart_properties copied to clipboard

Explicitly passing nil default property fails required check

Open sambostock opened this issue 8 years ago • 1 comments

Given a property defined as follows:

class Foo
  include SmartProperties
  property :bar, required: true, default: true
end

The following code works, as expected:

Foo.new             # => #<Thing @bar=true>
Foo.new(bar: true)  # => #<Thing @bar=true>
Foo.new(bar: false) # => #<Thing @bar=false>

However, explicitly providing the bar property as nil fails:

Foo.new(bar: nil)
# SmartProperties::MissingValueError: Foo requires the property bar to be set

Is this desired behaviour? It would seem logical that explicitly passing in nil should fall through to the default, which would satisfy the required constraint.

sambostock avatar Oct 10 '17 03:10 sambostock

This is indeed specified behaviour: https://github.com/t6d/smart_properties/blob/master/spec/required_values_spec.rb. That said, I'm currently working towards a 2.0 release and will investigate whether making the behaviour configurable is an option.

t6d avatar Aug 18 '21 16:08 t6d