These are chat archives for dry-rb/chat

15th
Apr 2016
Simon Schmid
@sled
Apr 15 2016 00:03
@AMHOL Yes my command is basically a pre-configured Dry::Transaction
Andy Holland
@AMHOL
Apr 15 2016 00:03
Ahh OK, cool
Simon Schmid
@sled
Apr 15 2016 00:05
maybe I could even chain up commands somehow
CommandA >> CommandB
:p
Pretty sure it's not released yet tho
Not sure why
Simon Schmid
@sled
Apr 15 2016 00:12
cool
the only thing I don't like in the dry-* collection is the Dry::Container to be honest :D
reminds me of the java days with spring
Andy Holland
@AMHOL
Apr 15 2016 00:14
Yeah, it seems to be the one people hate the most lol
Tim Cooper
@coop
Apr 15 2016 00:32
I kind of like it, I’ve been using it as a generic registry though - not really DI.
Tim Riley
@timriley
Apr 15 2016 00:40
I heart dry-container, @AMHOL :)
Andy Holland
@AMHOL
Apr 15 2016 00:56
:D
Tim Cooper
@coop
Apr 15 2016 05:54

@solnic I’ve implemented the DSL changes that were discussed in dry-rb/dry-validation#99

key -> required
added value
required -> filled

dry-rb/dry-validation#109

I’ve also updated the documentation to reflect these changes dry-rb/dry-rb.org#56.

Simon Schmid
@sled
Apr 15 2016 05:58
:+1:
Benjamin Klotz
@tak1n
Apr 15 2016 05:58
:clap:
Simon Schmid
@sled
Apr 15 2016 06:31
I just saw that dry-types has an AST compiler but is there a way to dump the AST from a type?
it seems like the to_ast is only available for validation schemas
Piotr Solnica
@solnic
Apr 15 2016 06:50
@sled not yet
@timriley sure
@sled dry-container has very little to do with java or .net IoC/DI. It is very lightweight and when used with auto-inject it basically feels like importing functions from namespaces with local aliases
Michał Pietrus
@blelump
Apr 15 2016 07:02
@sled , regarding the gist you've posted above, it has many things in common with TRB#OP. I even hit the dependency resolve problem more or less as your approach, but I finally abandoned such idea because I feel better with DI as
Fran Worley
@fran-worley
Apr 15 2016 07:02
@solnic what is the long term plan for Dry-V's Form schema? I'm coming to the conclusion that I might do better to
Use Dry-validation to its full potential and scrap Reform all together...
Simon Schmid
@sled
Apr 15 2016 07:03
@blelump so you pass the dependencies from the outside looking it up in the controller?
what I'm still researching is how to perform a locking, because often I need to ensure that the state I'm working with is reflected in the database, e.g if I pass in the dependency from the outside, the preconditions may pass but the actual record might have changed in the database when I get to execute the command :/
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 15 2016 07:06
I do locking in the process method of a TRB OP
Simon Schmid
@sled
Apr 15 2016 07:06
ok then you check the pre-conditions inside the process method ?
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 15 2016 07:07
def process(params)
  model.with_lock do
    if validate(params[:model], model) ... end
  end
end
depends on the operation and what models are involved ofc
Simon Schmid
@sled
Apr 15 2016 07:07
but the model has already loaded :)
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 15 2016 07:07
yes
and when you reload it withlock
it is read again
for me it is sufficient to know when i really start the operation
that im at a clean state at this point
this is what with_lock does for me
for instance my use case is bidding on auctions
when i start the bid operation i lock the auction model in process
then it is reloaded
Simon Schmid
@sled
Apr 15 2016 07:10
and reload the model inside the lock I assume
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 15 2016 07:10
with_lock does it automatically
in rails
starts an transaction
and locks the model
by reading it from DB
with sELECt ... FOR UPDATE
Simon Schmid
@sled
Apr 15 2016 07:10
wtf
how could I miss that :see_no_evil:
ofc you can acquire more locks inside the transaction if multiple models have to be locked
Simon Schmid
@sled
Apr 15 2016 07:12
convenient it reloads it :)
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 15 2016 07:12
just be sure to lock them in a deadlock free manner :D
yeah some parts of rails are still nice ;)
Simon Schmid
@sled
Apr 15 2016 07:13
yeah that's my fear with "nested" operations
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 15 2016 07:13
well
then i have the floowing:
following
then i lock on a global object
if it is not ensureable to be deadlock free
not the most efficient way I think but it is safe
Simon Schmid
@sled
Apr 15 2016 07:15
hm doesn't this run into a deadlock too if you try to acquire the same lock inside a nested operation?
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 15 2016 07:15
No
then one will wait
until the other finishes
deadlocks cant occur
if you only have one lock
or
Simon Schmid
@sled
Apr 15 2016 07:16
but the outer one depends on the inner one to finish, which waits for the outer one to finish
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 15 2016 07:16
you have to assure the locking order
if the order remains same
you cant deadlock
but it gets usecase specific on this point
what you are doing exactly
so in generall if I have operations for isntance
who work on model1, model2 and model3
and i need to lock all 3
but i cant assure the order
ill introduce a global lock 'superlock'
and when even i work with the operations
each of these operations have to lock superlock first
before locking the 3 models
then it is assured to be deadlock free
but when you can control the lockingo rder of model1, model2, model3
then you dont need such a global lock
always same locking order => deadlock free
something like this I could imagine
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 15 2016 07:21
thats fine
the inner transaction will then be part of the outer transaction
merged into one transaction
and because it is the same transaction
Simon Schmid
@sled
Apr 15 2016 07:22
hm, true
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 15 2016 07:22
the lock is still in your transaction
so no problem there
fun begins when you are working with more than one model at once
which have to be locked
the classical account transfer :)
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 15 2016 07:26
yes
Simon Schmid
@sled
Apr 15 2016 07:26
I often forget there's not just me using the app :D
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 15 2016 07:26
but as I said, when you can define a global locking order
you are safe
and if you cant define such order introduce a global lock
because I think data integrity > performance
Simon Schmid
@sled
Apr 15 2016 07:27
best case is always to lock parent to child order
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 15 2016 07:27
in most cases
yes that would be some sort of global order
or if you have multiple models of the same type
always lock the one with the smaller id first
Thats the stuff which spreads the hobbyists from the professionals :D because when these errors are popping and you dont know about it it is incredibly hard to debug
and they only pop when traffic and concurrency increases
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 15 2016 07:35
Now you remembered me on my pains when I was attending the lecture about parallel programming in my 4th term at the university xD
If you wanna really f*ck up things, give this a read:
I once had the case
and it was driving me nuts
Tim Cooper
@coop
Apr 15 2016 08:36
@solnic have you started on dry-rb/dry-validation#100 ?
Piotr Solnica
@solnic
Apr 15 2016 08:37
@coop no, don’t have time
I need to finish rom stuff first
Tim Cooper
@coop
Apr 15 2016 08:44
@solnic I can start - do you have any pointers on what you’d like the code to look like?
Piotr Solnica
@solnic
Apr 15 2016 08:50
@coop it’s a complex task but feel free to work on it of course
@coop it involes a massive code removal from error and hint compilers, basically all the mess that’s caused by inferring nested structures from [:set, …] nodes
Tim Cooper
@coop
Apr 15 2016 08:53
Yeah dude, I’m started looking at the code and I’ve found it really hard to start.
Fran Worley
@fran-worley
Apr 15 2016 08:57
Does any one have any strong feelings about dry-rb/dry-logic#8 ?
Piotr Solnica
@solnic
Apr 15 2016 08:58
@coop I would suggest skipping this tbh, I’ll do it pretty fast and you’d just waste a lot of time on understanding something that is going to be removed. makes no sense
it was a huge mistake I made. I really don’t know why I did that lol
Tim Cooper
@coop
Apr 15 2016 08:59
@solnic fair enough.
I got the refine DSL stuff up in a PR. Tests are all green. If you’re cool I’ll merge.
Fran Worley
@fran-worley
Apr 15 2016 09:41
Am I right that I can't use the latest dry-logic version with dry-validation version 0.6 ??
 Bundler::GemRequireError: There was an error while trying to load the gem 'dry-validation'.
NoMethodError: undefined method `visit_String' for 
#<Dry::Logic::RuleCompiler:0x007ffe8a44e6d0>
Piotr Solnica
@solnic
Apr 15 2016 09:43
yes
@fran-worley re Reform and plans for Schema.Form - the plan is not to become Reform, so if you need similar functionality, then you better stick to Reform
Fran Worley
@fran-worley
Apr 15 2016 09:47
:cry: Oh I wasn't expecting it to become Reform. I was interested in whether there was any scope to add the conversion of nested attribute params to an array of nested hashes.
I have had to hack Reform so much that I'm wondering why I bother...
Piotr Solnica
@solnic
Apr 15 2016 09:48
not following you right now, can you show me a simple example?
Fran Worley
@fran-worley
Apr 15 2016 09:58

Here is an example... (apologies for the semi pseudo code here)

class Parent < ActiveRecord::Base
  has_many :children 
end

class Child < ActiveRecord::Base
  belongs_to :parent
end

#ParentForm should have the following attributes
attributes :name, type: String
attributes :children, type: Array #should be an array of the attributes for children

However when using the rails form builder and fields for I get something like this:

{"name" => "John Doe", "children_attributes" => { "0" => { "name"=>"Fred", "age" => "15" },  "1" => { "name" => "Charlie", "age" => "5" } }

when I want:

{"name" => "John Doe", "children" => [{ "name"=>"Fred", "age" => "15" },   { "name" => "Charlie", "age" => "5" } ]

Is this conversion something that could be built into the Form schema or is it too coupled to rails?

Piotr Solnica
@solnic
Apr 15 2016 09:59
it is an integration layer between dry-v and an external system like rails
we need specialized objects that will provide required interface for stuff like form helpers
it’s definitely out of dry-v scope
Fran Worley
@fran-worley
Apr 15 2016 10:01
I guess I could define it as a special type & add a custom coercion for it. Could this be an extension ?
Tim Riley
@timriley
Apr 15 2016 10:01
@fran-worley we need to write something like this ourselves, because we have a form builder that handles nested attributes in a similar way. We haven’t started work on anything reusable yet, but our thinking would be that it’s an extra thing that “wraps” the dry-v form schema and transforms the specified data on the way in
Michał Pietrus
@blelump
Apr 15 2016 10:04
@fran-worley , re children_attributes, isnt it caused by accepts_nested_attrs_for from AR ?
Fran Worley
@fran-worley
Apr 15 2016 10:04
Nope. I don't use accepts_nested_attrs_for anymore.
Tim Riley
@timriley
Apr 15 2016 10:04
It’s how you have to structure any array of objects/hashes in a normal HTML form post.
Fran Worley
@fran-worley
Apr 15 2016 10:10
So we are essentially saying that we would have to pass the raw html params through something to convert the nested attributes into a format suitable for validation. Then pass that set of params into dry-v form schema?
Piotr Solnica
@solnic
Apr 15 2016 10:11
yes this is a data trasformation concern
and dry-v is not a data transformation lib
Fran Worley
@fran-worley
Apr 15 2016 10:12
Yes I can see that. Is there any scope for something like that in the Dry-RB project as a whole?
Michał Pietrus
@blelump
Apr 15 2016 10:12
@fran-worley , look at @solnic 's transproc lib
Benjamin Klotz
@tak1n
Apr 15 2016 10:12
@solnic but isn't coercion a kind of data transformation? and the Form schema is coercing the values?
Piotr Solnica
@solnic
Apr 15 2016 10:13
no, it’s coercion, specific to form input, we’re not changing the structure of the input
Benjamin Klotz
@tak1n
Apr 15 2016 10:13
okay
Piotr Solnica
@solnic
Apr 15 2016 10:13
it sits so close to validation rules that it makes sense to have it there
Benjamin Klotz
@tak1n
Apr 15 2016 10:13
makes sense :+1:
Piotr Solnica
@solnic
Apr 15 2016 10:13
it’s external to the validation though, input_processor is a dependency injected into schema
Fran Worley
@fran-worley
Apr 15 2016 10:15
@blelump thanks for the pointer, that looks like a very good starting point
Tim Riley
@timriley
Apr 15 2016 10:16
@fran-worley I’m sure a nice standalone helper library could be made, whether it’s prefixed by dry- or not. I’m not sure when we’ll get to it (we’ll need it for formalist), but I’ll be sure to ping you when we come up with something
Fran Worley
@fran-worley
Apr 15 2016 10:16
Well I either have to fix Reform and Dry-Validation 0.7+ or ditch Reform this weekend so I might come up with something...
Tim Riley
@timriley
Apr 15 2016 10:17
we’re just manually doing it in place right now
let me find an example
Michał Pietrus
@blelump
Apr 15 2016 10:19
@fran-worley I think data transformation should be performed before reform stuff is instantiated
Fran Worley
@fran-worley
Apr 15 2016 10:21
@blelump currently I just pass the raw params into reforms validate method and it cleans up my nested attributes using the collection populator.
Michał Pietrus
@blelump
Apr 15 2016 10:21
I see
Tim Riley
@timriley
Apr 15 2016 10:23
attrs.merge(
              contributors: attrs["contributors"].keys.sort_by {|key| key.to_i}.map {|key| attrs["contributors"][key]}
              )
@fran-worley ^^ right now we’re just hand-manipulating the data where we know we have nested attributes like that
Piotr Solnica
@solnic
Apr 15 2016 10:24
ugh that’s nasty we gotta do something about that
Fran Worley
@fran-worley
Apr 15 2016 10:26
@timriley I guess that is what I have to do.
@solnic If this is not just a rails issue but a html forms issue is it not worth considering it for the/ a library?
Tim Riley
@timriley
Apr 15 2016 10:26
Yes, this could surely be turned into a library. Just not dry-validation.
@solnic that’s definitely nasty. It’s just a stopgap measure right now :)
Fran Worley
@fran-worley
Apr 15 2016 10:27
@timriley I take it that you don't use Reform ?
Piotr Solnica
@solnic
Apr 15 2016 10:27
hmm, wait I’m missing something
Tim Riley
@timriley
Apr 15 2016 10:27
I’d imagine you could just build a transformer where you give it a series of keypaths that need to be mapped from HTML-style nested attributes to normal arrays of Ruby objects.
Piotr Solnica
@solnic
Apr 15 2016 10:27
you’re rewriting hash to have int keys?
Tim Riley
@timriley
Apr 15 2016 10:28
@solnic if you want to have an array of objects in an HTML form, they have to come back like this:
input name=“nested_things[0][email]”
input name=“nested_things[0][name]”

input name=“nested_things[1][email]”
input name=“nested_things[1][name]”
so they come back as a hash with whatever sort of numeric keys you decide to use in your form
and we need to map them into an array of hashes in ruby
@fran-worley nope, we’re building formalist as a form builder for complex forms, and otherwise just hand code the simple ones
Piotr Solnica
@solnic
Apr 15 2016 10:29
uhm
I’m almost sure that nested_things[][email] will give you an array
Tim Riley
@timriley
Apr 15 2016 10:29
@fran-worley and for processing forms we just use dry-v an a bunch of normal ruby
@solnic what about when you have a second nested object with an email in it?
nested_things[][email] and nested_things[][email] - you’d end up overwriting stuff (if it even managed to be a legitimate form post in the first place, I’m not sure)
Fran Worley
@fran-worley
Apr 15 2016 10:32
I think that is what I'll do. I use so little of the 'Power' of Reform that its probably simpler to just write it myself... (she says very naively)
Piotr Solnica
@solnic
Apr 15 2016 10:33
{“foo"=>[{"name"=>"Piotr"}, {"name"=>"Time"}]}
/admin/sign-in?foo[][name]=Piotr&foo[][name]=Time
@timriley ^^
Tim Riley
@timriley
Apr 15 2016 10:33
:astonished:
Piotr Solnica
@solnic
Apr 15 2016 10:33
heh…
sorry about the typo in your name ;)
timriley @timriley tries
Tim Riley
@timriley
Apr 15 2016 10:36
OMG
I have held this mistaken assumption for so long
Piotr Solnica
@solnic
Apr 15 2016 10:36
your welcome ;)
Tim Riley
@timriley
Apr 15 2016 10:36
I am sure there are reasons
Fran Worley
@fran-worley
Apr 15 2016 10:36
I think the reason that it is an issue in rails is because of the Index used to order the items.
Tim Riley
@timriley
Apr 15 2016 10:36
I am sure
Piotr Solnica
@solnic
Apr 15 2016 10:37
the order is the same as the params go
Fran Worley
@fran-worley
Apr 15 2016 10:37
Yeah but in rails it includes a number which is why you end up with the params mess
Piotr Solnica
@solnic
Apr 15 2016 10:38
you’re kidding? ;(
this is why we can’t have nice things
Fran Worley
@fran-worley
Apr 15 2016 10:38
Ergh... I miss the days when I loved rails I was a much happier person
Tim Riley
@timriley
Apr 15 2016 10:39
          <form action="/test" method="post">
            <input type="hidden" name="authors[][name]" value="tim">
            <input type="hidden" name="authors[][email]" value="tim@icelab.com.au">
            <input type="hidden" name="authors[][bio]" value="trying t work out nested attributes">
            <input type="hidden" name="authors[][name]" value="piotr">
            <input type="hidden" name="authors[][email]" value="piotr@poland.pl">
            <input type="hidden" name="authors[][name]" value="fran">
            <input type="hidden" name="authors[][email]" value="fran@gitter.im">
            <input type="submit" value ="submit">
          </form>
Andy Holland
@AMHOL
Apr 15 2016 10:39
:joy:
Tim Riley
@timriley
Apr 15 2016 10:39
{"authors"=>[{"name"=>"tim", "email"=>"tim@icelab.com.au", "bio"=>"trying t work out nested attributes"}, {"name"=>"piotr", "email"=>"piotr@poland.pl"}, {"name"=>"fran", "email"=>"fran@gitter.im"}]}
Andy Holland
@AMHOL
Apr 15 2016 10:39
Ignorance is bliss afterall
Tim Riley
@timriley
Apr 15 2016 10:39
it totally works
Piotr Solnica
@solnic
Apr 15 2016 10:39
@timriley good
timriley @timriley continues to try and break it
Tim Riley
@timriley
Apr 15 2016 10:40
Nope, still can’t break it.
so I suppose it was some accepts_nested_attributes_for implementation detail that dictated the need for a hash with numeric keys
It is true that the inputs here are less explicit
When does one object end and the next begin? Seems to be only when the first duplicate key is detected.
But I can probably live with this.
Piotr Solnica
@solnic
Apr 15 2016 10:42
just read params parsing code in rack
Fran Worley
@fran-worley
Apr 15 2016 10:42
If I remember rightly, they used it to map the fragment back to the record if you hadn't explicitly included the id in the attributes hash. Since I've been using rails they recommend that you do include the ID anyway so it makes it a little redundant
Piotr Solnica
@solnic
Apr 15 2016 10:42
if you feel uncomfortable relying on this, just read the code and understand what it’s doing
Tim Riley
@timriley
Apr 15 2016 10:42
Yeah.
Anyway. Amazing.
you just ticked off a big to-do on my list
:cake:
Piotr Solnica
@solnic
Apr 15 2016 10:43
omnomnomnom
Fran Worley
@fran-worley
Apr 15 2016 10:44
@timriley I might take a look at formalist how stable is it?
Tim Riley
@timriley
Apr 15 2016 10:44
@fran-worley it’s not fully stable yet, nor is it documented
It might take another couple of months unfortunately
Fran Worley
@fran-worley
Apr 15 2016 10:44
I saw that. I'll just override Rail's fields_for for now.
Tim Riley
@timriley
Apr 15 2016 10:45
Here’s a teaser though
Fran Worley
@fran-worley
Apr 15 2016 10:46
That is lovely. We use all those sorts of features powered by a lots of JS & rails patches
Tim Riley
@timriley
Apr 15 2016 10:46
Right now we’re targeting the kind of apps where you want to have many many different similarly behaving forms in e.g. an admin area.
Fran Worley
@fran-worley
Apr 15 2016 10:47
It is a great idea.
Piotr Solnica
@solnic
Apr 15 2016 10:47
it’s client-side only atm, right?
@timriley ^
Tim Riley
@timriley
Apr 15 2016 10:48
Yes, it’s 100% rendered via react.js right now. Before 1.0 and a big public announcement we’ll add a server-side renderer with simpler JS for the progressive enhancements.
Everyone loves JS though, amirite :joy:
Fran Worley
@fran-worley
Apr 15 2016 10:52
Better than coffeeScript.
Tim Riley
@timriley
Apr 15 2016 10:56
So I’m no longer going to be writing a nested attributes mapping gem, sorry @fran-worley :grimacing:
Andy Holland
@AMHOL
Apr 15 2016 10:57
I miss JS
dan-klasson
@dan-klasson
Apr 15 2016 10:57
@fran-worley have you looked at https://github.com/apotonick/formular ?
oh. so that's what you mean by form builder. my badd
Fran Worley
@fran-worley
Apr 15 2016 10:58
@dan-klasson yes, I've actually contributed to the library. But the key issues (no support for ERB and requires cells) mean that I can't use it in my project
@timriley No worries, now that we have established how easy it is to eliminate the problem entirely I'll just change the html form to render without the indexes that I never use anyway!
dan-klasson
@dan-klasson
Apr 15 2016 11:04
speaking of form builders. would be awesome to have one where you can resize and position the elements wherever you want on the page like:
Fran Worley
@fran-worley
Apr 15 2016 11:07
That is pretty cool. We are looking at implementing something like this as part of our UI for a form that builds forms
dan-klasson
@dan-klasson
Apr 15 2016 11:13
@fran-worley just looked at the formular repo. Seems you fixed the erb problem no? Also, can't you use cells in your current project for just those forms?
forgot to tag @timriley
Fran Worley
@fran-worley
Apr 15 2016 11:14
@dan-klasson from memory, I had great intentions to fix the ERB issue, realised it was something to do with the output buffer and gave up!
dan-klasson
@dan-klasson
Apr 15 2016 11:16
@fran-worley Yeah I was thinking something like this http://dobtco.github.io/formbuilder/ but with gridster
resizing the elements too when the grid is resized. a lot of work though
and making sure it's responsive somehow
Fran Worley
@fran-worley
Apr 15 2016 11:17
Yeah, we've build something ourselves from scratch as we had quite complex needs (special field types, validation and automation logic etc)
dan-klasson
@dan-klasson
Apr 15 2016 11:18
@fran-worley planning to open source it?
regarding the erb issue, did you discuss it with Nick?
Fran Worley
@fran-worley
Apr 15 2016 11:36
Yeah like a month or so ago
@dan-klasson unfortunately at this stage I doubt it as it's pretty heavily tied to our requirements and we've not got the resource to fund converting it for general use.
Piotr Solnica
@solnic
Apr 15 2016 11:55
@coop I kick-started refactoring: dry-rb/dry-validation#111 there’s an outline of what needs to be done, I won’t get back to it until I’m done with rom (maybe I’ll do it when I get back from holidays, but I’d prefer to focus on rom first). If you want, you could take a stab at it, but if it feels too overwhelming just let it go for now :)
^^ ¯_(⊙ʖ⊙)
Piotr Solnica
@solnic
Apr 15 2016 12:04
irb(main):027:0> run = -> ast, *args { comp.visit(ast).(*args) }
=> #<Proc:0x007fd76a863988@(irb):27 (lambda)>
irb(main):028:0> run.([:val, [:predicate, [:int?, []]]], 1)
=> #<Dry::Logic::Result::Value success?=true input=1 rule=#<Dry::Logic::Rule::Value predicate=#<Dry::Logic::Predicate id=:int? args=[]> options={}>>
Tim Riley
@timriley
Apr 15 2016 12:06
ASTs like that are very lispy, come to think of it.
Piotr Solnica
@solnic
Apr 15 2016 12:09
yeah, and that’s exactly what clojure is doing, turning its ast to java objects
while ast being the syntax, since it’s homoiconic
Simon Schmid
@sled
Apr 15 2016 12:15
btw @solnic can we turn a Dry::Schema into an AST somehow?
it seems like there is functionality to go from AST -> Dry::Type but not the other way around :/
given the AST, one could create an input compiler / parser for the rails quirkery :D
Piotr Solnica
@solnic
Apr 15 2016 12:22
Types don't support to_ast yet
But all rules are stored under Schema.config.rules and Schema.config.checks (the high lvl rules)
dan-klasson
@dan-klasson
Apr 15 2016 12:33
@solnic you from Krakow?
Piotr Solnica
@solnic
Apr 15 2016 12:42
yes
Piotr Solnica
@solnic
Apr 15 2016 12:49
@dan-klasson why?
dan-klasson
@dan-klasson
Apr 15 2016 13:20
@solnic I was there as a kid. my ex step mom is from there
zuuno
@zuuno
Apr 15 2016 16:27
Hi guys, first of all, thanks for your fantastic work. I have a couple of questions about dry-validations:
1/ I plan to use it inside Trailblazer operations, I've seen a pull request for handling 0.7.X versions, do you know if it's usable yet?
2/ Is there a way to change error objects default structure? I'd like to return different error codes. For instance, when name is missing, return {"name" => [{code: 422, "can't be blank"}]}
dan-klasson
@dan-klasson
Apr 15 2016 16:29
@zuuno for 1 you are better off asking in the Trailblazer channel. I was just trying to get it to work with no avail. But others have succeeded
*to
zuuno
@zuuno
Apr 15 2016 16:29
thanks :)
Piotr Solnica
@solnic
Apr 15 2016 16:36
@zuuno that code: 422 feels like http to me so I suspect you simply want to return it when schema didn’t succeed, why do you need to extend error hash with that info?
error compilation is abstracted away in dry-v so technically speaking you can do whatever you want with the errors
zuuno
@zuuno
Apr 15 2016 16:37
In fact, I'd like to implement a json api-like error handling:
http://jsonapi.org/examples/#error-objects-error-codes
Piotr Solnica
@solnic
Apr 15 2016 16:38
ie you can supply your own error compiler on top of the built in one and just traverse the hash and add error codes
zuuno
@zuuno
Apr 15 2016 16:38
this is great news :)
Piotr Solnica
@solnic
Apr 15 2016 16:39
yeah I wanted to add support for json-api stuff but I didn’t need it yet so …
zuuno
@zuuno
Apr 15 2016 16:40
there are many different acceptable implementations for errors if I understand it well
dan-klasson
@dan-klasson
Apr 15 2016 16:40
@solnic I like that too. better to use code for testing as the text might change
Andy Holland
@AMHOL
Apr 15 2016 16:44
@zuuno you shouldn't return a textual representation of error message from an API
dan-klasson
@dan-klasson
Apr 15 2016 16:45
@AMHOL why not? doesn't hurt unless the traffic is really bad
Andy Holland
@AMHOL
Apr 15 2016 16:46
Things like I18n shouldn't be a concern on an API
Benjamin Klotz
@tak1n
Apr 15 2016 16:46
@AMHOL I know many big api's which return error messages in addition to http status code and an api error code
translation not, but a general message which describes the error code is still okay IMO :)
zuuno
@zuuno
Apr 15 2016 16:47
it's more comfortable for a developer to know why the request has failed, and for the front-end app too
Piotr Solnica
@solnic
Apr 15 2016 16:47
require 'dry-validation'

class MyErrorComp < Dry::Validation::ErrorCompiler
  def visit_error(node, schema = false)
    super.each_with_object({}) do |(name, err), h|
      h[name] = {
        code: 666,
        source: "/data/attributes/#{name}",
        title: err,
        details: err
      }
    end
  end
end

schema = Dry::Validation.Schema do
  required(:name).filled(size?: 3..100)

  configure do
    def self.error_compiler
      MyErrorComp.new(messages)
    end
  end
end

schema.(name: ‘fo’).inspect
there, I wrote it for you
dan-klasson
@dan-klasson
Apr 15 2016 16:47
totally agree with that. imagine us getting error codes instead of error messages from the framework when we're developing
Piotr Solnica
@solnic
Apr 15 2016 16:47
#<Dry::Validation::Result output={:name=>”fo”} messages={:name=>{:code=>666, :source=>"/data/attributes/name", :title=>["length must be within 3 - 100", "size must be within 3 - 100"], :details=>["length must be within 3 - 100", "size must be within 3 - 100"]}}>
result ^
zuuno
@zuuno
Apr 15 2016 16:48
thank you so much @solnic ;)
Piotr Solnica
@solnic
Apr 15 2016 16:48
warning: it’s not production ready :laughing:
in dry-v error representation is completely abstract, it’s literally an ast
Andy Holland
@AMHOL
Apr 15 2016 16:50
@tak1n @zuuno it's OK in addition to an error code, but if you're only outputting your messages in English (insert language here), what about when you want multi-lingual support, do you start storing I18n keys in your database, or just handle translation on your front-end / client
Piotr Solnica
@solnic
Apr 15 2016 16:51
both schema and its error compilers are configurable, so we can go nuts with different error structures
zuuno
@zuuno
Apr 15 2016 16:52
@AMHOL didn't reflect on it yet, as my app will remain in English for a long time
Benjamin Klotz
@tak1n
Apr 15 2016 16:52
@AMHOL good point but I would say just english is enough for api error messages
dan-klasson
@dan-klasson
Apr 15 2016 16:52
@AMHOL you talking about APIs? English would suffice in 99% of all cases
Piotr Solnica
@solnic
Apr 15 2016 16:52
99.9999% even
Benjamin Klotz
@tak1n
Apr 15 2016 16:52
until the provider of the api only allows french people to use the api then it should be in french xD
Piotr Solnica
@solnic
Apr 15 2016 16:53
oh snap
zuuno
@zuuno
Apr 15 2016 16:53
@AMHOL is right when building I18n front-end apps
and if you'd like to add server-side (API-side) form validation
We can also have the case when building front-end components
Andy Holland
@AMHOL
Apr 15 2016 16:54
{
  "user" => {
    "name" => [
      {
        code: "blank",
        value: "",
        options: {}
      }
    ]
  }
}
{
  "user" => {
    "name" => ["can't be blank"]
  }
}
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 15 2016 16:56
@zuuno dry-v 0.7 actually only works with reform 2-2 branch at the moment
Andy Holland
@AMHOL
Apr 15 2016 16:56
In the first one, I can join the keys an code to call I18n.t('user.name.blank')
zuuno
@zuuno
Apr 15 2016 16:56
@cdennl : thanks
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 15 2016 16:56
i got it working, if you have questions ask in the trb channel :)
Andy Holland
@AMHOL
Apr 15 2016 16:57
But with the second one, the only error message I can ever show will be something like "User's name can't be blank"
zuuno
@zuuno
Apr 15 2016 16:57
nice. in a rails app?
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 15 2016 16:57
yes
it was a bit cumbersome
but at the ende it worked
*end
zuuno
@zuuno
Apr 15 2016 16:57
perfect. I'll try it this weekend :)
Christopher Dennl-Ortega Arrieta
@cdennl
Apr 15 2016 16:57
only the error message compilation is still ab it awkward
but they're working on it
zuuno
@zuuno
Apr 15 2016 17:00
@AMHOL good point
Piotr Solnica
@solnic
Apr 15 2016 18:46
hey so I’m leaving on holidays tomorrow and I’m gonna leave this channel so that I won’t be getting any email notifications, I’ll probably join again when I’m back. I want to finish rom 2.0 + rom-sql/repo upgrades ASAP after the holidays and take a longer OSS holidays afterwards :wave:
dan-klasson
@dan-klasson
Apr 15 2016 18:47
enjoy your vacation
Piotr Solnica
@solnic
Apr 15 2016 18:48
thank you
Andy Holland
@AMHOL
Apr 15 2016 18:52
:+1: Stay off github too, hope you have a good time and actually relax :laughing:
Luca Guidi
@jodosha
Apr 15 2016 18:54
@solnic I hope to talk with you, later when you'll be comfortable. In the meantime, enjoy your well deserved vacation. :tophat:
Piotr Solnica
@solnic
Apr 15 2016 18:56
@jodosha thanks and btw we’re going to Italy
@AMHOL I know, the problem is that I’ve no idea how to turn off all github notifications at once O_o
Andy Holland
@AMHOL
Apr 15 2016 18:57
Don't they have an unsubscribe link or something?
Piotr Solnica
@solnic
Apr 15 2016 18:57
well, I’m subscribed to a gazillion of issues and repos so…
don’t know how to turn it off completely and then turn it on again
I may just add a filter that will archive all emails from github
although having that in that archive will be tempting and I will probably fail and look there geeez I need a doctor
Andy Holland
@AMHOL
Apr 15 2016 19:01
lmao
Piotr Solnica
@solnic
Apr 15 2016 19:01
I actually don’t think that was funny ;)
Screen Shot 2016-04-15 at 21.02.58.png
OMG ^
I forgot this exists
well, problem solved
Andy Holland
@AMHOL
Apr 15 2016 19:03
:D
Doctor averted
Piotr Solnica
@solnic
Apr 15 2016 19:06
pheew
Fran Worley
@fran-worley
Apr 15 2016 19:48
Enjoy your holiday @solnic
Piotr Solnica
@solnic
Apr 15 2016 20:04
@fran-worley thanks
dan-klasson
@dan-klasson
Apr 15 2016 20:28
@solnic you could also log out of your email ;)