These are chat archives for dry-rb/chat

29th
Sep 2016
Sergey Kukunin
@Kukunin
Sep 29 2016 08:44
guys, any chance that dry-validation 0.10.4 will be released in foreseeable time?
Sergey Kukunin
@Kukunin
Sep 29 2016 08:52
I'm just deciding should I workaround bugs with current versions or just wait a little time
Egor Lynko
@flexoid
Sep 29 2016 09:45
guys, why this example from the doc is not working? http://dry-rb.org/gems/dry-validation/high-level-rules/
schema = Dry::Validation.Schema do
  required(:login).filled(:bool?)
  required(:email).maybe(:str?)

  rule(email_presence: [:login, :email]) do |login, email|
    login.true?.then(email.filled?)
  end
end


schema.call({login: false}).messages
{
    :email => [
        [0] "is missing"
    ]
}
Both on 0.9.5 and 0.10.2
Oskar Szrajer
@gotar
Sep 29 2016 09:50
require "dry-validation"

schema = Dry::Validation.Schema do
  required(:login).filled(:bool?)
  required(:email).maybe(:str?)

  rule(email_presence: [:login, :email]) do |login, email|
    login.true?.then(email.filled?)
  end
end


p schema.call({login: false}).messages
{
    :email => [
      "is missing"
    ]
}
works for me
0.10.3 version
~/Programowanie/TestDry bundle | grep dry-v
Using dry-validation 0.10.3
~/Programowanie/TestDry be ruby a.rb
{:email=>["is missing"]}
Egor Lynko
@flexoid
Sep 29 2016 09:51
But the rule is saying that email is not required when login is false, isn't it?
Oskar Szrajer
@gotar
Sep 29 2016 09:52
but error is from second line
required(:email).maybe(:str?)
this check if hash contain key email
this one will be ok
require "dry-validation"

schema = Dry::Validation.Schema do
  required(:login).filled(:bool?)
  required(:email).maybe(:str?)

  rule(email_presence: [:login, :email]) do |login, email|
    login.true?.then(email.filled?)
  end
end


p schema.call({login: false, email: nil}).messages
{
    :email => [
      "is missing"
    ]
}
i returns no error
Egor Lynko
@flexoid
Sep 29 2016 09:54
as I understand, that line checks that :email is a string if it is present
Oskar Szrajer
@gotar
Sep 29 2016 09:54
required(:something) just check if key exist
not value
if login will be true it will check if value of email is filled
Egor Lynko
@flexoid
Sep 29 2016 09:56
Oh, I got it
and schema.call({login: false, email: nil}).messages from your last example probably returns {}
thanks!
Oskar Szrajer
@gotar
Sep 29 2016 09:57
yeap
it's strange for beginning because most validation gems do not check keys, only value, and missing key and key will nil value is the same for them
here you have first declare what keys you require to exists and later what value, and on the end logick behind them, like above (conditional check)
it's very powerful tool when you understand how it works
Egor Lynko
@flexoid
Sep 29 2016 10:00
Agree, I already played with Dry-V for some time, and I very like it
I just misunderstood maybe predicate from that example
optional(:email).maybe(:str?) is suits better to my case, as I'm building API
Oskar Szrajer
@gotar
Sep 29 2016 10:07
yeap
Egor Lynko
@flexoid
Sep 29 2016 10:25
Is it possible to control which fields will be present in the output based on rules?
Oskar Szrajer
@gotar
Sep 29 2016 10:27
for me it's not a part of dry-v resposibility to control schema
I guess but it should not be possible
you should have different schema (model) written in dry-t or dry-struct and logic above
but it's how I will do this
maybe someone have any experience with scenario like that and will provide better answer
you design rest api?
for me all field shoudl be always in response
Egor Lynko
@flexoid
Sep 29 2016 10:32
yes, rest api
Oskar Szrajer
@gotar
Sep 29 2016 10:32
if you just start I can recommend JSON API. Good to have something other spend a lot of time to dscribe and find best solutions
it start to be a standard for modern api
Egor Lynko
@flexoid
Sep 29 2016 10:34
we decided to use it a week ago :)
Oskar Szrajer
@gotar
Sep 29 2016 10:34
good
so yaks, am::serializer, and few others are your answers
they will convert automatically output
Egor Lynko
@flexoid
Sep 29 2016 10:37
well, I'm thinking not about a response, but request processing. From the previous example, what if I want to set email to the model only if login is present
Surely I can define rule to forbid email in the input in that case, but what if I want just to ignore that field
Sergey Kukunin
@Kukunin
Sep 29 2016 10:39
btw, is there a way to make struct doesn't require all keys in constructor hash?
Oskar Szrajer
@gotar
Sep 29 2016 10:40
@Kukunin probably not, it's how struct works (and Anima), don't know dry-struct but I suppose it works the same
use dry-t inplace (or virtus if you really need or still prefer)
Sergey Kukunin
@Kukunin
Sep 29 2016 10:42
what do you mean under use dry-t inplace? how can I replace struct with dry-t?
I think I understand about dry-t inplace. just PORO + dry-t types
class Customer
  def name=(name)
    @name = Types::Strict::String[name]
  end
end
like this?
Oskar Szrajer
@gotar
Sep 29 2016 10:50
there was Maybe and optional too
declaration
but not sure how it works now
is it up to date
Sergey Kukunin
@Kukunin
Sep 29 2016 10:52
I know that. my original concern was that requiring all fields to set breaks encapsulation. but it makes perfect sense in case of structs - structs aren't objects, but data structures. so, no encapsulation there
Sergey Kukunin
@Kukunin
Sep 29 2016 19:40
is there a way to overwrite key in dry-container? I try to test it, but it raises exception saying key is already registered
Nikita Shilnikov
@flash-gordon
Sep 29 2016 19:42
You can use stubs, check out docs
Sergey Kukunin
@Kukunin
Sep 29 2016 19:43
stubs isn't too convenient because you need to stub default argument as well
like allow(container).to receive(:[]).and_call_original
btw, why do you need mutex in default registry if container is ::Concurrent::Hash.new
Nikita Shilnikov
@flash-gordon
Sep 29 2016 20:23
@Kukunin apparently this is not documented
::Concurrent::Hash.new doesn't prevent concurrent loading which is not a safe thing
I there is an issue about this though, perhaps it can cause a deadlock
Nikita Shilnikov
@flash-gordon
Sep 29 2016 21:27
^I think there, I missed the word