These are chat archives for dry-rb/chat

1st
Aug 2017
Marion Duprey
@TiteiKo
Aug 01 2017 11:04

Hi all, is there any way to dynamically configure the namespace of a schema?

Dry::Validation.Form do
  configure do
    config.messages = :i18n
    config.namespace = any_var_or_method
  end

  # ...
end

This doesn't work because (I think) of this

Tim Riley
@timriley
Aug 01 2017 11:05
@TiteiKo I gave this a try, and it seemed to work fine:
module MyNamespace
  def self.namespace
    :test
  end
end

schema = Dry::Validation.Form do
  configure do
    config.namespace = MyNamespace.namespace
  end
end

schema.config.namespace
# => :test
What kind of error/stack trace were you actually getting?
Marion Duprey
@TiteiKo
Aug 01 2017 11:05
Oh, my snippet wasn't precise enough
module ToBeIncluded
  def self.include(base)
    base.extend ClassMethods
  end

  module ClassMethods
    attr_reader :schema

    def params(&block)
      @schema = Dry::Validation.Form do
        configure do
          config.messages = :i18n
          config.namespace = self.name.underscore
        end

        instance_exec(&block)
      end
    end
  end
end
Problem here is that inside the Dry::Validation.Formblock, self is the form class
Tim Riley
@timriley
Aug 01 2017 11:09
ah
Marion Duprey
@TiteiKo
Aug 01 2017 11:10
I'm surely missing something obvious
Tim Riley
@timriley
Aug 01 2017 11:12
This one is probably worth filing an issue for
If for any reason it’s not feasible with the current design, I do know we’re planning to change things around a bit later this year, so we an at least keep this use case in mind
Marion Duprey
@TiteiKo
Aug 01 2017 11:13
Ok, I'll raise the issue right away
And try to work around this :)
Thanks!
Tim Riley
@timriley
Aug 01 2017 11:14
No probs!
Marion Duprey
@TiteiKo
Aug 01 2017 11:15
Hard part: finding the name for the issue
Oh
I can just do self.schema.config.namespace = whatever