These are chat archives for dry-rb/chat

10th
Mar 2016
Hannes Nevalainen
@kwando
Mar 10 2016 08:56
@solnic cannot get your basic example to work https://gist.github.com/kwando/d8ad54d98acc0c6ca7c7
I've not spent a ton of time at this so I might just have missed something obvious
Piotr Solnica
@solnic
Mar 10 2016 09:01
@kwando you need dry types and logic from master too
Sorry I forgot to mention that
Hannes Nevalainen
@kwando
Mar 10 2016 09:02
ah, I missed dry-logic in my Gemfile
thx for the hint
this reads a little funny optional(:name).required =P
though it appears to do the same as optional(:name, &filled?)
Piotr Solnica
@solnic
Mar 10 2016 09:08
It doesn't when you know how it works
Hannes Nevalainen
@kwando
Mar 10 2016 09:09
>.< I know
Piotr Solnica
@solnic
Mar 10 2016 09:09
Left side == key expectation
Right side == value expectation
Hannes Nevalainen
@kwando
Mar 10 2016 09:13
How would I express this key(:name){|n| n.none? | (n.filled? & n.size?(2..50)) }
Michał Pietrus
@blelump
Mar 10 2016 09:17
@kwando , I think smth like key(:name).maybe(size?: (2..50)), but dont trust me, I'm still learning it
Hannes Nevalainen
@kwando
Mar 10 2016 09:22
@solnic key(:name).required(size?: 2..25) blows up
/Users/kwando/projects/dry-v-playground/vendor/bundle/bundler/gems/dry-logic-2b89feb6e426/lib/dry/logic/predicate.rb:26:in `curry': wrong argument type FalseClass (expected Proc) (TypeError)
Piotr Solnica
@solnic
Mar 10 2016 09:44
@kwando ah that’s a known issue, we splat args so ranges and arrays get messed up, I’m gonna finally fix it
for now you can continue with the block-based approach
maybe means ‘a value can be nil or sth else'
in case of Form empty strings are turned into nils
Hannes Nevalainen
@kwando
Mar 10 2016 09:47
oh, didn't realize the block form still worked =)
Piotr Solnica
@solnic
Mar 10 2016 09:47
of course it does…it’s more expressive. macros are only there to remove boilerplate for simple/common cases
btw we instance_exec blocks now in the context of value dsl objects, so you just do none? | filled? & size?(2..50)
this removes a lot of noise from the dsl
Hannes Nevalainen
@kwando
Mar 10 2016 09:49
sweet! That is nicer to read <3
Piotr Solnica
@solnic
Mar 10 2016 09:49
exactly ;)
Hannes Nevalainen
@kwando
Mar 10 2016 09:51
I have to read up on the macro/rule stuff some more, not up to speed in that department yet.. I'll get there
Piotr Solnica
@solnic
Mar 10 2016 09:51
see specs for now, I’ll document them before the release
we are going to launch dryrb website too…so docs will be there
Hannes Nevalainen
@kwando
Mar 10 2016 09:55
sidenote, I think the older dry-v did a #uniq on the errors array. I get duplicate messages for size?(2..25)
Piotr Solnica
@solnic
Mar 10 2016 09:55
oh, interesting
gist?
{:email=>["email must be filled"], :name=>["name length must be within 5 - 25", "name size must be within 5 - 25"]}
oh, no it is not the same
srry
though it means the same
Piotr Solnica
@solnic
Mar 10 2016 09:58
k I’ll fix that
Hannes Nevalainen
@kwando
Mar 10 2016 09:58
no hurry
not using it for anything important until it is released =)
Piotr Solnica
@solnic
Mar 10 2016 10:01
and hey, thanks for testing this :) told ya it’s gonna be helpful :)
Hannes Nevalainen
@kwando
Mar 10 2016 10:03
I'm just happy to do whatever I can do to help =P
Piotr Solnica
@solnic
Mar 10 2016 10:04
<3
Michał Pietrus
@blelump
Mar 10 2016 11:19
@solnic , is it normal behaviour for Form?
https://gist.github.com/blelump/f92902ab4c2291aa3b5b
and it returns
{}
{:email=>["email is missing"], :age=>["age is missing", "age must be greater than 18"]}
Piotr Solnica
@solnic
Mar 10 2016 11:19
yes, it expects stringified keys
Michał Pietrus
@blelump
Mar 10 2016 11:20
I see, thanks !
Michał Pietrus
@blelump
Mar 10 2016 12:02
@solnic , I've got into a trap, perhaps you'd have an idea. The old DSL was executed on instance whereas now it's on class. In reform, the Schema::Form class was inherited by some internal, which got extra form argument. I consider it as quite useful, because sometimes validation performs against model object , not just the data.
Michał Pietrus
@blelump
Mar 10 2016 12:27
so far I have a nasty workaround with cattr_accessor :worried:
Michał Pietrus
@blelump
Mar 10 2016 12:36
uhm, it seems I've got into another issue
Piotr Solnica
@solnic
Mar 10 2016 13:15
@blelump you need to provide a message for this predicate
one sec
do this inside configure block
Michał Pietrus
@blelump
Mar 10 2016 13:20
@solnic , uhm, but why it expects a message as its valid ?
I mean the validation input
Piotr Solnica
@solnic
Mar 10 2016 13:21
oh, nice catch mate
seems like we’re building validation hints w/o any good reason
lemme fix that :)
ok, fixed
I also added a meaningful error when a message is missing, fwiw
we have a new feature now for injecting arbitrary deps into schema objects
Michał Pietrus
@blelump
Mar 10 2016 13:29
I see ! :) Let me chech it
@solnic , the with method always provide the execution context only, right ?
Michał Pietrus
@blelump
Mar 10 2016 13:34
Thats a blast! I like that
Piotr Solnica
@solnic
Mar 10 2016 14:00
glad you like it
Piotr Solnica
@solnic
Mar 10 2016 14:35
@kwando I just fixed that sort-of-duped msgs. but you gotta specify type expectation, otherwise it defaults to a message that may go to hints that does not match your (missing) expectation
btw I feel like type expectations are special-enough to justify a special API for defining them, sth to consider
Piotr Solnica
@solnic
Mar 10 2016 14:46
Dry::Validation.Schema do
  key(:email).required(Email)
  key(:age).maybe(Age)
end
this now works with dry types :tada:
Luca Guidi
@jodosha
Mar 10 2016 14:49
@solnic Sorry for the question out of blue. Is dryrb/validations HEAD safe to play again? :smile:
Piotr Solnica
@solnic
Mar 10 2016 14:51
@jodosha sort of, you need dry-types and dry-logic from HEAD too, so unless that bothers you, it’s a good moment to test it out
Luca Guidi
@jodosha
Mar 10 2016 14:52
@solnic Thanks :+1:
Piotr Solnica
@solnic
Mar 10 2016 14:52
in fact, I’m asking people to test it out, I’m trying to fix anything that people may find prior release
Michał Pietrus
@blelump
Mar 10 2016 14:54
@solnic , could you elaborate how key(:age).maybe(Age) works, e.g. what's the Age type?
Michał Pietrus
@blelump
Mar 10 2016 15:05
:+1:
Hannes Nevalainen
@kwando
Mar 10 2016 15:21
@solnic I registered a few custom types in my app, will it support them to?
Dry::Data.register('my_app.my_type', MyApp::MyType.method(:coerce))
where the coerce method tries to convert and return a value into a MyApp::MyType otherwise raises an error
(mytype is an immutable valueobject)
Piotr Solnica
@solnic
Mar 10 2016 15:24
@kwando uhm, you want to use dry-types not data
Hannes Nevalainen
@kwando
Mar 10 2016 15:25
yeah, sry
Piotr Solnica
@solnic
Mar 10 2016 15:25
API changed…
Hannes Nevalainen
@kwando
Mar 10 2016 15:25
ugh, need to investigate that
Piotr Solnica
@solnic
Mar 10 2016 15:27
@kwando Dry::Types::Definition.new(MyApp::MyType).constructor(MyApp::MyType.method(:coerce))
Hannes Nevalainen
@kwando
Mar 10 2016 15:27
@solnic and I want to register that with Dry::Types.register?
Piotr Solnica
@solnic
Mar 10 2016 15:27
type = Dry::Types::Definition.new(MyApp::MyType)
  .constructor(MyApp::MyType.method(:coerce))

Dry::Types.register(‘my_type’, type)
Hannes Nevalainen
@kwando
Mar 10 2016 15:27
ah, ninjad
<3
cool, will try that out =)
Piotr Solnica
@solnic
Mar 10 2016 15:28
btw I actually just added support for arbitrary coercions
Hannes Nevalainen
@kwando
Mar 10 2016 15:29
which means?
Piotr Solnica
@solnic
Mar 10 2016 15:29
so you’ll be able to do key(:foo).required(type?: MyType)
and in Form schema it’ll set up a coercer via Dry::Types[‘my_type’]
so const <=> identifier mapping must match
ie MyApp::MyType => my_app.my_type
it should work just fine
Hannes Nevalainen
@kwando
Mar 10 2016 15:30
is it possible to specify the identifier instead of the constant in type?: 'my_app.my_type'
Piotr Solnica
@solnic
Mar 10 2016 15:31
that should work too
irb(main):001:0> Dry::Types.identifier('my_app.my_type')
=> "my_app.my_type”
yeah it’ll work
Hannes Nevalainen
@kwando
Mar 10 2016 15:32
wow, if this works like advertised I have a bunch of code to remove =D
@solnic psst, `visit_predicate': message for type? was not found (Dry::Validation::MissingMessageError)
Piotr Solnica
@solnic
Mar 10 2016 15:36
ah, wait. that was pre-mature. it’s not gonna work yet
type?’s sig is type?(constant, input)
so you can’t do type?: ‘string’
require 'dry-validation'

