created birth date validator

This commit is contained in:
dinsmol 2021-07-14 12:40:49 +03:00
parent 33a855fbe7
commit addeebfeb3
6 changed files with 32 additions and 34 deletions

View file

@ -13,6 +13,7 @@ class Contact::Ident
validates :type, presence: true, inclusion: { in: proc { types } }
validates :country_code, presence: true, iso31661_alpha2: true
validates_with MismatchValidator
validates_with BirthDateValidator, if: :birthday?
def self.epp_code_map
{
@ -26,6 +27,7 @@ class Contact::Ident
[:code, :invalid_national_id],
[:code, :invalid_reg_no],
[:code, :invalid_iso8601_date],
[:code, :invalid_birth_date],
[:country_code, :invalid_iso31661_alpha2]
]
}

View file

@ -8,9 +8,6 @@ class Epp::Contact < Contact
# disable STI, there is type column present
self.inheritance_column = :sti_disabled
VALID_BIRTH_DATE_FROM = Time.zone.today - 150.years
VALID_BIRTH_DATE_TO = Time.zone.tomorrow
before_validation :manage_permissions
def manage_permissions
@ -19,28 +16,6 @@ class Epp::Contact < Contact
throw(:abort)
end
validate :validate_birthday_ident
def validate_birthday_ident
return unless Depp::Contact::SELECTION_TYPES[2].include?(ident_type)
begin
Date.parse(ident)
rescue ArgumentError
add_epp_error('2308', nil, nil, I18n.t('epp.contacts.errors.valid_ident_date_format'))
@error = true
return
end
contact_ident_date = Date.parse(ident)
valid_time_range = VALID_BIRTH_DATE_FROM...VALID_BIRTH_DATE_TO
return if valid_time_range.cover?(contact_ident_date)
add_epp_error('2308', nil, nil, I18n.t('epp.contacts.errors.valid_ident_date_range'))
@error = true
nil
end
class << self
# support legacy search
def find_by_epp_code(code)

View 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

View file

@ -8,12 +8,6 @@ en:
valid_ident: >-
Ident update is not allowed.
Consider creating new contact object
valid_ident_date_format: >-
Ident update is not allowed.
Date format is invalid
valid_ident_date_range: >-
Ident update is not allowed.
Age must be over 0 and under 150
ident_update: >-
Only ident type and country can be updated in case of invalid ident.
Please create new contact object to update ident code

View file

@ -9,3 +9,4 @@ en:
code:
invalid_national_id: does not conform to national identification number format of %{country}
invalid_reg_no: does not conform to registration number format of %{country}
invalid_birth_date: Birth date is invalid, age must be over 0 and under 150

View file

@ -119,7 +119,7 @@ class EppContactCreateBaseTest < EppTestCase
response_xml = Nokogiri::XML(response.body)
assert_correct_against_schema response_xml
assert_epp_response :data_management_policy_violation
assert_epp_response :parameter_value_syntax_error
request_xml.sub! ">#{birthday_wrong_format}<", ">#{birthday_above_valid_range}<"
assert_no_difference 'Contact.count' do
@ -129,7 +129,7 @@ class EppContactCreateBaseTest < EppTestCase
response_xml = Nokogiri::XML(response.body)
assert_correct_against_schema response_xml
assert_epp_response :data_management_policy_violation
assert_epp_response :parameter_value_syntax_error
request_xml.sub! ">#{birthday_above_valid_range}<", ">#{birthday_below_valid_range}<"
assert_no_difference 'Contact.count' do
@ -139,7 +139,7 @@ class EppContactCreateBaseTest < EppTestCase
response_xml = Nokogiri::XML(response.body)
assert_correct_against_schema response_xml
assert_epp_response :data_management_policy_violation
assert_epp_response :parameter_value_syntax_error
end
def test_responces_error_with_email_error