These are chat archives for dry-rb/chat

22nd
Sep 2016
Piotr Solnica
@solnic
Sep 22 2016 00:18 UTC
@janjiss <3
@flash-gordon it’d be fine to push 0.10.1 with your fix, I think, wdyt?
@timriley thanks, wasn’t easy, thankfully we’ve got amazing help from contributors :)
now I gotta sum this up in a blog post :sweat_smile:
Nikita Shilnikov
@flash-gordon
Sep 22 2016 00:26 UTC
@solnic yeah, think so
John Backus
@backus
Sep 22 2016 03:15 UTC
Good news
I think I found my first dry-v bug of the new release
gem 'dry-validation', '~> 0.10'

require 'dry/validation'

schema =
  Dry::Validation.Schema do
    required(:foo) { str? }
    required(:bar) { str? | int? }
  end

schema.call(foo: 1, bar: 'blah').message_set.first.text # => "must be a string"

begin
  schema.call(foo: 'hi', bar: 1.0).message_set.first.text
rescue => err
  p err
end

# >> #<NoMethodError: undefined method `text' for #<Dry::Validation::Message::Or:0x007fbcb32514b8>>
@solnic or @flash-gordon ^
Is it a bug or were we using private API?
John Backus
@backus
Sep 22 2016 03:26 UTC
works with #to_s
gem 'dry-validation', '~> 0.10'

require 'dry/validation'

SCHEMA =
  Dry::Validation.Schema do
    required(:foo) { str? }
    required(:bar, %i[string int]) { str? | int? }
  end

SCHEMA.call(foo: 1, bar: 'blah').message_set.first.to_s # => "must be a string"

begin
  SCHEMA.call(foo: 'hi', bar: 1.0).message_set.first.to_s # => "must be a string or must be an integer"
rescue => err
  p err
end
Piotr Solnica
@solnic
Sep 22 2016 03:59 UTC
@backus #to_s should be used, not text
there’s also Result#errors and Result#hints
John Backus
@backus
Sep 22 2016 04:03 UTC
cool
thanks
Joe Van Dyk
@joevandyk
Sep 22 2016 05:29 UTC
I have a form schema called X. How can I validate that an array is filled with X?
http://dry-rb.org/gems/dry-validation/reusing-schemas/ deals with a hash with a key that has a schema of X
Joe Van Dyk
@joevandyk
Sep 22 2016 05:35 UTC
I tired each { schema X } but got an error
oh hm.. that works with plain schemas, not form schemas
on dry-v master
if I use a form schema, I get:
/home/monkey/.gem/ruby/2.2.5/gems/dry-types-0.9.0/lib/dry/types/compiler.rb:80:in `merge': no implicit conversion of Dry::Types::Safe into Hash (TypeError)
    from /home/monkey/.gem/ruby/2.2.5/gems/dry-types-0.9.0/lib/dry/types/compiler.rb:80:in `each'
that a bug?
Kiril Dokh
@dsounded
Sep 22 2016 07:25 UTC
@flash-gordon I just though it cause Scala has getOrElse for Try as well, sure, in general you should work around exceptions somehow, but in some cases it could be helpful
Kiril Dokh
@dsounded
Sep 22 2016 08:01 UTC
@solnic, Am I able to pass a variable to validate error message somehos ?
Kiril Dokh
@dsounded
Sep 22 2016 08:44 UTC
@flash-gordon WDYT if it would be possible to perform pattern-mathing gem on top of dry-monads and dry-equalizer ?
will check this out
Nikita Shilnikov
@flash-gordon
Sep 22 2016 09:17 UTC
@dsounded re getOrElse, well, we can add it, though I'd add a disclaimer about its usage in Try and Either :)
re pattern matching, you can use case for simple ugh ... cases
Kiril Dokh
@dsounded
Sep 22 2016 09:23 UTC
Sure, but currently that gem oriented only for Either like behavior
Nikita Shilnikov
@flash-gordon
Sep 22 2016 09:26 UTC
in dry-matcher you can write a matcher for an arbitrary type http://dry-rb.org/gems/dry-matcher/
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 12:53 UTC
what exactly is the difference now between hints and errors?
Piotr Solnica
@solnic
Sep 22 2016 13:27 UTC
@cdennl hints are not errors, they are messages generated from your rule definitions, you can treat them separately to show useful feedback in UI so that the user will know about requirements for a given field
until 0.10 we only have messages that merge both errors and hints, this turned out to be problematic in some cases so it was separated in 0.10.0
Joe Van Dyk
@joevandyk
Sep 22 2016 15:06 UTC
@solnic should i submit a bug about not being able to reuse form schemas?
each { schema SomeFormSchema } doesnt seem to work. Using a non form schema does.
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:13 UTC
@solnic so errors are messages which happened during validations and hints are the stuff which might happen
Joshua Wilcox
@joshuaswilcox
Sep 22 2016 15:20 UTC
is is possible to have conditional required params in dry-validation? i.e. one of either user_id or username is required?
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:28 UTC
@joshuaswilcox yes with high level rules
Joshua Wilcox
@joshuaswilcox
Sep 22 2016 15:29 UTC
Ah yes I see that now, sorry guess I missed that page all together
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:30 UTC
@joshuaswilcox You're welcome
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:42 UTC
@solnic Are the messages for high-level rules now ONLY generated from the high-level rules' name and no predicate messages anymore?
when they are inside a highlevel rule
I didn't get it completely from the changelog
Piotr Solnica
@solnic
Sep 22 2016 15:44 UTC
@cdennl rule name overrides predicate name
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:44 UTC
@cd
Piotr Solnica
@solnic
Sep 22 2016 15:44 UTC
so foo: %i[one two] will use foo message if it’s present
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:44 UTC
@solnic ok so in my errors.yml I only have to specify a string for the rule name then
Joshua Wilcox
@joshuaswilcox
Sep 22 2016 15:44 UTC

So I tried

        required(:user_id).maybe(:int?)
        required(:global_auth_id).maybe(:str?)

        rule(valid_user: [:user_id, :global_auth_id]) do |user_id, global_auth_id|
          user_id.false?.then(global_auth_id.filled?)
        end

        rule(valid_auth_id: [:user_id, :global_auth_id]) do |user_id, global_auth_id|
          global_auth_id.false?.then(user_id.filled?)
        end

but i get

