These are chat archives for dry-rb/chat

12th
Jun 2016
Tim Riley
@timriley
Jun 12 2016 04:06
@zackp30 this slight adjustment and it works for me:
require 'thread_safe'
require 'dry-validation'

schema = Dry::Validation.Form do
  key(:test).required
  key(:test_date).maybe
  rule(test_date: [:test, :test_date]) do |test, test_date|
    test.filled?.then(test_date.true?)
  end
end

puts schema.call(test: '1', test_date: nil).messages
# {:test_date=>["must be true"]}
Tim Riley
@timriley
Jun 12 2016 05:26
quietly pushed new releases of roda-flow and dry-web and made the first release of dry-web-roda
Going to look at adding an app config feature into dry-web now
Vasilis Spilka
@vasspilka
Jun 12 2016 09:54
Warning!! Unrelated to dry :P
Hello there guys and gals yesterday I created this awsome datatracker, and I was wondering if there is demand I would turn it into a gem, take a look!
Piotr Solnica
@solnic
Jun 12 2016 10:53
@zach yeah sorry about that, we're gonna have it improved in 0.8.0 where the dsl will warn you if you forgot to add expectations for the values
Nikita Shilnikov
@flash-gordon
Jun 12 2016 10:55

@solnic FYI

#!/usr/bin/env ruby

require 'bundler'

Bundler.require

GC.start
p GC.stat[:total_allocated_objects] # => 598617

start = Time.now.to_f

OrderForm = Dry::Validation.Form do
  300.times do |i|
    required(:"test_#{i}").filled(:bool?)
  end
end

finish = Time.now.to_f

GC.start
p GC.stat[:total_allocated_objects] # => 7094131

p ((finish - start) * 1000).round # => 12288 msec

RSS goes from 31.48 MB to 176.15 MB

Piotr Solnica
@solnic
Jun 12 2016 10:58
I'll be working on performance, mostly because of the perf issues reported here
dry-rb/dry-validation#182
Nikita Shilnikov
@flash-gordon
Jun 12 2016 10:59
yep, this is a reduced repro from that PR
Piotr Solnica
@solnic
Jun 12 2016 10:59
so far I mostly focused on run-time performance, now I gotta look into making compilers faster (less object allocations, caching common predicates etc.)
I had no idea people will already try it out on a larger scale, that's really exciting and motivating :)
first of all with explicit type definitions we'll build input processors w/o ast involvement, so a huge perf boost just because of that
secondly we can cache common predicates on dry-logic side, another perf boost
and then all kinds of tweaks here and there to reduce the amount of objects we allocate, and it should be fast-enough at least
Nikita Shilnikov
@flash-gordon
Jun 12 2016 11:01
ahh, I see, make sense
perhaps we'll end up with people trying to build schema in runtime and asking "why so slow?" :)
Piotr Solnica
@solnic
Jun 12 2016 11:23
that's why I added block syntax with FooSchema = ...
for extreme cases we can build a compiler in rust :laughing:
Dmitriy Plekhanov
@onemanstartup
Jun 12 2016 11:47

I'm looking at https://github.com/solnic/rodakase-blog/blob/master/lib/persistence/commands/create_user.rb
and see this. Where to read about input? Can't find in docs.

      input Dry::Data['hash'].schema(
        email: 'string', name: 'string', encrypted_password: 'string'
      )

I want to use dry-validation schemas there if it possible :)

Piotr Solnica
@solnic
Jun 12 2016 11:52
@onemanstartup validation schemas should be used way earlier and in next rom release these input handlers are inferred automatically from relation schema definitions
Dmitriy Plekhanov
@onemanstartup
Jun 12 2016 11:54
@solnic where can I read about relation schemas?
Piotr Solnica
@solnic
Jun 12 2016 12:04
@onemanstartup not documented yet, docs will be updated along with the new release
Nikita Shilnikov
@flash-gordon
Jun 12 2016 12:09
Zack Piper
@zackp30
Jun 12 2016 12:29
has the way to define custom predicates changed on master?
require 'dry-validation'
schema = Dry::Validation.Form do
  configure do
    def test_predicate(value)
      true
    end
  end
  optional :test_date
  required(:test).maybe(:str?).when(:test_predicate) do
    value(:test_date).filled?
  end
end

