These are chat archives for dry-rb/chat

31st
Aug 2016
John Backus
@backus
Aug 31 2016 00:12
:)
Tim Riley
@timriley
Aug 31 2016 01:32
oh yes, definitely +1 to removing jruby-head
Kiril Dokh
@dsounded
Aug 31 2016 08:01
@flash-gordon Nikita, sounds funny :)
Michał Warda
@michalwarda
Aug 31 2016 09:52
Hi guys. I've got a very specific use case for dry-validation and I'm wondering if anyone already did something like that. I need to wrap into objects the result of Dry::Validation.Form. Because I define the schema in there I don't want to duplicate the code when defining ruby objects. Let's say for example I have a schema:
CitySchema = Dry::Validation.Form do 
  required(:id).filled(:int?)
  required(:name).filled(:str?)
end

PersonSchema = Dry::Validation.Form do 
  required(:name).filled(:str?)
  required(:city).schema(CitySchema)
end

on base of that I want to create classes:

class City
  attr_reader :id, :name
end

class Person
  attr_reader :name, :city
end

and then after validating the data cast the validated object into them.
So something like PersonSchema.call(name: 'Michal', city: {id: 1, name: 'Warsaw'}) will result with a Person object with the City under :city and rest filled.
Anyone done that by any chance?

Michał Warda
@michalwarda
Aug 31 2016 10:46
And if that question wasn't enough I've got a simpler one ;)
Sometimes a value under a specific key in my schema is an array and sometimes a object. Is there a way to handle that case?
Egor Lynko
@flexoid
Aug 31 2016 10:49
@michalwarda if you don't want to implement this by yourself, try to use reform gem with dry-validationvalidations backend
Fran Worley
@fran-worley
Aug 31 2016 10:51
@michalwarda as @flexoid says, reform handles this and a hole lot of other things besides. It would be useful to get a idea of what your goal is as I know that Reform isn't for everyone.
Egor Lynko
@flexoid
Aug 31 2016 10:52
for example, if you have polymorphic objects, consider other solution ;) at least for now
Fran Worley
@fran-worley
Aug 31 2016 10:52
@flexoid or if you're intention is super simple, Reform can be a bit overkill
Michał Warda
@michalwarda
Aug 31 2016 10:53
Yea i didn't want to use reform because I don't need any of the persistence features it provides
Fran Worley
@fran-worley
Aug 31 2016 10:55
@michalwarda you actually don't need to use the persistence elements. I use it quite often for virtual forms (sign-in, searching & filters etc.)
Michał Warda
@michalwarda
Aug 31 2016 10:55
I wanted to create a sanitizer for an outside API which is a bit weird
@fran-worley I guess so. I'll think about it a bit more.
And back to my second question: Sometimes a value under a specific key in my schema is an array and sometimes a object. Is there a way to handle that case?
Fran Worley
@fran-worley
Aug 31 2016 10:58

Sometimes a value under a specific key in my schema is an array and sometimes a object. Is there a way to handle that case?

You would need to write a custom predicate in your case to validate it.

Fran Worley
@fran-worley
Aug 31 2016 11:04
something like:
CitySchema = Dry::Validation.Form do 
  configure do
    config.messages_file = 'path_to_file' # you've got to have a custom message for your predicate

    def valid?(value)
      value.is_a?(Array) || value.is_a?(SomeObject)
    end
  end
  required(:id).filled(:int?)
  required(:name).filled(:str?)
  required(:some_key).filled(:valid?)
