mirror of
https://github.com/internetee/registry.git
synced 2025-06-11 23:24:48 +02:00
created birth date validator
This commit is contained in:
parent
33a855fbe7
commit
addeebfeb3
6 changed files with 32 additions and 34 deletions
26
app/validators/contact/ident/birth_date_validator.rb
Normal file
26
app/validators/contact/ident/birth_date_validator.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
class Contact::Ident::BirthDateValidator < ActiveModel::Validator
|
||||
VALID_BIRTH_DATE_FROM = Time.zone.today - 150.years
|
||||
VALID_BIRTH_DATE_TO = Time.zone.tomorrow
|
||||
|
||||
def validate(record)
|
||||
record.errors.add(:code, :invalid_birth_date) if birth_date_wrong?(record)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def birth_date_wrong?(record)
|
||||
return unless record.birthday?
|
||||
|
||||
begin
|
||||
Date.parse(record.code)
|
||||
rescue ArgumentError
|
||||
return true
|
||||
end
|
||||
|
||||
contact_ident_date = Date.parse(record.code)
|
||||
valid_time_range = VALID_BIRTH_DATE_FROM...VALID_BIRTH_DATE_TO
|
||||
return if valid_time_range.cover?(contact_ident_date)
|
||||
|
||||
true
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue