These are chat archives for dry-rb/chat

23rd
Jun 2017
Adam Davies
@adz
Jun 23 2017 04:09
Hi all...
Not sure if you are aware, but just tried going to link "https://discuss.dry-rb.org" -- it fails "ERR_CONNECTION_REFUSED".
However, go with 'http' and it redirects to https://discourse.dry-rb.org/
^ first link is in title bar, and a popup when first joining room
Tim Riley
@timriley
Jun 23 2017 04:14
Thanks for letting us know, @adz, we migrated our forum and obviously need to fix these links.
@adz fixed ‘em both!
Adam Davies
@adz
Jun 23 2017 04:17
:+1:
Adam Davies
@adz
Jun 23 2017 05:06

required(:answer).maybe seems to check answer is nil, or filled. It disallows blank....
I was a bit tripped up over this. Is it by design?

PS: I'm on dry-validation (0.10.5)

Joshua Hansen
@binarypaladin
Jun 23 2017 05:31
When configuring a dry-validation schema to use config.type_specs = true, how do I make sure keys in nested hashes get coerced to symbols? I've just started with this and it seems config.type_specs = true takes away A LOT of the magic.
Joshua Hansen
@binarypaladin
Jun 23 2017 05:47
Or, rather than doing something like config.type_specs = true is there a guide somewhere that shows a way that I could make my own custom_type? method that works for my own types in the same way int? or str? do?
Piotr Solnica
@solnic
Jun 23 2017 07:52
@adz @timriley this is odd, I've set up a redirect in dnsimple...seems not to work
Tim Riley
@timriley
Jun 23 2017 08:22
Redirect may not work with HTTPS?
Nikita Shilnikov
@flash-gordon
Jun 23 2017 08:54
after we had added gh links to gem pages I did mass find & replace for url schemas, that's the reason I beleive
Nikita Shilnikov
@flash-gordon
Jun 23 2017 09:17
OK, just fixed that, @adz thanks again for reporting
Joshua Hansen
@binarypaladin
Jun 23 2017 16:42
After more time at this, I seem to be mostly confused about how coercions work with config.type_specs = true at all. For instance, what type do I need to specify to turn "1" into 1 when I want an integer? When I define a custom type, I can assign a constructor that does this for me, but I can't seem to figure out how to get it to work with the builtin types. I thought the difference would be using something like Types::Form::Int rather than Types::Int or just :int but that doesn't work at all. "1" remains 1. Do I need to call some sort of constructor or what?
Joshua Hansen
@binarypaladin
Jun 23 2017 16:48
(And is this even the appropriate place to ask this sort of question or should I be posting it elsewhere?)
Piotr Solnica
@solnic
Jun 23 2017 19:16
@binarypaladin sorry this feature is not first class yet so docs etc are not there yet. You can use type identifiers or type objects from dry-types. Ie required(:num, "form.int").value(:int?, gt?: 18)
or required(:num, Types::Form::Int).value(...)
If you use Schema.Form it should automically assume Form namespace for types
Joshua Hansen
@binarypaladin
Jun 23 2017 19:19
@solnic I managed to get things working. It turns out the problem wasn't exactly with the coercion but with how I was using maybe. Lemme show you an example. (It's working beautifully now.)
schema = Dry::Validation.Form do
  configure { config.type_specs = true }
  required(:number, :int).filled(:int?, gt?: 0)

  optional(:nested, :hash).maybe do
    schema do
      required(:number, :int).filled(:int?, gt?: 0)
    end
  end
end

schema.('number' => '5', 'nested' => { 'number' => '5' })
=> { number: 5, nested: { 'number' => '5' }}
The problem is the maybe and I think it's just me not quite understanding it, although I know I saw an example like this in the docs somewhere. (I've been jumping through docs and code for a number of hours so it's a bit fuzzy.) However, this works fine (and less code):
schema = Dry::Validation.Form do
  configure { config.type_specs = true }
  required(:number, :int).filled(:int?, gt?: 0)

  optional(:nested, :hash).schema do
    required(:number, :int).filled(:int?, gt?: 0)
  end
end

schema.('number' => '5', 'nested' => { 'number' => '5' })
=> { number: 5, nested: { number: 5 }}
Joshua Hansen
@binarypaladin
Jun 23 2017 19:24
So I dunno if I'm missing something by omitting maybe, but it seems to behave as expected in my tests.
Piotr Solnica
@solnic
Jun 23 2017 19:28
@binarypaladin maybe means that a value can be nil
optional means that a key may not be present
Joshua Hansen
@binarypaladin
Jun 23 2017 19:54
@solnic Right, but introducing maybe on the schema breaks the coercion if that matters.
Either that or I'm supposed to be doing something else in that block. (For my purposes, maybe is unnecessary. It can't be nil if it's present.)
Piotr Solnica
@solnic
Jun 23 2017 19:59
It's...maybe a bug then :laughing:
Joshua Hansen
@binarypaladin
Jun 23 2017 20:00
Ha. Could be. Do you want me to file on issue on Github? (I'd offer to fix but I'm gonna have to spend more quality time with this code and some of the supporting libs first!)
Nikita Shilnikov
@flash-gordon
Jun 23 2017 20:03
if it can be nil, then the type spec have to be [:nil, :int] or something like that
Joshua Hansen
@binarypaladin
Jun 23 2017 20:04
In my case it can't, but I'm still trying to reconcile how this fits with the example here: http://dry-rb.org/gems/dry-validation/nested-data/#nested-maybe-hash
Is nesting a schema inside of a maybe pointless with type_specs = true?
Nikita Shilnikov
@flash-gordon
Jun 23 2017 20:24
@binarypaladin I read your conversation^ and it looks like a bug to me
in any case we need to sort this out so pls file an issue
Joshua Hansen
@binarypaladin
Jun 23 2017 21:32
@flash-gordon Will do. Thanks.