These are chat archives for dry-rb/chat

25th
May 2016
John Backus
@backus
May 25 2016 01:06
Would be nice if errors file let you namespace errors by schema type
so everything right now would be defined by en.schema.errors (for Dry::Validation.Schema)
then en.json.errors inherits from en.schema.errors but allows for specific additions
Nice for specific things like Dry::Validation.Schema { required(:data).value(:hash?) } would ideally then have a message saying "must be an object" instead of "must be a hash"
Tim Riley
@timriley
May 25 2016 01:14
@backus that sounds like a pretty helpful enhancement. perhaps you could write it up in a GH issue?
John Backus
@backus
May 25 2016 01:15
Sure thing
Tim Riley
@timriley
May 25 2016 01:23
Thanks!
Samson Ootoovak
@ootoovak
May 25 2016 01:25
So, I am using dry-types and I am trying to get it to do something I'm not sure if it should be of doing so I wanted to ask here.
John Backus
@backus
May 25 2016 01:26
Almost opened that issue on dry-types lol
Samson Ootoovak
@ootoovak
May 25 2016 01:27
I have defined an class with attributes:
  class HeaderRecord < Dry::Types::Struct

    module Types
      include Dry::Types.module
    end

    attributes  {
      creation_date: Types::Form::Date,
      creation_time: Types::Form::Time,
    }

  end
