These are chat archives for dry-rb/chat

10th
Mar 2017
Pablo Herrero
@pabloh
Mar 10 2017 00:27 UTC
@timriley , oh you're right, it works...
I'm still getting used to dry-v
Alex Heeton
@heeton
Mar 10 2017 14:50 UTC
Hey all :wave:
What's the usecase for using Dry::Validation.Form vs Dry::Validation.Schema? I was intrigued by this recent post (http://cucumbersome.net/2016/09/06/rails-form-objects-with-dry-rb.html) and can see that he's not using Validation.Form but the Dry docs do. Can anyone give more context?
Krzysztof Wawer
@wafcio
Mar 10 2017 15:23 UTC
Today I wanted to build simple ruby app with dry-system, unfortunatelly files from system/boot aren't run. I dig into dry-system source code and I found that in my case finalizers method in https://github.com/dry-rb/dry-system/blob/master/lib/dry/system/booter.rb#L67 returns nil and nil
file inside boot directory is simple:
App.finalize 'persistence' do |persistence|
  init do
    p "Init stage"
  end
end
Andy Holland
@AMHOL
Mar 10 2017 15:27 UTC
@heeton that article should be using Dry::Validation.Form, the difference is that (because dry-validation currently handles coercions and validations) Dry::Validation.Form provides form specific coercions, i.e. 0 => false, 1 => true for booleans, whereas Dry::Validation.Schema provides more generic coercions, see: http://dry-rb.org/gems/dry-validation/input-preprocessing/
Alex Heeton
@heeton
Mar 10 2017 15:30 UTC
Cool, thanks @AMHOL. I'm also wondering if the strict-mode stuff is only for a dry-type struct. I.e. can I use my schema to throw away extra values that were passed in? I.e. strong params
I can't seem to find methods on the schema that only return the subset of values defined in the schema
Andy Holland
@AMHOL
Mar 10 2017 15:36 UTC
@heeton seems to do that automagically
gemfile(true) do
  gem 'dry-validation'
end

schema = Dry::Validation.Form do
  required(:email).filled(:str?)
  required(:age).filled(:int?, gt?: 18)
end

schema.call(email: 'test', age: 18, blah: 'blag').to_h
# => {:email=>"test", :age=>18}
Alex Heeton
@heeton
Mar 10 2017 15:40 UTC
[23] pry(main)> ArticleSchema.call({blah: "sdf"}).to_h
=> {:blah=>"sdf"}
Huh, I'm a massive idiot, ignore me
I've been using the Schema this whole time, not Form :joy:
Thanks Andy
That post from Piotr is really interesting to see too
Andy Holland
@AMHOL
Mar 10 2017 15:46 UTC
Yeah, I think I got it out of context a bit at first, thought it was regarding coercions but then I actually read it and edited my comment lol
But still interesting :)
@wafcio do you call App.finalize! or explicitly boot persistence (App.boot('persistence') anywhere?
Andy Holland
@AMHOL
Mar 10 2017 16:08 UTC
Sorry, should be boot!
Krzysztof Wawer
@wafcio
Mar 10 2017 16:25 UTC
I call App.finalize!
booter.finalize! is run I investigated it
and I found that when call method is invoked then finalizers hash is empty
Krzysztof Wawer
@wafcio
Mar 10 2017 16:31 UTC
I mean not empty but container and finalizer variables are nil
ok I found what is going on, maybe it is bug, maybe it isn't:
my finalizers (persistence.rb) look like this:
ComicsScraper.namespace(:persistence) do |persistence|
  persistence.finalize(:rom) do
    init do
    end

    start do
    end
  end
end
finalizers hash contains this value: {:rom=>[ComicsScraper, #<Proc:0x007f9cb4205870@/Users/wafcio/projects/comics/scraper/system/boot/persistence.rb:2>]}
Krzysztof Wawer
@wafcio
Mar 10 2017 16:38 UTC
but call method is invoked with :persistence argument, so it looks :persistence key in finalizers hash.
probably I should call file rom.rb like name in finalize method, and problem will not appear, but in my opinion it is bug
Andy Holland
@AMHOL
Mar 10 2017 16:47 UTC
Oh, wait
ComicsScraper.namespace => ComicsScraper.finalize
Krzysztof Wawer
@wafcio
Mar 10 2017 16:48 UTC
ComicsScraper.finalize! I run it
Andy Holland
@AMHOL
Mar 10 2017 16:49 UTC

Yeah but this:

ComicsScraper.namespace(:persistence) do |persistence|
  persistence.finalize(:rom) do
    init do
    end

    start do
    end
  end
end

Should be:

ComicsScraper.finalize(:persistence) do |container|
  container.register(:rom) do
    init do
    end

    start do
    end
  end
end
Sorry, updated
I think anyway
Krzysztof Wawer
@wafcio
Mar 10 2017 16:50 UTC
let me check
Andy Holland
@AMHOL
Mar 10 2017 16:51 UTC
Or container.register should be called in the init block
# system/boot/db.rb

Application.finalize(:db) do |container|
  init do
    require 'some/3rd/party/db'

    container.register(:db, DB.configure(ENV['DB_URL']))
  end

  start do
    db.establish_connection
  end

  stop do
    db.close_connection
  end
end
without namespace it works
Andy Holland
@AMHOL
Mar 10 2017 16:54 UTC
Ahh OK, yeah, the idea there is that you add a bootable :rom component, but that registers persistence.rom in the container
Whereas you're trying to boot the namespace
Krzysztof Wawer
@wafcio
Mar 10 2017 16:55 UTC
thanks for help
Andy Holland
@AMHOL
Mar 10 2017 16:55 UTC
NP
Krzysztof Wawer
@wafcio
Mar 10 2017 20:28 UTC
I am creating console ruby app with dry-system and some features are missing, which exist in dry-web. dry-web shouldn't be small wrapper with rack, monitoring stuff, which easly allow to create web app ? Am I correct or not ?
Pablo Herrero
@pabloh
Mar 10 2017 21:18 UTC
Where can you find some documentation about defining custom types with coercion to use with dry-validation?
Don Morrison
@elskwid
Mar 10 2017 22:30 UTC
@pabloh I went looking for this the other day and ended up digging around in rom-sql since there are custom types built for postgres (for example)