reform icon indicating copy to clipboard operation
reform copied to clipboard

since 2.3.0rc1, unnested properties error on validate when parent property is nil

Open lastobelus opened this issue 7 years ago • 1 comments

Since the upgrade to 2.3.0rc1, unnested properties cause an error on validate when the parent property is nil, which was not the case in 2.2.0. Is this intended? Is there any workaround?

Steps to reproduce

class VendorLineItemForm < Reform::Form
  property :line_item_id
  property :vendor_fulfillment do
    property :fulfillment_service_id
  end
  unnest :fulfillment_service_id, from: :vendor_fulfillment
end

This form is used as the item form for a collection property in another form, which has a populator that finds or creates a vendor_fulfillment for the incoming fulfillment_service_id, using nil if it can't find one or fulfillment_service_id is nil, and then uses it in a VendorLineItem.

Now, I don't actually want vendor_fulfillment to be nil, and I have validation that verifies that it is not and that the fulfillment_service_id belongs to a scoped set of ids.

This worked in 2.2.0, but after upgrading to 2.3.0rc1 it raises after population has run but before it gets to my validation:

       undefined method `fulfillment_service_id' for nil:NilClass
     # representable-3.0.4/lib/representable/serializer.rb:6:in `public_send'
     # representable-3.0.4/lib/representable/serializer.rb:6:in `block in <module:Representable>'
     # representable-3.0.4/lib/representable/pipeline.rb:18:in `evaluate'
     # representable-3.0.4/lib/representable/pipeline.rb:10:in `block in call'
     # representable-3.0.4/lib/representable/pipeline.rb:9:in `each'
     # representable-3.0.4/lib/representable/pipeline.rb:9:in `inject'
     # representable-3.0.4/lib/representable/pipeline.rb:9:in `call'
     # representable-3.0.4/lib/representable/binding.rb:32:in `compile_fragment'
     # representable-3.0.4/lib/representable.rb:53:in `block in call'
     # representable-3.0.4/lib/representable.rb:51:in `each'
     # representable-3.0.4/lib/representable.rb:51:in `call'
     # representable-3.0.4/lib/representable.rb:70:in `representable_map!'
     # representable-3.0.4/lib/representable.rb:45:in `create_representation_with'
     # representable-3.0.4/lib/representable/hash.rb:34:in `to_hash'
     # reform-0753ac23c0aa/lib/reform/form/dry.rb:76:in `input_hash'
     # reform-0753ac23c0aa/lib/reform/form/dry.rb:50:in `call'
     # reform-0753ac23c0aa/lib/reform/validation/groups.rb:48:in `block in call'
     # reform-0753ac23c0aa/lib/reform/validation/groups.rb:45:in `collect'
     # reform-0753ac23c0aa/lib/reform/validation/groups.rb:45:in `call'
     # reform-0753ac23c0aa/lib/reform/contract/validate.rb:27:in `validate!'
     # reform-0753ac23c0aa/lib/reform/contract/validate.rb:52:in `block (2 levels) in validate_nested!'
     # disposable-0.4.3/lib/disposable/twin/property_processor.rb:24:in `block in collection!'
     # disposable-0.4.3/lib/disposable/twin/property_processor.rb:24:in `each'
     # disposable-0.4.3/lib/disposable/twin/property_processor.rb:24:in `each_with_index'
     # disposable-0.4.3/lib/disposable/twin/property_processor.rb:24:in `each'
     # disposable-0.4.3/lib/disposable/twin/property_processor.rb:24:in `collect'
     # disposable-0.4.3/lib/disposable/twin/property_processor.rb:24:in `collection!'
     # disposable-0.4.3/lib/disposable/twin/property_processor.rb:16:in `call'
     # reform-0753ac23c0aa/lib/reform/contract/validate.rb:49:in `block in validate_nested!'
     # disposable-0.4.3/lib/disposable/twin/definitions.rb:27:in `block in each'
     # disposable-0.4.3/lib/disposable/twin/definitions.rb:27:in `each'
     # disposable-0.4.3/lib/disposable/twin/definitions.rb:27:in `each'
     # reform-0753ac23c0aa/lib/reform/contract/validate.rb:47:in `validate_nested!'
     # reform-0753ac23c0aa/lib/reform/contract/validate.rb:33:in `validate!'
     # reform-0753ac23c0aa/lib/reform/contract/validate.rb:11:in `validate'
     # reform-0753ac23c0aa/lib/reform/form/validate.rb:24:in `validate'

is there a workaround, or do I always have to create an empty VendorFulfillment in my populator if I want to use unnest on its properties?

lastobelus avatar May 17 '17 23:05 lastobelus

This looks as if it's an issue with the to_hash method in representable being called from here - https://github.com/trailblazer/reform/blob/master/lib/reform/form/dry.rb#L76

@apotonick any suggestions on how Representable could return nil instead of raise an error in these cases?

fran-worley avatar Jun 15 '18 07:06 fran-worley