These are chat archives for dry-rb/chat

26th
May 2016
John Backus
@backus
May 26 2016 19:05
Sorry if this has been asked before, but is there any way to generate an error if a hash has an unexpected key with dry-validation?
Andy Holland
@AMHOL
May 26 2016 19:14
@backus I think you can just call schema :strict in your validation block
Joe Van Dyk
@joevandyk
May 26 2016 19:25
should there be any problems using dry-container or dry-auto_inject in rails?
remember seeing something about someone working on something rails related and it got released last week, but i’m getting all the dry gems confused
Nikita Shilnikov
@flash-gordon
May 26 2016 19:27
@joevandyk probably should work just fine if you register your constants in blocks, like this: register(:create_user) { CreateUser.new }
And move your container out of AS hot reload. I.e. I don't think you need to reload it, put it in lib/ or something like that
otherwise I don't guarantee that it would work :)
John Backus
@backus
May 26 2016 19:37
@AMHOL interesting. Would you mind sharing an example? I can't find any examples grepping the dry-validation specs for :strict
Maybe you are thinking of dry-types?
Joe Van Dyk
@joevandyk
May 26 2016 19:45
when's dry-auto_inject getting released?
want the kwargs
Nikita Shilnikov
@flash-gordon
May 26 2016 19:46
@timriley ^
@joevandyk btw we have at least one outstanding issue related to kwargs dry-rb/dry-auto_inject#16 Tim is working on it
Andy Holland
@AMHOL
May 26 2016 19:50
@backus yeah that was dry-types I was thinking of
@solnic you know of a way to do that?
Piotr Solnica
@solnic
May 26 2016 19:52
@AMHOL do what?
Andy Holland
@AMHOL
May 26 2016 19:53
@backus 
Sorry if this has been asked before, but is there any way to generate an error if a hash has an unexpected key with dry-validation?
Piotr Solnica
@solnic
May 26 2016 19:53
no, not yet
Andy Holland
@AMHOL
May 26 2016 19:53
Ahh OK
Piotr Solnica
@solnic
May 26 2016 19:53
I mean there's no built-in predicate for that
Andy Holland
@AMHOL
May 26 2016 19:54
OK, cheers
John Backus
@backus
May 26 2016 19:54
Any way to define it with a custom predicate?
My impression was that you weren't really able to define validations for the entire object being passed to the validator
Andy Holland
@AMHOL
May 26 2016 19:55
Thought I remembered something but I was probably thinking of dry-types
Like you said
John Backus
@backus
May 26 2016 19:55
Yeah
Andy Holland
@AMHOL
May 26 2016 19:56
Yeah, that was my thinking too
Piotr Solnica
@solnic
May 26 2016 20:09
it is possible but with limitations, so you can use hash? do ... end in the root
this will be applied to the input that's passed to validation schema
we can add support for more
Andy Holland
@AMHOL
May 26 2016 20:10
I ended up with something like:
gemfile(true) { gem 'dry-validation', github: 'dry-rb/dry-validation' }

module MyPredicates
  include Dry::Logic::Predicates

  ALLOWED_KEYS = %i(
    id
    name
    email
  ).freeze

  predicate(:restricted_hash?) do |hash|
    ALLOWED_KEYS & hash.keys == ALLOWED_KEYS
  end
end

schema = Dry::Validation.Schema do
  configure do
    config.predicates = MyPredicates
  end

  restricted_hash? do
    required(:id).filled(:int?)
    required(:name).filled
    required(:email).filled
  end
end

schema.(id: 1, name: 'Joe', email: 'joe@hotmail.com')
That seems to work, if you add the error message
John Backus
@backus
May 26 2016 20:12
Neat I'll play with that
Thank you
Andy Holland
@AMHOL
May 26 2016 20:19
NP
John Backus
@backus
May 26 2016 21:09

Interesting. The following works fine

Dry::Validation.Schema { hash? { required(:data) } }

Not the JSON version though