class Email
  attr_reader :value

  def self.new(input)
    super("#{input}.com")
  end

  def initialize(value)
    @value = value
  end
end

Dry::Types.register_class(Email)

schema = Dry::Validation.Form do
  key(:email).required(type?: Email)
end

puts schema.('email' => 'jane@doe').inspect
#<Dry::Validation::Result output={:email=>#<Email:0x007fb004a29ac0 @value="jane@doe.com">} messages={}>
@kwando here’s an example of what does work already ^
Piotr Solnica
@solnic
Mar 10 2016 15:43
I need help with…naming things (oh noez!)
Tiago Moraes
@tiagoefmoraes
Mar 10 2016 15:43
@solnic why do you define self.new instead of doing the interpolation in initialize?
Hannes Nevalainen
@kwando
Mar 10 2016 15:43
@solnic Alice is a cute name
Piotr Solnica
@solnic
Mar 10 2016 15:44
@tiagoefmoraes I just wanted to give @kwando an example of registering custom class in dry types and using type? with it, this example matches his use case closely as he has a custom coerce constructor method
Hannes Nevalainen
@kwando
Mar 10 2016 15:44
=P
Piotr Solnica
@solnic
Mar 10 2016 15:44
@AMHOL haha classic!
Andy Holland
@AMHOL
Mar 10 2016 15:44
:p
Piotr Solnica
@solnic
Mar 10 2016 15:45
anyhow…I need a name for something that will pre-process input before applying rules
it’s gonna be a generic concept. maybe it’ll sanitize, maybe it’ll coerce, maybe it’ll do both
Andy Holland
@AMHOL
Mar 10 2016 15:45
PreProcessor :laughing:
Input::PreProcessor
Piotr Solnica
@solnic
Mar 10 2016 15:46
oh it’s not gonna be a class in dry-v
it’s gonna be a hash schema from dry-types
but I need to name configuration for it somehow
and then name the ivar under which the hash schema will be stored
Andy Holland
@AMHOL
Mar 10 2016 15:46
Do you have any example code?
Piotr Solnica
@solnic
Mar 10 2016 15:46
it’s now called input_type but it’s stupid
Andy Holland
@AMHOL
Mar 10 2016 15:47
Naming things eh?
Piotr Solnica
@solnic
Mar 10 2016 15:47
yeah
Hannes Nevalainen
@kwando
Mar 10 2016 15:47
input mapper
Andy Holland
@AMHOL
Mar 10 2016 15:48
Dry::Types::SanitizationOrCoercionOrSanitizationAndCoercionPreProcesor
Piotr Solnica
@solnic
Mar 10 2016 15:48
ok I’m go with that
I like explicit names
Andy Holland
@AMHOL
Mar 10 2016 15:48
lol
Piotr Solnica
@solnic
Mar 10 2016 15:48
:joy:
Andy Holland
@AMHOL
Mar 10 2016 15:48
TBF I have seen similar naming conventions
Piotr Solnica
@solnic
Mar 10 2016 15:48
maybe ActiveInput? :joy:
Andy Holland
@AMHOL
Mar 10 2016 15:48
:joy:
input_handler, input_processor?
input_transformer
Hannes Nevalainen
@kwando
Mar 10 2016 15:50
input_filter
(overloaded worded though)
Piotr Solnica
@solnic
Mar 10 2016 15:50
input_proc? O_o
optimus_prime it is then
Andy Holland
@AMHOL
Mar 10 2016 15:51
:joy:
You should definitely actually call it that
Piotr Solnica
@solnic
Mar 10 2016 15:51
gotta pick up my son from daycare, bbiab
Andy Holland
@AMHOL
Mar 10 2016 15:51
Cya later
Hannes Nevalainen
@kwando
Mar 10 2016 15:51
cya
Andy Holland
@AMHOL
Mar 10 2016 15:51
I like input_transformer BTW
Piotr Solnica
@solnic
Mar 10 2016 15:57
k, thanks for suggestions. mapper or transformer are not good tbf, because we are not transforming input, we may only coerce some values
I think i’m gonna go with input_processor
because you know, it’s something that processes input :joy:
Andy Holland
@AMHOL
Mar 10 2016 15:59
:laughing: makes sense
Piotr Solnica
@solnic
Mar 10 2016 16:00
ok, that’s sth for tomorrow, and it’s the last task. remaining stuff includes updating docs, finishing website and wrapping up dry-types (just a couple of features to add)
Andy Holland
@AMHOL
Mar 10 2016 16:00
Don't you consider coercing some values a transformation BTW?
Piotr Solnica
@solnic
Mar 10 2016 16:00
but dry-v is 99% ready, I’m gonna wait few days and hopefuly more people will test it
Andy Holland
@AMHOL
Mar 10 2016 16:01
Just wondering whether I'm misunderstanding
Piotr Solnica
@solnic
Mar 10 2016 16:01
no, because I don’t change the shape of the input structure, only it’s values
Andy Holland
@AMHOL
Mar 10 2016 16:01
interesting
Piotr Solnica
@solnic
Mar 10 2016 16:01
so ie { foo: ‘1’ } => { foo: 1 } is a coercion of values in my dictionary
but { foo: ‘1’ } => { bar: { foo: 1 } } is a transformation
I had to establish this kind-of-a-definition
Andy Holland
@AMHOL
Mar 10 2016 16:02
I'd consider that a transformation too, but I'm probably wrong judging by past experience
Piotr Solnica
@solnic
Mar 10 2016 16:03
I made this distinction in ROM
where mappers can alter the structure returned by relations
and then types you instantiate may only coerce some values, but they will never change the structure
the structure is expected to be correct
Michał Pietrus
@blelump
Mar 10 2016 16:47
@solnic , I've integrated new dry-v DSL into reform and it seems it's working or at least specs say that :-)
Piotr Solnica
@solnic
Mar 10 2016 16:53
@blelump cool!
@blelump that dependency on form should be optional IMO. it’s gonna be very slow, as every time you do #with rules get compiled again
Michał Pietrus
@blelump
Mar 10 2016 16:57
I see
that would be tricky, though , since I don't know when it's needed
Piotr Solnica
@solnic
Mar 10 2016 16:59
I dunno why would anybody need that, FWIW
Michał Pietrus
@blelump
Mar 10 2016 17:11

the form in this case is a Reform instance, which internally is a Twin object of some model. As you might be guessing (or already know), twin needs an object to be a twin (uh, how it sounds...) and by design , twin appears a way before validation occurs. So if you're doing such validation:

      def token_valid?(token)
        !form.model.nil?
      end

you'd use the model object you've already queried before

makes any sense ?
and perhaps my assumption is wrong (or anyone else's) and we'd drop this dependency requirement
Piotr Solnica
@solnic
Mar 10 2016 17:17
it looks like unnecessary coupling to me
you should check with the dev who did it like that though
Michał Pietrus
@blelump
Mar 10 2016 17:23
OK, I'll make a note in PR so when @apotonick will be back, he would think about it