These are chat archives for dry-rb/chat

16th
Mar 2017
Ben Moskovitz
@moskyb
Mar 16 2017 01:28
Hey all, I'm having some trouble with dry-validation

I've found that, at least in my specific case,

each do
  schema do
    #some schema here
  end
end

is not equivalent to

FunSchema = Dry::Validation.Schema do
  # some schema here
end

# ... another schema
each do
  schema(FunSchema)
end
Philip Arndt
@parndt
Mar 16 2017 01:31
This message was deleted
Ben Moskovitz
@moskyb
Mar 16 2017 01:36
For reference, what I currently have is
PredefinedSchema = Dry::Validation.Schema do
  required(:audience).filled(:str?)
end

ComparisonSchema = Dry::Validation.Schema do
  %i(lt gt lte gte).each do |key|
    optional(key).maybe { int? | float? | time? }
  end
end

ParamSchema = Dry::Validation.Schema do
  required(:to) do
    str? & eql?('*') | schema(PredefinedSchema) | each do
      schema do
        required(:name).filled(:str?)
        required(:criteria).each do
          str? | int? | float? | time? | schema(ComparisonSchema)
        end
      end
    end
  end
end
And what I'd like to have is
CriterionSchema = Dry::Validation.Schema do
  required(:name).filled(:str?)
  required(:criteria).each do
    str? | int? | float? | time? | schema(ComparisonSchema)
  end
end

ParamSchema = Dry::Validation.Schema do
  required(:to) do
    str? & eql?('*') | schema(PredefinedSchema) | each do
      schema(CriterionSchema)
    end
  end
end
I understand that my use case here is kinda complex
Refactoring is fun
Nicolas Cavigneaux
@Bounga
Mar 16 2017 15:16
Hi! I'm reading the doc for custom error messages and found something not working: Dry::Validation::Messages.load('/path/to/our/errors.yml') doesn't work since load is a private method
Nikita Shilnikov
@flash-gordon
Mar 16 2017 15:22
@Bounga try Dry::Validation::Messages::YAML.load, looks like a mistake in docs
Nicolas Cavigneaux
@Bounga
Mar 16 2017 15:23
Yes it works that what I've used but I just wanted to let you know about this mistake
Nikita Shilnikov
@flash-gordon
Mar 16 2017 15:23
kk, I'll fix that, thanks
Nicolas Cavigneaux
@Bounga
Mar 16 2017 15:23
BTW the YAML.load expect an array
Nicolas Cavigneaux
@Bounga
Mar 16 2017 15:47
@flash-gordon You're fast!
Now there's another issue. user_messages[:filled?, rule: :address] returns nil not the provided custom message
I do think that nested rules are broken
Nicolas Cavigneaux
@Bounga
Mar 16 2017 15:56

oh no, the example is wrong. It should no be nested in another rules.

en:
  errors:
    rules:

      user:
        filled?: "name cannot be blank"

        rules:
          address:
            filled?: "You gotta tell us where you live"

should be

en:
  errors:

    rules:

      user:
        filled?: "name cannot be blank"
        address:
            filled?: "You gotta tell us where you live"
Nicolas Cavigneaux
@Bounga
Mar 16 2017 16:04
I already noticed that the doc is pretty outdated and contains many mistakes. Would you need some help to update it?
Nikita Shilnikov
@flash-gordon
Mar 16 2017 16:07
@Bounga ohh, help is always welcome :) Though I can since 1.0 is coming I'm not sure how the docs should look like, better wait for @solnic to comment on this
re rules, I'm not sure about this, I'll have a look a bit later, busy atm
Nicolas Cavigneaux
@Bounga
Mar 16 2017 16:10
np
Nikita Shilnikov
@flash-gordon
Mar 16 2017 23:29
@Bounga current example works for me
messages = Dry::Validation::Messages::YAML.load(['/tmp/errors.yml'])
user_messages = messages.namespaced(:user)
user_messages[:filled?, rule: :address] # => "You gotta tell us where you live"