mirror of
https://github.com/internetee/registry.git
synced 2025-08-06 09:45:11 +02:00
parent
0f352cab24
commit
d520b5b157
50 changed files with 2462 additions and 323 deletions
20
app/validators/contact/ident/mismatch_validator.rb
Normal file
20
app/validators/contact/ident/mismatch_validator.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
class Contact::Ident::MismatchValidator < ActiveModel::Validator
|
||||
Mismatch = Struct.new(:type, :country)
|
||||
|
||||
def self.mismatches
|
||||
[
|
||||
Mismatch.new('birthday', Country.new('EE')),
|
||||
]
|
||||
end
|
||||
|
||||
def validate(record)
|
||||
record.errors.add(:base, :mismatch, type: record.type, country: record.country) if mismatched?(record)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def mismatched?(record)
|
||||
mismatch = Mismatch.new(record.type, record.country)
|
||||
self.class.mismatches.include?(mismatch)
|
||||
end
|
||||
end
|
22
app/validators/contact/ident/national_id_validator.rb
Normal file
22
app/validators/contact/ident/national_id_validator.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
class Contact::Ident::NationalIdValidator < ActiveModel::EachValidator
|
||||
def self.country_specific_validations
|
||||
{
|
||||
Country.new('EE') => proc { |code| Isikukood.new(code).valid? },
|
||||
}
|
||||
end
|
||||
|
||||
def validate_each(record, attribute, value)
|
||||
validation = validation_for(record.country)
|
||||
|
||||
return unless validation
|
||||
|
||||
valid = validation.call(value)
|
||||
record.errors.add(attribute, :invalid_national_id, country: record.country) unless valid
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def validation_for(country)
|
||||
self.class.country_specific_validations[country]
|
||||
end
|
||||
end
|
21
app/validators/contact/ident/reg_no_validator.rb
Normal file
21
app/validators/contact/ident/reg_no_validator.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
class Contact::Ident::RegNoValidator < ActiveModel::EachValidator
|
||||
def self.country_specific_formats
|
||||
{
|
||||
Country.new('EE') => /\A[0-9]{8}\z/,
|
||||
}
|
||||
end
|
||||
|
||||
def validate_each(record, attribute, value)
|
||||
format = format_for(record.country)
|
||||
|
||||
return unless format
|
||||
|
||||
record.errors.add(attribute, :invalid_reg_no, country: record.country) unless value =~ format
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def format_for(country)
|
||||
self.class.country_specific_formats[country]
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue