These are chat archives for dry-rb/chat

5th
May 2016
Daniel Gollahon
@dgollahon
May 05 2016 01:21
I'm having trouble with the max_size? predicate with a required key in dry validation.
FooSchema = Dry::Validation.Schema { key(:foo).required { max_size?(10) } }
that produces no error messages with
FooSchema.call(foo: 'bar' * 100)
but if i change it to
FooSchema = Dry::Validation.Schema { key(:foo) { max_size?(10) } }
then i do get the expected error message of "size cannot be greater than 10"
is this a bug? or am i missing something?
Tim Riley
@timriley
May 05 2016 01:25
@dgollahon this looks like a bug to me. What you’re doing should work. I think there’s a problem compiling error messages.
For example:
When I set up a schema with foo max_size?(3)
[12] pry(main)> sch.(foo: "ab")
=> #<Dry::Validation::Result output={:foo=>"ab"} messages={}>
[13] pry(main)> sch.(foo: "abcde")
=> #<Dry::Validation::Result:0x3fc8cc90868c>
[14] pry(main)> sch.(foo: "abcde").messages
NameError: uninitialized constant ThreadSafe::Cache::NULL
from /Users/tim/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/thread_safe-0.3.5/lib/thread_safe/cache.rb:66:in `fetch_or_store'
There’s a problem building the error messages for the failing case there.
Would you mind filing a github issue?
Daniel Gollahon
@dgollahon
May 05 2016 01:28
Sure, no problem.
Tim Riley
@timriley
May 05 2016 01:50
Thanks!
Daniel Gollahon
@dgollahon
May 05 2016 02:43
Issue created. I was going to add your example to the issue also, but i couldn't reproduce it locally. I might have created a different looking schema than you did.
Daniel Gollahon
@dgollahon
May 05 2016 03:12
Err, also, sorry if you meant file an issue for the your case with the error compliation / cache problem and not the max_size? thing. I think what i created might just have been more/less a duplication of dry-rb/dry-validation#133.
Tim Riley
@timriley
May 05 2016 03:13
no worries @dgollahon, we’ll check it out in future and close it if it turns out to be a dup
Daniel Gollahon
@dgollahon
May 05 2016 03:13
cool, thanks
Jeff Dickey
@jdickey
May 05 2016 17:57

Dry::Types question: How would I define a constrained range of integers such that the range start is 0 or higher, and the range end is an integer higher than the start?

A close analogy using Virtus is at https://github.com/TheProlog/prolog-use_cases/blob/master/lib/prolog/support/form_object/integer_range.rb (nearly 50 lines)

Recommendations?

Andy Holland
@AMHOL
May 05 2016 18:03
Link is broked @jdickey
Jeff Dickey
@jdickey
May 05 2016 18:04
ack; sorry; hang on; I'll Gist it
Andy Holland
@AMHOL
May 05 2016 18:04
Should be quite simple I think
spent an hour-plus reading through the dry-types and dry-logic source/specs and I'm no closer to enlightenment than I was at midnight
Jeff Dickey
@jdickey
May 05 2016 18:13
how would you write a Dry::Types equivalent of that? I'm perfectly willing to live without the "default to (-1..-1) if anything goes hinky", but even the basics elude me :(
Andy Holland
@AMHOL
May 05 2016 18:18
Working it out
Jeff Dickey
@jdickey
May 05 2016 18:18
thanks
Andy Holland
@AMHOL
May 05 2016 18:26
Damn unless value.is_a?(Range) || value.to_i != 0 is confusing lol
Jeff Dickey
@jdickey
May 05 2016 18:28
That line basically means "bail unless value is a Range or it's a nonzero integer"; turned out to be rarely used. (we almost always have two non-zero endpoints coming across the wire)
Aditya Tiwari
@aditya01933
May 05 2016 18:36
why i m getting this NoMethodError: undefined methodkey' for UserSchema:Class`
.
require 'dry-validation'
class UserSchema < Dry::Validation::Schema
key(:username) { |name| name.filled? }
key(:email).required(format?: EMAIL_REGEX)
key(:password).required
end
Andy Holland
@AMHOL
May 05 2016 18:38
@jdickey replied on the gist, let me know whether that's right
@aditya01933 are you using dry-validation from master?
Aditya Tiwari
@aditya01933
May 05 2016 18:40
i think thats the problem
no
Andy Holland
@AMHOL
May 05 2016 18:40
What version are you using?
Aditya Tiwari
@aditya01933
May 05 2016 18:40
'~> 0.7.4'
Andy Holland
@AMHOL
May 05 2016 18:42
Think that should be:
require 'dry-validation'

UserSchema = Dry::Validation.Schema do
  key(:username) { |name| name.filled? }
  key(:email).required(format?: EMAIL_REGEX)
  key(:password).required
end
@aditya01933
Aditya Tiwari
@aditya01933
May 05 2016 18:45
@AMHOL now it is working :)
thanks
Andy Holland
@AMHOL
May 05 2016 18:45
Cool :)
NP
Jeff Dickey
@jdickey
May 05 2016 19:01
@AMHOL sorry for chasing my tail so long over here; thanks loads for your help. I'd agree with your comment that Range probably belongs in your core types set as well :)
plugged it into my use case and, as long as I get all the incantations in the correct order (mainly registering the new types before including Dry::Types.module, I'm good to go
Andy Holland
@AMHOL
May 05 2016 19:02
Yeah, forgot to mention that order was important there
include Dry::Types.module generates constants on the fly from the container items
Jeff Dickey
@jdickey
May 05 2016 19:04
figured it was something like that. It's 0300 over in this part of the world; I'm done to a crisp, and that kind of mistake is easy to make. Thanks again and good night
kaushik vinay TG
@kaushikvinaytg
May 05 2016 19:26
if i have to create a rule to check 'end_date' is greater than 'start_date' how would i do that?
n its in datetime format
Andy Holland
@AMHOL
May 05 2016 20:14
@kaushikvinaytg
schema = Dry::Validation.Schema do
  required(:starts_at) { date_time? }
  required(:ends_at) { date_time? }

  rule(starts_before_ends: %i(starts_at ends_at)) do |starts_at, ends_at|
    ends_at.gt?(starts_at)
  end
end

schema.(
  starts_at: DateTime.new(2016, 5, 5, 0, 0, 0),
  ends_at: DateTime.new(2016, 5, 5, 0, 0, 0)
)
# =>
  # #<Dry::Validation::Result
  #   output={
  #     :starts_at=>#<DateTime: 2016-05-05T00:00:00+00:00 ((2457514j,0s,0n),+0s,2299161j)>,
  #     :ends_at=>#<DateTime: 2016-05-05T00:00:00+00:00 ((2457514j,0s,0n),+0s,2299161j)>
  #   }
  #   messages={:ends_at=>["must be greater than 2016-05-05T00:00:00+00:00"]}>
schema.(
  starts_at: DateTime.new(2016, 5, 5, 0, 0, 0),
  ends_at: DateTime.new(2016, 5, 5, 1, 0, 0)
)
# =>
  # #<Dry::Validation::Result
  #   output={
  #     :starts_at=>#<DateTime: 2016-05-05T00:00:00+00:00 ((2457514j,0s,0n),+0s,2299161j)>,
  #     :ends_at=>#<DateTime: 2016-05-05T01:00:00+00:00 ((2457514j,3600s,0n),+0s,2299161j)>
  #   } messages={}>