Module: Pakyow::Validations::Presence

Defined in:
lib/pakyow/validations/presence.rb

Overview

Validates that the value is present, or that a value is non-empty, non-nil, and not a string consisting of whitespace only characters. Example values that will not pass this validation:

  • nil

  • “”

  • “ ”

  • []

  • {}

Class Method Summary collapse

Class Method Details

.messageObject



18
19
20
# File 'lib/pakyow/validations/presence.rb', line 18

def self.message(**)
  "cannot be blank"
end

.valid?(value) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
# File 'lib/pakyow/validations/presence.rb', line 22

def self.valid?(value, **)
  if value.is_a?(String)
    !value.strip.empty?
  elsif value.respond_to?(:empty?)
    !value.empty?
  else
    !!value
  end
end