These are chat archives for dry-rb/chat

7th
Jul 2016
Tim Riley
@timriley
Jul 07 2016 00:12 UTC
Rack 2.0 being out and expecting a minimum Ruby version of 2.2.2 is messing up with our CI
timriley @timriley wonders whether we should set that as our minimum too
Tim Riley
@timriley
Jul 07 2016 00:13 UTC
It only came out on June 30
It’s affecting gems that don’t even use rack, because capybara wants >= 1.0.0
I guess we just specify it in our projects’ Gemfiles as needed: jnicklas/capybara#1718
Rafael George
@cored
Jul 07 2016 00:33 UTC
@timriley I saw your talk about dry-web; was actually great
Tim Riley
@timriley
Jul 07 2016 00:54 UTC
@cored Thanks! :)
Rafael George
@cored
Jul 07 2016 00:57 UTC
@timriley I was having a weird discussion at work about functional objects
they keep insisting in comparing having a class method is better than having an instance with just one method
the problems that I see are related to encapsulation basically
but going back to your talk. I want to experiment a little bit on doing TDD from the start and also have all the components plug together to make it work. All dry-web components that's it
Tim Riley
@timriley
Jul 07 2016 01:01 UTC
sounds like a good experiment :)
dry-component is still young - there’s a lot we can still do to make clean component separation/orchestration better
Rafael George
@cored
Jul 07 2016 01:04 UTC
yes, but from what I see in your talk I think the experiment could work
I will try with an small user story and just build the business logic for it and then jump into providing a json API
Maciej Mensfeld
@mensfeld
Jul 07 2016 08:22 UTC
@solnic thank you for the answer on github :) will implement this now and check :)
Piotr Solnica
@solnic
Jul 07 2016 09:41 UTC
@cored we would be doing poor procedural programming if we were using module/class methods, that’s what people don’t get all the time. With objects we still do OO, we still can compose them and pass around, we still can extract logic into priv methods, we still can have MORE THAN ONE method (see relations in rom-rb) etc. etc.
@dnd I’d recommend putting a validation schema in front of constructing structs
Kiril Dokh
@dsounded
Jul 07 2016 09:48 UTC
hello
Can anyone give an advice how to use custom validators with custom messages ?

I can define them like
errors:
valid_category?: dasdas

in yml file, but what if I need to pass a varible to it ?

This message was deleted
@solnic Where can I do a pull request to the documentation ?
it doesn’t work in some cases for now :)
Piotr Solnica
@solnic
Jul 07 2016 09:52 UTC
@dsounded what kind of a variable?
Kiril Dokh
@dsounded
Jul 07 2016 09:52 UTC

Let’s say I need stuff like this:

errors:
valid_category?: could be only %{value}

Piotr Solnica
@solnic
Jul 07 2016 09:53 UTC
if you do def valid_category?(arg1, arg2, value); end then it will be available as %{value}
Kiril Dokh
@dsounded
Jul 07 2016 09:53 UTC
How can I put thi variable value from the
configure do def valid_category?(value) value.in?(categories) end
Piotr Solnica
@solnic
Jul 07 2016 09:53 UTC
and if it’s not happening, then I messed up :D
Kiril Dokh
@dsounded
Jul 07 2016 09:53 UTC
Hmm
Hold a second
will do pastebin
this one, now you see my problem ?)
I need exactly access to categories variable there
I mean I need to pass it in
  • can I do required(:category, :category2).filled(:custom_rule?)
as well, this is not documented
Like a scope validation
Piotr Solnica
@solnic
Jul 07 2016 09:59 UTC
you can’t do required(:category, :category2) the second argument is reverved for type specs that we recently added (disabled by default)
you can pass categories to schema using option + #with APIs
http://dry-rb.org/gems/dry-validation/basics/working-with-schemas/ <= see last paragraph about injecting things into schemas
Kiril Dokh
@dsounded
Jul 07 2016 10:01 UTC
Could you please give a small example for passing it ?
  • how can I do a validation for multiple fields ?
Piotr Solnica
@solnic
Jul 07 2016 10:01 UTC
just use required for all the fields
Kiril Dokh
@dsounded
Jul 07 2016 10:02 UTC
I mean, what if I need that field1 and field2 match_array [value1,value2] at the same time ?
Piotr Solnica
@solnic
Jul 07 2016 10:02 UTC
rule(foo: [:field1, :field2]) { |f1, f2| f1.eql?(“something”).and(f2.eql?(“something”)) }
Kiril Dokh
@dsounded
Jul 07 2016 10:03 UTC
Hmm, okay, will check rules, so basically I will make a PR for documentation, is it this one ?
https://github.com/dry-rb/dry-rb.org
Piotr Solnica
@solnic
Jul 07 2016 10:03 UTC
yes
Kiril Dokh
@dsounded
Jul 07 2016 10:03 UTC
thank you
Piotr Solnica
@solnic
Jul 07 2016 10:04 UTC
wdym by ‘match_array’?
Kiril Dokh
@dsounded
Jul 07 2016 10:04 UTC
In short, I have 3 fields, each of them should be in some array and combination of them should be in some hash
Piotr Solnica
@solnic
Jul 07 2016 10:07 UTC
ok, so wdym by ‘combination of them should be in some hash’? :)
Kiril Dokh
@dsounded
Jul 07 2016 10:07 UTC
will do pastebin
Kiril Dokh
@dsounded
Jul 07 2016 10:16 UTC

