These are chat archives for dry-rb/chat

14th
Nov 2016
Maciej Mensfeld
@mensfeld
Nov 14 2016 08:11
@solnic <3
Sergey Kukunin
@Kukunin
Nov 14 2016 14:02
I try to reduce code duplication of writing schemas for my entities (they are pretty the same). Is there a way create validation schema based on entity automatically? Is it good idea?
How do you threat such duplication?
In my application, I need to accept and save about 40 different attributes. It turns out in a bunch of entities for that 40 attributes, and the same amount of schema validators
Lucas Hosseini
@beauby
Nov 14 2016 14:03
@Kukunin Having both dynamically bound is probably a bad idea as your validations define an interface contract
Could you post an example?
Sergey Kukunin
@Kukunin
Nov 14 2016 14:07
@beauby I think so as well. It's probably should be not automatic generator, but some set of helpers. Currently I have a lot of this:
      CoerciblePurpose =
        Types::Strict::Symbol.constrained(included_in: Domain::Mortgage::Loan::PURPOSES)
                             .constructor do |value|
          next value unless Domain::Mortgage::Loan::PURPOSES.map(&:to_s).include?(value)
          value.to_sym
        end
       required(:purpose).filled(CoerciblePurpose)
because my entity is
      class Loan < Dry::Struct
        PURPOSES = [:purchase, :refinance, :construction].freeze
        Purpose = Types::Strict::Symbol.constrained(included_in: PURPOSES)
        attribute :purpose, Purpose
      end
at least, we can reuse Loan::Purpose type
Lucas Hosseini
@beauby
Nov 14 2016 14:08
Yeah that’s what I was gonna say