These are chat archives for dry-rb/chat

22nd
Sep 2016
Piotr Solnica
@solnic
Sep 22 2016 00:18
@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
@solnic yeah, think so
John Backus
@backus
Sep 22 2016 03:15
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
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
@backus #to_s should be used, not text
there’s also Result#errors and Result#hints
John Backus
@backus
Sep 22 2016 04:03
cool
thanks
Joe Van Dyk
@joevandyk
Sep 22 2016 05:29
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
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
@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
@solnic, Am I able to pass a variable to validate error message somehos ?
Kiril Dokh
@dsounded
Sep 22 2016 08:44
@flash-gordon WDYT if it would be possible to perform pattern-mathing gem on top of dry-monads and dry-equalizer ?
Kiril Dokh
@dsounded
Sep 22 2016 08:55
will check this out
Nikita Shilnikov
@flash-gordon
Sep 22 2016 09:17
@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
Sure, but currently that gem oriented only for Either like behavior
Nikita Shilnikov
@flash-gordon
Sep 22 2016 09:26
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
what exactly is the difference now between hints and errors?
Piotr Solnica
@solnic
Sep 22 2016 13:27
@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
@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
@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
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
@joshuaswilcox yes with high level rules
Joshua Wilcox
@joshuaswilcox
Sep 22 2016 15:29
Ah yes I see that now, sorry guess I missed that page all together
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:30
@joshuaswilcox You're welcome
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:42
@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
@cdennl rule name overrides predicate name
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:44
@cd
Piotr Solnica
@solnic
Sep 22 2016 15:44
so foo: %i[one two] will use foo message if it’s present
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:44
@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

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
@solnic ah it is optional? if it is not present it will use predicate?
Piotr Solnica
@solnic
Sep 22 2016 15:45
when it’s missing we won’t apply any other rule
you marked it as required
Joshua Wilcox
@joshuaswilcox
Sep 22 2016 15:46
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
@joshuaswilcox user_id.filled? xor global_auth_id.filled?
as one rule
Joshua Wilcox
@joshuaswilcox
Sep 22 2016 15:52
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
with both marked optional?
nah you don't i have the same sitations
with xor
Joshua Wilcox
@joshuaswilcox
Sep 22 2016 15:53
yeah both optional
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:53
its a common case
Joshua Wilcox
@joshuaswilcox
Sep 22 2016 15:53
xor doesn't work for me either
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:54
can you give me your code please the complete one
Joshua Wilcox
@joshuaswilcox
Sep 22 2016 15:55
 @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
xor
doesnt work?
or ^
Joshua Wilcox
@joshuaswilcox
Sep 22 2016 15:55
ArgumentError: +xor+ is not a valid predicate name
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:56
user_id.filled? ^ global_auth_id.filled?
or user_id.filled?.xor( global_auth_id.filled? )
Joshua Wilcox
@joshuaswilcox
Sep 22 2016 15:57
yeah ^ worked
Thanks for the help!
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 15:58
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
ah good to know thanks! yeah problem solved from a validation point of view :)
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 16:01
(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
@cdennl make both keys optional if that’s your case
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 16:03
@solnic I have no problem :) just tried to help joshua
Piotr Solnica
@solnic
Sep 22 2016 16:04
oh sorry ok cool cool :)
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 16:04
@solnic what I didnt get, ruley key for error message is optional or mandatory?
Piotr Solnica
@solnic
Sep 22 2016 16:10
it’s needed when custom rule name was provided
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 16:11
alright thanks
Joe Van Dyk
@joevandyk
Sep 22 2016 18:10
@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
@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
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
ok will do that thanks
Christopher Dennl-Ortega Arrieta
@cdennl
Sep 22 2016 19:05
@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
@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
@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
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
@andreimoment not yet. Feel free to report an issue and describe what you need
Andrei Andreev
@andreimoment
Sep 22 2016 23:21
thank you