These are chat archives for dry-rb/chat

6th
Sep 2017
Andy Holland
@AMHOL
Sep 06 2017 12:55
@cutalion you can use a custom loader for that:
class MyLoader < ::Dry::System::Loader
  def call(*args)
    if constant.kind_of?(::Dry::Validation::Schema)
      constant
    else
      super
    end
  end
end

class Container < Dry::Web::Container
  # ...
  configure do |config|
    config.loader = MyLoader
    # ...
  end

  # ...
end
Alexander
@cutalion
Sep 06 2017 15:12
Thanks @AMHOL!
But I've found other issues with dry-validation. For instance, it's hard to use it with dry-auto_inject (because there is no constructor). I'm currently trying to wrap it into a class. This looks ugly, but works.
require_relative '../base'

module Validation
  module Users
    class Update
      include Import['persistence.user_email_uniqueness_check']

      def initialize(*args)
        super
        @schema = Dry::Validation.Form(Base) do
          configure do
            option :user_email_uniqueness_check

            def email_unique?(value)
              user_email_uniqueness_check.call(value)
            end
          end

          optional(:first_name).filled(:str?)
          optional(:last_name).filled(:str?)
          optional(:email).filled(:str?, :email?)
          optional(:previous_email).maybe

          rule(email_unique: [:email, :previous_email]) do |email, previous_email|
            email.not_eql?(previous_email).then(email.email_unique?)
          end
        end
      end

      def call(*args)
        @schema
          .with(user_email_uniqueness_check: user_email_uniqueness_check)
          .call(*args)
      end
    end
  end
end
Andy Holland
@AMHOL
Sep 06 2017 15:17
@cutalion I'm not sure that's a good idea, it's better to construct validations outside of the request lifecycle I think as I imagine it's quite an expensive thing to do
How come it doesn't work with auto inject? If it registers in the container you should be able to inject it
Perhaps you could use something similar?
Alexander
@cutalion
Sep 06 2017 15:19
in my example, there is a dependency in schema - user_email_uniqueness_check
I'm trying to repeat this https://github.com/icelab/berg/blob/master/apps/admin/lib/admin/people/validation/form.rb
(actually yesterday I did it wrong, I have to step back and try once again)
My mistake was to import relation directly, which was not loaded yet (instead of repository)
Andy Holland
@AMHOL
Sep 06 2017 15:23
Perhaps you could register it manually in a bootable dependency?
Alexander
@cutalion
Sep 06 2017 16:04
@AMHOL hooray! it works with correct order of auto_registration paths and custom loader. many thanks!
Andy Holland
@AMHOL
Sep 06 2017 16:04
:+1: cool
Alexander
@cutalion
Sep 06 2017 16:11
however, I wonder why dry-validation is made as "constructable functions with inheritance ability"
Andy Holland
@AMHOL
Sep 06 2017 16:12
You can inherit from a validator?
Alexander
@cutalion
Sep 06 2017 16:12
it's so different from other dry-* gems
there is a way to share configuration
Update = Dry::Validation.Form(Base) do end
class Base < Dry::Validation::Schema
  configure do
    config.messages = :i18n
  end
end
Andy Holland
@AMHOL
Sep 06 2017 16:14
Yeah, that makes sense though, as it's a class that inherits from Validation::Schema rather than a built schema instance
Although I'm not all that familiar with dry-v myself, so perhaps I'm missing something
Alexander
@cutalion
Sep 06 2017 16:16
yeah, I saw a lot of its internals )
just cannot understand what's the reason to make it so different. Are there any benefits?
Andy Holland
@AMHOL
Sep 06 2017 16:21
I'm sure there are reasons for the way it's built, would have to ask @solnic
Or raise concerns on https://discourse.dry-rb.org/