These are chat archives for dry-rb/chat

15th
Jul 2016
Patrik Wenger
@paddor
Jul 15 2016 01:52
I'm trying to use dry-rb, specifically Dry::Types::Struct. The attribute :foo, which can come from form params, should be coerced into a Float, but I don't want to allow syntax like "0xaf". How to do this?
Patrik Wenger
@paddor
Jul 15 2016 02:10
Using Dry::Validation.Form now and having the same problem.
Piotr Solnica
@solnic
Jul 15 2016 06:54
@paddor try number? so required(:foo).filled(:number?, :float?)
Kiril Dokh
@dsounded
Jul 15 2016 10:42
@solnic hello, mate

included_in works perfectly, but I still can’t get this to work:

But I still can’t get this to work, where c,c2,c3 are some dynamic vars

http://pastebin.com/8Bh0WeaP

Piotr Solnica
@solnic
Jul 15 2016 10:55
@dsounded this doesn’t work like that, you can’t use arbitrary code in blocks, only predicates are allowed. just define your own predicate and use it there
pls paste your whole schema and I’ll figure it out
Kiril Dokh
@dsounded
Jul 15 2016 10:56
hmm, I could see, but will I have vars in localization then ?
Piotr Solnica
@solnic
Jul 15 2016 10:57
yes
Kiril Dokh
@dsounded
Jul 15 2016 10:57
Ok, will try and update you with result )
@solnic Hmm, let’s say I need to pass all the object to custom validation predicate, not just one value, how can I do this ?
And can I add error to some field like base in Rails, not for the existing attribute I mean, because I need to validate combination of the fields, which is not a field by itself
Kiril Dokh
@dsounded
Jul 15 2016 11:05
required(:field1, :field2, :field3).filled(valid_combination?: some_array_of_hashes) will be what I need
Piotr Solnica
@solnic
Jul 15 2016 11:10
@dsounded can you show me a sample data structure that you want to validate and how you imagine the validation of these fields?
I think it’s possible already, but I need to be sure that I understand what you need
Kiril Dokh
@dsounded
Jul 15 2016 11:10
I can make a pastebin
Piotr Solnica
@solnic
Jul 15 2016 11:12
re “base” from AM::V - we don’t have that concept, just define a high-lvl rule and give it any name you want and provide custom error message for it
Kris Leech
@krisleech
Jul 15 2016 11:15
I'm working on a Rails project today and I'd like to replace the homegrown ActiveModel form base class with dry-validations, but of course it still needs to conform to ActiveModel, I think the main thing would be outputting #errors in the correct format, I wonder if anyone has done this yet?
It sounds good for me with the rule if it’s applicable to my situation
Piotr Solnica
@solnic
Jul 15 2016 11:24
@krisleech Reform gives you that OOTB
Kris Leech
@krisleech
Jul 15 2016 11:25
@solnic thanks, I looked at Reform but I didn't need/want the #save / #sync stuff, I wanted some plainer. I guess I could just not use those methods :)
Piotr Solnica
@solnic
Jul 15 2016 11:26
@krisleech that’s probably the fastest way right now
I suggested building dry-validation-rails
for cases like yours
AM needs stateful objects with attributes, which is pretty horrible
Kris Leech
@krisleech
Jul 15 2016 11:27
Probably that is what I need to look at building (assuming it hasn't been started) :D
Piotr Solnica
@solnic
Jul 15 2016 11:27
so the integration would have to provide such object along with a dry-v errors => AM::Errors converter
some people are interested in doing that
Kris Leech
@krisleech
Jul 15 2016 11:28
that is what I was thinking
Piotr Solnica
@solnic
Jul 15 2016 11:28
I could set up a repo for it once some code is written and working
you could look at Reform and see how they are converting this
this as in errors
in dry-v 1.0.0 we’ll have a public API for setting your own error compiler, so this will be helpful
Kris Leech
@krisleech
Jul 15 2016 11:29
sure, I'll try some stuff out in our app and see how it goes. I can then always extract to a gist.
Piotr Solnica
@solnic
Jul 15 2016 11:29
that would be great
Andrew Kozin
@nepalez
Jul 15 2016 11:29
@krisleech recently I used reform with a bit of monkey-patching to make it work with dry-types in a style 'property :name, type: Types::Coercible::String' without sync or save
Kris Leech
@krisleech
Jul 15 2016 11:30
is it public anywhere, there might be some useful hints.
Andrew Kozin
@nepalez
Jul 15 2016 11:31
No it is not, but when I return to my notebook I could send you private gist
Kris Leech
@krisleech
Jul 15 2016 11:31
I'd appreciate it, thanks.
Andrew Kozin
@nepalez
Jul 15 2016 11:32
np, will do in a couple hours
henricus louwhoff
@hl
Jul 15 2016 11:47
@timriley will there ever be a html parser for formalist
Kiril Dokh
@dsounded
Jul 15 2016 11:47

@solnic could you look at this ?)

http://pastebin.com/0KDxKgZD

