These are chat archives for dry-rb/chat

11th
Nov 2016
Nick Sutterer
@apotonick
Nov 11 2016 00:21
@timriley i meant that you push the strategy to TRB ;)
Tim Riley
@timriley
Nov 11 2016 01:21
Hah!
I'm happy for you to include it/distribute it as you see fit, @apotonick :)
Steve
@dnd
Nov 11 2016 04:20
when are you supposed to use a high level rule vs a custom validation block? I want to verify that the sum of amounts in an array of nested objects is equal to an amount on the main object being validated. Not sure which way exactly I should go about it. Similar to some of the other stuff I have posted recently {total_amount: 100, items: [{amount: 50}, {amount: 40}]} total_amount would probably have the error message that the amounts don't sum up.
Joe Van Dyk
@joevandyk
Nov 11 2016 20:22
i'm dry-v, i have a field that the user can leave nil. if it's nil i would like to default it to an empty string. what's the best way to do that?
Lucas Hosseini
@beauby
Nov 11 2016 20:24
@joevandyk I’d say default values are out of the realm of validation/type coercion, so just do it the obvious way (foo[:bar] ||= “”)
Joe Van Dyk
@joevandyk
Nov 11 2016 20:25
@beauby doesn't dry-v coerce "" to nil? http://dry-rb.org/gems/dry-validation/forms/
Lucas Hosseini
@beauby
Nov 11 2016 20:27
You are right – I’m not sure what’s the reason behind this.
Tim Riley
@timriley
Nov 11 2016 21:23
That's because there's no way a form can post a real nil
@joevandyk in the schema, you could use a type definition that coerces a nil input to ""
required(:foo, EmptyStringType).maybe(:str?) or something like that
That's using the new type specs feature of dry-v
EmptyStringType would be a dry-types Strict::String type with a custom constructor that does the coercion from nil to ""
Steve
@dnd
Nov 11 2016 21:35
@timriley would you be able to answer my question above about rules vs custom blocks?
Lucas Hosseini
@beauby
Nov 11 2016 21:36
@timriley Then you cannot distinguish between “no value provided” and “empty value provided”, right?
Plus dry-validation can be used on parsed JSON as well, in which case there is a distinction between empty string and null
Tim Riley
@timriley
Nov 11 2016 21:38
@beauby for HTML forms they are one and the same. That JSON thing could be considered a bug though - please file an issue!
Lucas Hosseini
@beauby
Nov 11 2016 21:40
@timriley for html forms, sure, but not for general case of an HTTP POST request
Tim Riley
@timriley
Nov 11 2016 21:40
@dnd sorry, I don't know that answer to that off the top of my head - and I'm just on phone now - if you could post to discuss.dry-rb.org, then we can keep track of your question :)
@beauby sure, but it is called a "form" validation :)
Maybe file another issue about that too?
Lucas Hosseini
@beauby
Nov 11 2016 21:41
@timriley Nope, it’s called "Data validation” according to GitHub :p
Oh shit
Sorry – I hadn’t realized what I was reading
(i.e. the “Form validation” section haha)
Basically, please disregard everything I said 🙄
Tim Riley
@timriley
Nov 11 2016 21:45
@beauby haha! No worries :)
Piotr Solnica
@solnic
Nov 11 2016 21:48
@joevandyk why do you want an empty string?
Joe Van Dyk
@joevandyk
Nov 11 2016 21:55
because database doesn't allow nil. but i want my api to be flexible.
another question: is there a way to get the JSON validation schema to coerce strings to integers? i'd like to have an json api that automatically converts "1" to 1 -- is the only way to do that easily via the Form API?
Lucas Hosseini
@beauby
Nov 11 2016 21:58
@joevandyk Why not directly transmit integers as integers in the JSON payload?
Steve
@dnd
Nov 11 2016 21:58
@solnic I got back to you on the discussion board about the error messages return format. Let me know what you think.
Joe Van Dyk
@joevandyk
Nov 11 2016 21:58
@beauby i don't want to force clients to have to do that
right now i'm working on an app that parses a csv file, turns it into json and hits the api. i don't want the csv code to have to worry about coercing input.. it should basically always be passing strings
Piotr Solnica
@solnic
Nov 11 2016 22:03
@joevandyk hmm if you allow empty string in the db then why there's a not-null constraint?
Joe Van Dyk
@joevandyk
Nov 11 2016 22:03
@solnic i don't want to allow nil. if the data is empty then i'd like an empty string.
Piotr Solnica
@solnic
Nov 11 2016 22:03
@dnd I saw that. Need to think about it a bit :)
Steve
@dnd
Nov 11 2016 22:04
did you see my new post about the high level rule vs custom validation block? I also posted it here up a little bit
not sure what the right way to do that is
@solnic no problem about needing to think about it. I tried to basically cover all the bases with it. I also updated it to include hints as I realized those were left off in the first one, but could still be useful.
Steve
@dnd
Nov 11 2016 22:11
I was thinking if anything maybe this would be the output from MessageCompiler, and then anything with less data than this could just be accomplished maybe through a method on the returned MessageSet.
Piotr Solnica
@solnic
Nov 11 2016 22:14
@joevandyk it won't be coerced to nil of there's none? check or you don't use maybe macro. So empty string should stay as it is. Ie required(:foo).value(:str?) should work just fine
Piotr Solnica
@solnic
Nov 11 2016 22:40
Remember that nil coercion is inferred from predicates so if you don't expect a nil then you won't get a nil from an emptry string