Dry::Validation.JSON { hash? { required(:data) } }
Error for JSON is:
> Dry::Validation.JSON { hash? { required(:data) } }
NoMethodError: undefined method `visit_d' for #<Dry::Types::Compiler:0x007fcdc2856cf8 @registry=Dry::Types>
Did you mean?  visit
  from /Users/johnbackus/.rvm/gems/ruby-2.3.1/bundler/gems/dry-types-ce8761715332/lib/dry/types/compiler.rb:15:in `visit'
  from /Users/johnbackus/.rvm/gems/ruby-2.3.1/bundler/gems/dry-types-ce8761715332/lib/dry/types/compiler.rb:80:in `block in merge_with'
  from /Users/johnbackus/.rvm/gems/ruby-2.3.1/bundler/gems/dry-types-ce8761715332/lib/dry/types/compiler.rb:80:in `map'
  from /Users/johnbackus/.rvm/gems/ruby-2.3.1/bundler/gems/dry-types-ce8761715332/lib/dry/types/compiler.rb:80:in `merge_with'
  from /Users/johnbackus/.rvm/gems/ruby-2.3.1/bundler/gems/dry-types-ce8761715332/lib/dry/types/compiler.rb:67:in `visit_json_hash'
  from /Users/johnbackus/.rvm/gems/ruby-2.3.1/bundler/gems/dry-types-ce8761715332/lib/dry/types/compiler.rb:28:in `visit_type'
  from /Users/johnbackus/.rvm/gems/ruby-2.3.1/bundler/gems/dry-types-ce8761715332/lib/dry/types/compiler.rb:15:in `visit'
  from /Users/johnbackus/.rvm/gems/ruby-2.3.1/bundler/gems/dry-types-ce8761715332/lib/dry/types/compiler.rb:11:in `call'
  from /Users/johnbackus/.rvm/gems/ruby-2.3.1/bundler/gems/dry-validation-fa557778ff0f/lib/dry/validation/input_processor_compiler.rb:16:in `call'
  from /Users/johnbackus/.rvm/gems/ruby-2.3.1/bundler/gems/dry-validation-fa557778ff0f/lib/dry/validation/schema.rb:125:in `input_processor'
  from /Users/johnbackus/.rvm/gems/ruby-2.3.1/bundler/gems/dry-validation-fa557778ff0f/lib/dry/validation/schema.rb:148:in `default_options'
  from /Users/johnbackus/.rvm/gems/ruby-2.3.1/bundler/gems/dry-validation-fa557778ff0f/lib/dry/validation/schema.rb:46:in `new'
  from /Users/johnbackus/.rvm/gems/ruby-2.3.1/bundler/gems/dry-validation-fa557778ff0f/lib/dry/validation.rb:37:in `Schema'
  from /Users/johnbackus/.rvm/gems/ruby-2.3.1/bundler/gems/dry-validation-fa557778ff0f/lib/dry/validation.rb:46:in `JSON'
  from (irb):22
  from /Users/johnbackus/.rvm/rubies/ruby-2.3.1/bin/irb:11:in `<main>'
Wish I could just tell gitter to open an issue with the content of my last few messages
Once @AMHOL or @solnic confirms this is a bug I'll open an issue
Andy Holland
@AMHOL
May 26 2016 21:18
@backus Think that's come up before
Just use Dry::Validation.JSON { schema { hash? { required(:data) } } }
John Backus
@backus
May 26 2016 21:18
Ah ok
Andy Holland
@AMHOL
May 26 2016 21:18
Schema will be required in future anyway
It simplifies the internals a lot
John Backus
@backus
May 26 2016 21:19
sounds good to me
Piotr Solnica
@solnic
May 26 2016 21:29
required(:foo) should not be allowed, we should raise when there's a key rule w/o any rules for its value
Schema is not building input processor and that error comes from input processor builder which chokes on an invalid rule ast
I mean it is valid rule ast but input processor expects type definitions too
obviously, this should provide a nicer error message ;)
Piotr Solnica
@solnic
May 26 2016 21:50
@backus ^
Tim Riley
@timriley
May 26 2016 22:32
@joevandyk hopefully next week, if I can fix that bug