dry-rb / dry-schema

Coercion and validation for data structures
https://dry-rb.org/gems/dry-schema
MIT License
425 stars 109 forks source link

"Object doesn't support #inspect" error raised while validating object that contains an array type attribute that contains the custom objects #380

Open dawlib opened 3 years ago

dawlib commented 3 years ago

Describe the bug

Object doesn't support #inspect error is raised when validating an object shown in the example below.

To Reproduce

require 'dry/schema'

module Types
  include Dry::Types()
end

class MyItem1 < Dry::Schema::JSON
  define do
    required(:name).filled(Types::String.enum(*%w[a]))
    optional(:time).filled(:time)
  end
end

class MyItem2 < Dry::Schema::JSON
  define do
    required(:name).filled(Types::String.enum(*%w[b]))
    optional(:time).filled(:time)
  end
end

class MyList < Dry::Schema::JSON
  define do
    required(:list).value(:array).each { MyItem1.new | MyItem2.new }
  end
end

item1 = { 'name' => 'a', 'time' => Time.now.iso8601 }
item2 = { 'name' => 'b', 'time' => Time.now.iso8601 }

MyItem1.new.call(item1).failure? # => false
MyItem2.new.call(item2).failure? # => false
MyList.new.call('list' => [item1, item2]).failure? # => true

MyList.new.call('list' => [item1, item2])
(Object doesn't support #inspect)
=>

Expected behavior

I would expect to receive a proper result of validation while executing the command below:

MyList.new.call('list' => [item1, item2])

My environment