>> @schema.call(global_auth_id: '2')
=> #<Dry::Validation::Result output={:global_auth_id=>"2"} messages={:user_id=>["is missing"]}>
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:45 UTC
@solnic ah it is optional? if it is not present it will use predicate?
Piotr Solnica
@solnic
Sep 22 2016 15:45 UTC
when it’s missing we won’t apply any other rule
you marked it as required
Joshua Wilcox
@joshuaswilcox
Sep 22 2016 15:46 UTC
so optional then?
with that I get
>> @schema.call(user_id: nil)
=> #<Dry::Validation::Result output={:user_id=>nil} messages={}>
basically I am trying to use two different params to do the same thing, so either is ok, but at least one and not both, ha
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:49 UTC
@joshuaswilcox user_id.filled? xor global_auth_id.filled?
as one rule
Joshua Wilcox
@joshuaswilcox
Sep 22 2016 15:52 UTC
same result
>> @schema.call(user_id: 1)
=> #<Dry::Validation::Result output={:user_id=>1} messages={:global_auth_id=>["is missing"]}>
>> @schema.call(global_auth_id: '1')
=> #<Dry::Validation::Result output={:global_auth_id=>"1"} messages={:user_id=>["is missing"]}>
>> @schema.call(global_auth_id: '1', user_id: nil)
=> #<Dry::Validation::Result output={:user_id=>nil, :global_auth_id=>"1"} messages={:user_id=>["must be filled"]}>
I think maybe I am approaching my problem the wrong way
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:53 UTC
with both marked optional?
nah you don't i have the same sitations
with xor
Joshua Wilcox
@joshuaswilcox
Sep 22 2016 15:53 UTC
yeah both optional
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:53 UTC
its a common case
Joshua Wilcox
@joshuaswilcox
Sep 22 2016 15:53 UTC
xor doesn't work for me either
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:54 UTC
can you give me your code please the complete one
Joshua Wilcox
@joshuaswilcox
Sep 22 2016 15:55 UTC
 @schema = Dry::Validation.Form do
        optional(:user_id).maybe(:int?)
        optional(:global_auth_id).maybe(:str?)

        rule(valid_user: [:user_id, :global_auth_id]) do |user_id, global_auth_id|
          user_id.filled? or global_auth_id.filled?
        end
      end
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:55 UTC
xor
doesnt work?
or ^
Joshua Wilcox
@joshuaswilcox
Sep 22 2016 15:55 UTC
ArgumentError: +xor+ is not a valid predicate name
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:56 UTC
user_id.filled? ^ global_auth_id.filled?
or user_id.filled?.xor( global_auth_id.filled? )
Joshua Wilcox
@joshuaswilcox
Sep 22 2016 15:57 UTC
yeah ^ worked
Thanks for the help!
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:58 UTC
no problem, so the complete problem is solved?
another solution would have been: (user_id.filled? & global_id.none?) | (user_id.none? & global_id.filled?) that would be the xor expanded
Joshua Wilcox
@joshuaswilcox
Sep 22 2016 15:59 UTC
ah good to know thanks! yeah problem solved from a validation point of view :)
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 16:01 UTC
(and,or,not)x(true,false) defines a complete base of boolean logic, any predicate logic term can be assembled out from these three operations on true,flase
Piotr Solnica
@solnic
Sep 22 2016 16:03 UTC
@cdennl make both keys optional if that’s your case
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 16:03 UTC
@solnic I have no problem :) just tried to help joshua
Piotr Solnica
@solnic
Sep 22 2016 16:04 UTC
oh sorry ok cool cool :)
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 16:04 UTC
@solnic what I didnt get, ruley key for error message is optional or mandatory?
Piotr Solnica
@solnic
Sep 22 2016 16:10 UTC
it’s needed when custom rule name was provided
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 16:11 UTC
alright thanks
Joe Van Dyk
@joevandyk
Sep 22 2016 18:10 UTC
@solnic no luck on setting config.type_specs = true
Form = Dry::Validation.Form do
end

FormContainer = Dry::Validation.Form do
  configure { config.type_specs = true }
  each { schema Form }
end
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 18:15 UTC
@solnic is it possible to make a own section for rule messages in the error hash like so:
required( :param ).filled(:int?)

rule(check_special: [ :param ]) do |param|
  param.gt?(10)
end

errors should be something like: { param: ["must be filled"], rule: { check_special: ["must definitely be greater than 10 you moron"] } }
It would be nice to have a mechanism to know instantly whether a high-level rule generated the error or primitive statement
however this implies reserving a key in the errors hash
for now I am working around by using a name scheme for highlevel rules, but this feels not clean enough
Piotr Solnica
@solnic
Sep 22 2016 18:18 UTC
please report issues describing what you need
this way it is easier to handle that
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 18:19 UTC
ok will do that thanks
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 19:05 UTC
@solnic Do I get it right, that validate is like rule, but passes in the actual values and the code block just return true or false?
Piotr Solnica
@solnic
Sep 22 2016 19:43 UTC
@cdennl yes, it is like defining a predicate method that will be executed in the context of your schema, so its state is available there
Piotr Solnica
@solnic
Sep 22 2016 19:56 UTC
@dsounded re custom vars in messages, could you report an issue about that? it’s on the 1.0 roadmap but it’s not written down anywhere…
Andrei Andreev
@andreimoment
Sep 22 2016 22:54 UTC
A question about dry-validation: is it possible to override error messages at runtime?
Considering dry-validation for a form-builder solution where the user would be able to provide custom error messages.
Piotr Solnica
@solnic
Sep 22 2016 23:17 UTC
@andreimoment not yet. Feel free to report an issue and describe what you need
Andrei Andreev
@andreimoment
Sep 22 2016 23:21 UTC
thank you