Eger Andreas
@andreaseger
Jul 15 2016 12:57
Hey, should it (in dry-types) work to combine an optional type with an default in a hash schema? because right now this doesn't work:
#!/usr/bin/env ruby
require "bundler/inline"

gemfile do
  source "https://rubygems.org"
  gem "dry-types"
end

module Types
  include Dry::Types.module
  OptionalString = Types::Strict::String.optional
  DefaultOptionalString = OptionalString.default(nil)
  DefaultString = Types::Strict::String.default("foo")
end

schema = Types::Hash.symbolized(
  foo: Types::DefaultString,
  bar: Types::DefaultOptionalString
)

p schema[{}]
#> {:foo=>"foo", :bar=>nil}
p schema[foo: "asd"]
#> {:foo=>"asd", :bar=>nil}
p schema[bar: "qwe"]
#> /home/ane/.rbenv/versions/ruby-2.3.1/lib/ruby/gems/2.3.0/gems/dry-types-0.8.1/lib/dry/types/decorator.rb:55:in `method_missing': undefined method `success' for #<Dry::Types::Default:0x0055b241fab998> (NoMethodError)
#>         from /home/ane/.rbenv/versions/ruby-2.3.1/lib/ruby/gems/2.3.0/gems/dry-types-0.8.1/lib/dry/types/default.rb:44:in `try'
#>         from /home/ane/.rbenv/versions/ruby-2.3.1/lib/ruby/gems/2.3.0/gems/dry-types-0.8.1/lib/dry/types/safe.rb:16:in `try'
#>         from /home/ane/.rbenv/versions/ruby-2.3.1/lib/ruby/gems/2.3.0/gems/dry-types-0.8.1/lib/dry/types/safe.rb:11:in `call'
#>         from /home/ane/.rbenv/versions/ruby-2.3.1/lib/ruby/gems/2.3.0/gems/dry-types-0.8.1/lib/dry/types/hash/schema.rb:85:in `block in call'
#>         from /home/ane/.rbenv/versions/ruby-2.3.1/lib/ruby/gems/2.3.0/gems/dry-types-0.8.1/lib/dry/types/hash/schema.rb:83:in `each'
#>         from /home/ane/.rbenv/versions/ruby-2.3.1/lib/ruby/gems/2.3.0/gems/dry-types-0.8.1/lib/dry/types/hash/schema.rb:83:in `each_with_object'
#>         from /home/ane/.rbenv/versions/ruby-2.3.1/lib/ruby/gems/2.3.0/gems/dry-types-0.8.1/lib/dry/types/hash/schema.rb:83:in `call'
#>         from dry-test.rb:26:in `<main>'
Joshua Wilcox
@joshuaswilcox
Jul 15 2016 13:18
schema.call(bar: "qwe")?
Eger Andreas
@andreaseger
Jul 15 2016 13:24
schema[] and schema.call() does exactly the same AFAIK
Joshua Wilcox
@joshuaswilcox
Jul 15 2016 13:33
good call, sorry
Andy Holland
@AMHOL
Jul 15 2016 13:46
Piotr Solnica
@solnic
Jul 15 2016 13:56
@andreaseger optional means that nil is default so this doesn't make sense. We should raise a meaningful error in such cases I believe
@dsounded sorry busy with other stuff, I'll get back to you soon
Eger Andreas
@andreaseger
Jul 15 2016 14:02

hmm but if I omit the default(nil) or just use Types::Strict::String.optionalthere isn't actually any default set

schema = Types::Hash.symbolized( foo: Types::Strict::String.optional)
p schema[{}]
#> {}

I'd like it to return {foo: nil}

Venkat Dinavahi
@venkatd
Jul 15 2016 16:17
hi @solnic have been using dry-validation in an older rails project--would love to get some feedback on how I can build something similar in elixir
my thinking was I could define a DSL in elixir which ends up getting translated to a data structure (struct) of validation rules
Piotr Solnica
@solnic
Jul 15 2016 16:27
@venkatd hey! I was planning to port dry-v to elixir, so this is cool :D
I’ve no idea how to do it, yet
should be way way simpler
Venkat Dinavahi
@venkatd
Jul 15 2016 16:28
oh awesome!
would love to help in any way I can
Piotr Solnica
@solnic
Jul 15 2016 16:29
we could set up a repo and try to figure it out
although I won’t have time this month to do any coding, but could provide some feedback
Venkat Dinavahi
@venkatd
Jul 15 2016 16:29
that's fine by me.. my time will be more flexible next month anyway
i'll try and follow README-driven development
and iterate on the readme first
the elixir absinthe library for graphql and ecto schemas are both interesting
Piotr Solnica
@solnic
Jul 15 2016 17:49
aah yeah I gotta check it out
Piotr Solnica
@solnic
Jul 15 2016 18:50
@dsounded damn, seems like this is still not gonna work in your use-case. I forgot to add support for this to high-level rules ;(
I’ll do that in 0.9.3 or 0.10.0
almost there ;)
Tim Riley
@timriley
Jul 15 2016 21:13
@hl I'm not sure what you mean by "parser" here, but we do plan to write a server-side Ruby-based form renderer before we get to 1.0