But when I am using the class (getting the data from a CSV) the time field only has time "13:41:34" and no date. So when it is coerced it uses todays date as the base for that time. What I would like is it to use the creation_date as the base for the time that is coerces to but I'm not sure how to do that.
Or if dry-types is even the right thing to do that with. (i.e. I might be trying to do too much with it.)
Might have to sanitise the data a bit before I use it with .new({ ... })
Andy Holland
@AMHOL
May 25 2016 01:34
Pretty sure you can configure a namespace in dry-v schemas?
@ootoovak What input are you giving to get that?
Also, not sure if it's intentional in this case, but the Types module is intended to be global, i.e. you don't want to define a new types module each time you inherit from Dry::Types::Struct
Samson Ootoovak
@ootoovak
May 25 2016 01:37
For example if I did
header_record = HeaderRecord.new(creation_date: "03/03/2016", creation_time: "13:41:34")
header_record.creation_time #=> 2016-05-25 13:41:34 +1200
Which is understandable but I want the time to use the creation_date as the base take for the time. Just not sure the best way to hook into that.
Going to try override initialize in HeaderRecord and just hand build the input then call super I think.
Andy Holland
@AMHOL
May 25 2016 01:40
Nah, I wouldn't do that
Samson Ootoovak
@ootoovak
May 25 2016 01:44
@AMHOL so is there a better way or do you think dry-types is just not suited to this case?
Andy Holland
@AMHOL
May 25 2016 01:44
Ahh, you mean build the two inputs into a datetime?
Samson Ootoovak
@ootoovak
May 25 2016 01:45
No for my use case I need them to be seperate attributes. But when converted to a Ruby object time needs a date.
Thinking about it now, I might be doing it wrong. Either I do have to join them, or I have to keep them as a string...
Andy Holland
@AMHOL
May 25 2016 01:46
Yeah, I'm not sure there's a way to compose a type from two input values at the moment, sorry, I think your initial idea of preprocessing is the best way to go
Yeah, that's where I got to aswell
Samson Ootoovak
@ootoovak
May 25 2016 01:48
Cool thanks for the feedback and rubber ducking! :D
Andy Holland
@AMHOL
May 25 2016 01:48
Probably worth opening an issue about it for discussion
It seems reasonable for this to be supported to me
Yeah, sorry about my initial reaction, it's 3am here xD
Samson Ootoovak
@ootoovak
May 25 2016 01:50
Ha ha, all good. I appreciate you even taking the time to help. :+1:
Andy Holland
@AMHOL
May 25 2016 01:51
:)
John Backus
@backus
May 25 2016 02:29
Just for my own sanity: can someone confirm for me that dry-rb/dry-validation#169 is a bug?
Piotr Solnica
@solnic
May 25 2016 07:28
@backus si
Alexander Gräfe
@rickenharp
May 25 2016 07:36
I just ran into a tricky little 'buglet' in dry-initializer: dry-rb/dry-initializer#7 which makes the examples from the README fail, which is :(
Andrew Kozin
@nepalez
May 25 2016 07:55
@rickenharp thank you! I've added your fix into v0.3.2
Alexander Gräfe
@rickenharp
May 25 2016 07:55
Wow, that was quick! :thumbsup:
Nick Sutterer
@apotonick
May 25 2016 08:17
hey friends, i have a question regarding dry-autoinject
i am thinking about using it in TRB's operation
@AMHOL or @solnic you here?
Piotr Solnica
@solnic
May 25 2016 08:20
yea?
Nick Sutterer
@apotonick
May 25 2016 08:21
heya my friend
if i had a PersistedUser as in the example, and i wanted to override the auto-inject because i got my instance of UserRepository already, what's the API?
my idea is to make Operation compatible so i can add container/autoinject "later"
Piotr Solnica
@solnic
May 25 2016 08:24
@timriley ^^ ???
Nick Sutterer
@apotonick
May 25 2016 08:41
he's having a beer at the pub, probably
not an aussie nerd like i am haha
Tim Riley
@timriley
May 25 2016 08:41
@apotonick haha. Putting a kid into pajamas.
Nick Sutterer
@apotonick
May 25 2016 08:41
i am guessing the API is kwargs
Tim Riley
@timriley
May 25 2016 08:41
@apotonick are you talking about supplying a particular dependency manually via the object initializer?
Nick Sutterer
@apotonick
May 25 2016 08:42
right now, i want to have an API compatible with your stuff
without using auto_inject
but since i'm not an asshole, i'm planning to integrate rodakase/dryrb with my stuff :P
Tim Riley
@timriley
May 25 2016 08:43
right. Sounds like hash or kwargs arguments initializers will be the way to go.
Nick Sutterer
@apotonick
May 25 2016 08:43
and i'm at a point where operation can have arbitrary objects injected
Tim Riley
@timriley
May 25 2016 08:43
Because then it still feels ergonomic to supply your dependencies manually
And someone can use AutoInject in their classes later on to do the same
E.g your initialozer will be something like def initialize(policy:) or something, right?
In someone's subclass of that, if they are using AutoInject, they can just include AutoInject.kwargs[policy: "name.of.my_policy"]
And it'll all line up
Nick Sutterer
@apotonick
May 25 2016 08:45
correct!
Tim Riley
@timriley
May 25 2016 08:46
Cool! Then you should be good to go. All of this stuff dropped into master in he last couple of weeks :)
Nick Sutterer
@apotonick
May 25 2016 08:46
well if i use your API, we can integrate it when it's released?
Nikita Shilnikov
@flash-gordon
May 25 2016 08:47
@timriley re dry-rb/dry-auto_inject#13 solves the problem having a mixin that has dependencies e.g.: https://github.com/dry-rb/dry-auto_inject/pull/14#issuecomment-218562210
Nick Sutterer
@apotonick
May 25 2016 08:47
another thing i'd love to talk about @timriley is, what if someone wants the class injected but do the instantiation themselves?
that's not really what autoinject is about, right?
Michał Pietrus
@blelump
May 25 2016 08:57
@apotonick , I recall @timriley recently has added kwargs support for autoinject. I point it, because it would make things easier to implement for TRB. So far I've used TRB + autoinject in a composite OP class. I wonder, however, what would be the impact of adding autoinject to TRB#OP. Sounds like a huge change in internal TRB API
Nick Sutterer
@apotonick
May 25 2016 08:57
what makes you think so, @blelump ?
it's like 2 lines of code that change
@timriley or maybe there's a module that does override the constructor that i could use, without the auto-instantiation
Michał Pietrus
@blelump
May 25 2016 09:05
@apotonick , uhm, OK, the API stays the same and autoinject for external deps. got it now!
Nick Sutterer
@apotonick
May 25 2016 09:15
exactly!
Nick Sutterer
@apotonick
May 25 2016 09:17
yeah kinda like that, @blelump - great! :+1:
Tim Riley
@timriley
May 25 2016 09:20
@apotonick what do you mean by wanting a class to be injected but also doing instantiatiin themself?
S/class/object
Nikita Shilnikov
@flash-gordon
May 25 2016 09:30
@timriley btw what's the problem with dry-rb/dry-auto_inject#16 ? Perhaps I can give advice
Tim Riley
@timriley
May 25 2016 09:33
@flash-gordon I'm pretty sure just ran out of time in trying to get it done. The last thing I got to was when there is a kwargs initialize with a super, and that super has a specific list of keywords , then we only want to pass it those particular keywords, even if we have injected more than that
Nikita Shilnikov
@flash-gordon
May 25 2016 09:39
I see. The problem is very similar to superfluous sequential arguments. It's true we can have a tricky parameters analyzer. This should work fine for kwargs but I'm afraid wouldn't always work for args. But as I said we can have different strategies for args in that case
Tim Riley
@timriley
May 25 2016 09:50
Yeah.
I'll take another stab in the next few days, @flash-gordon, and will push up whatever I get done, so you can take a look.
Nikita Shilnikov
@flash-gordon
May 25 2016 09:51
cool, I'll be there :)
Chris Richards
@cmrichards
May 25 2016 10:24
This message was deleted
Chris Richards
@cmrichards
May 25 2016 10:33
@solnic Once I have validated my form params using Dry::Validation.Form, how do I then create an object containing attributes that have been coerced into the right types?
Tim Riley
@timriley
May 25 2016 10:34
@cmrichards that is really just up to you. Dry-v doesn't care what happens to its output data. In many cases, we just keep it as pure data.
Since we're usually just passing it to a database create/update command, for example
Chris Richards
@cmrichards
May 25 2016 10:34
Or a service object
Tim Riley
@timriley
May 25 2016 10:34
Yeah.
It's really just WhateverYouWant.new(validation_result.output)
No need for anything fancier than that, really.
Piotr Solnica
@solnic
May 25 2016 10:35
s/output/to_h/
Tim Riley
@timriley
May 25 2016 10:35
Yes, thay
Piotr Solnica
@solnic
May 25 2016 10:35
if your interface works with hash-coercible types, you can just use the result object
Chris Richards
@cmrichards
May 25 2016 10:35
hmmm.... wouldn't it be nicer to have a nice object than a hash?
Tim Riley
@timriley
May 25 2016 10:35
(Old API stuck in my head)
Piotr Solnica
@solnic
May 25 2016 10:35
(inorite)
@cmrichards nice object?
Chris Richards
@cmrichards
May 25 2016 10:36
sorry, ignore me. I'll checkout hash-coercible
Normally I pass in objects with typed attributes into my service objects.
Rather than a hash
It's just different
Piotr Solnica
@solnic
May 25 2016 10:37
nothing is stopping you, just instantiate your own objects using validated data
Chris Richards
@cmrichards
May 25 2016 10:39
I normally use Virtus where the attribute types are defined in the class. Using Dry::Validation.Form would seem to suggest that the class doesn't need to define the attribute types
Chris Richards
@cmrichards
May 25 2016 10:44
Would it make sense to have a "class MyForm < Dry::Types::Struct" and a "class MyFormvalidation < Dry::Validation.Form" object?
There seems to be some overlap
Tim Riley
@timriley
May 25 2016 10:46
That'd work, sure. We normally use dry-types structs on the other end, when we fetch already-valdated records out of the database.
But IMO it would be worth seeing how far you can get with a hash-like object first :)
Chris Richards
@cmrichards
May 25 2016 10:47
ok :-)
What if you could say "Create this form object, MyFormValidation, based on the types defined in MyForm"
Otherwise you have to duplicate the type information in both objects
Piotr Solnica
@solnic
May 25 2016 10:52
this won’t be supported
if you want heavier functionality Reform will deliver it
Chris Richards
@cmrichards
May 25 2016 10:52
ok. it was just a thought :-)
Piotr Solnica
@solnic
May 25 2016 10:52
we support dry-type structs though but
it’s very early and experimental
personally I don’t see the reason why MyForm should exist
Chris Richards
@cmrichards
May 25 2016 10:58
you're probably right, but isn't there some value in knowing exactly what type of object your service object is receiving? If I'm developing in a service object then a quick glance at the MyFrom class will tell me the attributes types.
I wish someone would implement a ruby 2.2 -> ruby 1.87 transpiler so I could use this stuff in my older rails apps
;-)
Andy Holland
@AMHOL
May 25 2016 11:08
@cmrichards wouldn't you be able to tell the same from the validation schema?
Piotr Solnica
@solnic
May 25 2016 11:12
uhm yeah, validation schema is as precise as it can be wrt how the data look like
Eduardo de Oliveira Hernandes
@eduardodeoh
May 25 2016 12:27
hi guys, what version of dry-v i should use, 0.7.4 or master ? Docs uses a different syntax that code from tests
Tim Riley
@timriley
May 25 2016 12:29
master has some API changes, which will be released sometime in June
Eduardo de Oliveira Hernandes
@eduardodeoh
May 25 2016 12:30
ok, thanks @timriley
Tim Riley
@timriley
May 25 2016 12:30
Starting at 0.7.4 and then making the changes when 0.8.0 is released would still be pretty fine, IMO
It’d mostly be some renaming of things.
Ralf Schmitz Bongiolo
@mrbongiolo
May 25 2016 14:24
required(:skills).each(included_in?: %w(some value)) shouldn't this work?
Maciej Mensfeld
@mensfeld
May 25 2016 14:31
Hey guys - is there a way to combine Dry::Types::Structs in a bigger object? I would like to have a owning object in which I can say that it expect a certain attribute to be an instace of another Dry::Types::Struct descendant
Eduardo de Oliveira Hernandes
@eduardodeoh
May 25 2016 15:46

