Module: Fend::Plugins::FullMessages

Defined in:
lib/fend/plugins/full_messages.rb

Overview

full_messages plugin adds #full_messages method to Result which returns error messages with prependend param name.

class UserValidation < Fend
  plugin :full_messages

  # ...
end
result = UserValidation.call(email: "invalid", profile: "invalid", address: { })

result.full_messages
#=> { email: ["email is in invalid format"], profile: ["profile must be hash"], address: { city: ["city must be string"] } }

## Array members

When validating array elements, messages are returned with prependend index, since array members don’t have a name.

{ tags: { 0 => ["0 must be string"] } }

In order to make full messages nicer for array elements, pass :array_member_names option when loading the plugin:

plugin :full_messages, array_member_names: { tags: :tag }

# which will produce
{ tags: { 0 => ["tag must be string"] } }

:array_member_names options is inheritable, so it’s possible to define it globaly by loading the plugin directly through Fend class.

Fend.plugin :full_messages, array_member_names: { octopi: :octopus }

## Base errors

Full messages are not generated for errors added with base_errors plugin, since those messages are not connected to specific param(s).

Defined Under Namespace

Modules: ResultMethods Classes: Generator

Class Method Summary collapse

Class Method Details

.configure(validation, opts = {}) ⇒ Object



43
44
45
46
# File 'lib/fend/plugins/full_messages.rb', line 43

def self.configure(validation, opts = {})
  validation.opts[:full_messages_array_member_names] = (validation.opts[:full_messages_array_member_names] || {}).merge(opts[:array_member_names] || {})
  validation.const_set(:FullMessagesGenerator, Generator) unless validation.const_defined?(:FullMessagesGenerator)
end