These are chat archives for dry-rb/chat

12th
Jul 2017
Ilya Bylich
@iliabylich
Jul 12 2017 13:07
Does dry-validation support forms inheritance? From what I see in https://github.com/dry-rb/dry-validation/blob/master/lib/dry/validation.rb#L28 Dry::Validation.Form accepts only inheritance from classes
I was using schemes before, but now I need to have a preprocessing mechanism in my scheme. Can I use it with schemes? If so I guess I don't need forms.
The only difference between Form and Schema seems to be a default config that is defined for Form here - https://github.com/dry-rb/dry-validation/blob/master/lib/dry/validation/schema/form.rb
Ilya Bylich
@iliabylich
Jul 12 2017 13:24
ChildForm = Dry::Validation.Form(ParentForm.class) {} seems to work. It inherits all rules and configuration
Nikita Shilnikov
@flash-gordon
Jul 12 2017 13:26
@iliabylich https://github.com/dry-rb/dry-validation/blob/master/lib/dry/validation.rb#L20 there's also build: false when you don't need to build a class automaticatlly. Calling .class works too
Ilya Bylich
@iliabylich
Jul 12 2017 13:30
@flash-gordon Do you mean something like Dry::Validation.Form(ParentForm, build: false)? Is so it doesn't work, method Form automatically makes a Class.new from the argument.
Nikita Shilnikov
@flash-gordon
Jul 12 2017 13:36
ParentForm must be a class
ParentForm = Dry::Validation.Schema(..., build: false) { .... }
then ParentFormInstance = ParentForm.new or whatever you call it
see Defining Base Schema Class
Ilya Bylich
@iliabylich
Jul 12 2017 13:45
@flash-gordon @solnic Thank you!
soylent
@soylent
Jul 12 2017 13:45
I've been profiling my app and found out that dry-logic allocates the most amount of memory
allocated memory by location
-----------------------------------
     25400  ~/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/dry-logic-0.4.1/lib/dry/logic/rule.rb:47
Piotr Solnica
@solnic
Jul 12 2017 13:57
@soylent are you having issues with memory consumption?
Pierpaolo Frasa
@Fryie
Jul 12 2017 14:11
I'm trying to understand how best to use dry-rb gems... can maybe somebody validate if what I'm trying to do makes sense?
soylent
@soylent
Jul 12 2017 14:11
@solnic not really, there are no memory leaks
I use dry-struct and when I initialize a struct object it allocates a lot of objects
Pierpaolo Frasa
@Fryie
Jul 12 2017 14:11
I have some complex response from an external service that is however maintained in-house. I want to put that response into some object (so I can add some useful methods and don't have to deal with hash structures), so presumably dry-struct fits the bill.
But I also (since it's an in-house somewhat hackish service) would like get very quick feedback (i.e. raise an application error) if my understanding of the payload doesn't match what the service actually sends. That's why I wanted to a) first run the payload through dry-validations and b) then put it into a dry-struct object.
Does that make sense or am I doing something weird?
Piotr Solnica
@solnic
Jul 12 2017 14:32
@Fryie perfect sense, this is how we want people to do things
Pierpaolo Frasa
@Fryie
Jul 12 2017 14:36
great. :)
I just now find that I'm duplicating a lot of the structural information between the dry-validation schema and the Dry::Struct definition
is there any way to avoid that?
btw I really like this project :)
Piotr Solnica
@solnic
Jul 12 2017 14:44
@Fryie not at the moment, there's plan to add support for inferring schemas<=>structs
Ilya Bylich
@iliabylich
Jul 12 2017 14:46
How can I make my sub-form to inherit type_map of its parent form? Does it work by default?
I can't get it working even by redefining self.inherited on my parent form into:
def self.inherited(klass)
    super
    klass.config.type_map = config.type_map.dup
  end
Pierpaolo Frasa
@Fryie
Jul 12 2017 14:48
ok cool! I also find that in my head it's not entirely clear yet what the difference is between the runtime "type checks" of dry-struct and the validations... maybe I still lack some philosophical insight
Piotr Solnica
@solnic
Jul 12 2017 14:49
@iliabylich may not work yet, type specs are not yet a 1st class feature
@Fryie structs are meant to be always valid, whereas validation means that something could be invalid, so it's semantic difference
validation gives you error messages, and checks everything, structs will crash your app if invalid attributes are passed to the constructor
Ilya Bylich
@iliabylich
Jul 12 2017 14:51

There's an intermediate class in the chain ParentForm -> #<Class ...> -> ChildForm. ParentForm and anonymous class have type_map, but ChildForm doesn't.

pry(main)> ChildForm.class.config.type_map
=> {}
pry(main)> ChildForm.class.superclass.config.type_map
=> {:field1=>#<Dry::Types::Constructor type=#<Dry::Types::Definition primitive=String options={} meta={}>>}

@solnic Is there any way to fix it?

I guess it initializes a new hash for every scheme under the hood when I define it using DSL, that's why inherited works for anonymous class but not for my ChildScheme
Pierpaolo Frasa
@Fryie
Jul 12 2017 14:58
ok thanks, I think I understand the difference. Hopefully the inference will work at some point :)
Pierpaolo Frasa
@Fryie
Jul 12 2017 15:12
btw what's everyone's approach to testing validations? On the one hand one could say "don't test the library", but maybe I'd like to double check that I've used it correctly / defined my schema well? Especially with custom rules etc? Should I use a pragmatic "test what you're not 100% sure about" approach?
Sean Collins
@cllns
Jul 12 2017 17:19
Currently using dry-validation on a Rails project. This is how I did it. Any suggestions for how to improve this?
class BooksController < ApplicationController
  def create
    book_params = schema.call(params)
    if book_params.success?
      @book = Book.new(book_params[:book])
      render :new
    else
      redirect_to new_book_path, flash: book_params.messages
    end
  end

  private

  def schema
    Dry::Validation.Form do
      required(:book).schema do
        required(:title).filled(:str?)
        required(:author).filled(:str?)
      end
    end
  end
end
Pablo Herrero
@pabloh
Jul 12 2017 22:29
@cllns when using form objects, I usually place them under app/forms/