hey guys, i have this json:

{
        name: "name",
        user_id: "01dd7006-8b96-48e8-954e-5a9da4cd72e0",
        control_id: "01dd7006-8b96-48e8-954e-5a9da4cd72e0",
        implementation: "imp lang",
        key_phrases: [
                {
                  key_phrase: "ph1",
                  weight: 5
                },
                {
                  key_phrase: "ph2",
                  weight: 6
                }
              ],
}

I need to validate key_phrases property for that each key_phrase value in this array doesn’t be repeated, and i need too validate that each property weight value to be greater than 0 and less than 10.
I tried many ways with dry-validation, but no success. This high level rules are possible? Any help? @solnic ?

Piotr Solnica
@solnic
May 25 2016 16:53
@eduardodeoh no built in predicate for that yet
I mean nothing for checking uniqueness
so you can configure { def unique?(v); v.map { |i| i[:key_phrase] }.uniq.size == v.size } } and rule(unique_key_phrases: :key_phrases) { |kp| kp.unique? }
Eduardo de Oliveira Hernandes
@eduardodeoh
May 25 2016 16:57
@solnic i will post a gist here with my attempts
Eduardo de Oliveira Hernandes
@eduardodeoh
May 25 2016 17:53
hey @solnic, if possible, take a look: https://gist.github.com/eduardodeoh/f26735d2edb771f5bd1149035ac337c8 - This is my first attempts with dry-validations, but no success :cry:
Ralf Schmitz Bongiolo
@mrbongiolo
May 25 2016 18:05
So each() can only accept a single predicate? This required(:skills).each(:is_valid_skill?, :str?) throws => `block in each': undefined method 'str?' for [:val, [:predicate, [:is_valid_skill?, []]]]:Dry::Validation::Schema::Rule (NoMethodError). Is it by definition, am I using it wrong or should I open a ticket?
Piotr Solnica
@solnic
May 25 2016 18:08
@mrbongiolo uhm no, one or more should work
@eduardodeoh I'll get back to you later/tomorrow, busy with work atm
Ralf Schmitz Bongiolo
@mrbongiolo
May 25 2016 18:09
@solnic I'll open a ticket then, this is on master BTW
Eduardo de Oliveira Hernandes
@eduardodeoh
May 25 2016 18:09
no problems @solnic, i appreciate your help.
Ralf Schmitz Bongiolo
@mrbongiolo
May 25 2016 18:10
@eduardodeoh are you getting any kind of error, or just now the desired behaviour?
Eduardo de Oliveira Hernandes
@eduardodeoh
May 25 2016 18:14
@mrbongiolo i get errors and when no errors appears i don’t get desired behavior