I have option :categories in my schema,
I have my key in yml: valid_category?: ‘value should be %{categories}’

And I have error I18n::MissingInterpolationArgument: missing interpolation argument :categories in

Is any easier way to put custom message for the custom mathcer ?
Kiril Dokh
@dsounded
Jul 07 2016 10:23 UTC
@solnic ^
Piotr Solnica
@solnic
Jul 07 2016 10:25 UTC
@dsounded looks like it’s not working atm, I’ll fix in 0.8.1
Kiril Dokh
@dsounded
Jul 07 2016 10:29 UTC
Btw, IMO it seems like custom validation with custom message is trivial stuff, so if we gonna use option for this, this hint makes me upset :(
Currently with will cause all rules to be re-built, so keep in mind the impact on performance
Thank you, anyway, maybe you’ll find the way to implement this in other way :)
Piotr Solnica
@solnic
Jul 07 2016 10:34 UTC
@dsounded wdym that this hint makes you upset?
you mean the impact on performance?
Kiril Dokh
@dsounded
Jul 07 2016 10:35 UTC
yeah
Piotr Solnica
@solnic
Jul 07 2016 10:37 UTC
yes well the impact is that rather than being 4 x faster than ActiveModel, it’s only 2 x faster
sorry about that :laughing:
I’m gonna remove this info because it’s honestly irrelevant for 99% of the users
@dsounded btw I just fixed interpolation issue
Kiril Dokh
@dsounded
Jul 07 2016 10:39 UTC

:)

I am not sure if 99% use only default validations :)

:clap:
It makes a lot of sense compared to activemodel, it’s the reason why I try to use it in my case :)
Dmitriy Plekhanov
@onemanstartup
Jul 07 2016 10:43 UTC
Should dry-validation messages with i18n fallback to default en? or is it something I should care about?
Piotr Solnica
@solnic
Jul 07 2016 10:44 UTC
I meant that in 99% of the cases perf impact won’t make any difference
@onemanstartup I haven’t thought about fallbacks yet
@dsounded ugh sorry, seems like I only partially fixed interpolation, it’s actually a bug (that is fixed) + a missing feature
Kiril Dokh
@dsounded
Jul 07 2016 10:45 UTC
@solnic I see, please update me when it will be fixed :shipit:
Piotr Solnica
@solnic
Jul 07 2016 10:58 UTC
yeah so providing interpolation data that is part of a schema state is gonna be tricky, I was aware of that since day 1 and I’ll defo make that possible before 1.0
currently we dynamically extract argument names/values from predicates and make them available for interpolation, which is super neat, but there’s no way to tell schema “hey, pass categories to valid_categories? failure message”
implementing this is gonna be rather simple, it’s just a hash that we need to pass in to the error compiler from schema and merge it in with default tokens, really simple
the tricky part is to come up with a nice API for setting that :)
ie should we blindly pass that in? what if there are naming conflicts between arg names of predicates and a custom option? etc.
I guess we could just make sure there are no conflicts and raise if there are, but I wouldn’t want that to happen at run-time but maybe I’m obsessing too much with performance now :laughing:
Fran Worley
@fran-worley
Jul 07 2016 11:13 UTC
You could get around this currently by making it an arg & passing it in when you're defining your rule/predicate
Piotr Solnica
@solnic
Jul 07 2016 11:18 UTC
not really…because it is dynamic, happens at run-time
Fran Worley
@fran-worley
Jul 07 2016 11:20 UTC
I ment:
UserSchema = Dry::Validation.Schema do
  configure do
    def some_custom_predicate(categories, input)
      # do something with input
    end
  end

  required(:category).filled(some_custom_predicate: categories)
