These are chat archives for dry-rb/chat

15th
Nov 2016
Ramon Tayag
@ramontayag
Nov 15 2016 01:54

Given

schema = Dry::Validation.Schema do
  required(:age)
end

I expect to see an error that the key is missing when I do schema.({}).errors. Am I understanding right?

Tim Riley
@timriley
Nov 15 2016 03:35
@ramontayag required(:age).maybe will work for you
Ramon Tayag
@ramontayag
Nov 15 2016 05:36
@timriley does that mean required should always be used with maybe or filled?
Tim Riley
@timriley
Nov 15 2016 07:12
At this point, I'd say yes. With something on the RHS, anyway. Whether it should need something on the RHS if you want to require a key only is another question, though: maybe file a GitHub issue about that?
Lucas Hosseini
@beauby
Nov 15 2016 16:15
how do you guys handle objects shared among multiple custom predicates? (Such as a ROM repository to ensure uniqueness of a user email)
and is it possible to do some kind of dependency injection
Sergey Kukunin
@Kukunin
Nov 15 2016 17:09
is there a way to make dry-validation schema to return empty values for missed in input hash attributes? Like
L = Dry::Validation.Form do
  optional(:name).maybe(:str?)
end
L.call({}).output # have {name: nil}
Lucas Hosseini
@beauby
Nov 15 2016 17:12
@Kukunin If the data comes from an actual html form, then you just have to replace your line with required(:name).value(:str?)
Sergey Kukunin
@Kukunin
Nov 15 2016 17:13
@beauby it comes from API, where client may not define every attribute
so I have currently ugly solution to create attribute with nil if there is no attribute in hash
Lucas Hosseini
@beauby
Nov 15 2016 17:14
In that case, I think the cleanest way is to set the default values after validation
Sergey Kukunin
@Kukunin
Nov 15 2016 17:15
I do this now. Solution isn't as clean as wanted =) and also very fat
I need to enumerate every attribute again, third time
first in entity, second in schema, third in attribute default injector
Lucas Hosseini
@beauby
Nov 15 2016 17:15
Would it be possible for you to force the client to send a complete payload?
Sergey Kukunin
@Kukunin
Nov 15 2016 17:16
it's not desirable - in my domain I have about 40 fields, 30 of them are optional. Also, when I add new field - old clients break
it's not good for public API
Lucas Hosseini
@beauby
Nov 15 2016 17:17
To be fair, your database schema, your domain-level entity, and the user input are three different concepts, they just happen to be almost the same in lots of cases
are all your default values empty strings?
Sergey Kukunin
@Kukunin
Nov 15 2016 17:19
with nils
Lucas Hosseini
@beauby
Nov 15 2016 17:19
In that case I have a one-line hackish solution for you
Sergey Kukunin
@Kukunin
Nov 15 2016 17:20
@beauby yep, I understand that way as well. Just want to build good DSL for such things, like ROM::Mapper
Lucas Hosseini
@beauby
Nov 15 2016 17:20
defaulted_params = Hash.new { “” }.merge!(params)
if params = { foo: “bar” }, you have defaulted_params[:foo] == “bar” and defaulted_params[:baz] == “”
though the keys do not exist, so you can’t really pass it directly as a hash for persistence for instance
Sergey Kukunin
@Kukunin
Nov 15 2016 17:22
yes. I'm going to pass output of schema to Dry::Struct - which will reject such things
Lucas Hosseini
@beauby
Nov 15 2016 17:23
Does dry-struct not support default values? It would be a good place to take care of it
Sergey Kukunin
@Kukunin
Nov 15 2016 17:23
you still need to pass key in hash, even with nil
Do you think it's easy to implement own input_processor and use it with config.input_processor = :form ?
Lucas Hosseini
@beauby
Nov 15 2016 17:25
No idea to be honest
Sergey Kukunin
@Kukunin
Nov 15 2016 17:26
thanks anyway. Just discussion with you gave me some ideas for implementation
Lucas Hosseini
@beauby
Nov 15 2016 17:26
Cool :)