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


14
15
16
# File 'lib/diaspora_federation/validators/rules/birthday.rb', line 14

def error_key
  :birthday
end

#paramsHash

This rule has no params.


33
34
35
# File 'lib/diaspora_federation/validators/rules/birthday.rb', line 33

def params
  {}
end

#valid_value?(value) ⇒ Boolean

Determines if value is a valid birthday date.


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

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

  if value =~ /[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