Class: Validation::Rule::Birthday

Inherits:
Object
  • Object
show all
Defined in:
lib/diaspora_federation/validators/rules/birthday.rb

Overview

Birthday validation rule

Valid is:

  • nil or an empty String

  • a Date object

  • a String with the format “yyyy-mm-dd” and is a valid Date, example: 2015-07-25

Instance Method Summary collapse

Instance Method Details

#error_keySymbol

The error key for this rule

Returns:

  • (Symbol)

    error key



16
17
18
# File 'lib/diaspora_federation/validators/rules/birthday.rb', line 16

def error_key
  :birthday
end

#paramsHash

This rule has no params.

Returns:

  • (Hash)

    params



35
36
37
# File 'lib/diaspora_federation/validators/rules/birthday.rb', line 35

def params
  {}
end

#valid_value?(value) ⇒ Boolean

Determines if value is a valid birthday date.

Returns:



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/diaspora_federation/validators/rules/birthday.rb', line 21

def valid_value?(value)
  return true if value.nil? || (value.is_a?(String) && value.empty?)
  return true if value.is_a? Date

  if value.is_a?(String) && value.match?(/[0-9]{4}-[0-9]{2}-[0-9]{2}/)
    date_field = value.split("-").map(&:to_i)
    return Date.valid_civil?(date_field[0], date_field[1], date_field[2])
  end

  false
end