These are chat archives for dry-rb/chat

6th
Dec 2016
Ralf Schmitz Bongiolo
@mrbongiolo
Dec 06 2016 00:31
Yes, I guess it makes sense to be able to coerce array values in the way that you expressed. Not sure if that would be doable or way to off of what each() proposes
Christian Meier
@mkristian
Dec 06 2016 07:49
hi, I am looking for some way to share validation rules between server and javascript client. I wonder if dry-validation can export its 'rules' in one way or the other ?
Andy Holland
@AMHOL
Dec 06 2016 16:48
@mkristian I've thought about this a lot in the past, the best solution I could think of was a custom verb like DESCRIBE to build the model representation on start-up, but I think just sticking to server-side validation is the better option
Having said that dry-validation builds an AST, so you could use that to create some representation of the validation predicates
Christian Meier
@mkristian
Dec 06 2016 16:52
that sound interesting.
server side validation is easier but with javascript only clients not an option - anymore
Andy Holland
@AMHOL
Dec 06 2016 16:54
Not sure what you mean, surely you send your data to the server at some point?
Christian Meier
@mkristian
Dec 06 2016 16:56
yes, we do but client side validation is much more responsive. the AST idea I will look into it. as we already decided to have only 'simple' validation on the client and all those custom and more complex stuff stays on the server
Andy Holland
@AMHOL
Dec 06 2016 17:01
Yeah, it would be nice in an ideal world, I just think the cost outweighs the benefits personally
Christian Meier
@mkristian
Dec 06 2016 17:02
I personally are more on your side but it is not my decision I just need to find a solution :)
Fran Worley
@fran-worley
Dec 06 2016 17:04
If you really don't want to write the js validations yourself you could just Ajax the data on change to the server and display any errors
Christian Meier
@mkristian
Dec 06 2016 17:06
@fran-worley I want to have both and want to share the validation rules at some of them between the client and the server
Andy Holland
@AMHOL
Dec 06 2016 17:06
Well if a client/my boss asked me to do that, my suggested solution would be let's leave that rabbit hole alone, have the user hit submit and display the errors in one hit, but I know that's not always an option :p
Christian Meier
@mkristian
Dec 06 2016 17:08
well, I was hopping (not too much) that there is already some solution out there with dry-validation
but from hear I get the courage and question the client side validation approach tomorrow - let's see what response I get
Andy Holland
@AMHOL
Dec 06 2016 17:12
Cool, well if you end up having to implement it and need help, feel free to post on https://discuss.dry-rb.org/, it will require some intimate knowledge of the library and would be an interesting project, so would be nice to have it documented
Christian Meier
@mkristian
Dec 06 2016 17:13
sure will do
Andy Holland
@AMHOL
Dec 06 2016 17:29
FYI you can return the ast like this:
require 'bundler/inline'
gemfile(true) { gem 'dry-validation' }

s = Dry::Validation.Schema(build: false) do
  required(:age) { int? & gt?(18) }
end

s.ast
# => [[:rule, [:age, [:and, [[:rule, [:age, [:predicate, [:key?, [[:name, :age], [:input, Undefined]]]]]], [:rule, [:age, [:and, [[:rule, [:age, [:key, [:age, [:predicate, [:int?, [[:input, Undefined]]]]]]]], [:rule, [:age, [:key, [:age, [:predicate, [:gt?, [[:num, 18], [:input, Undefined]]]]]]]]]]]]]]]]]
Christian Meier
@mkristian
Dec 06 2016 17:29
cool