end
That is if you have access to 'with' variables at that point
Kiril Dokh
@dsounded
Jul 07 2016 11:21 UTC
@fran-worley +1
And it will be ok if you can pass whatever you want, and all the stuff silently goes to localization even if it doesn’t need all the variables from it
Piotr Solnica
@solnic
Jul 07 2016 11:28 UTC
@fran-worley that’s not possible
you can’t have access to instance variables in the dsl because at that point there is no instance
Fran Worley
@fran-worley
Jul 07 2016 11:29 UTC
:(
Piotr Solnica
@solnic
Jul 07 2016 11:29 UTC
maybe we can do something smart there, dunno
like detect if a given name is one of the schema options
but then how would you convert that, into an argument that’s automatically evaluated? :D
Kiril Dokh
@dsounded
Jul 07 2016 11:30 UTC
I have one more question, seems like this is related to DSL as well
Fran Worley
@fran-worley
Jul 07 2016 11:30 UTC
I think I prefer that than making all options available to all messages.
Kiril Dokh
@dsounded
Jul 07 2016 11:30 UTC
Let’s say I want to use rule
Piotr Solnica
@solnic
Jul 07 2016 11:30 UTC
@fran-worley yes defo this should be somehow configured
Kiril Dokh
@dsounded
Jul 07 2016 11:31 UTC

And I want to match some stuff with my custom method’s data, for now it throws an error like:

+combinations+ is not a valid predicate name where combinations is just a method called name

Fran Worley
@fran-worley
Jul 07 2016 11:31 UTC
@solnic I think (from memory) that we planned on having some standard conversions for tokens e.g. if array then join, if range then provide left and right etc.
Piotr Solnica
@solnic
Jul 07 2016 11:32 UTC
@fran-worley yes, I’m sure there’s a nice way of handling this
oh wait…we actually MIGHT be able to achieve what you showed above here
Fran Worley
@fran-worley
Jul 07 2016 11:33 UTC
You'll need my PR for the conversions
Piotr Solnica
@solnic
Jul 07 2016 11:33 UTC
since we bind predicates to schema instance…we could look if any of the curried args is a method from that instance and simply bind+call it to get the arg value :laughing:
Fran Worley
@fran-worley
Jul 07 2016 11:34 UTC
now there's an idea :)
Piotr Solnica
@solnic
Jul 07 2016 11:34 UTC
that’s CRAZY but huh, why not? :D
I mean…you do call this method during validation, doesn’t matter when it’s called
Fran Worley
@fran-worley
Jul 07 2016 11:35 UTC

That would be awesome, then you could do things like:

eql?(value_from_method)

Piotr Solnica
@solnic
Jul 07 2016 11:35 UTC
so we’d basically call + evaluate args + curry predicate and then it’s done
Fran Worley
@fran-worley
Jul 07 2016 11:35 UTC
gt?(todays_date)
Piotr Solnica
@solnic
Jul 07 2016 11:35 UTC
yeah it’s worth a shot at least
@dsounded could you show me an example?
@dsounded dsl is only for composing rules, you don’t actually execute code inside of it, actual logic should live inside predicate methods
and as you can see, we’re missing some features related to dynamic data
Fran Worley
@fran-worley
Jul 07 2016 11:39 UTC
@solnic not for long. :smile:
Piotr Solnica
@solnic
Jul 07 2016 11:39 UTC
yeah it’ll all be there for 1.0 :)
Tom Willis
@twillis
Jul 07 2016 11:50 UTC
quick question, I'm comfortable with using dry-validation to validate json blobs etc... is there a way to write schemas that validate domain objects? or is the best strategy to write a hash representation of the domain object to be validated and validate the hash produced?
Piotr Solnica
@solnic
Jul 07 2016 11:57 UTC
@twillis we had that via attr DSL but it was removed and nobody asked about it until now :)
the philosophy is that we validate data not objects as in hashes/arrays
Tom Willis
@twillis
Jul 07 2016 12:01 UTC
:) that's cool I was just curious. I'll just write me a bunch of to_hash code it's probably for the best
Piotr Solnica
@solnic
Jul 07 2016 12:07 UTC
RSpec.describe Dry::Validation::Schema, 'dynamic predicate args' do
  subject(:schema) do
    Dry::Validation.Schema do
      configure do
        def data
          %w(a b c)
        end
      end

      required(:letter).filled(included_in?: data)
    end
  end

  it 'evaluates predicate arguments' do
    expect(schema.(letter: 'a')).to be_success
  end
