These are chat archives for dry-rb/chat

27th
Jul 2016
Fran Worley
@fran-worley
Jul 27 2016 02:08
Where does your ID? Predicate come from? I'm guessing that you've worked out that in reform you get given form as a variable from which you can grab the model?
Fran Worley
@fran-worley
Jul 27 2016 11:39
@solnic It's probably somewhere but how can I use Schema with stringified keys ?
Piotr Solnica
@solnic
Jul 27 2016 11:42
@fran-worley you mean w/o coercing them to symbols?
Fran Worley
@fran-worley
Jul 27 2016 11:43
Yeah yeah... Is that possible or can we use Form without inferring coercions?
(coercions of values)
Piotr Solnica
@solnic
Jul 27 2016 11:43
yes but you need to use string keys in rule defs
required(“foo”).filled
Fran Worley
@fran-worley
Jul 27 2016 11:46
mmm yeah, not ideal for us. Can we use Form w/o value coercions or do we need a custom input processor for Reform?
Piotr Solnica
@solnic
Jul 27 2016 11:54
Schema doesn’t perform coercions
only Schema::Form and Schema::JSON are configured to infer coercions
Kevin Ross
@rosskevin
Jul 27 2016 14:00
can I pass additional information to a custom predicate?
for context, I would like a generic def unique?(attribute, value, options = {})
what I have is the model, but I don't have the attribute, and sometimes I may want to scope the options
actually, this is somewhat in-line with what @fran-worley is asking, because I could use the key if I could get to it
(for the attribute name)
the only odd case then would be when I needed to use options for scoping uniqueness
Kevin Ross
@rosskevin
Jul 27 2016 14:26
Here's an example of some validation cruft I'd like to clean up:
        validation :default, schema_class: User::SCHEMA do
          configure do
            def unique_email?(value)
              ActiveRecordORM.unique?(form.model.class, :email, value)
            end
          end

          rule(unique_email: [:email]) do |email|
            email.unique_email?
          end
        end
Where :email and :password are already generically validated in User::SCHEMA, I want to add uniqueness check on this specific form (new registration)
Any way to clean this up or make it smaller?
:dancers: :tada:
Vladimir Dralo
@vladra
Jul 27 2016 15:55
This message was deleted
Piotr Solnica
@solnic
Jul 27 2016 15:56
yeah I think gh-pages is having issues
it works for me now, I re-pushed the whole branch
Kevin Ross
@rosskevin
Jul 27 2016 16:06
I'm checking a custom type with required(:catalog).value(type?: Catalog) and getting Nothing registered with the key "af_core.catalog" (Dry::Container::Error) using reform. Did I specify the rule incorrectly?
Piotr Solnica
@solnic
Jul 27 2016 16:18
@rosskevin we don’t support custom class names yet
only native primitives are supported
you could report an issue about that
Jérémie Horhant
@Titinux
Jul 27 2016 16:19
Congratulations to everyone who worked on version 2.0 :tada:
Piotr Solnica
@solnic
Jul 27 2016 16:29
:)
Kevin Ross
@rosskevin
Jul 27 2016 16:51
reported: dry-rb/dry-validation#223
can I implement this inline?
Piotr Solnica
@solnic
Jul 27 2016 16:53
you can just add your own for now like is_a?: Catalog or sth
Kevin Ross
@rosskevin
Jul 27 2016 17:55
this yields wrong number of args, example on args to a custom predicate?
      def is_a?(value, args)
        value.is_a?(args)
      end
Piotr Solnica
@solnic
Jul 27 2016 17:56
the input value is always the last arg
so do is_a?(type, input); input.is_a?(type); end
Kevin Ross
@rosskevin
Jul 27 2016 17:57
is this not the equivalent call? .value(is_a?: Catalog)
both of these yield the same result: required(:catalog) { |catalog| catalog.is_a? Catalog } # .value(is_a?: Catalog)
      def is_a?(type, value)
        value.is_a?(type)
      end
`is_a?': wrong number of arguments (given 1, expected 2) (ArgumentError)
Piotr Solnica
@solnic
Jul 27 2016 18:01
@rosskevin required(:catalog).value(is_a?: Catalog)
Kevin Ross
@rosskevin
Jul 27 2016 18:01
yes, that yields `is_a?': wrong number of arguments (given 1, expected 2) (ArgumentError)
Piotr Solnica
@solnic
Jul 27 2016 18:07
ah shit, I think I’ve added is_a? as a dsl method
could you rename it to sth else and try?
Kevin Ross
@rosskevin
Jul 27 2016 18:07
sure
Piotr Solnica
@solnic
Jul 27 2016 18:07
DSLs objects are BasicObject instances but I needed is_a?, I gotta fix that if that’s the case
Kevin Ross
@rosskevin
Jul 27 2016 18:09
yes, that got me past it
I switched it to kind_of? and it worked
Is there a way to send more than arg to a custom predicate?
Piotr Solnica
@solnic
Jul 27 2016 18:12
yes, pass an array
required(:foo).value(something?: [1, 2, 3])
Kevin Ross
@rosskevin
Jul 27 2016 18:13
so sig would be def my_predicate?(args, value)?
or def my_predicate?(x, y, z, value)?
Piotr Solnica
@solnic
Jul 27 2016 18:15
the former
we don’t splat them
Kevin Ross
@rosskevin
Jul 27 2016 18:15
awesome, thank you
Piotr Solnica
@solnic
Jul 27 2016 18:15
you gotta splat inside the method
so x, y, z = args
Kevin Ross
@rosskevin
Jul 27 2016 18:16
the only thing that would make this better is if I had access to the key
      def unique?(args, value)
        attribute, options = args
        ActiveRecordORM.unique?(form.model.class, attribute, value, options)
     end
called with required(:code).value(unique?: [:code, {scope: :catalog}])
still, should work well and is a big improvement on my old code
Piotr Solnica
@solnic
Jul 27 2016 18:19
we could improve this in the future
predicates are actually objects, we could introduce APIs for configuring them in various ways, we’ll see
Kevin Ross
@rosskevin
Jul 27 2016 18:20
can I write this shorter? required(:code) { |code| code.none? || (code.filled? && code.unique?([:code])) }
Piotr Solnica
@solnic
Jul 27 2016 18:21
required(:code).maybe(unique?: :code)
Kevin Ross
@rosskevin
Jul 27 2016 18:21
beautiful, thank you
Piotr Solnica
@solnic
Jul 27 2016 18:21
you can also write your own macros
so ie you could define your own maybe_unique_key(name) expanding to required(name).maybe(unique?: name)
or sth like that :)
Kevin Ross
@rosskevin
Jul 27 2016 18:23
:clap:
Kevin Ross
@rosskevin
Jul 27 2016 18:38
when extending a schema, can I add another rule of the same key? or does it wipe out the original rule?
        validation :default, schema_class: User::SCHEMA do
          rule(unique_email: [:email]) do |email|
            email.unique?(:email)
          end
        end
currently written like this because I expect basic email validation to be done first in User::SCHEMA
could I switch this to my new macro and have just unique(:email)
Piotr Solnica
@solnic
Jul 27 2016 18:42
rule can refer to the same values multiple times