These are chat archives for dry-rb/chat

26th
Sep 2017
Pierpaolo Frasa
@Fryie
Sep 26 2017 11:22
hello, I was wondering if there was an easy way to define a custom error message for a single new predicate without having to create a new YAML file (seems like a bit of an overhead). If not:
a) do I have to copy the entire default YAML file or will the files be merged somehow?
b) How can I make sure all my schemas use that file without having to repeat the configure block over and over again
c) what does the search path for the file look like? Do I need to specify a path relative to the file it's declared in?
d) how does this affect performance? will the YAML file be re-read every time there is a validation error?
Oskar Szrajer
@gotar
Sep 26 2017 11:26
     Class.new(Dry::Validation::Schema) do
      def self.messages
        Dry::Validation::Messages.default.merge(
          en: { errors: { email?: 'must be a valid email' } }
        )
      end
    end
but there was a possibility to define it inline
Pierpaolo Frasa
@Fryie
Sep 26 2017 11:26
perfect, thank you :)
Oskar Szrajer
@gotar
Sep 26 2017 11:26
need to check
hmm do not see it, maybe was dropped, but above should works
Pierpaolo Frasa
@Fryie
Sep 26 2017 11:32
is there a way to do this with Dry::Validation.Form?
Tim Riley
@timriley
Sep 26 2017 11:33
self.messages works there in the same way
@Fryie if you look in the spec/ dir you’ll see other examples of it
Pierpaolo Frasa
@Fryie
Sep 26 2017 11:37

ok then I'm not sure how the syntax is supposed to look like because this:

SCHEMA = Dry::Validation.Form do
  def self.messages
    Dry::Validation::Messages.default.merge(en: { errors: { uri_encoded_max_length?: "message is too long" } })
  end
  configure { def uri_encoded_max_length?(length, value); ...; end }
  required(:turn).schema { required(:user_message).schema { required(:content).filled(uri_encoded_max_length?: 1800) } }
end

doesn't seem to work (i.e. the predicate works, but the error message doesn't)

Tim Riley
@timriley
Sep 26 2017 11:39
@Fryie put def self.messages inside the configure block
Pierpaolo Frasa
@Fryie
Sep 26 2017 11:39
ah :)
thanks
yes, this works! thank you