These are chat archives for dry-rb/chat

12th
Jun 2017
siassaj
@siassaj
Jun 12 2017 05:26
hi all

I have this rule;

      optional(:location_id).filled
      optional(:address_params).filled
      rule(has_one_place: [:location_id, :address_params]) do |location_id, address_params|
        location_id.filled? ^ address_params.filled?
      end

However it makes more sense to me that only one of those keys is supplied. So I tried

      rule(has_one_place: [:location_id, :address_params]) do |location_id, address_params|
        key?(:location_id) ^ key?(:address_params)
      end

but the error came out like this

{ :has_one_place=>[[:has_one_place, ["is missing", "is missing"]]] }
I'm doing this wrong, it seems. Is there a correct way?
siassaj
@siassaj
Jun 12 2017 06:17
Shouldn't this rule just fail or not, and use they correct I18n rule for has_one_place: "should only have one place"
siassaj
@siassaj
Jun 12 2017 06:30
ok, i structured the I18n wrongly
Gustavo Caso
@GustavoCaso
Jun 12 2017 06:30
How is your I18n file looks like ?
siassaj
@siassaj
Jun 12 2017 06:43

hi @GustavoCaso

optional(:location_id).filled
optional(:address_params).filled

rule(has_one_place: [:location_id, :address_params]) do |location_id, address_params|
  location_id.filled? ^ address_params.filled?
end

and I had the yaml

  errors:
    rules:
      order/checkout:
        rules:
          has_one_place:
            filled?: "my error"

But i tried ( for my own understanding why filled? was needed)

rule(has_one_place: [:location_id, :address_params]) do |location_id, address_params|
  location_id.none? ^ address_params.filled?
end

which will fail both, and I needed to put

  errors:
    rules:
      order/checkout:
        rules:
          has_one_place:
            filled?: "my error"
            none?: "another error"

and also gave 2 messages for my error, like has_one_place: ["my error", "another error"]

Then, after looking at the yml file for dry-validations I noticed a lack of "rules" key so tried

  errors:
    rules:
      order/checkout:
        has_one_place: "an error"

which giave an error like has_one_place: ["an error", "an error"]

so... how do I just get an error like has_one_place: "you messed up something"
Gustavo Caso
@GustavoCaso
Jun 12 2017 06:44
Uhh let me check
I think you have to specify the the optional rules before rule which can access already defined rules for specific keys.
Gustavo Caso
@GustavoCaso
Jun 12 2017 06:56
@siassaj with a schema like this
schema = Dry::Validation.Schema do
  optional(:location_id).filled
  optional(:address_params).filled

  rule(has_one_place: [:location_id, :address_params]) do |location_id, address_params|
    location_id.filled? ^ address_params.filled?
  end
end
I get this errors without modifying the errors.yml file
bundle :011 > schema.call({})
 => #<Dry::Validation::Result output={} errors={:has_one_place=>["must be filled"]}>
In other hand using the schema
schema = Dry::Validation.Schema do
  rule(has_one_place: [:location_id, :address_params]) do |location_id, address_params|
    location_id.filled? ^ address_params.filled?
  end
end
I get the error:
schema.call({})
 => #<Dry::Validation::Result output={} errors={}>
I think creating the other rules before using the rule is important
Gustavo Caso
@GustavoCaso
Jun 12 2017 07:09
Did it help you @siassaj ?
siassaj
@siassaj
Jun 12 2017 07:27
hrm weird I need to check this out a bit more
siassaj
@siassaj
Jun 12 2017 08:02
schema = Dry::Validation.Schema do
  optional(:location_id).maybe
  optional(:address_params).maybe

  rule(has_one_place: [:location_id, :address_params]) do |location_id, address_params|
    location_id.none? ^ address_params.filled?
  end
end;

schema.call({location_id: nil, address_params: { address_1: "whatever"}}).errors

=> {:has_one_place=>["cannot be defined", "must be filled"]}
so, basically it's that
2 errosr for the 1 rule