Module: CMDx::Validators::Presence

Extended by:
Presence
Included in:
Presence
Defined in:
lib/cmdx/validators/presence.rb

Overview

Validates that a value is present: non-‘nil`, non-empty, and (for strings) not whitespace-only.

Instance Method Summary collapse

Instance Method Details

#call(value, options = EMPTY_HASH) ⇒ Validators::Failure?

Parameters:

  • value (Object)
  • options (Hash{Symbol => Object}) (defaults to: EMPTY_HASH)

Options Hash (options):

  • :message (String)

    override for the failure message

Returns:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cmdx/validators/presence.rb', line 15

def call(value, options = EMPTY_HASH)
  present =
    if value.is_a?(String)
      /\S/.match?(value)
    elsif value.respond_to?(:empty?)
      !value.empty?
    else
      !value.nil?
    end

  return if present

  Failure.new(options[:message] || I18nProxy.t("cmdx.validators.presence"))
end