end
@fran-worley ^^^ I just made that pass :laughing:
Tim Riley
@timriley
Jul 07 2016 12:08 UTC
:astonished:
Piotr Solnica
@solnic
Jul 07 2016 12:08 UTC
and it was very simple :D
Fran Worley
@fran-worley
Jul 07 2016 12:09 UTC
Nice :) that + dry-rb/dry-validation#185 should mean that you can sort messages then
Piotr Solnica
@solnic
Jul 07 2016 12:09 UTC
yeah we need specialized arg processors for interpolation too
Kiril Dokh
@dsounded
Jul 07 2016 12:10 UTC
@solnic will do a pastebin
Piotr Solnica
@solnic
Jul 07 2016 12:14 UTC
BECAUSE WHY NOT: dry-rb/dry-validation@5bb9241
Fran Worley
@fran-worley
Jul 07 2016 12:14 UTC
@solnic do you want me to move that too dry-logic or are you happy for it to stay there for now... ?
Piotr Solnica
@solnic
Jul 07 2016 12:16 UTC
@dsounded yeah so this does not work like that, you can only compose predicates/rules inside the DSL, this does not support arbitrary code by design. just encapsulate that logic in a predicate method and use that in rule BUT…0.8.0 doesn’t support dynamic args (ie your combinations method) so you gotta either try out master or wait for 0.8.1
@fran-worley thanks but maybe let’s wait a lil bit
I’d say this should be released as a total experiment for 0.8.1 and finalize in 0.9.0 and then we can update dry-logic too
Kiril Dokh
@dsounded
Jul 07 2016 12:19 UTC
It even doesn’t work if I pass my combinations as option :(
Kiril Dokh
@dsounded
Jul 07 2016 12:24 UTC
I do can move my work to pending status and come back after 0.8.1 release, could you please give a ballpark date of this release ? Will it include messages + that dynamic stuff ?
@solnic ^
Piotr Solnica
@solnic
Jul 07 2016 12:25 UTC
@dsounded next Friday
Kiril Dokh
@dsounded
Jul 07 2016 12:25 UTC
@solnic thanks for the fastest feedback ever and todays conversation :) Hope you’ll do it :)
Piotr Solnica
@solnic
Jul 07 2016 12:26 UTC
sure, and me too :laughing:
Fran Worley
@fran-worley
Jul 07 2016 12:30 UTC
@solnic I've updated this dry-rb/dry-validation#185
Piotr Solnica
@solnic
Jul 07 2016 12:32 UTC
@fran-worley cool, I’ll check it out
so, dyn args don’t work with hints atm, but it’s easy to make them work so…I’ll give it a shot and hopefuly we’ll have that available in 0.8.1
funny coz I always thought it’s gonna require more code :laughing:
this gets us closer to 1.0.0
Fran Worley
@fran-worley
Jul 07 2016 12:37 UTC
Have you got any formal plan as to what's left before 1.0.0 ?
Piotr Solnica
@solnic
Jul 07 2016 12:38 UTC
I’ll create a milestone on gh and describe it there
fwiw I know that I want, but you folks should chime in
probably the most difficult part will be, wait for it, documentation!
we’re gonna need to update website docs (as I mentioned they barely cover 50% of the features….and we need AR/AM=>dry=v guides etc.) + we have zero API docs in YARD :(
oh and dry-v 1.0.0 means dry-types 1.0.0 and dry-logic 1.0.0 too
initially I thought dry-v 1.0 will be released faster than dry-types but eventually I realized it makes no sense to release stable dry-v on top of unstable dry-t…
Fran Worley
@fran-worley
Jul 07 2016 12:44 UTC
A way to go then...
I'll make a stab at getting the site running locally again. Until I've done that I can't help that much with docs
Chris Richards
@cmrichards
Jul 07 2016 14:16 UTC
It always takes me a moment to adjust when looking at a dry-validation schema. "required" doesn't mean what you would normally expect in a validation context. Normally you expect it to mean something about the data itself, and not the keys. I think using "required_key" would clarify what it meant. Obviously you get used to it, but it still seems a bit strange.
Piotr Solnica
@solnic
Jul 07 2016 14:18 UTC
it is not strange, it is different from what you are used to. required followed by name of a key reads pretty good, it's just a matter of learning about the distinction we make between presence of keys vs values
required :foo <= where :foo is the key name describes it nicely, it's just that this concept doesn't exist in popular solutions so there's a learning curve
esp that it's a schema, so it defines the structure too
we had key(:foo).required before and that confused people more so...
it's also the reason why we added value macro to make it clear that this is for the values
so ie required(:foo).value(:str?) looks good IMO, it's just that people are not used to the fact we're validating structure AND values..
Krzysztof Wawer
@wafcio
Jul 07 2016 16:45 UTC
This message was deleted
Rafael George
@cored
Jul 07 2016 19:20 UTC
@solnic is there a way to jump into this bandwagon
I want to get some sanity even from Go lang community
Rafael George
@cored
Jul 07 2016 19:34 UTC
like smaller fruits to jump in
Piotr Solnica
@solnic
Jul 07 2016 19:55 UTC
@cored bandwagon being? :)
Rafael George
@cored
Jul 07 2016 20:40 UTC
@solnic this bandwagon - rom.rb, dry-rb
Piotr Solnica
@solnic
Jul 07 2016 21:08 UTC
@cored depends on what you need to do