puts schema.call(test: '1').messages
/home/zack/s/.direnv/ruby/bundler/gems/dry-validation-8329e0bb7a1c/lib/dry/validation/predicate_registry.rb:81:in `raise_unknown_predicate_error': +test_predicate+ is not a valid predicate name (ArgumentError)
:(
Piotr Solnica
@solnic
Jun 12 2016 12:39
Custom predicates must end with a question mark so test_predicate? will work
Zack Piper
@zackp30
Jun 12 2016 12:47
thanks!
Zack Piper
@zackp30
Jun 12 2016 13:06
strange
i did the same in my rspec example, with a question mark, and i'm getting the same error
Zack Piper
@zackp30
Jun 12 2016 16:40

is

en:
  errors:
    ymd_date?: "string must be YYYY/MM/DD formatted"

ccorrect?

Ralf Schmitz Bongiolo
@mrbongiolo
Jun 12 2016 16:46
@zackp30 this should be right, unless you're using the custom message within a rule, in which case you have to setup the messages file like this:
en:
  errors:
    rules:
      key_name:
        ymd_date?: "string must be YYYY/MM/DD formatted"
Zack Piper
@zackp30
Jun 12 2016 16:47
weird
i'm getting message for ymd_date? was not found
(yes i'm providing the message file in the config)
Dry::Validation::Schema.config.messages_file = './spec/messages.yml'
Ralf Schmitz Bongiolo
@mrbongiolo
Jun 12 2016 16:51
@zackp30 can you show your schema? What version of dry-v are you using?
Zack Piper
@zackp30
Jun 12 2016 16:51
latest from master, also i found the issue
I added
  configure do
    config.messages_file = './spec/messages.yml'
  end
and it worked
perhaps it doesn't like using the first thing I did (Schema.config.mesages_file =...)
sure, i'll cook up a reproducable recipe
Ralf Schmitz Bongiolo
@mrbongiolo
Jun 12 2016 16:55
yeah, you should provide the configure block directly in the schema, or configure "all" Schemas like this:
Dry::Validation::Schema.configure do |config|
  config.messages_file = './spec/messages.yml'
end
Zack Piper
@zackp30
Jun 12 2016 16:57
the configure block in the schema works, but your snippet returned the aforementioned error :(
(yeah, i need to configure it globally)
Zack Piper
@zackp30
Jun 12 2016 17:05
require 'dry-validation'

module CustomPredicates
  include Dry::Logic::Predicates
  predicate(:ymd_date?) do |value|
    false
  end
end

Dry::Validation::Schema.configure do |config|
  config.predicates = CustomPredicates
  config.messages_file = './messages.yml'
end

schema = Dry::Validation.Form do
  optional(:end_date, &:ymd_date?)
end

schema.call(end_date: 'not a date')
@mrbongiolo ^^ this is on master of dry-validation, dry-logic, and dry-types
full stacktrace:
/home/zack/s/.direnv/ruby/bundler/gems/dry-validation-8329e0bb7a1c/lib/dry/validation/predicate_registry.rb:81:in `raise_unknown_predicate_error': +ymd_date?+ is not a valid predicate name (ArgumentError)
    from /home/zack/s/.direnv/ruby/bundler/gems/dry-validation-8329e0bb7a1c/lib/dry/validation/predicate_registry.rb:74:in `ensure_valid_predicate'
    from /home/zack/s/.direnv/ruby/bundler/gems/dry-validation-8329e0bb7a1c/lib/dry/validation/schema/key.rb:42:in `method_missing'
    from /home/zack/s/.direnv/ruby/bundler/gems/dry-validation-8329e0bb7a1c/lib/dry/validation/schema/dsl.rb:81:in `instance_eval'
    from /home/zack/s/.direnv/ruby/bundler/gems/dry-validation-8329e0bb7a1c/lib/dry/validation/schema/dsl.rb:81:in `define'
    from /home/zack/s/.direnv/ruby/bundler/gems/dry-validation-8329e0bb7a1c/lib/dry/validation/schema/dsl.rb:28:in `optional'
    from 1.rb:16:in `block in <main>'
    from /home/zack/s/.direnv/ruby/bundler/gems/dry-validation-8329e0bb7a1c/lib/dry/validation.rb:28:in `instance_exec'
    from /home/zack/s/.direnv/ruby/bundler/gems/dry-validation-8329e0bb7a1c/lib/dry/validation.rb:28:in `Schema'
    from /home/zack/s/.direnv/ruby/bundler/gems/dry-validation-8329e0bb7a1c/lib/dry/validation.rb:46:in `Form'
    from 1.rb:15:in `<main>'
Ralf Schmitz Bongiolo
@mrbongiolo
Jun 12 2016 17:30
@zackp30 I'm not sure if that's the issue, but you're setting configs for Dry::Validation::Schema but then calling a Dry::Validation::Form
Piotr Solnica
@solnic
Jun 12 2016 17:30
@zackp30 it's a really bad moment to try stuff on master if you're not following development closely, I suggest to hold off until the release. I'll do my best to wrap this up and push 0.8 before the end of next week
Zack Piper
@zackp30
Jun 12 2016 17:36
ah, alright :)