These are chat archives for dry-rb/chat

1st
Oct 2018
Kacper Pucek
@KacperPucek
Oct 01 2018 16:07

Hello everyone, I got a question regarding dry-validations. I successfully set default messages_file with:

Dry::Validation::Schema.configure do |config|
  config.messages_file = ERRORS_PATH
end

It seems though, that it's only used for Dry::Validation.Schema and Dry::Validation.Params. Is this intended behaviour?

Jaromír Červenka
@Cervajz
Oct 01 2018 16:22
@KacperPucek Hello :) I don't understand the question - where else is it supposed to be?
Schema is parent for Params and JSON so the config is shared there as well
I use it like this:
class ApplicationSchema < Dry::Validation::Schema
  configure do |config|
    option :record
    option :machine_repository, Machine
    option :user_repository, User
    option :temporary_token_repository, TemporaryToken

    config.messages_file = 'config/locales/validations.yml'
  end

...

end

module Sessions
  AuthenticateUserSchema = Dry::Validation.Schema(ApplicationSchema) do
    required(:email).filled(:str?)
    required(:password).filled(:str?)

    validate(exists?: :email) do |email|
      user_repository.find_by(email: email).present?
    end
  end
end
Kacper Pucek
@KacperPucek
Oct 01 2018 16:27
[1] pry(main)> Dry::Validation.Schema
=> #<#<Class:0x00007fb7c16fe4a8>:0x00007fb7c794ba70
 @checks={},
 @config=
  #<#<Class:0x00007fb7c0d8e760>:0x00007fb7c16fe2c8
   @config=
    {:input_processor=>:noop,
     :hash_type=>:weak,
     :type_map=>{},
     :path=>[],
     :predicates=>Dry::Logic::Predicates,
     :registry=>#<Dry::Validation::PredicateRegistry::Unbound:0x00007fb7c16fe0e8 @external=Dry::Logic::Predicates, @predicates={}>,
     :messages=>:yaml,
     :messages_file=>#<Pathname:/Users/xxxx/xxxx/xxxx/config/locales/validations.yml>,

vs

[2] pry(main)> Dry::Validation.Params
=> #<#<Class:0x00007fb7c0d2d4d8>:0x00007fb7c0cdbae8
 @checks={},
 @config=
  #<#<Class:0x00007fb7c1b09f48>:0x00007fb7c0d2c3a8
   @config=
    {:input_processor=>:params,
     :hash_type=>:symbolized,
     :type_map=>{},
     :path=>[],
     :predicates=>Dry::Logic::Predicates,
     :registry=>#<Dry::Validation::PredicateRegistry::Unbound:0x00007fb7c0d27448 @external=Dry::Logic::Predicates, @predicates={}>,
     :messages=>:yaml,
     :messages_file=>nil,

Only in case of Dry::Validation.Schema messages_file is present

@Cervajz
I can get it to work this way or another, that's not an issue. I'm just wondering if this is expected behaviour or maybe I'm just doing something conceptually wrong here :D
Jaromír Červenka
@Cervajz
Oct 01 2018 16:44
@KacperPucek The parent for your Params (Schema) class is different :)
If you want to share configuration, you need to reuse the Schema while calling .Params method
As:
def self.Params(base = nil, **options, &block)
      klass = base ? Schema::Params.configure(Class.new(base)) : Schema::Params
      Validation.Schema(klass, options, &block)
end
In your example:
config=
  #<#<Class:0x00007fb7c0d8e760>:0x00007fb7c16fe2c8
   @config=

vs

@config=
  #<#<Class:0x00007fb7c1b09f48>:0x00007fb7c0d2c3a8
   @config=
    {:input_processor=>:params,
two different objects
(as per their ID)
Or I can be completely wrong :D
Jaromír Červenka
@Cervajz
Oct 01 2018 16:50
So what I'd do is Dry::Validation.Params(MyParentSchemaWithConfig) do ... end
Kacper Pucek
@KacperPucek
Oct 01 2018 16:55
Thanks for looking into this, that's exactly what I ended up with for now - simply Dry::Validation.Params(Dry::Validation::Schema). Thanks again :)
Jaromír Červenka
@Cervajz
Oct 01 2018 16:59
We would probably have to look how dry-configurable works.
Interesting
Now I am trying to understand how that works :D
From line #19
And it's parent is Schema
So it's acutally Schema.configure do |config| on that line #9
I am lost :/