These are chat archives for dry-rb/chat

18th
Dec 2015
Piotr Solnica
@solnic
Dec 18 2015 11:10
dryrb/dry-validation#40
@AMHOL @kwando I can haz some feedback? ^
it’s a pretty big addition
Andy Holland
@AMHOL
Dec 18 2015 12:08
:joy:
I'll take the red pill
Piotr Solnica
@solnic
Dec 18 2015 12:18
haha
Hannes Nevalainen
@kwando
Dec 18 2015 12:43
@solnic niceness! I'll have to use it before I can have better feedback =)
Hannes Nevalainen
@kwando
Dec 18 2015 14:04
dry-validation for validating ENV on boot => friendly error messages <3
Piotr Solnica
@solnic
Dec 18 2015 14:37
Yep. One of the many use cases

validateallthethings

Whoops
Hannes Nevalainen
@kwando
Dec 18 2015 14:38
markdown =P
Piotr Solnica
@solnic
Dec 18 2015 14:38
Hashtag goes wrong ;)
Andy Holland
@AMHOL
Dec 18 2015 14:49

hashtaggoeswrong

Chris Richards
@cmrichards
Dec 18 2015 15:36
Hey solnic, thanks for all your work on Virtus
Piotr Solnica
@solnic
Dec 18 2015 15:56
@cmrichards hey, my pleasure, although I’m no longer working on it ;)
Chris Richards
@cmrichards
Dec 18 2015 16:18
I know, that's why I'm saying thanks!
Any idea when dry-data and dry-validation will be ready enough to use in place of Virtus?
@solnic
Piotr Solnica
@solnic
Dec 18 2015 16:20
depends on what you need :)
I stopped using virtus already fwiw
philosophy is quite different than in virtus
so it requires a different approach
which is why it cannot be treated as a replacement
Chris Richards
@cmrichards
Dec 18 2015 16:23
@solnic I use Virtus only for web forms in rails. I include activerecord validations. It works perfectly.
It's hard to imagine anything simpler
Piotr Solnica
@solnic
Dec 18 2015 16:23
really? dry-data/validation is not just simpler but more powerful :D
Chris Richards
@cmrichards
Dec 18 2015 16:24
Great, i'll try it.
Piotr Solnica
@solnic
Dec 18 2015 16:24
I'm wrapping up stuff for dry-v 0.4.0
Chris Richards
@cmrichards
Dec 18 2015 16:25
If I have a User class, "class User < Dry::Data::Struct", can you just do User.new(params[:user]) to create an instance?
Piotr Solnica
@solnic
Dec 18 2015 16:25
it's gonna reach 1.0.0 once I figure out how to optimize errors, this part will need quite some work
Chris Richards
@cmrichards
Dec 18 2015 16:25
It's not clear in the docs
Piotr Solnica
@solnic
Dec 18 2015 16:25
yes, although structs are strict wrt hash input, it'll raise when keys are missing
it's not a replacement for virtus models
ie dry-v builds up a coercible hash using dry-d for Schema::Form, it does form coercions for you and symbolizes keys
there are different types of hashes in dry-d, so you have a strict one, a non-strict one, non-strict with symbolized-keys (that's the one that dry-v is using)
Piotr Solnica
@solnic
Dec 18 2015 16:30
dry-d structs and values are meant to be used as your domain data types, strictness is encouraged too
not just wrt type primitives but also any possible constraints
there’s a lot tbd to improve errors coming from dry-d though, it’s a bit rough right now
Chris Richards
@cmrichards
Dec 18 2015 16:32
So how would you do something simple like this in drd? https://gist.github.com/cmrichards/364920a5ffd6c49420c5
Piotr Solnica
@solnic
Dec 18 2015 16:35
gimme a minute, I actually just added new features to dry-v to allow this
Piotr Solnica
@solnic
Dec 18 2015 16:43
class BarcodeSchema < Dry::Validation::Schema::Form
  key(:barcode, &:filled?)
  key(:job_number) { |v| v.none? | v.int? }
  key(:sample_number) { |v| v.none? | v.int? }

  rule(:barcode_only) do
    rule(:barcode) ^ (rule(:job_number) | rule(:sample_number))
  end

  def self.messages
    Dry::Validation::Messages.default.merge(
      en: { errors: {
        barcode_only: 'Can only enter data into barcode OR job/sample fields, not both'
      }}
    )
  end
end
@cmrichards something like that will be possible starting from dry-v 0.4.0
I’m not sure if people will like using logic in the long term so maybe we can introduce higher-level macros, but I dunno, I kinda like it, forces you to think in terms of pure logic
Chris Richards
@cmrichards
Dec 18 2015 16:44
I agree.
It looks pretty good
and you do BarcodeScheme.new(params[..]) ?
Piotr Solnica
@solnic
Dec 18 2015 16:45
oh and typically you want to put error messages in a yaml file
uhm, actually this is not an equivalent of your example, seems like we need a negation support there
Chris Richards
@cmrichards
Dec 18 2015 16:47
I get the idea though
Piotr Solnica
@solnic
Dec 18 2015 16:47
ok cool :)
Chris Richards
@cmrichards
Dec 18 2015 16:47
is there a "valid?" method or is that done differently?
Piotr Solnica
@solnic
Dec 18 2015 16:48
no, it’s stateless
so it’s schema = MySchema.new and schema.call(params)
it returns a result object back with a ton of information
Chris Richards
@cmrichards
Dec 18 2015 16:48
And the result object is a hash?
Piotr Solnica
@solnic
Dec 18 2015 16:49
it’s a validation result object, it responds to #messages which gives you compiled error messages as a hash with access to string error messages and the input for each error
{:barcode_only=>[["Can only enter data into barcode OR job/sample fields, not both"], "123"]}
sth like that
Chris Richards
@cmrichards
Dec 18 2015 16:50
I see. So there will be more work needed for it to work with Rails form_for helpers?
I look forward to using it. For now I gotta go. Thanks.
This seems a bit like reform contracts?
the "reform" gem
Piotr Solnica
@solnic
Dec 18 2015 16:51
reform will use it soon (or even already has support for it)
we collaborate with Nick a lot lately
Chris Richards
@cmrichards
Dec 18 2015 16:52
excellent
Piotr Solnica
@solnic
Dec 18 2015 16:52
btw the missing ingredient is this:
rule(:barcode_only) do
  rule(:barcode) ^ not(rule(:job_number) | rule(:sample_number))
end
notice not
I gotta add it, it’s gonna be useful in many places anyway
Chris Richards
@cmrichards
Dec 18 2015 16:53
What's the ^?
Piotr Solnica
@solnic
Dec 18 2015 16:53
xor
Chris Richards
@cmrichards
Dec 18 2015 16:53
k
Piotr Solnica
@solnic
Dec 18 2015 16:53
either side must be true to make it all true
Chris Richards
@cmrichards
Dec 18 2015 16:53
I know my CS ;-)
Piotr Solnica
@solnic
Dec 18 2015 16:53
:)
Chris Richards
@cmrichards
Dec 18 2015 16:54
Although I didn't know it was a ruby operator until now!
Piotr Solnica
@solnic
Dec 18 2015 16:54
hah yeah, it’s a native operator in ruby
true ^ true => false
true ^ false => true
Chris Richards
@cmrichards
Dec 18 2015 16:54
yeah I just tried that
Piotr Solnica
@solnic
Dec 18 2015 16:55
cool :)
Piotr Solnica
@solnic
Dec 18 2015 18:16
class BarcodeSchema < Dry::Validation::Schema::Form
  key(:barcode, &:filled?)
  key(:job_number, &:int?)
  key(:sample_number, &:int?)

  rule(:barcode_only) do
    rule(:barcode) ^ (rule(:job_number) | rule(:sample_number))
  end
end
@cmrichards so, that’s the most concise way of writing this ^^^…but, there are a couple of things I realized while writing this
it seems like we have two concerns here, first one being the validation schema for valid types, the second one being high level rules that should be applied to already validated input
and…you can’t do it in one go, it’s a two step process, I gotta come up with a solution for this