end
Piotr Solnica
@solnic
Aug 31 2016 11:05
there’s an issue atm wher type? freaks out when you pass a custom class, I’ll be fixing that soon
although help-wanted applies here :D
timriley @timriley wants help-wanted printed on a t-shirt
solnic @solnic wants help-wanted tattooed on his forehead
Tim Riley
@timriley
Aug 31 2016 11:09
:wink:
Fran Worley
@fran-worley
Aug 31 2016 11:10
@solnic yeah, my suggestion was more of a temporary work around. Though it gives you the flexibility to actually validate when something should be one type vs another as it is just your own method
Piotr Solnica
@solnic
Aug 31 2016 11:12
@fran-worley eventually you should be able to simply do required(:some_key) { type?(Array) | type?(SomeObject) }
Michał Warda
@michalwarda
Aug 31 2016 11:13
and is there a way to then depending on the type apply each(Schema) or just schema(Schema)?
Rafael George
@cored
Aug 31 2016 12:29
@solnic I'm back to give some help
are there any low hanging mangoes for me ?
Piotr Solnica
@solnic
Aug 31 2016 13:34
@cored hey, great :) I’ll get back to you later today, busy working atm
Rafael George
@cored
Aug 31 2016 13:34
cool
Piotr Solnica
@solnic
Aug 31 2016 14:13
@fran-worley pheew, I made all integration/predicates/**/* specs passing in dry-v against refactored dry-logic :)
Fran Worley
@fran-worley
Aug 31 2016 14:14
@solnic hero!
Piotr Solnica
@solnic
Aug 31 2016 14:14
things slowly start to look really good, there’s already a perf boost visible, even w/o any optimizations, so that’s a good sign, but I gotta say ‘applied rule’ idea is probably not gonna cut it at the end of the day and I need to re-add some concept of a result, but this time it’s gonna be 10xsimpler thanks to refactored internals (esp rel to Each and Set)
I’m trying to figure out what the essence is wrt results, so we can use the absolute min amount of data for producing result asts
the bigger challenge though is to add configurable execution where success results are not built, so that we avoid the overhead which comes with results for valid input
right now dry-v is ~4x slower than AM::V for valid input :(
Fran Worley
@fran-worley
Aug 31 2016 14:17
:( that definitely needs improvements
Piotr Solnica
@solnic
Aug 31 2016 14:17
…I mean in my refactoring branches, 0.9.5 is ~1.2-3x slower for valid input, but I’m pretty sure we can make it either equally fast or faster :)
yeah, basically rule execution needs to be externalized and injected into rules and ops
so that we can make that configurable, that’s all
anyhow, I’m in mojowork-mode now, so I’ll get back to this stuff tomorrow, I think I’ll be done with all of that by Friday
Fran Worley
@fran-worley
Aug 31 2016 14:20
you're so quick with all of this. My day job is just sapping all of my time at the moment
Piotr Solnica
@solnic
Aug 31 2016 14:21
I’m not quick, I just work too much O_o
Fran Worley
@fran-worley
Aug 31 2016 14:27
@solnic what's an applicable rule?
did you mean appliable (as in methods related to applied rules?)
Piotr Solnica
@solnic
Aug 31 2016 14:30
@fran-worley I knew there’s something wrong with that name :)
Piotr Solnica
@solnic
Aug 31 2016 17:25
@fran-worley si :)
Fran Worley
@fran-worley
Aug 31 2016 17:26
Weird that rxb is failing when the rest are fine. Are you using options.fetch(:name)instead of options[:name] because you want an error raised if it doesn't exist?
Piotr Solnica
@solnic
Aug 31 2016 17:27
yeah although this is gonna be renamed to :path and slightly modified
Fran Worley
@fran-worley
Aug 31 2016 17:28
so path and name get merged into an array with the name at the end ?
Piotr Solnica
@solnic
Aug 31 2016 17:29
@fran-worley there’s :id concept for giving rules identifiers, name is going away, except Rule::Predicate where it’s the name of the predicate method, and :path can be an array or a symbol
Fran Worley
@fran-worley
Aug 31 2016 17:30
Can I be of any help now? Having brain block at work and want to get my teeth into something else.
Piotr Solnica
@solnic
Aug 31 2016 17:32
@fran-worley oh damn, not a good moment as I’ve got a big mess locally that I haven’t pushed yet…:/
Fran Worley
@fran-worley
Aug 31 2016 17:33
Ok I'll just leave it then. Give me a shout if/when you need some extra hands. I'll read through what you've done so far and check I still get it!
Piotr Solnica
@solnic
Aug 31 2016 17:34
it’s rather similar, I suppose :)
Fran Worley
@fran-worley
Aug 31 2016 17:35
Alot clearer, cracking job
Michał Gutowski
@CucumisSativus
Aug 31 2016 17:41
Hey guys, to put a message for custom predicate do I need to put it inside yml or can I just put a string somewhere?
(in dry-validation ofc)
Fran Worley
@fran-worley
Aug 31 2016 17:42
@CucumisSativus you'll need to put it inside a yml
Michał Gutowski
@CucumisSativus
Aug 31 2016 17:43
aw snap, i thought that there is a faster way to achieve this
but thanks
Russell Edens
@rx
Aug 31 2016 18:09
Is any one planning on presenting dry-rb at rubyconf? The CFP is next week. I'm considering a clean architecture exploration/survey with dry-rb as a subject. Something like "making up for the lost years". If I can get some non proprietary samples and review help from the brain trust here.
Michał Gutowski
@CucumisSativus
Aug 31 2016 18:10
I'm trying to implement custom validation that goes as follows:
if country requires state, check is state is present
both country and state are custom classes. I tried with rules but i think i'm missing something. Could you guys take a look at my schema and tell me what's wrong? https://gist.github.com/CucumisSativus/a06a2cea58ac18e0f6000c032db21e2d
Michał Gutowski
@CucumisSativus
Aug 31 2016 18:24
ok, i managed to solve it, i had wrong logic inside the rule, the correct answer is country.state_required? > (state.filled?)