Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
Sean Winner
@swinner2

I was able to get around it by doing

    validate(valid_foo: :foo) do |foo|
      if foo
        if foo.first.present?
          true
        else
          false
        end
      else
        true
      end
    end

but I figured people may have run into this problem before

Grant Shangreaux
@gcentauri
i assume that is happening because it is validating each index in the array, and is trying to let you know which index failed validation.
Piotr Solnica
@solnic
@swinner2 what @gcentauri said, this is a feature, w/o it you wouldn't know which element caused validation errors
Sean Winner
@swinner2
Yeah i get that @solnic @gcentauri. For some reason with multi-select boxes, when a user selects nothing i still get a key with an empty array in params. So this causes problems with mapping the error messages. with simple form
Piotr Solnica
@solnic
@swinner2 you can reject blank values prior validation
validation logic should not be bothered by a messy input
Sean Winner
@swinner2
@solnic Yep I was confused because I didn’t realize rails adds the hidden_field to multi-select form elements. So I created a type that will remove the blank field since the first member of the array will always be a blank value.
  MultiSelectInputArray = Types::Array.constructor do |array|
    # (array.size == 1) means that the array is [""] and nothing was selected. So, we return nil.
    if array.size == 1 && array.first.empty?
      nil
    else
      array.delete("")
      array
    end
  end
Igor Alexandrov
@igor-alexandrov
Hi guys, does anybody know what to do with this: dry-rb/dry-validation#424
Piotr Solnica
@solnic
@igor-alexandrov this won't be fixed until 1.0.0 :(
BTW - I'll probably ship dry-schema before dry-validation 1.0.0, it will cover many typical use cases of some dry-validation users
Piotr Solnica
@solnic
also, coincidentally, I've been working on type specs in dry-schema recently, just one failing spec left to fix :)
Igor Alexandrov
@igor-alexandrov
@solnic we are open to help you with dry-schema. Let's discuss this in private
Igor Alexandrov
@igor-alexandrov
Regarding the problem above, is there a way to change input processor on base schema?
Igor Alexandrov
@igor-alexandrov
Updated pull request to work with 0.12.2: dry-rb/dry-validation#425
@solnic, @flash-gordon don't you think that it is critical and should be merged?
Piotr Solnica
@solnic
I'll take a look
Igor Alexandrov
@igor-alexandrov
Thanks
Igor Alexandrov
@igor-alexandrov
BridgeExperienceSchema = Dry::Validation.Params do
  configure do
    config.type_specs = true
  end

  required(:transaction_purpose, :string).filled(included_in?: Loan::TRANSACTION_PURPOSES)

  required(:properties).each do
    schema do
      optional(:repair_value, LendingOne::Types::Currency).maybe(:number?)

      optional(:construction_budget, LendingOne::Types::Currency).maybe(:number?)
      rule(construction_budget: [:transaction_purpose, :repair_value, :construction_budget]) do |transaction_purpose, repair_value, construction_budget|
        transaction_purpose.eql?('purchase') & repair_value.filled? & repair_value.gt?(0) > construction_budget.filled?
      end
    end
  end
end
In example above how can I validate construction_budget to be filled only if transaction_purpose is equal to 'purchase'?
Brian Suh
@bsuh
I want to do some validation on a dynamically shaped hash instead of a hash with keys known ahead of time. Is there a way to check each key fits a regex format and apply some predicates on each value?
Igor Alexandrov
@igor-alexandrov
@bsuh I don't think that it is possible
Grant Shangreaux
@gcentauri
you might be able to figure something out using types, but it sounds somewhat dubious to me
Brian Suh
@bsuh
i just ended up creating a custom predicate
Ahmad Musaffa
@musaffa
What is the best place to put a rack middleware (for example, warden) in a dry-web-roda app? Is it only config.ru or we can put a rack middleware in other places?
Tim Riley
@timriley
@ahmgeek inside the Web class that inherits from Dry::Web::Roda::Application
Ahmad Musaffa
@musaffa
Oh thanks. By the way, I'm not ahmgeek. ;)
Tim Riley
@timriley
Sorry, gitter autocomplete is not very helpful
Ahmad Musaffa
@musaffa
Ha ha. No worries.
I'm having a great success with dry-web-roda. Hopefully, an API project built with dry-web-roda will go live in the next month. Will start contributing back to the dry ecosystem when I'm done.
Kacper Pucek
@KacperPucek

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
@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
[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
@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
So what I'd do is Dry::Validation.Params(MyParentSchemaWithConfig) do ... end
Kacper Pucek
@KacperPucek
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
We would probably have to look how dry-configurable works.
Interesting
Now I am trying to understand how that works :D