These are chat archives for dry-rb/chat

11th
Aug 2017
Aaron Barthel
@abrthel
Aug 11 2017 01:58
@binarypaladin you're right, thats what I get for not reading my own example well enough. I got a chance to play around with a code sample and I've put this together off of the sample you provided. It's probably not as clean as you'd like but take a look and see if it works for you.
    Dry::Validation.Schema do
      configure do
        def self.messages
          Dry::Validation::Messages.default.merge(
            en: { errors: { company_contact_rule: 'A contact or an executive name must be set',
                            person_contact_rule:  'Date of birth is a required field' } }
          )
        end

        config.type_specs = true

        def company?(value)
          !person?(value)
        end

        def entity_type?(value)
          %w[company person trust].include?(value)
        end

        def person?(value)
          value == 'person'
        end
      end

      required(:name, :string).filled(:str?)
      required(:type, :string).filled(:str?, :entity_type?)
      optional(:executive_name).filled(:str?)
      optional(:contact_name).filled(:str?)
      optional(:date_of_birth).filled(:date?)

      rule(company_contact_rule: [:type, :executive_name, :contact_name, :date_of_birth]) do |type, en, cn, dob|
        type.company? > (en.filled? | cn.filled?)
      end

      rule(person_contact_rule: [:type, :date_of_birth]) do |type, dob|
        type.person? > dob.filled?
      end
    end
  end
Aaron Barthel
@abrthel
Aug 11 2017 02:08
Btw, are you attempting to implement a Party Data Model? If so, I'd recommend creating seperate forms and schemas for each type of party. Mixing it all together was extremely painful and was the major reason I discovered dry-rb libs and rom.