Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • 07:07

    solnic on master

    Cast identifier to String durin… Merge pull request #169 from ad… (compare)

  • 07:07
    solnic closed #169
  • 00:21
    adam12 review_requested #169
  • 00:21
    adam12 opened #169
  • Apr 15 08:14
    chriscz commented #421
  • Apr 15 07:59
    flash-gordon commented #421
  • Apr 15 07:39
    solnic closed #421
  • Apr 15 07:39
    solnic commented #421
  • Apr 15 06:48
    flash-gordon unlabeled #421
  • Apr 15 06:48
    flash-gordon unlabeled #421
  • Apr 15 06:47
    flash-gordon commented #421
  • Apr 14 12:48
    chriscz labeled #421
  • Apr 14 12:48
    chriscz labeled #421
  • Apr 14 12:48
    chriscz opened #421
  • Apr 14 08:16
    solnic commented #164
  • Apr 13 23:25
    timriley milestoned #168
  • Apr 13 23:25
    timriley labeled #168
  • Apr 13 23:25
    timriley opened #168
  • Apr 13 23:19
    timriley commented #164
Chris Richards
@cmrichards
and you do BarcodeScheme.new(params[..]) ?
Piotr Solnica
@solnic
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
I get the idea though
Piotr Solnica
@solnic
ok cool :)
Chris Richards
@cmrichards
is there a "valid?" method or is that done differently?
Piotr Solnica
@solnic
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
And the result object is a hash?
Piotr Solnica
@solnic
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
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
reform will use it soon (or even already has support for it)
we collaborate with Nick a lot lately
Chris Richards
@cmrichards
excellent
Piotr Solnica
@solnic
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
What's the ^?
Piotr Solnica
@solnic
xor
Chris Richards
@cmrichards
k
Piotr Solnica
@solnic
either side must be true to make it all true
Chris Richards
@cmrichards
I know my CS ;-)
Piotr Solnica
@solnic
:)
Chris Richards
@cmrichards
Although I didn't know it was a ruby operator until now!
Piotr Solnica
@solnic
hah yeah, it’s a native operator in ruby
true ^ true => false
true ^ false => true
Chris Richards
@cmrichards
yeah I just tried that
Piotr Solnica
@solnic
cool :)
Piotr Solnica
@solnic
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
Chris Richards
@cmrichards
What's your solution?
Piotr Solnica
@solnic
dryrb/dry-validation@0955118 <= this /c @cmrichards
after this addition your example can be written like that:
class BarcodeSchema < Dry::Validation::Schema::Form
  key(:barcode) do |barcode|
    barcode.none? | barcode.filled?
  end

  key(:job_number) do |job_number|
    job_number.none? | job_number.int?
  end

  key(:sample_number) do |sample_number|
    sample_number.none? | sample_number.int?
  end

  rule(:barcode_only) do
    rule(:barcode).filled? ^ (rule(:job_number).int? | rule(:sample_number).int?)
  end
end
notice that now you can compose higher level rules using specific predicates
Chris Richards
@cmrichards
Yes, it looks better now
Piotr Solnica
@solnic
this pretty much means we can easily specify validations that are strictly about type-safety and a separate set of higher level rules that rely on existing validation results
Chris Richards
@cmrichards
Shouldn't the last | be a & ?
Piotr Solnica
@solnic
no, because from what I remember you wanted to check if barcode is present only if both job number and sample number are empty
you could express it like that too: rule(:barcode).filled? & (rule(:job_number).none? & rule(:sample_number).none?)